<?php

namespace App\Http\Controllers\Api;

use App\Servers\QueryDrugReportService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Servers\SyncDrugReportService;

class QueryDrugReportController extends Api
{
    /**
     * 查询上游企业的待签收药检报告信息
     * （?begin_time=2025-11-01 00:00:00&end_time=2025-11-31 23:59:59&page=1&page_size=20）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function queryDrugReport(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("begin_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $data = $service->queryDrugReport($beginTime, $endTime, $page, $pageSize);
        dd($data);
//        return response()->json($data);
    }

    /**
     * 查询上传报告信息接口
     * （?begin_time=2025-03-01 00:00:00&end_time=2025-03-31 23:59:59&page=1&page_size=20&is_seal=0）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function querySealDrugReport(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("start_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $isSeal = $request->get("is_seal") ?? 0;
        $data = $service->querySealDrugReport($beginTime, $endTime, $page, $pageSize, $isSeal);
        dd($data);
//        return response()->json($data);
    }

    /**
     * 药检报告操作日志
     * （?begin_date=2025-11-01&end_date=2025-11-31&page=1&page_size=20）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function drugReportOptHistory(Request $request, QueryDrugReportService $service)
    {
        $beginDate = $request->get("start_time") ?? Carbon::now()->startOfMonth()->toDateString();
        $endDate = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $data = $service->drugReportOptHistory($beginDate, $endDate, $page, $pageSize);
        dd($data);
//        return response()->json($data);
    }
    /**
     * 查询上游出库单，支持以自己身份查询或者货主身份查询
     * （?begin_time=2025-03-01 00:00:00&end_time=2025-03-31 23:59:59&page=1&page_size=20&is_seal=0）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function queryDrugReportass(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("start_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $isSeal = $request->get("is_seal") ?? 0;
        $batchNo = $request->get("batch_no") ?? 0;
        $drugId = $request->get("drug_id") ?? 0;
        $time_type = $request->get("time_type") ?? "0";
        $batch_no = $request->get("batch_no") ?? '';
        $drug_id = $request->get("drug_id") ?? '';
        $bill_code = $request->get("bill_code") ?? '';
        $data = $service->querydrugreportass($beginTime, $endTime, $page, $pageSize, $isSeal,$time_type,$bill_code,$batch_no,$drug_id);
        dd($data);
//        return response()->json($data);
    }
    /**
     * 同步上游出库单数据，支持以自己身份查询或者货主身份查询
     * （?begin_time=2025-03-01 00:00:00&end_time=2025-03-31 23:59:59&page=1&page_size=20&is_seal=0）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function syncDrugReportass(Request $request,SyncDrugReportService $SyncDrugReportService)
    {
        $source = $request->get('source', 'querydrugreportass');
        $isFull = (bool)$request->get('full', true);
        $startTime = $request->get('start_time') ?? Carbon::today()->startOfDay();
        $endTime = $request->get('end_time') ?? ''; // Carbon::now()
        $page = $request->get('page') ?? 1;
        $pageSize = $request->get('page_size') ?? 20;

        if (empty($source)) {
            echo "参数错误，未指定来源[source]", PHP_EOL;
            return Command::FAILURE;
        }

        echo $isFull ? "全量同步开始" : "增量同步开始", sprintf('[%d, %s, %s, %d, %d]……', $isFull, $startTime, $endTime, $page, $pageSize), PHP_EOL;

        $count = $SyncDrugReportService->syncDrugReport($source, $isFull, $startTime, $endTime, $page, $pageSize);

        echo $isFull ? "全量同步结束" : "增量同步结束", "，共处理 {$count} 条记录！", PHP_EOL;
//        return response()->json($data);
    }
    /**
     * 查询一张网报告操作日志
     * （?begin_time=2025-03-01 00:00:00&end_time=2025-03-31 23:59:59&page=1&page_size=20&is_seal=0）
     * @param Request $request
     * @param QueryDrugReportService $service
     * @return JsonResponse
     * @throws \Exception
     */
    public function historyAll(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("begin_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $isSeal = $request->get("is_seal") ?? 0;
        $batchNo = $request->get("batch_no") ?? 0;
        $drugId = $request->get("drug_id") ?? 0;
        $data = $service->querydrugreportass($beginTime, $endTime, $page, $pageSize, $isSeal);
        dd($data);
//        return response()->json($data);
    }
    /**
     * 查询一段时间内已经签收的单据
     */
    public function getSignedAss(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("start_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $data = $service->queryDrugReportSignedAss($beginTime, $endTime, $page, $pageSize);
        dd($data);
//        return response()->json($data);
    }
    /**
     * 查询药检报告签章信息
     */
    public function synergyDrugReport(Request $request, QueryDrugReportService $service)
    {
        $reportId = $request->get("report_id") ?? 0;
        $data = $service->synergyDrugReport($reportId);
        dd($data);
//        return response()->json($data);
    }
    /**
     * 查询已经签收但是还未盖章的单据
     */
    public function signednotstampedbill(Request $request, QueryDrugReportService $service)
    {
        $beginTime = $request->get("start_time") ?? Carbon::now()->startOfMonth()->toDateTimeString();
        $endTime = $request->get("end_time") ?? Carbon::now()->endOfMonth()->toDateTimeString();
        $page = $request->get("page") ?? 1;
        $pageSize = $request->get("page_size") ?? 20;
        $data = $service->signednotstampedbill($beginTime, $endTime, $page, $pageSize);
        dd($data);
//        return response()->json($data);
    }
}
