Pārlūkot izejas kodu

命令行-修复拉取微唯宝商品逻辑

sunxbiao 6 mēneši atpakaļ
vecāks
revīzija
b69857204e

+ 61 - 80
app/command/WiwibaoProductSync.php

@@ -4,10 +4,14 @@ declare (strict_types=1);
4
 namespace app\command;
4
 namespace app\command;
5
 
5
 
6
 use app\common\enum\douhuomall\DouhuomallEnum;
6
 use app\common\enum\douhuomall\DouhuomallEnum;
7
+use app\common\enum\store\ProductEnum;
7
 use app\traits\GongApiRequest;
8
 use app\traits\GongApiRequest;
8
 use think\console\Command;
9
 use think\console\Command;
9
 use think\console\Input;
10
 use think\console\Input;
10
 use think\console\Output;
11
 use think\console\Output;
12
+use think\db\exception\DataNotFoundException;
13
+use think\db\exception\DbException;
14
+use think\db\exception\ModelNotFoundException;
11
 use think\facade\Db;
15
 use think\facade\Db;
12
 use think\facade\Log;
16
 use think\facade\Log;
13
 
17
 
@@ -20,7 +24,7 @@ class WiwibaoProductSync extends Command
20
     protected $config = [
24
     protected $config = [
21
         'page_size' => 100,      // 每页拉取数量
25
         'page_size' => 100,      // 每页拉取数量
22
         'max_pages' => 0,       // 最大页数限制
26
         'max_pages' => 0,       // 最大页数限制
23
-        'price_change_rate' => 0.1, // 价格变动比率阈值(10%)
27
+        'price_change_rate' => 0.0, // 价格变动比率阈值(10%)
24
     ];
28
     ];
25
 
29
 
26
     protected function configure()
30
     protected function configure()
@@ -337,8 +341,8 @@ class WiwibaoProductSync extends Command
337
             // 检查是否有已下架的商品
341
             // 检查是否有已下架的商品
338
             $count = Db::name('store_product')
342
             $count = Db::name('store_product')
339
                 ->where('spu_id', $newData['spu_id'])
343
                 ->where('spu_id', $newData['spu_id'])
340
-                ->where('is_show', 0)
341
-                ->where('is_status', 0)
344
+                ->where('is_show', ProductEnum::IS_SHOW['No']['code'])
345
+                ->where('is_status', ProductEnum::IS_STATUS['Automatic']['code'])
342
                 ->count();
346
                 ->count();
343
 
347
 
344
             return $count > 0;
348
             return $count > 0;
@@ -355,8 +359,8 @@ class WiwibaoProductSync extends Command
355
         Db::name('store_product')
359
         Db::name('store_product')
356
             ->where('spu_id', $spuId)
360
             ->where('spu_id', $spuId)
357
             ->update([
361
             ->update([
358
-                'is_show' => 0,
359
-                'status' => 1
362
+                'is_show' => ProductEnum::IS_SHOW['No']['code'],
363
+                'status' => ProductEnum::STATUS['Approved']['code']
360
             ]);
364
             ]);
361
 
365
 
362
         Log::info("恢复商品上架(恢复到仓库中)状态: spu_id={$spuId}");
366
         Log::info("恢复商品上架(恢复到仓库中)状态: spu_id={$spuId}");
@@ -368,73 +372,57 @@ class WiwibaoProductSync extends Command
368
     protected function checkAndProcessOnlineProduct($spuId, $syncData)
372
     protected function checkAndProcessOnlineProduct($spuId, $syncData)
