Explorar o código

在线选品管理

xialei hai 1 ano
pai
achega
a13a6016d4
Modificáronse 3 ficheiros con 969 adicións e 0 borrados
  1. 622 0
      app/controller/admin/gong/Goods.php
  2. 335 0
      app/controller/admin/gong/Product.php
  3. 12 0
      route/admin.php

+ 622 - 0
app/controller/admin/gong/Goods.php

@@ -0,0 +1,622 @@
1
+<?php
2
+
3
+
4
+namespace app\controller\admin\gong;
5
+
6
+
7
+use app\common\repositories\store\product\ProductRepository;
8
+use app\validate\merchant\GongProductValidate;
9
+use crmeb\basic\BaseController;
10
+use think\Exception;
11
+use think\facade\Cache;
12
+use think\facade\Db;
13
+use think\facade\Log;
14
+
15
+class Goods extends BaseController
16
+{
17
+    //特殊商户ID  正式测试321
18
+    private $specialMerId = 321;
19
+    private $signKey = 'tdba2023!@#';
20
+    // 商品类表
21
+    public function lst()
22
+    {
23
+        [$page, $limit] = $this->getPage();
24
+        $params = $this->request->params(['cate_ids','title', 'type', 'profit', 'price']);
25
+
26
+        $product_model = Db::name('douhuomall')->whereIn('status', [0, 1]);
27
+        $gongProduct = $this->getJoinProduct();
28
+        if ($params['type'] == 1) {
29
+            // 已加入商品管理
30
+            $product_model->where('status', 1);
31
+        } elseif ($params['type'] == 2) {
32
+            // 未加入商品管理
33
+            $product_model->where('status', 0);
34
+        }
35
+
36
+        // SQL条件拼装
37
+        $product_model->whereIn('status', [0, 1])
38
+                    ->when(!empty($params['title']), function($query) use ($params) {
39
+                        $query->whereLike('title','%'.$params['title'].'%');
40
+                    })
41
+                    ->when(!empty($params['cate_ids']), function ($query) use ($params) {
42
+                        $query->where(function($que) use ($params) {
43
+                            $cateIds = explode(',', $params['cate_ids']);
44
+                            foreach ($cateIds as $key => $value) {
45
+                                $que->whereFindInSet('cate_ids', $value, 'OR');
46
+                            }
47
+                        });
48
+                    })
49
+                    ->field('spu_id,title,cost_price,market_price,profit,spuId_info,status,ot_price,mer_profit,pension');
50
+        // 获取总数
51
+        $count=$product_model->count('spu_id');
52
+
53
+        // 按创客利润排序
54
+        if (!empty($params['profit'])) {
55
+            if ($params['profit'] == 'up') {
56
+                $product_model->order('mer_profit', 'asc');
57
+            }elseif ($params['profit'] == 'down') {
58
+                $product_model->order('mer_profit', 'desc');
59
+            }
60
+        }
61
+
62
+        // 按销售价排序
63
+        if (!empty($params['price'])) {
64
+            if ($params['price'] == 'up') {
65
+                $product_model->order('market_price', 'asc');
66
+            }elseif ($params['price'] == 'down') {
67
+                $product_model->order('market_price', 'desc');
68
+            }
69
+        }
70
+
71
+        if (empty($params['price']) && empty($params['profit'])) {
72
+            $product_model->order('create_time', 'desc');
73
+        }
74
+        // 获取分页数据
75
+        $product_lst=$product_model->page($page,$limit)->select()->toArray();
76
+        foreach ($product_lst as $k=>&$v){
77
+            //商品图片处理
78
+            $v['spuId_info']=json_decode($v['spuId_info'],true);
79
+            $v['img']=$v['spuId_info']['cover_url'];
80
+            if(in_array($v['spu_id'], $gongProduct)){
81
+                $v['is_join']=1;
82
+            }else{
83
+                $v['is_join']=0;
84
+            }
85
+            unset($v['spuId_info']);
86
+        }
87
+        $retrun_data=[
88
+            'count'=>$count,
89
+            'data'=>$product_lst
90
+        ];
91
+        return app('json')->success($retrun_data);
92
+    }
93
+
94
+    // 获取已加入选品库的商品
95
+    public function getJoinProduct()
96
+    {
97
+        $gongProduct = Db::name('store_product')
98
+            ->where('status', 1)
99
+            ->whereNotNull('spu_id')
100
+            ->column('spu_id');
101
+        return $gongProduct;
102
+    }
103
+
104
+
105
+    // 获取选品库中全部、已加入3号库、未加入3号库的数量
106
+    public function getStatusFilter()
107
+    {
108
+   
109
+   
110
+        // 获取全部数据
111
+        $allNum =  Db::name('douhuomall')->whereIn('status', [0, 1])->count();
112
+        // 获取已加入3号库的数据
113
+        $joinNum = Db::name('douhuomall')->where('status', 1)->count();
114
+        // 获取未加入3号库的数据
115
+        $notJoinNum = Db::name('douhuomall')->where('status', 0)->count();
116
+
117
+
118
+        $res = [
119
+            [
120
+                'type' => 0,
121
+                'title' => '全部',
122
+                'count' => $allNum
123
+            ],
124
+            [
125
+                'type' => 1,
126
+                'title' => '已加入3号库',
127
+                'count' => $joinNum
128
+            ],
129
+            [
130
+                'type' => 2,
131
+                'title' => '未加入3号库',
132
+                'count' => $notJoinNum
133
+            ]
134
+        ];
135
+        return app('json')->success($res);
136
+    }
137
+
138
+    //商品详情
139
+    public function product_details()
140
+    {
141
+        $spu_id = $this->request->param('spu_id');
142
+        $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
143
+        if (empty($product_data))
144
+            return app('json')->success([]);
145
+        $product_data['spuId_info']=json_decode($product_data['spuId_info'],true);
146
+        $product_data['skuId_info']=json_decode($product_data['skuId_info'],true);
147
+        foreach ($product_data['skuId_info'] as &$skuId){
148
+            $name='';
149
+            foreach($skuId['attribute_json'] as $k=>$v){
150
+                $name.=$v['val'];
151
+            }
152
+            $skuId['attribute_json']=$name;
153
+        }
154
+        return app('json')->success($product_data);
155
+    }
156
+
157
+
158
+    // 修改价格
159
+    public function update_Price()
160
+    {
161
+        $data = $this->request->param();
162
+        $spu_id = $data['spu_id'];
163
+        
164
+        $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
165
+            'update_time' => date('Y-m-d H:i:s'),
166
+            'cost_price' => $data['cost_price'],
167
+            'market_price' => $data['market_price'],
168
+            'ot_price' => $data['ot_price'],
169
+            'mer_profit' => $data['mer_profit'],
170
+            'pension' => $data['pension'],
171
+            'profit' => $data['profit'],
172
+            'title' => $data['title'],
173
+        ]);
174
+
175
+        if($res) {
176
+            return app('json')->success('修改成功');
177
+        }
178
+        return app('json')->fail('修改失败');
179
+    }
180
+
181
+
182
+    // 上下架
183
+    public function join_selection_library()
184
+    {
185
+        $data = $this->request->params(['spu_id']);
186
+        $spu_id = $data['spu_id'];
187
+
188
+        $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
189
+        if (empty($product_data)) return app('json')->fail('设置失败,没有找到该商品');
190
+
191
+        if ($product_data['status'] == 1) {
192
+            $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
193
+                'update_time' => date('Y-m-d H:i:s'),
194
+                'status' => 0,
195
+            ]);
196
+        }else{
197
+            $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
198
+                'update_time' => date('Y-m-d H:i:s'),
199
+                'status' => 1,
200
+            ]);
201
+        }
202
+
203
+        if($res) {
204
+            return app('json')->success('设置成功');
205
+        }
206
+        return app('json')->fail('设置失败');
207
+    }
208
+
209
+    // 批量加入选品
210
+    public function batch_join_selection_library()
211
+    {
212
+        $data = $this->request->params(['spu_ids']);
213
+        if (empty($data['spu_ids'])) {
214
+            return app('json')->fail('参数错误');
215
+        }
216
+        $mer_id=$this->request->merId();
217
+        $joinNum = 0;
218
+        $successJoin = 0;
219
+        $failJoin = 0;
220
+        $spuIds = explode(',', $data['spu_ids']);
221
+        foreach ($spuIds as $spu_id) {
222
+            $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->where('is_del', 0)->value('product_id');
223
+            if(!empty($store_product_data)) {
224
+                ++$joinNum;
225
+                continue;
226
+            }
227
+            $res = $this->insert_product($spu_id, $mer_id);
228
+            $res ? ++$successJoin : ++$failJoin;
229
+        }
230
+        return app('json')->success($successJoin . '个商品加入成功,' . $failJoin . '个商品加入失败,' . $joinNum . '个商品已经加入');
231
+    }
232
+
233
+    public function insert_product($spu_id, $mer_id) {
234
+        try {
235
+            $data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
236
+            // 是否为多规格
237
+            $sku_data=json_decode($data['skuId_info'],true);
238
+            $spec_type=count($sku_data);
239
+            if($spec_type>1){
240
+                $spec_type=1;
241
+            }else{
242
+                $spec_type=0;
243
+            }
244
+
245
+            //商品主图
246
+            $spuId_info=json_decode($data['spuId_info'],true);
247
+            $slider_image=$spuId_info['detail_img_list']??'';
248
+            $image=$spuId_info['cover_url']??'';
249
+
250
+            $slider_image = empty($slider_image) ? $image : implode(',',json_decode($slider_image,true));
251
+            $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
252
+            //商品入库数据
253
+            $product_insert_data=[
254
+                'mer_id'=>$mer_id,
255
+                'image'=>$image,
256
+                'slider_image'=>$slider_image,
257
+                'store_name'=>$data['title'],
258
+                'store_info'=>1,
259
+                'keyword'=>mb_substr($data['title'],0,3),
260
+                'is_show'=> ($mer_id == $this->specialMerId) ? 1:0,
261
+                'status'=>1,
262
+                'cate_id'=> ($mer_id == $this->specialMerId) ? $data['cxb_cate_id'] : $data['cate_ids'],
263
+                'unit_name'=>'个',
264
+                'price'=>$data['market_price'],
265
+                'cost'=>$data['cost_price'],
266
+                'ot_price'=>$data['ot_price'],
267
+                'stock'=>'1000',
268
+                'spec_type'=>$spec_type,
269
+                'extension_type'=>1,
270
+                'mer_status'=>1,
271
+                'is_used'=>1,
272
+                'old_product_id'=>$data['id'],
273
+                'volunteer'=>0,
274
+                'type'=>($mer_id == $this->specialMerId) ? 5: 1,
275
+                'pension'=>'0.00',
276
+                'commission'=>5,
277
+                'spu_id'=>$data['spu_id'],
278
+                'temp_id' => 105,
279
+                'plate_mer_profit' => $data['mer_profit'],
280
+		'concession_pri' => $data['mer_profit'],
281
+                'yanglaojin_scale' => $yanglaojin_scale
282
+            ];
283
+            $insert_id=Db::name('store_product')->insertGetId($product_insert_data);
284
+
285
+            $detail = $spuId_info['detail'];
286
+            if(!is_null(json_decode($detail))){
287
+                $detail_list = json_decode($detail);
288
+                $detail = '';
289
+                foreach($detail_list as $value){
290
+                    $detail .= '<img src="'.$value.'"></img>';
291
+                }
292
+            }
293
+            Db::name('store_product_content')->insert(['content'=>$detail,'product_id'=>$insert_id]);
294
+            $this->insert_sku($insert_id,$sku_data);
295
+            return true;
296
+        } catch (Exception $e) {
297
+            Log::error($spu_id . "加入选品失败:" . $e->getMessage());
298
+            return false;
299
+        }
300
+    }
301
+
302
+    public function insert_sku($id,$data)
303
+    {
304
+        $sku_attr_data=array_column($data,'attribute_json');
305
+        $name='';
306
+        $ProductRepository = app()->make(ProductRepository::class);
307
+        if(empty($sku_attr_data[0][0])){
308
+            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
309
+            $key='';
310
+            $num=1;
311
+            foreach ($data as $k=>$v){
312
+                Db::name('store_product_attr_value')->insert([
313
+                    'product_id'=>$id,
314
+                    'detail'=>json_encode(['规格'=>$key]),
315
+                    'sku'=>$key,
316
+                    'image'=>$v['img_url'],
317
+                    'cost'=>$v['cost_price'],
318
+                    'ot_price'=>$v['retail_price'],
319
+                    'price'=>$v['market_price'],
320
+                    'unique'=>$ProductRepository->setUnique($id,$v['sku_sn'],0),
321
+                    'stock'=>100,
322
+                    'cost_price'=>$v['cost_price'],
323
+                    'extension_one'=>5,
324
+                    'gong_sku_id'=>$v['sku_sn'],
325
+                    'gong_mer_profit' => $v['profit'],
326
+                    'gong_pension' => $v['yanglaojin'],
327
+                    'gong_market_price' => $v['market_price'],
328
+                    'plate_mer_profit' => $v['profit']
329
+                ]);
330
+                $key='';
331
+                $num++;
332
+            }
333
+        }else{
334
+            foreach ($sku_attr_data as $kk=>$vv){
335
+                foreach ($vv as $k1=>$v1){
336
+                    $name.=$v1['val'];
337
+                }
338
+                $name.='-!-';
339
+            }
340
+            $name=rtrim($name,'-!-');
341
+            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
342
+            $key='';
343
+            $num=1;
344
+            foreach ($data as $k=>$v){
345
+                foreach ($v['attribute_json'] as $k2=>$v2){
346
+                    $key.=$v2['val'];
347
+                }
348
+
349
+                Db::name('store_product_attr_value')->insert([
350
+                    'product_id'=>$id,
351
+                    'detail'=>json_encode(['规格'=>$key]),
352
+                    'sku'=>$key,
353
+                    'image'=>$v['img_url'],
354
+                    'cost'=>$v['cost_price'],
355
+                    'ot_price'=>$v['retail_price'],
356
+                    'price'=>$v['market_price'],
357
+                    'unique'=>$ProductRepository->setUnique($id,$v['sku_sn'],0),
358
+                    'stock'=>100,
359
+                    'cost_price'=>$v['cost_price'],
360
+                    'extension_one'=>5,
361
+                    'gong_sku_id'=>$v['sku_sn'],
362
+                    'gong_mer_profit' => $v['profit'],
363
+                    'gong_pension' => $v['yanglaojin'],
364
+                    'gong_market_price' => $v['market_price'],
365
+                    'plate_mer_profit' => $v['profit']
366
+                ]);
367
+                $key='';
368
+                $num++;
369
+            }
370
+        }
371
+    }
372
+
373
+    public function add(GongProductValidate $validate)
374
+    {
375
+        $data = $this->request->params(['spu_id', 'skuId_info', 'sys_id', 'goods_id', 'spuId_info', 'sku_id', 'cate_ids', 'title', ['cxb_cate_id', 0], 'sign']);
376
+        Log::channel("gong")->info("加入3号库供应链数据:" . json_encode($data));
377
+        // 校验sign
378
+        $checkSign = $this->checkSign($data);
379
+        if (!$checkSign) {
380
+            Log::channel("gong")->error('sign校验失败');
381
+            return app('json')->fail('sign校验失败');
382
+        }
383
+        unset($data['sign']);
384
+        $validate->check($data);
385
+        $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find();
386
+        if (!empty($findData) && $findData['status'] == 1)
387
+            return app('json')->success('商品已存在');
388
+        try {
389
+            $data['create_time'] = date('Y-m-d H:i:s');
390
+            $data['status'] = 1;
391
+            $data['update_time'] = date('Y-m-d H:i:s');
392
+            $skuInfo = $this->arrSort(json_decode($data['skuId_info'], true), 'cost_price', SORT_ASC);
393
+            $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
394
+            $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
395
+            $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
396
+            $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
397
+            $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
398
+            $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
399
+
400
+            if (empty($findData)) {
401
+                Db::name('douhuomall')->insert($data);
402
+            } else {
403
+                Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
404
+                    'status' => 1,
405
+                    'create_time' => date('Y-m-d H:i:s'),
406
+                    'update_time' => date('Y-m-d H:i:s'),
407
+                    'cost_price' => $data['cost_price'],
408
+                    'market_price' => $data['market_price'],
409
+                    'ot_price' => $data['ot_price'],
410
+                    'mer_profit' => $data['mer_profit'],
411
+                    'pension' => $data['pension'],
412
+                    'profit' => $data['profit'],
413
+                    'skuId_info' => $data['skuId_info'],
414
+                    'spuId_info' => $data['spuId_info'],
415
+                    'title' => $data['title'],
416
+                    'cate_ids' => $data['cate_ids'],
417
+                    'cxb_cate_id' => $data['cxb_cate_id']
418
+                ]);
419
+            }
420
+            // 特殊商户直接加入商品库
421
+            $is_exit = Db::name('store_product')
422
+                ->field('product_id')
423
+                ->where('spu_id', $data['spu_id'])
424
+                ->where('mer_id', $this->specialMerId)
425
+                ->where('is_del', 0)
426
+                ->find();
427
+            if (empty($is_exit)) {
428
+                $this->insert_product($data['spu_id'], $this->specialMerId);
429
+            }
430
+            return app('json')->success('添加成功');
431
+        } catch (Exception $e) {
432
+            Log::channel("gong")->error("获取供应链数据失败:" . $e->getMessage());
433
+            return app('json')->fail($e->getMessage());
434
+        }
435
+    }
436
+
437
+    public function remove()
438
+    {
439
+        $data = $this->request->params(['token', 'sign']);
440
+        Log::channel("gong")->info("供应链取消商品数据:" . json_encode($data));
441
+        // 校验sign
442
+        $checkSign = $this->checkSign($data);
443
+        if (!$checkSign) {
444
+            Log::channel("gong")->error('sign校验失败');
445
+            return app('json')->fail('sign校验失败');
446
+        }
447
+        unset($data['sign']);
448
+        if (empty($data['token'])) {
449
+            return app('json')->fail('参数为空');
450
+        }
451
+        $spuIds = explode(',', $data['token']);
452
+
453
+        Db::startTrans();
454
+        try{
455
+            // 删除选品库对应的数据
456
+            Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
457
+                'status' => 9,
458
+                'update_time' => date('Y-m-d H:i:s')
459
+            ]);
460
+            // 删除商品管理对应的数据
461
+            Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
462
+                'is_del' => 1,
463
+                'is_show' => 0
464
+            ]);
465
+            Db::commit();
466
+            return app('json')->success('操作成功');
467
+        } catch (Exception $e) {
468
+            Db::rollback();
469
+            Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
470
+            return app('json')->fail($e->getMessage());
471
+        }
472
+    }
473
+
474
+    public function edit()
475
+    {
476
+        $params = $this->request->params(['spu_id', 'sku_info', 'cxb_cate_id', 'sign']);
477
+        Log::channel("gong")->info('供应链修改商品数据:' . json_encode($params));
478
+
479
+        // 校验sign
480
+        $checkSign = $this->checkSign($params);
481
+        if (!$checkSign) {
482
+            Log::channel("gong")->error('sign校验失败');
483
+            return app('json')->fail('sign校验失败');
484
+        }
485
+        unset($params['sign']);
486
+        $spu_id = $params['spu_id'];
487
+        $sku_info = $params['sku_info'];
488
+        $cxb_cate_id = $params['cxb_cate_id'];
489
+        if (!empty($cxb_cate_id) && !empty($spu_id) && empty($sku_info)) {
490
+            try{
491
+                Db::startTrans();
492
+                Db::name('douhuomall')->where('spu_id', $spu_id)->update([
493
+                    'cxb_cate_id' => $cxb_cate_id,
494
+                    'update_time' => date('Y-m-d H:i:s')
495
+                ]);
496
+                Db::name('store_product')->where('spu_id', $spu_id)->where('mer_id', $this->specialMerId)->update(['cate_id' => $cxb_cate_id]);
497
+                Db::commit();
498
+                return app('json')->success('操作成功');
499
+            } catch (Exception $e) {
500
+                Db::rollback();
501
+                Log::channel("gong")->error('供应链修改商品分类失败:' . $e->getMessage());
502
+                return app('json')->fail($e->getMessage());
503
+            }
504
+        }
505
+
506
+        if (!empty($spu_id) && !empty($sku_info)) {
507
+            try {
508
+                // 修改选品库中的sku信息
509
+                $data['skuId_info'] = $sku_info;
510
+                $sku_info = json_decode($sku_info, true);
511
+                $skuInfo = $this->arrSort($sku_info, 'cost_price', SORT_ASC);
512
+                $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
513
+                $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
514
+                $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
515
+                $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
516
+                $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
517
+                $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
518
+                $data['update_time'] = date('Y-m-d H:i:s');
519
+                Db::startTrans();
520
+                Db::name('douhuomall')->where('spu_id', $spu_id)->update($data);
521
+                $productModel = Db::name('store_product')->where('spu_id', $spu_id)->where('is_del', 0);
522
+                $productIds = $productModel->column('product_id');
523
+                // 修改商品库中的sku信息
524
+                foreach ($sku_info as $value) {
525
+                    $skuModel = Db::name('store_product_attr_value')
526
+                        ->where('gong_sku_id', $value['sku_sn'])
527
+                        ->whereIn('product_id', $productIds);
528
+                    $skuOld = $skuModel->select()->toArray();
529
+                    Log::channel("gong")->info('供应链修改之前的sku信息数据:' . json_encode($skuOld));
530
+                    $skuModel->update([
531
+                        'price' => $value['market_price'],
532
+                        'gong_mer_profit' => $value['profit'],
533
+                        'gong_pension' => $value['yanglaojin'],
534
+                        'gong_market_price' => $value['market_price'],
535
+                        'plate_mer_profit' => $value['profit'],
536
+                        'ot_price' => $value['retail_price']
537
+                    ]);
538
+                }
539
+                $productOld = $productModel->select()->toArray();
540
+                Log::channel("gong")->info('供应链修改之前的商品基本信息数据:' . json_encode($productOld));
541
+                $yanglaojin_scale = (!empty($data['market_price']) && !empty($data['pension'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
542
+                $productModel->where('mer_id', '<>', $this->specialMerId)->update([
543
+                    'is_show' => 0,
544
+                    'price'=>$data['market_price'],
545
+                    'cost'=>$data['cost_price'],
546
+                    'ot_price'=>$data['ot_price'],
547
+                    'plate_mer_profit' => $data['mer_profit'],
548
+                    'yanglaojin_scale' => $yanglaojin_scale
549
+                ]);
550
+
551
+                Db::name('store_product')
552
+                    ->where('mer_id', $this->specialMerId)
553
+                    ->where('spu_id', $spu_id)
554
+                    ->where('is_del', 0)
555
+                    ->update([
556
+                        'price'=>$data['market_price'],
557
+                        'cost'=>$data['cost_price'],
558
+                        'ot_price'=>$data['ot_price'],
559
+                        'plate_mer_profit' => $data['mer_profit'],
560
+                        'yanglaojin_scale' => $yanglaojin_scale
561
+                    ]);
562
+                Db::commit();
563
+                return app('json')->success('操作成功');
564
+            } catch (Exception $e) {
565
+                Db::rollback();
566
+                Log::channel("gong")->error('供应链修改商品销售价失败:' . $e->getMessage());
567
+                return app('json')->fail($e->getMessage());
568
+            }
569
+        }
570
+        return app('json')->fail('数据错误');
571
+    }
572
+
573
+    public function category()
574
+    {
575
+        $url = "https://dc-api.hxxfb.com/douhuomallCategory/getCateAll";
576
+        $result = curlGet($url);
577
+        if (!isset($result['status']) || $result['status'] != 200) {
578
+            return app('json')->success([]);
579
+        }
580
+        return app('json')->success($result);
581
+    }
582
+
583
+    /**
584
+     * 二维数组根据某个字段排序
585
+     * @param array $array 要排序的数组
586
+     * @param string $keys   要排序的键字段
587
+     * @param string $sort  排序类型  SORT_ASC     SORT_DESC
588
+     * @return array 排序后的数组
589
+     */
590
+    function arrSort($array, $keys, $sort = SORT_DESC) {
591
+        $keysValue = [];
592
+        foreach ($array as $k => $v) {
593
+            $keysValue[$k] = $v[$keys];
594
+        }
595
+        array_multisort($keysValue, $sort, $array);
596
+        return $array;
597
+    }
598
+
599
+    /** 校验sign
600
+     * @param $data
601
+     * @return false
602
+     */
603
+    function checkSign($data) {
604
+        if (empty($data['sign'])) {
605
+            return false;
606
+        }
607
+        $originalSign = $data['sign'];
608
+        unset($data['sign']);
609
+        ksort($data);
610
+        $params = '';
611
+        foreach($data as $key => $value) {
612
+            $params .= '&' . $key . '=' . $value;
613
+        }
614
+        $params = ltrim($params, '&');
615
+        $sign = md5($params . '&signKey=' . $this->signKey);
616
+        Log::channel("gong")->info("参数sign:" .$originalSign . ',校验生成sign' . $sign);
617
+        if ($originalSign != $sign) {
618
+            return false;
619
+        }
620
+        return true;
621
+    }
622
+}

+ 335 - 0
app/controller/admin/gong/Product.php

@@ -0,0 +1,335 @@
1
+<?php
2
+
3
+
4
+namespace app\controller\admin\gong;
5
+use app\common\repositories\store\product\ProductRepository;
6
+use crmeb\basic\BaseController;
7
+use think\facade\Db;
8
+
9
+class Product extends BaseController
10
+{
11
+    protected $chengben = 0.13;
12
+    protected $lingshou = 0.65;
13
+    protected $ot = 0.8;
14
+    //商品列表
15
+    public function lst()
16
+    {
17
+        $mer_id=$this->request->merId();
18
+        $chengbenjiabili=0.04;
19
+        $lingshoujiabili=0.3;
20
+        [$page, $limit] = $this->getPage();
21
+        $params=$this->request->param();
22
+        $product_sql=Db::name('douhuomall')->where('status',1)->field('spu_id,title,cost_price,market_price,profit,spuId_info');
23
+        $count=$product_sql->count('spu_id');
24
+        $product_sql=$this->product_where($product_sql,$params);
25
+        $product_lst=$product_sql->page($page,$limit)->select()->toArray();
26
+        foreach ($product_lst as $k=>&$v){
27
+            if(isset($v['is_show'])){
28
+                $v['is_show']=(int)$v['is_show'];
29
+            }
30
+
31
+            //商品图片处理
32
+            $v['spuId_info']=json_decode($v['spuId_info'],true);
33
+            $v['img']=$v['spuId_info']['cover_url'];
34
+            unset($v['spuId_info']);
35
+
36
+
37
+            $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$v['spu_id'])->value('product_id');
38
+            if($store_product_data){
39
+                $v['is_join']=0;
40
+            }else{
41
+                $v['is_join']=1;
42
+            }
43
+            $cost_price = $v['cost_price'];
44
+            //成本价 加价
45
+            $product_lst[$k]['cost_price'] = sprintf("%01.2f", $cost_price + $cost_price * $this -> chengben);//四舍五入保留两位小数
46
+            //零售价 加价
47
+            $product_lst[$k]['market_price']= sprintf("%01.2f", $cost_price + $cost_price * $this -> lingshou);//四舍五入保留两位小数
48
+            //最小利润
49
+            $product_lst[$k]['small_profit']=bcsub($product_lst[$k]['market_price'],$product_lst[$k]['cost_price'],2);
50
+            //利润率
51
+            $product_lst[$k]['profit'] = sprintf("%01.2f", ($product_lst[$k]['market_price'] - $product_lst[$k]['cost_price']) / $product_lst[$k]['cost_price']);//四舍五入保留两位小数
52
+//            //利润率处理
53
+//            $v['profit']=explode('.',$v['profit']);
54
+//            if($v['profit'][1]=='00'){
55
+//                $v['profit']=$v['profit'][0].'%';
56
+//            }else{
57
+//                $v['profit']=$v['profit'][0].'.'.$v['profit'][1].'%';
58
+//            }
59
+        }
60
+        $retrun_data=[
61
+            'count'=>$count,
62
+            'data'=>$product_lst
63
+        ];
64
+        return app('json')->success($retrun_data);
65
+    }
66
+
67
+    public function product_where($sql,$where)
68
+    {
69
+        //商品名称
70
+        if (isset($where['title'])&&$where['title']!=''){
71
+            $sql->whereLike('title','%'.$where['title'].'%');
72
+        }
73
+        //商品分类
74
+        if (isset($where['cate_ids'])&&$where['cate_ids']!==''){
75
+            $sql->whereLike('cate_ids',$where['cate_ids'].',%');
76
+        }
77
+        //最高利润率
78
+        if(isset($where['profit_max'])&&$where['profit_max']){
79
+            $sql->where('profit','<',$where['profit_max']);
80
+        }
81
+        //最低利润率
82
+        if(isset($where['profit_small'])&&$where['profit_small']){
83
+            $sql->where('profit','>',$where['profit_small']);
84
+        }
85
+        //成本价
86
+        if (isset($where['cost_price'])&&$where['cost_price']==1){
87
+            $sql->order('cost_price','desc');
88
+        }elseif(isset($where['cost_price'])&&$where['cost_price']==2){
89
+            $sql->order('cost_price','asc');
90
+        }
91
+        //零售价
92
+        if (isset($where['market_price'])&&$where['market_price']==1){
93
+            $sql->order('market_price','desc');
94
+        }elseif(isset($where['cost_price'])&&$where['market_price']==2){
95
+            $sql->order('market_price','asc');
96
+        }
97
+        return $sql;
98
+    }
99
+    //分类列表
100
+    public function get_category()
101
+    {
102
+        [$page, $limit] = $this->getPage();
103
+        $data=Db::name('douhuomall_category')->page($page,$limit)->select()->toArray();
104
+        $count=Db::name('douhuomall_category')->count('id');
105
+        $retrun_data=[
106
+            'count'=>$count,
107
+            'data'=>$data
108
+        ];
109
+        return app('json')->success($retrun_data);
110
+    }
111
+    //商品详情
112
+    public function product_details($spu_id)
113
+    {
114
+        $chengbenjiabili=0.04;
115
+        $lingshoujiabili=0.3;
116
+        $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->field('skuId_info,spuId_info,cost_price,market_price,profit')->find();
117
+        $product_data['spuId_info']=json_decode($product_data['spuId_info'],true);
118
+//        foreach ($product_data['spuId_info'] as $k=> &$spuid){
119
+            //成本价 成本价=供应链成本价+供应链成本价*成本价加价比例
120
+//            $product_data['cost_price']=bcadd(bcmul($product_data['cost_price'],$chengbenjiabili,2),$product_data['cost_price'],2);
121
+//            //零售价 零售价=平台成本价+平台成本价*零售价格加价比例
122
+//            $product_data['market_price']=bcadd($product_data['cost_price'],bcmul($product_data['cost_price'],$lingshoujiabili,2),2);
123
+//            $product_data['retail_price']=$product_data['market_price'];
124
+
125
+        $cost_price = $product_data['cost_price'];
126
+        //成本价 加价45%
127
+        $product_data['cost_price'] = sprintf("%01.2f", $cost_price + $cost_price * $this -> chengben);//四舍五入保留两位小数
128
+        //零售价 加价65%
129
+        $product_data['market_price']= sprintf("%01.2f", $cost_price + $cost_price * $this -> lingshou);//四舍五入保留两位小数
130
+        $product_data['retail_price']=$product_data['market_price'];
131
+//        }
132
+        $product_data['skuId_info']=json_decode($product_data['skuId_info'],true);
133
+        foreach ($product_data['skuId_info'] as &$skuId){
134
+//            //成本价 成本价=供应链成本价+供应链成本价*成本价加价比例
135
+//            $skuId['cost_price']=bcadd(bcmul($skuId['cost_price'],$chengbenjiabili,2),$skuId['cost_price'],2);
136
+//            //零售价 零售价=平台成本价+平台成本价*零售价格加价比例
137
+//            $skuId['market_price']=bcadd($skuId['cost_price'],bcmul($skuId['cost_price'],$lingshoujiabili,2),2);
138
+//            $skuId['retail_price']=$skuId['market_price'];
139
+//            $sku_cost_price = $product_data['cost_price'];
140
+//            //成本价 加价45%
141
+//            $product_data['cost_price'] = sprintf("%01.2f", $sku_cost_price + $sku_cost_price * 0.45);//四舍五入保留两位小数
142
+//            //零售价 加价65%
143
+//            $product_data['market_price']= sprintf("%01.2f", $sku_cost_price + $sku_cost_price * 0.65);//四舍五入保留两位小数
144
+//            $product_data['retail_price']=$product_data['market_price'];
145
+
146
+            $name='';
147
+            foreach($skuId['attribute_json'] as $k=>$v){
148
+//                $skuId['attribute_json']=$skuId['attribute_json'][0]['val'].$skuId['attribute_json'][1]['val'];
149
+                $name.=$v['val'];
150
+            }
151
+            $skuId['attribute_json']=$name;
152
+        }
153
+        return app('json')->success($product_data);
154
+    }
155
+    //加入选品库
156
+    public function join_selection_library()
157
+    {
158
+        $data = $this->request->params(['spu_id']);
159
+        $spu_id = $data['spu_id'];
160
+        $mer_id=$this->request->merId();
161
+        $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->value('product_id');
162
+        if($store_product_data)
163
+            return app('json')->fail('此商品已加入选品库');
164
+        $cate_id=$this->request->param('cate_id','');
165
+        if($cate_id==''){
166
+            return app('json')->fail('请选择分类ID');
167
+        }
168
+        $chengbenjiabili=0.04;
169
+        $lingshoujiabili=0.3;
170
+        $data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
171
+        //最低售价
172
+        $sku_data=json_decode($data['skuId_info'],true);
173
+        $spec_type=count($sku_data);
174
+        if($spec_type>1){
175
+            $spec_type=1;
176
+        }else{
177
+            $spec_type=0;
178
+        }
179
+//        $price=array_column($sku_data,'cost_price_f');//使用加完运费的成本价  原成本价字段是 cost_price
180
+//        //成本价 成本价=供应链成本价+供应链成本价*成本价加价比例
181
+//        $cost_price=bcadd(bcmul(min($price),$chengbenjiabili,2),min($price),2);
182
+//        //零售价 零售价=平台成本价+平台成本价*零售价格加价比例
183
+//        $price=bcadd($cost_price,bcmul($cost_price,$lingshoujiabili,2),2);
184
+
185
+        $cost_price_g = $data['cost_price'];
186
+        //成本价 加价
187
+        $cost_price = sprintf("%01.2f", $cost_price_g + $cost_price_g * $this -> chengben);//四舍五入保留两位小数
188
+        //零售价 加价
189
+        $price= sprintf("%01.2f", $cost_price_g + $cost_price_g * $this -> lingshou);//四舍五入保留两位小数
190
+        //零售价 加价
191
+        $ot_price= sprintf("%01.2f", $cost_price_g + $cost_price_g * $this -> ot);//四舍五入保留两位小数
192
+
193
+        //商品主图
194
+        $spuId_info=json_decode($data['spuId_info'],true);
195
+        $slider_image=$spuId_info['detail_img_list']??'';
196
+        $image=$spuId_info['cover_url']??'';
197
+        try {
198
+            $slider_image = implode(',',json_decode($slider_image,true));
199
+        }catch (\Exception $exception){
200
+            $slider_image = $image;
201
+        }
202
+        //商品入库数据
203
+        $product_insert_data=[
204
+            'mer_id'=>$mer_id,
205
+            'image'=>$image,
206
+            'slider_image'=>$slider_image,
207
+            'store_name'=>$data['title'],
208
+            'store_info'=>1,
209
+            'keyword'=>mb_substr($data['title'],0,3),
210
+            'is_show'=>0,
211
+            'status'=>1,
212
+            'cate_id'=>$cate_id,
213
+            'unit_name'=>'个',
214
+            'price'=>$price,
215
+            'cost'=>$cost_price,
216
+            'ot_price'=>$ot_price,
217
+            'stock'=>'1000',
218
+            'spec_type'=>$spec_type,
219
+            'extension_type'=>1,
220
+            'mer_status'=>1,
221
+            'is_used'=>1,
222
+            'old_product_id'=>$data['id'],
223
+            'volunteer'=>0,
224
+            'type'=>1,
225
+            'pension'=>'0.00',
226
+            'commission'=>5,
227
+            'spu_id'=>$data['spu_id'],
228
+            'temp_id' => 105
229
+        ];
230
+        $insert_id=Db::name('store_product')->insertGetId($product_insert_data);
231
+//        $insert_id=440;
232
+        $detail = $spuId_info['detail'];
233
+        if(!is_null(json_decode($detail))){
234
+            $detail_list = json_decode($detail);
235
+            $detail = '';
236
+            foreach($detail_list as $value){
237
+                $detail .= '<img src="'.$value.'"></img>';
238
+            }
239
+        }
240
+        Db::name('store_product_content')->insert(['content'=>$detail,'product_id'=>$insert_id]);
241
+        $this->insert_sku($insert_id,$sku_data);
242
+        return app('json')->success('操作成功');
243
+    }
244
+
245
+    public function insert_sku($id,$data)
246
+    {
247
+        $chengbenjiabili=0.04;
248
+        $lingshoujiabili=0.3;
249
+        $sku_attr_data=array_column($data,'attribute_json');
250
+        $name='';
251
+        $ProductRepository = app()->make(ProductRepository::class);
252
+        if(empty($sku_attr_data[0][0])){
253
+            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
254
+            $key='';
255
+            $num=1;
256
+            foreach ($data as $k=>$v){
257
+//                $cost=bcadd(bcmul($v['cost_price_f'],$chengbenjiabili,2),$v['cost_price_f'],2);
258
+//                $price=bcadd($cost,bcmul($cost,$lingshoujiabili,2),2);
259
+
260
+                $cost = $v['cost_price'];
261
+                //成本价 加价45%
262
+                $v['cost_price'] = sprintf("%01.2f", $cost + $cost * $this -> chengben);//四舍五入保留两位小数
263
+                //销售价 加价65%
264
+                $price = sprintf("%01.2f", $cost + $cost * $this -> lingshou);//四舍五入保留两位小数
265
+                //原价
266
+                $ot_price = sprintf("%01.2f", $cost + $cost * $this -> ot);//四舍五入保留两位小数
267
+
268
+
269
+                Db::name('store_product_attr_value')->insert([
270
+                    'product_id'=>$id,
271
+                    'detail'=>json_encode(['规格'=>$key]),
272
+                    'sku'=>$key,
273
+                    'image'=>$v['img_url'],
274
+                    'cost'=>$v['cost_price'],
275
+                    'ot_price'=>$ot_price,
276
+                    'price'=>$price,
277
+                    'unique'=>$ProductRepository->setUnique($id,'',0),
278
+                    'stock'=>100,
279
+                    'cost_price'=>$cost,
280
+                    'extension_one'=>5,
281
+                    'gong_sku_id'=>$v['sku_sn']
282
+                ]);
283
+                $key='';
284
+                $num++;
285
+            }
286
+        }else{
287
+            foreach ($sku_attr_data as $kk=>$vv){
288
+
289
+                foreach ($vv as $k1=>$v1){
290
+                    $name.=$v1['val'];
291
+                }
292
+                $name.='-!-';
293
+            }
294
+            $name=rtrim($name,'-!-');
295
+            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
296
+            $key='';
297
+            $num=1;
298
+            foreach ($data as $k=>$v){
299
+                foreach ($v['attribute_json'] as $k2=>$v2){
300
+                    $key.=$v2['val'];
301
+                }
302
+//                $cost=bcadd(bcmul($v['cost_price_f'],$chengbenjiabili,2),$v['cost_price_f'],2);
303
+//                $price=bcadd($cost,bcmul($cost,$lingshoujiabili,2),2);
304
+
305
+                $cost = $v['cost_price'];
306
+                //成本价 加价45%
307
+                $v['cost_price'] = sprintf("%01.2f", $cost + $cost * $this -> chengben);//四舍五入保留两位小数
308
+                //销售价 加价65%
309
+                $price = sprintf("%01.2f", $cost + $cost * $this -> lingshou);//四舍五入保留两位小数
310
+                //原价
311
+                $ot_price = sprintf("%01.2f", $cost + $cost * $this -> ot);//四舍五入保留两位小数
312
+
313
+
314
+                Db::name('store_product_attr_value')->insert([
315
+                    'product_id'=>$id,
316
+                    'detail'=>json_encode(['规格'=>$key]),
317
+                    'sku'=>$key,
318
+                    'image'=>$v['img_url'],
319
+                    'cost'=>$v['cost_price'],
320
+                    'ot_price'=>$ot_price,
321
+                    'price'=>$price,
322
+                    'unique'=>$ProductRepository->setUnique($id,'',0),
323
+                    'stock'=>100,
324
+                    'cost_price'=>$cost,
325
+                    'extension_one'=>5,
326
+                    'gong_sku_id'=>$v['sku_sn']
327
+                ]);
328
+                $key='';
329
+                $num++;
330
+            }
331
+        }
332
+
333
+
334
+    }
335
+}

+ 12 - 0
route/admin.php

@@ -625,6 +625,18 @@ Route::group(config('admin.api_admin_prefix') . '/', function () {
625 625
 
626 626
         })->prefix('admin.store.StoreProduct');
627 627
 
628
+        Route::group('gong/goods',function(){
629
+            Route::get('lst', '/lst');
630
+            Route::post('detail', '/product_details')->name('goods_product_details');
631
+            Route::post('updatePrice', '/update_Price')->name('goods_product_updatePrice');
632
+            Route::post('join_selection_library', '/join_selection_library')->name('join_selection_library');
633
+            Route::post('batch_join_selection_library', '/batch_join_selection_library')->name('batch_join_selection_library');
634
+            Route::get('status_filter', '/getStatusFilter')->name('getStatusFilter');
635
+        })->prefix('admin.gong.Goods');
636
+
637
+        Route::get('/gong/category', 'admin.gong.Goods/category');
638
+
639
+
628 640
         //直播间
629 641
         Route::group('broadcast/room', function () {
630 642
             Route::get('lst', '/lst')->name('systemBroadcastRoomLst');