| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Servers;
- use App\Models\DrugReportInfo;
- use App\Models\DrugReportAss;
- use App\Servers\Aliyun\Oss;
- use App\Servers\QueryDrugReportService;
- use App\Servers\SyncDrugReportService;
- use Carbon\Carbon;
- use App\Facades\Servers\Logs\Log;
- use GuzzleHttp\Client;
- class IncrementDrugReportAssService
- {
- /**
- * 分批次同步数据
- * @param string $source
- * @param bool $isFull
- * @param string $startTime
- * @param string $endTime
- * @param int $page
- * @param int $pageSize
- */
- public function syncDrugReport(string $source, string $startTime, string $endTime = '', int $page = 1, int $pageSize = 20)
- {
- $total = 0;
- $total_num = 0;
- $QueryDrugReportService = new QueryDrugReportService();
- do {
- try {
- sleep(1);
- // 分批次拉取数据
- $data = $QueryDrugReportService->querydrugreportass($startTime, $endTime, $page, $pageSize, 0, "1");
- $total_num = $data['total_num'];
- $data = $data['data'];
- if (empty($data)) {
- break;
- }
- var_dump($total_num,$startTime, $endTime, $page);
- Log::info('increment_drug','增量同步', ['total_num'=>$total_num,'startTime'=>$startTime, 'endTime'=>$endTime, 'page'=>$page]);
- // 处理并保存数据
- $saveCount = $this->saveDrugReport($data);
- Log::info('increment_drug','成功增量:'.$saveCount);
- $total += $saveCount;
- // 检查是否还有更多数据
- if (count($data) <> $pageSize) {
- break;
- }
- $page++;
- } catch (\Exception $e) {
- Log::info('increment_drug','增量同步错误', ['error'=>$e->getMessage()]);
- break;
- }
- } while (true);
- return ['total'=>$total,'total_num'=>$total_num];
- }
- /**
- * 处理并保存批次数据
- * @param array $data
- * @return int
- */
- protected function saveDrugReport(array $data): int
- {
- try {
- $SyncDrugReportService = new SyncDrugReportService();
- $data = $SyncDrugReportService->correctDrugReportAss($data);
- return DrugReportAss::bulkUpsert($data);
- } catch (\Throwable $e) {
- var_dump($e->getMessage());
- Log::info('increment_drug', '处理数据异常', ['data'=>$data, 'err'=>$e->getMessage()]);
- return 0;
- }
- }
- /**
- * 处理【上游出库单】医药报告批次数据 排除已存在的数据
- * @param array $data
- */
- protected function exclude(array $data)
- {
- foreach ($data as $key=>$item){
- $map = [
- 'batch_no' => $item['produce_batch_no'],
- 'drug_id' => $item['drug_id'],
- 'bill_id' => $item['bill_id'],
- 'bill_detail_id' => $item['bill_detail_id'],
- ];
- $res = DrugReportAss::query()->where($map)->first();
- if ($res) {
- unset($data[$key]);
- }
- }
- return (new SyncDrugReportService())->correctDrugReportAss($data);
- }
- }
|