package com.hotwj.platform.reportStatistics.controller;

import cn.dev33.satoken.spring.SpringMVCUtil;
import cn.hutool.core.io.IoUtil;
import com.hotwj.platform.reportStatistics.service.IVehicleFilePrintService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 车辆档案打印模块
 *
 * @author shihongwei
 * @date 2025-12-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/reportStatistics/vehicleFile")
@Tag(name = "车辆档案打印", description = "车辆档案打印")
public class VehicleFileController extends BaseController {

    private final IVehicleFilePrintService vehicleFilePrintService;

    /**
     * 公司车辆基本信息表
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:baseInfo")
    @PostMapping("/baseInfo")
    @Operation(summary = "公司车辆基本信息表")
    public void baseInfo(@RequestParam("ids") List<Long> vehicleIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = vehicleFilePrintService.generateBaseInfoPdf(vehicleIds);
        writeAttachment(response, "公司车辆基本信息.pdf", pdf, "application/pdf");
    }

    /**
     * 车辆行驶证驾驶证
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:license")
    @PostMapping("/license")
    @Operation(summary = "车辆行驶证驾驶证")
    public void license(@RequestParam("ids") List<Long> vehicleIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = vehicleFilePrintService.generateLicensePdf(vehicleIds);
        writeAttachment(response, "车辆行驶证驾驶证.pdf", pdf, "application/pdf");
    }

    /**
     * 安全技术检测报告
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:inspectionReport")
    @Operation(summary = "安全技术检测报告")
    @PostMapping("/inspectionReport")
    public void inspectionReport(@RequestParam("ids") List<Long> vehicleIds,
                                 @RequestParam(value = "inspectionStartDate", required = false) String inspectionStartDate,
                                 @RequestParam(value = "inspectionEndDate", required = false) String inspectionEndDate,
                                 @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }

        byte[] pdfBytes = vehicleFilePrintService.generateInspectionReportPdf(vehicleIds, inspectionStartDate, inspectionEndDate, printEmptyData);
        writeAttachment(response, "安全技术检测报告.pdf", pdfBytes, "application/pdf");
    }


    /**
     * 二级维护记录
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:maintenanceRecord")
    @PostMapping("/maintenanceRecord")
    @Operation(summary = "二级维护记录")
    public void maintenanceRecord(@RequestParam("ids") List<Long> vehicleIds,
                                  @RequestParam(value = "maintenanceStartDate", required = false) String maintenanceStartDate,
                                  @RequestParam(value = "maintenanceEndDate", required = false) String maintenanceEndDate,
                                  @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
        }
        byte[] pdfBytes = vehicleFilePrintService.generateMaintenanceRecordPdf(vehicleIds, maintenanceStartDate, maintenanceEndDate, printEmptyData);
        writeAttachment(response, "二级维护记录.pdf", pdfBytes, "application/pdf");
    }

    /**
     * 车辆出险记录
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:accidentRecords")
    @PostMapping("/accidentRecords")
    @Operation(summary = "车辆出险记录")
    public void accidentRecords(@RequestParam("ids") List<Long> vehicleIds,
                                @RequestParam(value = "beginTime", required = false) String beginTime,
                                @RequestParam(value = "endTime", required = false) String endTime,
                                @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdfBytes = vehicleFilePrintService.generateClaimRecordPdf(vehicleIds, beginTime, endTime, printEmptyData);
        writeAttachment(response, "车辆出险记录.pdf", pdfBytes, "application/pdf");
    }


    /**
     * 车辆行驶里程记录
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:mileageRecord")
    @PostMapping("/mileageRecord")
    @Operation(summary = "车辆行驶里程记录")
    public void mileageRecord(@RequestParam("ids") List<Long> vehicleIds,
                              @RequestParam(value = "beginTime", required = false) String beginTime,
                              @RequestParam(value = "endTime", required = false) String endTime,
                              @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdfBytes = vehicleFilePrintService.generateMileageRecordPdf(vehicleIds, beginTime, endTime, printEmptyData);
        writeAttachment(response, "车辆行驶里程记录.pdf", pdfBytes, "application/pdf");
    }

    /**
     * 车辆维护和修理记录打印
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:maintenanceRepairRecords")
    @PostMapping("/maintenanceRepairRecords")
    @Operation(summary = "车辆维护和修理记录打印")
    public void maintenanceRepairRecords(@RequestParam("ids") List<Long> vehicleIds,
                                         @RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "endTime", required = false) String endTime,
                                         @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdfBytes = vehicleFilePrintService.generateRepairRecordPdf(vehicleIds, beginTime, endTime, printEmptyData);
        writeAttachment(response, "车辆维护和修理记录.pdf", pdfBytes, "application/pdf");
    }

    /**
     * 交通事故记录
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:trafficAccidentRecord")
    @PostMapping("/trafficAccidentRecord")
    @Operation(summary = "交通事故记录打印")
    public void trafficAccidentRecord(@RequestParam("ids") List<Long> vehicleIds,
                                      @RequestParam(value = "beginTime", required = false) String beginTime,
                                      @RequestParam(value = "endTime", required = false) String endTime,
                                      @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdfBytes = vehicleFilePrintService.generateTrafficAccidentRecordPdf(vehicleIds, beginTime, endTime, printEmptyData);
        writeAttachment(response, "交通事故记录.pdf", pdfBytes, "application/pdf");
    }

    /**
     * 一键打印
     */
    //@SaCheckPermission("reportStatistics:vehicleFile:oneClickPrint")
    @PostMapping("/oneClickPrint")
    @Operation(summary = "车辆档案一键打印")
    public void oneClickPrint(@RequestParam("ids") List<Long> vehicleIds,
                              @RequestParam("types") List<String> types,
                              @RequestParam(value = "printEmptyData", required = false) Integer printEmptyData) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少车辆ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        if (types == null || types.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少打印类型参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdfBytes = vehicleFilePrintService.generateOneClickPrintPdf(vehicleIds, types, printEmptyData);
        writeAttachment(response, "车辆档案一键打印.pdf", pdfBytes, "application/pdf");
    }


}
