<?php

namespace App\Servers;

use App\Models\DrugReportAss;
use App\Servers\Aliyun\Oss;
use App\Servers\QueryDrugReportService;
use Carbon\Carbon;
use App\Facades\Servers\Logs\Log;

use GuzzleHttp\Client;

class SignedDrugReportService
{
    public function syncDrugReport(string $startTime, string $endTime = '', int $page = 1, int $pageSize = 20): int
    {

        $total = 0;
        $time  = time();
        $QueryDrugReportService = new QueryDrugReportService();
        $DrugReportAss          = new DrugReportAss();
        do {
            try {
                // 分批次拉取数据
                $data = $QueryDrugReportService->queryDrugReportSignedAss($startTime, $endTime, $page, $pageSize);
                $data = $data['data'];
                if (empty($data)) {
                    break;
                }
                var_dump($startTime, $endTime, $page);
                foreach ($data as $item){
                    $signed_time    = strtotime($item['signed_time']);
                    $res  = $DrugReportAss->where([
                        'batch_no' => $item['produce_batch_no'],
                        'drug_id' => $item['drug_id'],
                        'bill_detail_id' => $item['bill_detail_id'],
                        'bill_id' => $item['bill_id'],
                    ])->update(['drug_report_sign_status'=>3,'signed_time'=>$signed_time,'update_time'=>$time]);
                }
                // 检查是否还有更多数据
                if (count($data) <> $pageSize) {
                    break;
                }
                $page++;
                sleep(1);

            } catch (\Exception $e) {
                Log::info('drug-report-signed','已经签收同步错误', ['err'=>$e->getMessage()]);
                break;
            }
        } while (true);

        return $total;
    }
}
