Quellcode durchsuchen

feat(command): 添加命令行参数限制处理条数功能

- 新增 --limit 参数用于限制处理条数
- 在命令帮助文档中添加使用示例
- 验证 limit 参数值,确保不小于 0
- 输出信息中显示当前限制条数设置
- 查询数据库时根据 limit 参数添加数量限制
shichen vor 2 Monaten
Ursprung
Commit
8ed639a144
1 geänderte Dateien mit 15 neuen und 4 gelöschten Zeilen
  1. 15 4
      app/command/AlibabaTodayImport.php

+ 15 - 4
app/command/AlibabaTodayImport.php

@@ -23,6 +23,7 @@ use think\facade\Db;
23
  *   php think alibaba:today-import --mer_id=3447 --date=2026-05-21          # 指定商户+日期
23
  *   php think alibaba:today-import --mer_id=3447 --date=2026-05-21          # 指定商户+日期
24
  *   php think alibaba:today-import --batch=100                              # 每批处理100条(默认50)
24
  *   php think alibaba:today-import --batch=100                              # 每批处理100条(默认50)
25
  *   php think alibaba:today-import --skip=10                                # 跳过前10条(断点续传)
25
  *   php think alibaba:today-import --skip=10                                # 跳过前10条(断点续传)
26
+ *   php think alibaba:today-import --limit=5                                # 只处理5条(不设置则全部处理)
26
  *
27
  *
27
  * 定时任务配置(每天23:55执行):
28
  * 定时任务配置(每天23:55执行):
28
  *   55 23 * * * php /www/wwwroot/shop/think alibaba:today-import --mer_id=3447 >> /tmp/alibaba_today_import.log 2>&1
29
  *   55 23 * * * php /www/wwwroot/shop/think alibaba:today-import --mer_id=3447 >> /tmp/alibaba_today_import.log 2>&1
@@ -49,7 +50,8 @@ class AlibabaTodayImport extends Command
49
             ->addOption('mer_id', null, \think\console\input\Option::VALUE_OPTIONAL, '商户ID', '3447')
50
             ->addOption('mer_id', null, \think\console\input\Option::VALUE_OPTIONAL, '商户ID', '3447')
50
             ->addOption('date', null, \think\console\input\Option::VALUE_OPTIONAL, '指定日期 (Y-m-d),默认今天', '')
51
             ->addOption('date', null, \think\console\input\Option::VALUE_OPTIONAL, '指定日期 (Y-m-d),默认今天', '')
51
             ->addOption('batch', null, \think\console\input\Option::VALUE_OPTIONAL, '每批处理数量', '50')
52
             ->addOption('batch', null, \think\console\input\Option::VALUE_OPTIONAL, '每批处理数量', '50')
52
-            ->addOption('skip', null, \think\console\input\Option::VALUE_OPTIONAL, '跳过前N条(断点续传)', '0');
53
+            ->addOption('skip', null, \think\console\input\Option::VALUE_OPTIONAL, '跳过前N条(断点续传)', '0')
54
+            ->addOption('limit', null, \think\console\input\Option::VALUE_OPTIONAL, '限制处理条数,不设置则全部处理', '0');
53
     }
55
     }
54
 
56
 
55
     protected function execute(Input $input, Output $output)
57
     protected function execute(Input $input, Output $output)
@@ -66,9 +68,11 @@ class AlibabaTodayImport extends Command
66
         $date  = $input->getOption('date');
68
         $date  = $input->getOption('date');
67
         $batch = (int)$input->getOption('batch');
69
         $batch = (int)$input->getOption('batch');
68
         $skip  = (int)$input->getOption('skip');
70
         $skip  = (int)$input->getOption('skip');
71
+        $limit = (int)$input->getOption('limit');
69
 
72
 
70
         if ($batch < 1) $batch = 50;
73
         if ($batch < 1) $batch = 50;
71
         if ($skip < 0) $skip = 0;
74
         if ($skip < 0) $skip = 0;
75
+        if ($limit < 0) $limit = 0;
72
 
76
 
73
         if (empty($date)) {
77
         if (empty($date)) {
74
             $date = date('Y-m-d');
78
             $date = date('Y-m-d');
@@ -87,6 +91,7 @@ class AlibabaTodayImport extends Command
87
         $output->writeln("查询日期:       {$date}");
91
         $output->writeln("查询日期:       {$date}");
88
         $output->writeln("每批数量:       {$batch}");
92
         $output->writeln("每批数量:       {$batch}");
89
         $output->writeln("跳过前N条:      {$skip}");
93
         $output->writeln("跳过前N条:      {$skip}");
94
+        $output->writeln("限制条数:      " . ($limit > 0 ? $limit : '不限制'));
90
         $output->writeln('分润设置:');
95
         $output->writeln('分润设置:');
91
         $output->writeln("  extension_type:  {$profitConfig['extension_type']} (开启分润)");
96
         $output->writeln("  extension_type:  {$profitConfig['extension_type']} (开启分润)");
92
         $output->writeln("  concession_rate: {$profitConfig['concession_rate']} (售价×{$profitConfig['concession_rate']})");
97
         $output->writeln("  concession_rate: {$profitConfig['concession_rate']} (售价×{$profitConfig['concession_rate']})");
@@ -97,12 +102,18 @@ class AlibabaTodayImport extends Command
97
         $todayEnd   = $date . ' 23:59:59';
102
         $todayEnd   = $date . ' 23:59:59';
98
 
103
 
99
         $output->write('正在查询今日待入库商品... ');
104
         $output->write('正在查询今日待入库商品... ');
100
-        $allIds = Db::name('alibaba_import_goods')
105
+        $query = Db::name('alibaba_import_goods')
101
             ->where('status', 1)
106
             ->where('status', 1)
102
             ->where('create_time', '>=', $todayStart)
107
             ->where('create_time', '>=', $todayStart)
103
             ->where('create_time', '<=', $todayEnd)
108
             ->where('create_time', '<=', $todayEnd)
104
-            ->order('id', 'ASC')
105
-            ->column('id');
109
+            ->order('id', 'ASC');
110
+
111
+        // 如果设置了 limit,则限制查询数量
112
+        if ($limit > 0) {
113
+            $query->limit($limit);
114
+        }
115
+
116
+        $allIds = $query->column('id');
106
 
117
 
107
         $total = count($allIds);
118
         $total = count($allIds);
108
         $output->writeln("共 {$total} 条");
119
         $output->writeln("共 {$total} 条");