<?php

namespace App\Console\Commands;

use App\Models\DrugReportInfo;
use App\Servers\IncrementDrugReportService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Facades\Servers\Logs\Log;

class IncrementSyncDrugReportCommand extends Command
{
    protected $signature = 'increment-drug-report';

    protected $description = '增量同步药检报告数据';

    public function handle(IncrementDrugReportService $service): int
    {
        Log::info('increment_drug_report','增量同步开始');
        $startTime      = cache('increment:drug:report:time');
        if (empty($startTime)){
            $startTime = Carbon::now()->startOfDay();
        }
        $source = 'querydrugreport';
        $endTime = Carbon::now();
        Log::info('increment_drug_report','增量同步开始', ['startTime'=>$startTime, 'endTime'=>$endTime]);
        $page = 1;
        $pageSize = 20;
        $data = $service->syncDrugReport($source, $startTime, $endTime, $page, $pageSize,2);
        $sealData = $service->syncDrugReport($source, $startTime, $endTime, $page, $pageSize,1);
        Log::info('increment_drug_report','增量同步已盖章结束'. "，共{$data['total_num']}，处理 {$data['total']} 条记录！", ['startTime'=>$startTime, 'endTime'=>$endTime]);
        Log::info('increment_drug_report','增量同步未盖章结束'. "，共{$sealData['total_num']}，处理 {$sealData['total']} 条记录！", ['startTime'=>$startTime, 'endTime'=>$endTime]);
        cache(['increment:drug:report:time'=>$endTime]);
        return 0;
    }

}
