Explorar o código

梳理从供货商选品至商品表功能

sunxbiao hai 1 ano
pai
achega
1669f7072f
Modificáronse 1 ficheiros con 52 adicións e 46 borrados
  1. 52 46
      app/controller/merchant/gong/Goods.php

+ 52 - 46
app/controller/merchant/gong/Goods.php

@@ -300,6 +300,7 @@ class Goods extends BaseController
300 300
             }
301 301
             Db::name('store_product_content')->insert(['content' => $detail, 'product_id' => $insert_id]);
302 302
 
303
+            // 6、
303 304
             $this->insert_sku($insert_id, $sku_data);
304 305
             return true;
305 306
         } catch (Exception $e) {
@@ -308,72 +309,77 @@ class Goods extends BaseController
308 309
         }
309 310
     }
310 311
 
311
-    public function insert_sku($id,$data)
312
+    public function insert_sku($id, $data)
312 313
     {
313
-        $sku_attr_data=array_column($data,'attribute_json');
314
-        $name='';
314
+        // 1、获取 所有SKU的 规格属性值
315
+        $sku_attr_data = array_column($data, 'attribute_json');
316
+        $name = '';
317
+
318
+        // 2、写入 商品属性表
315 319
         $ProductRepository = app()->make(ProductRepository::class);
316
-        if(empty($sku_attr_data[0][0])){
317
-            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
318
-            $key='';
319
-            $num=1;
320
-            foreach ($data as $k=>$v){
320
+        if (empty($sku_attr_data[0][0])) {
321
+            // 写入商品属性表
322
+            Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
323
+            $key = '';
324
+            $num = 1;
325
+            // 写入 SKU 商品属性值表
326
+            foreach ($data as $k => $v) {
321 327
                 Db::name('store_product_attr_value')->insert([
322
-                    'product_id'=>$id,
323
-                    'detail'=>json_encode(['规格'=>$key]),
324
-                    'sku'=>$key,
325
-                    'image'=>$v['img_url'],
326
-                    'cost'=>$v['cost_price'],
327
-                    'ot_price'=>$v['retail_price'],
328
-                    'price'=>$v['market_price'],
329
-                    'unique'=>$ProductRepository->setUnique($id,$v['sku_id'],0),
330
-                    'stock'=>100,
331
-                    'cost_price'=>$v['cost_price'],
332
-                    'extension_one'=>5,
333
-                    'gong_sku_id'=>$v['sku_id'],
328
+                    'product_id' => $id,
329
+                    'detail' => json_encode(['规格' => $key]),
330
+                    'sku' => $key,
331
+                    'image' => $v['img_url'],
332
+                    'cost' => $v['cost_price'],
333
+                    'ot_price' => $v['retail_price'],
334
+                    'price' => $v['market_price'],
335
+                    'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
336
+                    'stock' => 100,
337
+                    'cost_price' => $v['cost_price'],
338
+                    'extension_one' => 5,
339
+                    'gong_sku_id' => $v['sku_id'],
334 340
                     'gong_mer_profit' => $v['mer_profit'],
335 341
                     'gong_pension' => $v['yanglaojin'],
336 342
                     'gong_market_price' => $v['market_price'],
337 343
                     'plate_mer_profit' => $v['mer_profit']
338 344
                 ]);
339
-                $key='';
345
+                $key = '';
340 346
                 $num++;
341 347
             }
342
-        }else{
343
-            foreach ($sku_attr_data as $kk=>$vv){
344
-                foreach ($vv as $k1=>$v1){
345
-                    $name.=$v1['val'];
348
+        } else {
349
+            foreach ($sku_attr_data as $kk => $vv) {
350
+                foreach ($vv as $k1 => $v1) {
351
+                    $name .= $v1['val'];
346 352
                 }
347
-                $name.='-!-';
353
+                $name .= '-!-';
348 354
             }
349
-            $name=rtrim($name,'-!-');
350
-            Db::name('store_product_attr')->insert(['product_id'=>$id,'attr_name'=>'规格','attr_values'=>$name]);
351
-            $key='';
352
-            $num=1;
353
-            foreach ($data as $k=>$v){
354
-                foreach ($v['attribute_json'] as $k2=>$v2){
355
-                    $key.=$v2['val'];
355
+            $name = rtrim($name, '-!-');
356
+            Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
357
+            $key = '';
358
+            $num = 1;
359
+            foreach ($data as $k => $v) {
360
+                foreach ($v['attribute_json'] as $k2 => $v2) {
361
+                    $key .= $v2['val'];
356 362
                 }
357 363
 
358 364
                 Db::name('store_product_attr_value')->insert([
359
-                    'product_id'=>$id,
360
-                    'detail'=>json_encode(['规格'=>$key]),
361
-                    'sku'=>$key,
362
-                    'image'=>$v['img_url'],
363
-                    'cost'=>$v['cost_price'],
364
-                    'ot_price'=>$v['retail_price'],
365
-                    'price'=>$v['market_price'],
366
-                    'unique'=>$ProductRepository->setUnique($id,$v['sku_id'],0),
367
-                    'stock'=>100,
368
-                    'cost_price'=>$v['cost_price'],
369
-                    'extension_one'=>5,
370
-                    'gong_sku_id'=>$v['sku_id'],
365
+                    'product_id' => $id,
366
+                    'detail' => json_encode(['规格' => $key]),
367
+                    'sku' => $key,
368
+                    'image' => $v['img_url'],
369
+                    'cost' => $v['cost_price'],
370
+                    'ot_price' => $v['retail_price'],
371
+                    'price' => $v['market_price'],
372
+                    'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
373
+                    'stock' => 100,
374
+                    'cost_price' => $v['cost_price'],
375
+                    'extension_one' => 5,
376
+                    'gong_sku_id' => $v['sku_id'],
371 377
                     'gong_mer_profit' => $v['mer_profit'],
372 378
                     'gong_pension' => $v['yanglaojin'],
373 379
                     'gong_market_price' => $v['market_price'],
374 380
                     'plate_mer_profit' => $v['mer_profit']
375 381
                 ]);
376
-                $key='';
382
+                $key = '';
377 383
                 $num++;
378 384
             }
379 385
         }