<?php

namespace App\Servers;

use App\Models\DrugReportInfo;
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 IncrementDrugReportService
{

    /**
     * 分批次同步数据
     * @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,$isSeal="1")
    {

        $total      = 0;
        $total_num  = 0;
        $QueryDrugReportService       = new QueryDrugReportService();
        do {
            try {
                sleep(1);
                // 分批次拉取数据
                $data = $QueryDrugReportService->querySealDrugReport($startTime, $endTime, $page, $pageSize, $isSeal);
                $total_num = $data['total_num'];
                $data = $data['data'];
                if (empty($data)) {
                    break;
                }
                var_dump($isSeal,$total_num,$startTime, $endTime, $page);
                // 处理并保存数据
                $saveCount = $this->saveDrugReport($data, $isSeal);
                Log::info('increment_drug_report','成功增量：'.$saveCount);
                $total += $saveCount;
                // 检查是否还有更多数据
                if (count($data) <> $pageSize) {
                    break;
                }
                $page++;

            } catch (\Exception $e) {
                Log::info('increment_drug_report','增量同步错误', ['error'=>$e->getMessage()]);
                break;
            }
        } while (true);

        return ['total'=>$total,'total_num'=>$total_num];
    }


    /**
     * 处理并保存批次数据
     * @return int
     */
    protected function saveDrugReport(array $data, $isSeal=1): int
    {
        try {
            $SyncDrugReportService = new SyncDrugReportService();
            $data   =  $SyncDrugReportService->correctSealDrugReport($data,$isSeal);
            return DrugReportInfo::bulkUpsert($data);
        } catch (\Throwable $e) {
            var_dump($e->getMessage());
            Log::info('increment_drug_report', '处理数据异常', ['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);
    }
}
