IncrementDrugReportAssService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\DrugReportInfo;
  4. use App\Models\DrugReportAss;
  5. use App\Servers\Aliyun\Oss;
  6. use App\Servers\QueryDrugReportService;
  7. use App\Servers\SyncDrugReportService;
  8. use Carbon\Carbon;
  9. use App\Facades\Servers\Logs\Log;
  10. use GuzzleHttp\Client;
  11. class IncrementDrugReportAssService
  12. {
  13. /**
  14. * 分批次同步数据
  15. * @param string $source
  16. * @param bool $isFull
  17. * @param string $startTime
  18. * @param string $endTime
  19. * @param int $page
  20. * @param int $pageSize
  21. */
  22. public function syncDrugReport(string $source, string $startTime, string $endTime = '', int $page = 1, int $pageSize = 20)
  23. {
  24. $total = 0;
  25. $total_num = 0;
  26. $QueryDrugReportService = new QueryDrugReportService();
  27. do {
  28. try {
  29. sleep(1);
  30. // 分批次拉取数据
  31. $data = $QueryDrugReportService->querydrugreportass($startTime, $endTime, $page, $pageSize, 0, "1");
  32. $total_num = $data['total_num'];
  33. $data = $data['data'];
  34. if (empty($data)) {
  35. break;
  36. }
  37. var_dump($total_num,$startTime, $endTime, $page);
  38. Log::info('increment_drug','增量同步', ['total_num'=>$total_num,'startTime'=>$startTime, 'endTime'=>$endTime, 'page'=>$page]);
  39. // 处理并保存数据
  40. $saveCount = $this->saveDrugReport($data);
  41. Log::info('increment_drug','成功增量:'.$saveCount);
  42. $total += $saveCount;
  43. // 检查是否还有更多数据
  44. if (count($data) <> $pageSize) {
  45. break;
  46. }
  47. $page++;
  48. } catch (\Exception $e) {
  49. Log::info('increment_drug','增量同步错误', ['error'=>$e->getMessage()]);
  50. break;
  51. }
  52. } while (true);
  53. return ['total'=>$total,'total_num'=>$total_num];
  54. }
  55. /**
  56. * 处理并保存批次数据
  57. * @param array $data
  58. * @return int
  59. */
  60. protected function saveDrugReport(array $data): int
  61. {
  62. try {
  63. $SyncDrugReportService = new SyncDrugReportService();
  64. $data = $SyncDrugReportService->correctDrugReportAss($data);
  65. return DrugReportAss::bulkUpsert($data);
  66. } catch (\Throwable $e) {
  67. var_dump($e->getMessage());
  68. Log::info('increment_drug', '处理数据异常', ['data'=>$data, 'err'=>$e->getMessage()]);
  69. return 0;
  70. }
  71. }
  72. /**
  73. * 处理【上游出库单】医药报告批次数据 排除已存在的数据
  74. * @param array $data
  75. */
  76. protected function exclude(array $data)
  77. {
  78. foreach ($data as $key=>$item){
  79. $map = [
  80. 'batch_no' => $item['produce_batch_no'],
  81. 'drug_id' => $item['drug_id'],
  82. 'bill_id' => $item['bill_id'],
  83. 'bill_detail_id' => $item['bill_detail_id'],
  84. ];
  85. $res = DrugReportAss::query()->where($map)->first();
  86. if ($res) {
  87. unset($data[$key]);
  88. }
  89. }
  90. return (new SyncDrugReportService())->correctDrugReportAss($data);
  91. }
  92. }