SyncSealDrugReportService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Services;
  3. use App\Models\DrugReportInfo;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\Log;
  6. class SyncSealDrugReportService
  7. {
  8. protected $source = 'querysealdrugreport';
  9. /**
  10. * 分批次同步数据
  11. * @param bool $isFull
  12. * @param string $startTime
  13. * @param string $endTime
  14. * @param int $page
  15. * @param int $pageSize
  16. * @return int
  17. */
  18. public function syncSealDrugReport(bool $isFull, string $startTime, string $endTime = '', int $page = 1, int $pageSize = 20): int
  19. {
  20. $hasMore = true;
  21. $total = 0;
  22. Log::channel('sync')->warning($isFull ? "开始全量同步" : "开始增量同步", [
  23. 'source' => $this->source,
  24. 'params' => [
  25. 'is_full' => $isFull,
  26. 'start_time' => $startTime,
  27. 'end_time' => $endTime,
  28. 'page' => $page,
  29. 'page_size' => $pageSize,
  30. ],
  31. ]);
  32. if (empty($endTime)) {
  33. $endTime = Carbon::parse($startTime)->copy()->endOfMonth()->toDateTimeString();
  34. }
  35. $queryDrugReportService = app(QueryDrugReportService::class);
  36. while ($hasMore) {
  37. try {
  38. if (Carbon::parse($startTime)->isFuture()) {
  39. Log::channel('sync')->warning("时间大于当前时间,同步终止", [
  40. 'source' => $this->source,
  41. 'params' => [
  42. 'is_full' => $isFull,
  43. 'start_time' => $startTime,
  44. 'end_time' => $endTime,
  45. 'page' => $page,
  46. 'page_size' => $pageSize,
  47. ]
  48. ]);
  49. break;
  50. }
  51. Log::channel('sync')->info("批次 {$page} 开始处理", [
  52. 'source' => $this->source,
  53. 'params' => [
  54. 'is_full' => $isFull,
  55. 'start_time' => $startTime,
  56. 'end_time' => $endTime,
  57. 'page' => $page,
  58. 'page_size' => $pageSize,
  59. ]
  60. ]);
  61. // 分批次拉取数据
  62. $data = $queryDrugReportService->querySealDrugReport($startTime, $endTime, $page, $pageSize);
  63. if (empty($data)) {
  64. Log::channel('sync')->warning("批次 {$page} 没有数据", [
  65. 'source' => $this->source,
  66. 'params' => [
  67. 'is_full' => $isFull,
  68. 'start_time' => $startTime,
  69. 'end_time' => $endTime,
  70. 'page' => $page,
  71. 'page_size' => $pageSize,
  72. ]
  73. ]);
  74. if ($isFull) {
  75. $startTime = Carbon::parse($startTime)->copy()->addMonth()->startOfMonth()->toDateTimeString();
  76. $endTime = Carbon::parse($endTime)->copy()->addMonth()->endOfMonth()->toDateTimeString();
  77. $page = 1;
  78. continue;
  79. }
  80. // $hasMore = false;
  81. break;
  82. }
  83. // 处理并保存数据
  84. $saveCount = $this->saveSealDrugReport($data);
  85. $total += $saveCount;
  86. Log::channel('sync')->info("批次 {$page} 处理完成", [
  87. 'source' => $this->source,
  88. 'fetch_count' => count($data),
  89. 'save_count' => $saveCount,
  90. 'total' => $total,
  91. ]);
  92. // 检查是否还有更多数据
  93. $hasMore = (count($data) === $pageSize);
  94. $page++;
  95. // 避免请求过于频繁
  96. sleep(1);
  97. } catch (\Exception $e) {
  98. Log::channel('sync')->error('获取批次数据失败', [
  99. 'source' => $this->source,
  100. 'params' => [
  101. 'is_full' => $isFull,
  102. 'start_time' => $startTime,
  103. 'end_time' => $endTime,
  104. 'page' => $page,
  105. 'page_size' => $pageSize,
  106. ],
  107. 'error' => $e->getMessage()
  108. ]);
  109. continue;
  110. }
  111. }
  112. Log::channel('sync')->warning($isFull ? "全量同步完成" : "增量同步完成", [
  113. 'source' => $this->source,
  114. 'total' => $total
  115. ]);
  116. return $total;
  117. }
  118. /**
  119. * 处理并保存批次数据
  120. * @param array $data
  121. * @return int
  122. */
  123. protected function saveSealDrugReport(array $data): int
  124. {
  125. try {
  126. $data = $this->correctSealDrugReport($data);
  127. return DrugReportInfo::bulkUpsert($data);
  128. } catch (\Throwable $e) {
  129. Log::channel('sync')->error("保存批次数据失败", [
  130. 'data' => $data,
  131. 'error' => $e->getMessage()
  132. ]);
  133. return 0;
  134. }
  135. }
  136. /**
  137. * 处理批次数据
  138. * @param array $data
  139. * @return array
  140. */
  141. protected function correctSealDrugReport(array $data): array
  142. {
  143. return array_map(function ($item) {
  144. return [
  145. 'drug_report_v2_id' => $item['drug_report_v2_id'] ?? '',
  146. 'report_id' => $item['report_id'] ?? '',
  147. 'report_name' => $item['drug_report_name'] ?? '',
  148. 'report_no' => $item['report_no'] ?? '',
  149. 'report_date' => $item['report_date'] ?? '',
  150. 'batch_no' => $item['batch_no'] ?? '',
  151. 'drug_id' => $item['drug_ent_base_info_id'] ?? '',
  152. 'drug_name' => $item['drug_name'] ?? '',
  153. 'prod_code' => $item['prod_code'] ?? '',
  154. 'pkg_spec' => $item['pkg_spec'] ?? '',
  155. 'prepn_spec' => $item['prepn_spec'] ?? '',
  156. 'pkg_ratio_list' => json_encode($item['pkg_ratio_list'] ?? [], JSON_UNESCAPED_UNICODE),
  157. 'sealed_report_url' => $item['sealed_report_url'] ?? '',
  158. 'seal_raw_data' => json_encode($data ?? [], JSON_UNESCAPED_UNICODE),
  159. ];
  160. }, $data);
  161. }
  162. }