369
     {
373
     {
370
         // 检查商品是否已上架
374
         // 检查商品是否已上架
371
-        $product = Db::name('store_product')
372
-            ->where('spu_id', $spuId)
373
-            ->find();
374
-
375
-        if (!$product) {
376
-            return;
377
-        }
378
-
379
-        // 获取本地商品价格信息
380
-        $localPrices = $this->getLocalProductPrices($product['product_id']);
381
-
382
-        // 解析同步数据中的价格
383
-        $syncSkuInfo = $syncData['skuId_info'];
384
-        $syncPrices = $this->extractPricesFromSkuInfo($syncSkuInfo);
385
-
386
-        // 比较价格,判断是否需要下架
387
-        $priceChanged = $this->checkPriceChange($localPrices, $syncPrices);
375
+        try {
376
+            $productList = Db::name('store_product')
377
+                ->where('is_show', ProductEnum::IS_SHOW['Yes']['code'])
378
+                ->where('spu_id', $spuId)
379
+                ->select()
380
+                ->toArray();
381
+
382
+            if (empty($productList)) {
383
+                return;
384
+            }
388
 
385
 
389
-        if ($priceChanged) {
390
-            // 价格变动超过阈值,下架商品并记录
391
-            $this->offlineProductWithRecord($product, $syncData, $localPrices, $syncPrices);
392
-        } else {
393
-            // 价格未变动或变动很小,只更新图片
394
-            $this->updateProductImages($spuId);
395
-        }
396
-    }
386
+            // 解析同步数据中的价格
387
+            $syncPrices = [];
388
+            foreach ($syncData['skuId_info'] as $sku) {
389
+                $syncPrices[] = [
390
+                    'sku_id' => $sku['sku_id'] ?? '',
391
+                    'market_price' => $sku['market_price'] ?? 0,
392
+                    'cost_price' => $sku['cost_price'] ?? 0,
393
+                    'retail_price' => $sku['retail_price'] ?? 0
394
+                ];
395
+            }
397
 
396
 
398
-    /**
399
-     * 获取本地商品价格信息
400
-     */
401
-    protected function getLocalProductPrices($productId)
402
-    {
403
-        $product = Db::name('store_product')
404
-            ->where('product_id', $productId)
405
-            ->field('price, cost, ot_price')
406
-            ->find();
397
+            foreach ($productList as $product) {
398
+                // 获取本地商品价格信息
399
+                // 获取SKU价格
400
+                $skus = Db::name('store_product_attr_value')
401
+                    ->where('product_id', $product['product_id'])
402
+                    ->field('sku, price, cost')
403
+                    ->select()
404
+                    ->toArray();
405
+                $localPrices = [
406
+                    'product_price' => $product['price'] ?? 0,
407
+                    'product_cost' => $product['cost'] ?? 0,
408
+                    'product_ot_price' => $product['ot_price'] ?? 0,
409
+                    'skus' => $skus
410
+                ];
407
 
411
 
408
-        // 获取SKU价格
409
-        $skus = Db::name('store_product_attr_value')
410
-            ->where('product_id', $productId)
411
-            ->field('sku, price, cost')
412
-            ->select()
413
-            ->toArray();
412
+                // 比较价格,判断是否需要下架
413
+                $priceChanged = $this->checkPriceChange($localPrices, $syncPrices);
414
 
414
 
415
-        return [
416
-            'product_price' => $product['price'] ?? 0,
417
-            'product_cost' => $product['cost'] ?? 0,
418
-            'product_ot_price' => $product['ot_price'] ?? 0,
419
-            'skus' => $skus
420
-        ];
421
-    }
415
+                if ($priceChanged) {
416
+                    // 价格变动超过阈值,下架商品并记录
417
+                    $this->offlineProductWithRecord($product, $syncData, $localPrices, $syncPrices);
418
+                } else {
419
+                    // 价格未变动或变动很小,只更新图片
420
+                    $this->updateProductImages($spuId);
421
+                }
422
+            }
423
+        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
422
 
424
 
423
-    /**
424
-     * 从SKU信息中提取价格
425
-     */
426
-    protected function extractPricesFromSkuInfo($skuInfo)
427
-    {
428
-        $prices = [];
429
-        foreach ($skuInfo as $sku) {
430
-            $prices[] = [
431
-                'sku_id' => $sku['sku_id'] ?? '',
432
-                'market_price' => $sku['market_price'] ?? 0,
433
-                'cost_price' => $sku['cost_price'] ?? 0,
434
-                'retail_price' => $sku['retail_price'] ?? 0
435
-            ];
436
         }
425
         }
437
-        return $prices;
438
     }
426
     }
439
 
427
 
440
     /**
428
     /**
@@ -515,9 +503,9 @@ class WiwibaoProductSync extends Command
515
         Db::name('store_product')
503
         Db::name('store_product')
516
             ->where('product_id', $productId)
504
             ->where('product_id', $productId)
517
             ->update([
505
             ->update([
518
-                'is_show' => 0,
519
-                'status' => -2,
520
-                'is_status' => 0
506
+                'is_show' => ProductEnum::IS_SHOW['No']['code'],
507
+                'status' => ProductEnum::STATUS['Withdrawn']['code'],
508
+                'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
521
             ]);
509
             ]);
522
 
510
 
523
         // 3. 收集变动后数据
511
         // 3. 收集变动后数据
@@ -638,22 +626,15 @@ class WiwibaoProductSync extends Command
638
 
626
 
639
         foreach ($products as $product) {
627
         foreach ($products as $product) {
640
             // 记录下架前数据
628
             // 记录下架前数据
641
-            $beforeData = [
642
-                'product_id' => $product['product_id'],
643
-                'spu_id' => $spuId,
644
-                'title' => $product['store_name'] ?? '',
645
-                'status' => $product['is_show'] ?? 0,
646
-                'price' => $product['price'] ?? 0,
647
-                'record_time' => date('Y-m-d H:i:s')
648
-            ];
629
+            $beforeData = $product;
649
 
630
 
650
             // 下架商品
631
             // 下架商品
651
             Db::name('store_product')
632
             Db::name('store_product')
652
                 ->where('product_id', $product['product_id'])
633
                 ->where('product_id', $product['product_id'])
653
                 ->update([
634
                 ->update([
654
-                    'is_show' => 0,
655
-                    'status' => -2,
656
-                    'is_status' => 0
635
+                    'is_show' => ProductEnum::IS_SHOW['No']['code'],
636
+                    'status' => ProductEnum::STATUS['Withdrawn']['code'],
637
+                    'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
657
                 ]);
638
                 ]);
658
 
639
 
659
             // 记录日志
640
             // 记录日志

+ 5 - 0
app/common/enum/store/ProductEnum.php

@@ -36,4 +36,9 @@ class ProductEnum extends CommonEnum
36
         'No' => ['code' => 0, 'name' => '否'],
36
         'No' => ['code' => 0, 'name' => '否'],
37
         'Yes' => ['code' => 1, 'name' => '是']
37
         'Yes' => ['code' => 1, 'name' => '是']
38
     ];
38
     ];
39
+
40
+    const IS_STATUS = [
41
+        'Automatic' => ['code' => 0, 'name' => '系统自动操作'],
42
+        'Triggered' => ['code' => 1, 'name' => '人为触发操作']
43
+    ];
39
 }
44
 }