Kaynağa Gözat

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

- 新增 --limit 参数用于限制处理条数
- 在命令帮助文档中添加使用示例
- 验证 limit 参数值,确保不小于 0
- 输出信息中显示当前限制条数设置
- 查询数据库时根据 limit 参数添加数量限制
shichen 2 ay önce
ebeveyn
işleme
8ed639a144
1 değiştirilmiş dosya ile 15 ekleme ve 4 silme
  1. 15 4
      app/command/AlibabaTodayImport.php

+ 15 - 4
app/command/AlibabaTodayImport.php

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