package com.hotwj.platform.reportStatistics.controller;

import cn.dev33.satoken.spring.SpringMVCUtil;
import cn.hutool.core.io.IoUtil;
import com.hotwj.platform.reportStatistics.service.IDriverFilePrintService;
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 2026-01-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/reportStatistics/driverFile")
@Tag(name = "驾驶员档案打印", description = "驾驶员档案打印")
public class DriverFileController extends BaseController {

    private final IDriverFilePrintService driverFilePrintService;

    /**
     * 驾驶员应聘表
     */
    //@SaCheckPermission("reportStatistics:driverFile:applicationForm")
    @PostMapping("/applicationForm")
    @Operation(summary = "驾驶员应聘表")
    public void applicationForm(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateApplicationForm(driverIds);
        writeAttachment(response, "驾驶员应聘表.pdf", pdf, "application/pdf");
    }


    /**
     * 身份证扫描件
     */
    //@SaCheckPermission("reportStatistics:driverFile:idCardScan")
    @PostMapping("/idCardScan")
    @Operation(summary = "驾驶员身份证扫描件")
    public void idCardScan(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateIdCardScan(driverIds);
        writeAttachment(response, "驾驶员身份证扫描件.pdf", pdf, "application/pdf");
    }
    /**
     * 驾驶员机动车驾驶证复印件
     */
    //@SaCheckPermission("reportStatistics:driverFile:driverLicenseCopy")
    @PostMapping("/driverLicenseCopy")
    @Operation(summary = "驾驶员机动车驾驶证复印件")
    public void driverLicenseCopy(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateDriverLicenseCopy(driverIds);
        writeAttachment(response, "驾驶员机动车驾驶证复印件.pdf", pdf, "application/pdf");
    }

	/**
     * 驾驶员从业资格证复印件
     */
    //@SaCheckPermission("reportStatistics:driverFile:qualificationCertificateCopy")
    @PostMapping("/qualificationCertificateCopy")
    @Operation(summary = "驾驶员从业资格证复印件")
    public void qualificationCertificateCopy(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateQualificationCertificateCopy(driverIds);
        writeAttachment(response, "驾驶员从业资格证复印件.pdf", pdf, "application/pdf");
    }

	/**
     * 驾驶员资格审查及技能考核登记表
     */
    //@SaCheckPermission("reportStatistics:driverFile:qualificationReview")
    @PostMapping("/qualificationReview")
    @Operation(summary = "驾驶员资格审查及技能考核登记表")
    public void qualificationReview(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateQualificationReview(driverIds);
        writeAttachment(response, "驾驶员资格审查及技能考核登记表.pdf", pdf, "application/pdf");
    }

    /**
     * 驾驶员安全行车事故记录
     */
    //@SaCheckPermission("reportStatistics:driverFile:accidentRecord")
    @PostMapping("/accidentRecord")
    @Operation(summary = "驾驶员安全行车事故记录")
    public void accidentRecord(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateAccidentRecord(driverIds);
        writeAttachment(response, "驾驶员安全行车事故记录.pdf", pdf, "application/pdf");
    }

    /**
     * 驾驶员违章记录
     */
    //@SaCheckPermission("reportStatistics:driverFile:violationRecord")
    @PostMapping("/violationRecord")
    @Operation(summary = "驾驶员违章记录")
    public void violationRecord(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateViolationRecord(driverIds);
        writeAttachment(response, "驾驶员违章记录.pdf", pdf, "application/pdf");
    }

    //@SaCheckPermission("reportStatistics:driverFile:annualAssessmentRecord")
    @PostMapping("/annualAssessmentRecord")
    @Operation(summary = "驾驶员年度考核记录")
    public void annualAssessmentRecord(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateAnnualAssessmentRecord(driverIds);
        writeAttachment(response, "驾驶员年度考核记录.pdf", pdf, "application/pdf");
    }

    //@SaCheckPermission("reportStatistics:driverFile:medicalExaminationReport")
    @PostMapping("/medicalExaminationReport")
    @Operation(summary = "驾驶员体检报告")
    public void medicalExaminationReport(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateMedicalExaminationReport(driverIds);
        writeAttachment(response, "驾驶员体检报告.pdf", pdf, "application/pdf");
    }

    //@SaCheckPermission("reportStatistics:driverFile:rewardPunishmentRecord")
    @PostMapping("/rewardPunishmentRecord")
    @Operation(summary = "员工奖惩记录表")
    public void rewardPunishmentRecord(@RequestParam("ids") List<String> driverIds) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.isEmpty()) {
            response.setStatus(400);
            IoUtil.write(response.getOutputStream(), false, "缺少驾驶员ID参数".getBytes(StandardCharsets.UTF_8));
            return;
        }
        byte[] pdf = driverFilePrintService.generateRewardPunishmentRecord(driverIds);
        writeAttachment(response, "驾驶员奖惩记录表.pdf", pdf, "application/pdf");
    }

    /**
     * 一键打印
     */
    //@SaCheckPermission("reportStatistics:driverFile:oneClickPrint")
    @PostMapping("/oneClickPrint")
    @Operation(summary = "驾驶员档案一键打印")
    public void oneClickPrint(@RequestParam("ids") List<String> driverIds, @RequestParam("types") List<String> types) throws Exception {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        if (driverIds == null || driverIds.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 = driverFilePrintService.generateOneClickPrintPdf(driverIds, types);
        writeAttachment(response, "驾驶员档案一键打印.pdf", pdfBytes, "application/pdf");
    }
}
