Forráskód Böngészése

分批次同步数据(优化流程)

LAPTOP-VT1IP978\suxio 2 hete
szülő
commit
293c0198ee

+ 7 - 7
app/Console/Commands/SyncSealDrugReportCommand.php

@@ -8,24 +8,24 @@ use Illuminate\Console\Command;
 
 class SyncSealDrugReportCommand extends Command
 {
-    // php artisan sync:seal-drug-report --start-time="2025-03-01 00:00:00" --end-time="2025-03-31 23:59:59"
-    protected $signature = 'sync:seal-drug-report {--start-time=} {--end-time=} {--page=1} {--page-size=20}';
+    // php artisan sync:seal-drug-report --full --start-time="2025-03-01 00:00:00" --end-time="2025-03-31 23:59:59"
+    protected $signature = 'sync:seal-drug-report {--full : 全量模式} {--start-time= : 开始时间} {--end-time= : 结束时间} {--page=1} {--page-size=20}';
 
     protected $description = '同步已签收药检报告数据';
 
     public function handle(SyncSealDrugReportService $service): int
     {
+        $isFull = (bool)$this->option('full');
         $startTime = $this->option('start-time') ?? Carbon::today()->startOfDay();
-        $endTime = $this->option('end-time') ?? Carbon::now();
+        $endTime = $this->option('end-time')/* ?? Carbon::now()*/;
         $page = $this->option('page') ?? 1;
         $pageSize = $this->option('page-size') ?? 20;
 
-        echo sprintf('已签收药检报告数据[%s, %s, %d, %d]:', $startTime, $endTime, $page, $pageSize), PHP_EOL;
-        echo "全量同步开始……", PHP_EOL;
+        echo $isFull ? "全量同步开始" : "增量同步开始", sprintf('[%d, %s, %s, %d, %d]……', $isFull, $startTime, $endTime, $page, $pageSize), PHP_EOL;
 
-        $count = $service->syncSealDrugReport($startTime, $endTime, $page, $pageSize);
+        $count = $service->syncSealDrugReport($isFull, $startTime, $endTime, $page, $pageSize);
 
-        echo "全量同步结束,共处理 {$count} 条记录!", PHP_EOL;
+        echo $isFull ? "全量同步结束" : "增量同步结束", ",共处理 {$count} 条记录!", PHP_EOL;
 
         return Command::SUCCESS;
     }

+ 56 - 6
app/Services/SyncSealDrugReportService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Models\DrugReportInfo;
+use Carbon\Carbon;
 use Illuminate\Support\Facades\Log;
 
 class SyncSealDrugReportService
@@ -11,20 +12,22 @@ class SyncSealDrugReportService
 
     /**
      * 分批次同步数据
+     * @param bool $isFull
      * @param string $startTime
      * @param string $endTime
      * @param int $page
      * @param int $pageSize
      * @return int
      */
-    public function syncSealDrugReport(string $startTime, string $endTime, int $page, int $pageSize): int
+    public function syncSealDrugReport(bool $isFull, string $startTime, string $endTime = '', int $page = 1, int $pageSize = 20): int
     {
         $hasMore = true;
         $total = 0;
 
-        Log::channel('sync')->warning("开始全量同步", [
+        Log::channel('sync')->warning($isFull ? "开始全量同步" : "开始增量同步", [
             'source' => $this->source,
             'params' => [
+                'is_full' => $isFull,
                 'start_time' => $startTime,
                 'end_time' => $endTime,
                 'page' => $page,
@@ -32,14 +35,61 @@ class SyncSealDrugReportService
             ],
         ]);
 
+        if (empty($endTime)) {
+            $endTime = Carbon::parse($startTime)->copy()->endOfMonth()->toDateTimeString();
+        }
+
         $queryDrugReportService = app(QueryDrugReportService::class);
 
         while ($hasMore) {
             try {
+                if (Carbon::parse($startTime)->isFuture()) {
+                    Log::channel('sync')->warning("时间大于当前时间,同步终止", [
+                        'source' => $this->source,
+                        'params' => [
+                            'is_full' => $isFull,
+                            'start_time' => $startTime,
+                            'end_time' => $endTime,
+                            'page' => $page,
+                            'page_size' => $pageSize,
+                        ]
+                    ]);
+                    break;
+                }
+
+                Log::channel('sync')->info("批次 {$page} 开始处理", [
+                    'source' => $this->source,
+                    'params' => [
+                        'is_full' => $isFull,
+                        'start_time' => $startTime,
+                        'end_time' => $endTime,
+                        'page' => $page,
+                        'page_size' => $pageSize,
+                    ]
+                ]);
+
                 // 分批次拉取数据
                 $data = $queryDrugReportService->querySealDrugReport($startTime, $endTime, $page, $pageSize);
                 if (empty($data)) {
-                    $hasMore = false;
+                    Log::channel('sync')->warning("批次 {$page} 没有数据", [
+                        'source' => $this->source,
+                        'params' => [
+                            'is_full' => $isFull,
+                            'start_time' => $startTime,
+                            'end_time' => $endTime,
+                            'page' => $page,
+                            'page_size' => $pageSize,
+                        ]
+                    ]);
+
+                    if ($isFull) {
+                        $startTime = Carbon::parse($startTime)->copy()->addMonth()->startOfMonth()->toDateTimeString();
+                        $endTime = Carbon::parse($endTime)->copy()->addMonth()->endOfMonth()->toDateTimeString();
+                        $page = 1;
+                        continue;
+                    }
+
+//                    $hasMore = false;
                     break;
                 }
 
@@ -51,14 +101,13 @@ class SyncSealDrugReportService
                     'source' => $this->source,
                     'fetch_count' => count($data),
                     'save_count' => $saveCount,
-                    'total' => $total
+                    'total' => $total,
                 ]);
 
                 // 检查是否还有更多数据
                 $hasMore = (count($data) === $pageSize);
 
                 $page++;
-                break;
 
                 // 避免请求过于频繁
                 sleep(1);
@@ -67,6 +116,7 @@ class SyncSealDrugReportService
                 Log::channel('sync')->error('获取批次数据失败', [
                     'source' => $this->source,
                     'params' => [
+                        'is_full' => $isFull,
                         'start_time' => $startTime,
                         'end_time' => $endTime,
                         'page' => $page,
@@ -78,7 +128,7 @@ class SyncSealDrugReportService
             }
         }
 
-        Log::channel('sync')->warning("全量同步完成", [
+        Log::channel('sync')->warning($isFull ? "全量同步完成" : "增量同步完成", [
             'source' => $this->source,
             'total' => $total
         ]);