| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Services;
- use App\Models\DrugReportInfo;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- class SyncSealDrugReportService
- {
- protected $source = 'querysealdrugreport';
- /**
- * 分批次同步数据
- * @param bool $isFull
- * @param string $startTime
- * @param string $endTime
- * @param int $page
- * @param int $pageSize
- * @return 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($isFull ? "开始全量同步" : "开始增量同步", [
- 'source' => $this->source,
- 'params' => [
- 'is_full' => $isFull,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'page' => $page,
- 'page_size' => $pageSize,
- ],
- ]);
- 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)) {
- 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;
- }
- // 处理并保存数据
- $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++;
- // 避免请求过于频繁
- sleep(1);
- } catch (\Exception $e) {
- Log::channel('sync')->error('获取批次数据失败', [
- 'source' => $this->source,
- 'params' => [
- 'is_full' => $isFull,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'page' => $page,
- 'page_size' => $pageSize,
- ],
- 'error' => $e->getMessage()
- ]);
- continue;
- }
- }
- Log::channel('sync')->warning($isFull ? "全量同步完成" : "增量同步完成", [
- '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);
- }
- }
|