warning("开始全量同步", [ 'source' => $this->source, 'params' => [ 'start_time' => $startTime, 'end_time' => $endTime, 'page' => $page, 'page_size' => $pageSize, ], ]); $queryDrugReportService = app(QueryDrugReportService::class); while ($hasMore) { try { // 分批次拉取数据 $data = $queryDrugReportService->querySealDrugReport($startTime, $endTime, $page, $pageSize); if (empty($data)) { $hasMore = false; break; } // 处理并保存数据 $saveCount = $this->saveSealDrugReport($data); $total += $saveCount; Log::channel('sync')->info("批次 {$page} 处理完成", [ 'source' => $this->source, 'fetch_count' => count($data), 'save_count' => $saveCount, 'total' => $total ]); // 检查是否还有更多数据 $hasMore = (count($data) === $pageSize); $page++; break; // 避免请求过于频繁 sleep(1); } catch (\Exception $e) { Log::channel('sync')->error('获取批次数据失败', [ 'source' => $this->source, 'params' => [ 'start_time' => $startTime, 'end_time' => $endTime, 'page' => $page, 'page_size' => $pageSize, ], 'error' => $e->getMessage() ]); continue; } } Log::channel('sync')->warning("全量同步完成", [ 'source' => $this->source, 'total' => $total ]); return $total; } /** * 处理并保存批次数据 * @param array $data * @return int */ protected function saveSealDrugReport(array $data): int { try { $data = $this->correctSealDrugReport($data); return DrugReportInfo::bulkUpsert($data); } catch (\Throwable $e) { Log::channel('sync')->error("保存批次数据失败", [ 'data' => $data, 'error' => $e->getMessage() ]); return 0; } } /** * 处理批次数据 * @param array $data * @return array */ protected function correctSealDrugReport(array $data): array { return array_map(function ($item) { return [ 'drug_report_v2_id' => $item['drug_report_v2_id'] ?? '', 'report_id' => $item['report_id'] ?? '', 'report_name' => $item['drug_report_name'] ?? '', 'report_no' => $item['report_no'] ?? '', 'report_date' => $item['report_date'] ?? '', 'batch_no' => $item['batch_no'] ?? '', 'drug_id' => $item['drug_ent_base_info_id'] ?? '', 'drug_name' => $item['drug_name'] ?? '', 'prod_code' => $item['prod_code'] ?? '', 'pkg_spec' => $item['pkg_spec'] ?? '', 'prepn_spec' => $item['prepn_spec'] ?? '', 'pkg_ratio_list' => json_encode($item['pkg_ratio_list'] ?? [], JSON_UNESCAPED_UNICODE), 'sealed_report_url' => $item['sealed_report_url'] ?? '', 'seal_raw_data' => json_encode($data ?? [], JSON_UNESCAPED_UNICODE), ]; }, $data); } }