chengkun
2025-09-09 1ff9e27b020822168a95edd83be567e7153837f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
 
namespace app\admin\controller;
 
use app\supplier\model\DistributionArea as DistributionAreaModel;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\supplier\model\GoodsAnnex;
use app\supplier\model\GoodsSpec;
use app\supplier\model\GoodsAttribute;
use app\supplier\model\GoodsLabel;
use app\supplier\model\GoodsRetailPriceLimit;
 
//use app\supplier\model\Goods;
 
//商品零售价限制
 
class Goods extends Common {
    
    /**
     * 商品管理
     * @return string
     */
    public function index() {
        return View::fetch();
    }
    
    /**
     * 获取商品列表
     * @return mixed
     */
    public function getGoodsList() {
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $platform_goods_code = input('platform_goods_code');
            $supplier_goods_code = input('supplier_goods_code');
            
            $condition['g.istemp'] = 1;//编辑过的商品
            if ($platform_goods_code) {
                $condition['g.platform_goods_code'] = $platform_goods_code;
            }
            if ($supplier_goods_code) {
                $condition['g.supplier_goods_code'] = $supplier_goods_code;
            }
            $p = input('p', 1);
            $_GET['p'] = $p;
            $other['page_size'] = input('page_size', 20);
            $order = 'g.id asc';
            $count = \app\supplier\model\Goods::alias('g')->where($condition)->count();
            $list = \app\supplier\model\Goods::alias('g')
                ->join('goods_desc gd', 'g.id=gd.goods_id')
                ->field("g.id,g.sales_form,g.supplier_goods_code,g.platform_goods_code,g.goods_purpose,g.add_time,g.status,g.publish_status,g.whether_dis,gd.goods_title_cn,g.first_cate_code,g.second_cate_code,g.three_cate_code,(select url from web_goods_picture where goods_id=g.id order by id asc limit 1) as picture,g.review_comments,g.is_show")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
            $other['count'] = $count;
            if (!$list) {
                throw new \Exception(lang('no_data_found'));
            }
            $categorylist = get_category();
            $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
            foreach ($list as &$item) {
                $item['first_cate_name'] = $categorylist[$item['first_cate_code']]['cate_name'];
                $item['second_cate_name'] = $categorylist[$item['second_cate_code']]['cate_name'];
                $item['three_cate_name'] = $categorylist[$item['three_cate_code']]['cate_name'];
            }
            $result['list'] = $list;
            $result['other'] = $other;
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    function updateExt() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $postField = 'id,is_show';
            $data = Request::only(explode(',', $postField), 'post');
            if (!$data['id']) {
                throw new Exception(lang('parameter_error'));
            }
            Db::name('goods')->update($data);
            $result['message'] = '操作成功';
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    /**
     * 审核商品
     * @return array|int[]
     */
    public function reviewGoods() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $id = input('post.id');
            if (!is_numeric($id)) {
                throw new \Exception(lang('invalid_id'));
            }
            
            $status = input('post.status');
            //更新数据
            $info = Db::name('goods')->field('id,status')->where('id', $id)->find();
            if (!$info) {
                return $this->errorResponse(lang('request_method_incorrect'));
            }
            if ($status == 1) {
                Db::name('goods')->where('id', $id)->update(['status' => 1, 'review_comments' => '']);
                $message = '操作成功,审核通过';
            } else {
                $review_comments = trim(input('review_comments'));
                if (!$review_comments) {
                    throw new \Exception('请填写审核不通过原因');
                }
                Db::name('goods')->where('id', $id)->update(['status' => 3, 'review_comments' => $review_comments]);
                $message = '操作成功,审核不通过';
            }
            // 提交事务
            Db::commit();
            return $this->successResponse($message);
            
        } catch (\Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 创建商品
     * @return string
     */
    public function create() {
        ////////
        View::assign('menuitem', 'goods-index');
        $id = input('id');
        if (!is_numeric($id)) {
            $this->redirect(url('/admin/goods/index')->build());
        }
        $other['id'] = $id;
        View::assign('other', $other);
        return View::fetch();
    }
    
    public function getGoodsInfo() {
        
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $id = input('id');
            if (!is_numeric($id)) {
                throw new \Exception(lang('invalid_id'));
            }
            
            $where = [
                'id' => $id,
            ];
            $info = Db::name('goods')->field('id,first_cate_code,second_cate_code,three_cate_code,brand_code')->where($where)->find();
            if ($info['three_cate_code']) {
                $info['cate_code'] = [$info['first_cate_code'], $info['second_cate_code'], $info['three_cate_code']];
            }
            $info['goods_purpose'] = json_decode($info['goods_purpose'], TRUE);
            //////获取描述标题数据/////////
            $where_other = [
                'goods_id' => $info['id'],
            ];
            $goods_desc = Db::name('goods_desc')->field('goods_title_en,goods_title_cn')->where($where_other)->find();
            //获取描述内容数据
            $goods_desc_content = Db::name('goods_desc_content')->field('goods_desc_en,goods_desc_cn')->where($where_other)->find();
            
            ////////////////
            $result['info'] = $info;
            $result['goods_desc'] = $goods_desc;
            $result['goods_desc_content'] = $goods_desc_content;
            return $this->successResponse($result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    /**
     * 获取商品分销信息
     * @return array|int[]
     */
    public function getGoodsDisInfo() {
        
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $id = input('id');
            
            $condition['g.istemp'] = 1;//编辑过的商品
            if ($id) {
                $condition['g.id'] = $id;
            }
            
            $info = \app\supplier\model\Goods::alias('g')
                ->join('goods_desc gd', 'g.id=gd.goods_id')
                ->join('goods_desc_content gdc', 'g.id=gdc.goods_id')
                ->field("g.id,g.warehouse_mode,g.sales_form,g.supplier_goods_code,g.platform_goods_code,g.goods_purpose,g.add_time,g.status,g.publish_status,g.whether_dis,g.is_limit_dis_platform,gd.goods_title_cn,gd.goods_title_en,gd.goods_keyword_en,gd.goods_keyword_cn,g.first_cate_code,g.second_cate_code,g.three_cate_code,g.docking_way,g.price_method,g.freight_attr_code,g.is_tort,g.is_stop_buy,g.recommend_man,gdc.goods_desc_en,gdc.goods_desc_cn")->where($condition)->find();
            if ($info) {
//                $categorylist = get_category();
//                $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
//                $info['first_cate_name'] = $categorylist[$info['first_cate_code']]['cate_name'];
//                $info['second_cate_name'] = $categorylist[$info['second_cate_code']]['cate_name'];
//                $info['three_cate_name'] = $categorylist[$info['three_cate_code']]['cate_name'];
                $info['attr_name'] = Db::name('goods_freight_attr')->where('attr_code', $info['freight_attr_code'])->value('attr_name');
                $info['goods_keyword_en'] = $info['goods_keyword_en'] ? json_decode($info['goods_keyword_en'], TRUE) : [];
                $info['goods_keyword_cn'] = $info['goods_keyword_cn'] ? json_decode($info['goods_keyword_cn'], TRUE) : [];
                
                $info['goodsSpecInfo'] = Db::name('goods_spec')->field('*')->where('goods_id', $info['id'])->find();
                $goods_attribute_info = Db::name('goods_attribute')->field('*')
                    ->withAttr('material_main', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_one', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_two', function ($value, $data) {
                        return Db::name('goods_material')->where('id', $value)->value('material_name');
                    })
                    ->withAttr('material_three', function ($value, $data) {
                        if ($value) {
                            $value = json_decode($value, TRUE);
                            return Db::name('goods_material')->whereIn('id', $value)->column('material_name');
                        } else {
                            return [];
                        }
                    })
                    ->where('goods_id', $info['id'])->find();
                $info['goodsAttributeInfo'] = $goods_attribute_info;
                $info['goodsAttributExtendList'] = Db::name('goods_attribute_extend')->field('*')->where('goods_id', $info['id'])->select()->toArray();
                
                $info['goodsRetailPriceLimitList'] = GoodsRetailPriceLimit::field('*')->where('goods_id', $info['id'])
                    ->withAttr('sale_country_code', function ($value, $data) {
                        return Db::name('stock_country')->where('id', $value)->value("CONCAT(country_code,' ',country)");
                    })
                    ->withAttr('sales_platform', function ($value, $data) {
                        return Db::name('sales_platform')->where('id', $value)->value("platform_name");
                    })
                    ->order('id asc')->select()->toArray();///
                $info['goodsLabelList'] = GoodsLabel::field('label_type,goods_label')->where('goods_id', $info['id'])->order('id asc')->select()->toArray();///
                if ($info['is_limit_dis_platform'] == 1) {
                    $info['goodsLimitDisPlatformList'] = Db::name('goods_limit_dis_platform')
                        ->alias('a')
                        ->join('dis_platform b', 'a.dis_platform = b.id')
                        ->field('b.platform_name')->where('goods_id', $info['id'])->order('a.id asc')->select()->toArray();///
                } else {
                    $info['goodsLimitDisPlatformList'] = [];
                }
                $result['info'] = $info;
                ///分销区域//
                $disAreaList = Db::name('goods_distribution_area')
                    ->alias('gda')
                    ->join('distribution_area da', 'da.id=gda.distribution_area_id')
                    ->field("gda.one_shipment,gda.distribution_area_id")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)->withAttr('one_shipment', function ($value, $data) {
                        return $value == 1 ? '一件代发' : '';
                    })->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })->select()->toArray();
                $result['disAreaList'] = $disAreaList;
                //分销库存
                $disStockList = Db::name('goods_distribution_area_stock')
                    ->alias('gdas')
                    ->join('third_party_stock tps', 'tps.id=gdas.third_party_stock_id')
                    ->field("gdas.distribution_area_id,gdas.supplier_stock_amount,gdas.platform_stock_amount,gdas.goods_distribution_area_id,tps.third_party_stock_name,tps.docking_code,tps.stock_type")->where('gdas.goods_id', $info['id'])->where('gdas.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })->select()->toArray();
                $result['disStockList'] = $disStockList;
                //分销价格///
                $disPriceList = Db::name('goods_distribution_area_price')
                    ->alias('gdap')
                    ->join('goods_distribution_area gda', 'gda.id=gdap.goods_distribution_area_id')
                    ->join('logistics_product lp', 'lp.id=gdap.logistics_product_id')
                    ->field("gdap.distribution_area_id,gdap.goods_distribution_area_id,lp.product_name_cn,gdap.supply_price,gdap.dis_price,gdap.package_goods_amount,gda.take_effect_time")->where('gdap.goods_id', $info['id'])->where('gda.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })
                    ->withAttr('take_effect_time', function ($value, $data) {
                        return date('Y-m-d H:i:s', $value);
                    })->select()->toArray();
                $result['disPriceList'] = $disPriceList;
                //退货地址列表//
                $disReturnGoodsList = Db::name('goods_distribution_area')
                    ->alias('gda')
                    ->join('third_party_stock tps', 'tps.id=gda.third_party_stock_id')
                    ->join('third_party_stock_address tpsa', 'tpsa.id=gda.third_party_stock_address_id')
                    ->field("gda.distribution_area_id,tps.third_party_stock_name,tpsa.stock_address,tpsa.stock_city,tpsa.stock_province,tpsa.stock_post_code,tpsa.stock_country_id,tpsa.receive_man,tpsa.tel")->where('gda.goods_id', $info['id'])->where('gda.whether_dis', 1)
                    ->withAttr('distribution_area_id', function ($value, $data) {
                        return $this->getDisArea($value);
                    })
                    ->withAttr('stock_country_id', function ($value, $data) {
                        return Db::name('stock_country')->where('id', $value)->value('country');
                    })
                    ->select()->toArray();
                $result['disReturnGoodsList'] = $disReturnGoodsList;
                $categorylist = get_category();
                $categorylist = datalist($categorylist, 'cate_code', '', FALSE);
                $catelist = Db::name('goods_category')
                    ->alias('GC')
                    ->join('docking_platform DP', 'DP.cate_code=GC.cate_code')
                    ->field('GC.*,DP.docking_platform_name')->where(['GC.goods_id' => $info['id'], 'GC.status' => 1])->select()->toArray();
                foreach ($catelist as &$value) {
                    $value['first_cate_name'] = $categorylist[$value['first_cate_code']]['cate_name'];
                    $value['second_cate_name'] = $categorylist[$value['second_cate_code']]['cate_name'];
                    $value['three_cate_name'] = $categorylist[$value['three_cate_code']]['cate_name'];
                }
                $result['catelist']=$catelist;
                return $this->successResponse($result);
            } else {
                return $this->errorResponse(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 获取商品图片列表
     * @return array|int[]
     */
    public function getGoodsPictureList() {
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $goods_id = input('goods_id');
            $picture_type = input('picture_type');
            
            if (is_array($picture_type)) {
                $where[] = ['a.goods_id', '=', $goods_id];
                if (is_array($picture_type)) {
                    $where[] = ['b.type', 'in', $picture_type];
                }
                $list = Db::name('goods_picture')
                    ->alias('a')
                    ->join('goods_picture_type b', 'a.id=b.picture_id')
                    ->field('DISTINCT  a.id,a.url')->where($where)->order('id asc')->select()->toArray();
            } else {
                $where[] = ['goods_id', '=', $goods_id];
                $list = Db::name('goods_picture')->field('id,url')->where($where)->order('id asc')->select()->toArray();
            }
            if (!$list) {
                throw new \Exception(lang('no_data_found'));
            }
            $result['list'] = $list;
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 根据类型获取商品图片数量
     * @return void
     */
    public function getGoodsPictureCount() {
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $goods_id = input('goods_id');
            $goodsPictureCount['type0'] = Db::name('goods_picture')->where(['goods_id' => $goods_id])->count();
            $goodsPictureCount['type1'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 1])->count();
            $goodsPictureCount['type2'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 2])->count();
            $goodsPictureCount['type3'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 3])->count();
            $goodsPictureCount['type4'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 4])->count();
            $goodsPictureCount['type5'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 5])->count();
            $goodsPictureCount['type6'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 6])->count();
            $goodsPictureCount['type7'] = Db::name('goods_picture_type')->where(['goods_id' => $goods_id, 'type' => 7])->count();
            $result['goodsPictureCount'] = $goodsPictureCount;
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 获取分销区域
     */
    private function getDisArea($id) {
        return Db::name('distribution_area')
            ->alias('da')
            ->join('deliver_region dr', 'dr.deliver_region_id=da.deliver_region_id')
            ->field("da.dis_currency,dr.region_name,da.dis_model")
            ->where('da.id', $id)->find();
    }
    
    /**
     * 获取分类列表
     * @return array
     */
    public function getCategoryList() {
        try {
            if (!Request::isPost()) {
                throw new \Exception(lang('request_method_incorrect'));
            }
            $result['list'] = $this->getchildrenids(0);
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 循环获取分类数据
     * @param $id
     * @return array|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getchildrenids($id) {
        $where['father_code'] = $id;
        $result = Db::name('category')->field('id,cate_code as value,cate_name as label')->where($where)->cacheAlways(TRUE, 0, 'category')->order('id asc')->select()->toArray();
        if ($result) {
            foreach ($result as &$val) {
                $val['children'] = $this->getchildrenids($val['value']); ////
            }
            return $result;
        }
    }
 
}