IncrementDrugReportService.php 3.1 KB

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