SyncSealDrugReportService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Services;
  3. use App\Models\DrugReportInfo;
  4. use Illuminate\Support\Facades\Log;
  5. class SyncSealDrugReportService
  6. {
  7. protected $source = 'querysealdrugreport';
  8. /**
  9. * 分批次同步数据
  10. * @param string $startTime
  11. * @param string $endTime
  12. * @param int $page
  13. * @param int $pageSize
  14. * @return int
  15. */
  16. public function syncSealDrugReport(string $startTime, string $endTime, int $page, int $pageSize): int
  17. {
  18. $hasMore = true;
  19. $total = 0;
  20. Log::channel('sync')->warning("开始全量同步", [
  21. 'source' => $this->source,
  22. 'params' => [
  23. 'start_time' => $startTime,
  24. 'end_time' => $endTime,
  25. 'page' => $page,
  26. 'page_size' => $pageSize,
  27. ],
  28. ]);
  29. $queryDrugReportService = app(QueryDrugReportService::class);
  30. while ($hasMore) {
  31. try {
  32. // 分批次拉取数据
  33. $data = $queryDrugReportService->querySealDrugReport($startTime, $endTime, $page, $pageSize);
  34. if (empty($data)) {
  35. $hasMore = false;
  36. break;
  37. }
  38. // 处理并保存数据
  39. $saveCount = $this->saveSealDrugReport($data);
  40. $total += $saveCount;
  41. Log::channel('sync')->info("批次 {$page} 处理完成", [
  42. 'source' => $this->source,
  43. 'fetch_count' => count($data),
  44. 'save_count' => $saveCount,
  45. 'total' => $total
  46. ]);
  47. // 检查是否还有更多数据
  48. $hasMore = (count($data) === $pageSize);
  49. $page++;
  50. break;
  51. // 避免请求过于频繁
  52. sleep(1);
  53. } catch (\Exception $e) {
  54. Log::channel('sync')->error('获取批次数据失败', [
  55. 'source' => $this->source,
  56. 'params' => [
  57. 'start_time' => $startTime,
  58. 'end_time' => $endTime,
  59. 'page' => $page,
  60. 'page_size' => $pageSize,
  61. ],
  62. 'error' => $e->getMessage()
  63. ]);
  64. continue;
  65. }
  66. }
  67. Log::channel('sync')->warning("全量同步完成", [
  68. 'source' => $this->source,
  69. 'total' => $total
  70. ]);
  71. return $total;
  72. }
  73. /**
  74. * 处理并保存批次数据
  75. * @param array $data
  76. * @return int
  77. */
  78. protected function saveSealDrugReport(array $data): int
  79. {
  80. try {
  81. $data = $this->correctSealDrugReport($data);
  82. return DrugReportInfo::bulkUpsert($data);
  83. } catch (\Throwable $e) {
  84. Log::channel('sync')->error("保存批次数据失败", [
  85. 'data' => $data,
  86. 'error' => $e->getMessage()
  87. ]);
  88. return 0;
  89. }
  90. }
  91. /**
  92. * 处理批次数据
  93. * @param array $data
  94. * @return array
  95. */
  96. protected function correctSealDrugReport(array $data): array
  97. {
  98. return array_map(function ($item) {
  99. return [
  100. 'drug_report_v2_id' => $item['drug_report_v2_id'] ?? '',
  101. 'report_id' => $item['report_id'] ?? '',
  102. 'report_name' => $item['drug_report_name'] ?? '',
  103. 'report_no' => $item['report_no'] ?? '',
  104. 'report_date' => $item['report_date'] ?? '',
  105. 'batch_no' => $item['batch_no'] ?? '',
  106. 'drug_id' => $item['drug_ent_base_info_id'] ?? '',
  107. 'drug_name' => $item['drug_name'] ?? '',
  108. 'prod_code' => $item['prod_code'] ?? '',
  109. 'pkg_spec' => $item['pkg_spec'] ?? '',
  110. 'prepn_spec' => $item['prepn_spec'] ?? '',
  111. 'pkg_ratio_list' => json_encode($item['pkg_ratio_list'] ?? [], JSON_UNESCAPED_UNICODE),
  112. 'sealed_report_url' => $item['sealed_report_url'] ?? '',
  113. 'seal_raw_data' => json_encode($data ?? [], JSON_UNESCAPED_UNICODE),
  114. ];
  115. }, $data);
  116. }
  117. }