Ver código fonte

feat(console): 添加商品价格更新命令

- 在控制台配置中注册新的 ProductPriceUpdate 命令
- 创建 ProductPriceUpdate 类实现价格更新功能
- 实现对指定商品ID的价格递增更新逻辑
- 添加数据库事务确保更新操作的原子性
- 实现价格变更日志记录功能
- 配置命令描述为"商品价格更新"
shichen 5 meses atrás
pai
commit
e6a8e973f3
2 arquivos alterados com 66 adições e 0 exclusões
  1. 65 0
      app/command/ProductPriceUpdate.php
  2. 1 0
      config/console.php

+ 65 - 0
app/command/ProductPriceUpdate.php

@@ -0,0 +1,65 @@
1
+<?php
2
+
3
+namespace app\command;
4
+
5
+use app\common\model\shared\SharedProsperityPartnerConsumeUpdateLog;
6
+use app\common\repositories\shared\SharedProsperityRecommendConfigRepository;
7
+use think\console\Command;
8
+use think\console\Input;
9
+use think\console\Output;
10
+use think\facade\Db;
11
+
12
+class ProductPriceUpdate extends Command
13
+{
14
+    // 变更数量
15
+    const NUMBER = 5;
16
+
17
+    const PRODUCT_IDS = [31053, 32171];
18
+
19
+    protected function configure()
20
+    {
21
+        // 指令配置
22
+        $this->setName('ProductPriceUpdate')
23
+            ->setDescription('商品价格更新');
24
+    }
25
+
26
+    protected function execute(Input $input, Output $output)
27
+    {
28
+        $output->writeln('定时任务执行中...');
29
+
30
+        $this->priceUpdate();
31
+
32
+        $output->writeln('定时任务执行完成');
33
+    }
34
+
35
+    private function priceUpdate()
36
+    {
37
+        // 获取当前数据
38
+        $productData = Db::name('store_product')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
39
+        $attrData = Db::name('store_product_attr_value')->where(['product_id' => self::PRODUCT_IDS])->field('product_id,price')->select()->toArray();
40
+        Db::startTrans();
41
+        try {
42
+            foreach ($productData as $product) {
43
+                $log = [
44
+                    'product_id' => $product['product_id'],
45
+                    'old_price' => $product['price'],
46
+                    'number' => self::NUMBER,
47
+                    'new_price' => $product['price'] + self::NUMBER,
48
+                ];
49
+                Db::name('product_price_update_log')->insert($log);
50
+
51
+                Db::name('store_product')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
52
+                Db::name('store_product_attr_value')->where(['product_id' => $product['product_id']])->inc('price', self::NUMBER)->update();
53
+            }
54
+
55
+;
56
+            Db::commit();
57
+            return true;
58
+        } catch (\Exception $e) {
59
+            Db::rollback();
60
+            throw $e;
61
+        }
62
+
63
+    }
64
+
65
+}

+ 1 - 0
config/console.php

@@ -35,5 +35,6 @@ return [
35 35
         'repair_fzone_pay_type' => 'app\command\repairFzonePayType',
36 36
         'wiwibaoproductsync' => 'app\command\WiwibaoProductSync',
37 37
         'sharedPartnerConsumeUpdate' => 'app\command\SharedPartnerConsumeUpdate',
38
+        'productPriceUpdate' => 'app\command\ProductPriceUpdate',
38 39
     ],
39 40
 ];