package com.hotwj.platform.reportStatistics.controller;

import cn.dev33.satoken.spring.SpringMVCUtil;
import com.hotwj.platform.reportStatistics.domain.bo.ReportLedgerQueryBo;
import com.hotwj.platform.reportStatistics.domain.vo.*;
import com.hotwj.platform.reportStatistics.service.ILedgerReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 台账信息
 *
 * @author shihongwei
 * @date 2026-01-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/reportStatistics/ledger")
@Tag(name = "台账信息", description = "台账信息")
public class LedgerController extends BaseController {

    private final ILedgerReportService ledgerReportService;

    //@SaCheckPermission("reportStatistics:driverInfoLedger:list")
    @GetMapping("/driverInfo/list")
    @Operation(summary = "从业人员信息管理台账-分页")
    public TableDataInfo<DriverInfoLedgerVo> driverInfoList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryDriverInfo(null, bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:driverInfoLedger:export")
    @PostMapping("/driverInfo/export")
    @Operation(summary = "从业人员信息管理台账-导出")
    public void driverInfoExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        ExcelUtil.exportExcel(ledgerReportService.listDriverInfo(null, bo), "从业人员信息管理台账", DriverInfoLedgerVo.class, response);
    }

    //@SaCheckPermission("reportStatistics:driverHealthLedger:list")
    @GetMapping("/driverHealth/list")
    @Operation(summary = "驾驶员身体健康台账-分页")
    public TableDataInfo<DriverHealthLedgerVo> driverHealthList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryDriverHealth(null, bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:driverHealthLedger:export")
    @PostMapping("/driverHealth/export")
    @Operation(summary = "驾驶员身体健康台账-导出")
    public void driverHealthExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        ExcelUtil.exportExcel(ledgerReportService.listDriverHealth(null, bo), "驾驶员身体健康台账", DriverHealthLedgerVo.class, response);
    }

    //@SaCheckPermission("reportStatistics:profCertScoreLedger:list")
    @GetMapping("/profCertScore/list")
    @Operation(summary = "从业资格证记分登记台账-分页")
    public TableDataInfo<ProfCertScoreLedgerVo> profCertScoreList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryProfCertScore(null, bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:profCertScoreLedger:export")
    @PostMapping("/profCertScore/export")
    @Operation(summary = "从业资格证记分登记台账-导出")
    public void profCertScoreExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        ExcelUtil.exportExcel(ledgerReportService.listProfCertScore(null, bo), "从业资格证打分登记台账", ProfCertScoreLedgerVo.class, response);
    }

    //@SaCheckPermission("reportStatistics:driverIntegrityLedger:list")
    @GetMapping("/driverIntegrity/list")
    @Operation(summary = "驾驶员诚信考核等级台账-分页")
    public TableDataInfo<DriverIntegrityLedgerVo> driverIntegrityList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryDriverIntegrity(null, bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:driverIntegrityLedger:export")
    @PostMapping("/driverIntegrity/export")
    @Operation(summary = "驾驶员诚信考核等级台账-导出")
    public void driverIntegrityExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        ExcelUtil.exportExcel(ledgerReportService.listDriverIntegrity(null, bo), "驾驶员诚信考核等级台账", DriverIntegrityLedgerVo.class, response);
    }

    //@SaCheckPermission("reportStatistics:vehicleInfoLedger:list")
    @GetMapping("/vehicleInfo/list")
    @Operation(summary = "车辆信息登记台账-分页")
    public TableDataInfo<VehicleInfoLedgerVo> vehicleInfoList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryVehicleInfo(bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:vehicleInfoLedger:export")
    @PostMapping("/vehicleInfo/export")
    @Operation(summary = "车辆信息登记台账-导出")
    public void vehicleInfoExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        ExcelUtil.exportExcel(ledgerReportService.listVehicleInfo(bo), "车辆信息登记台账", VehicleInfoLedgerVo.class, response);
    }

    //@SaCheckPermission("reportStatistics:accidentLedger:list")
    @GetMapping("/accident/list")
    @Operation(summary = "安全事故登记台账-分页")
    public TableDataInfo<AccidentLedgerVo> accidentList(@Valid ReportLedgerQueryBo bo, PageQuery pageQuery) {
        return ledgerReportService.queryAccident(bo, pageQuery);
    }

    //@SaCheckPermission("reportStatistics:accidentLedger:export")
    @PostMapping("/accident/export")
    @Operation(summary = "安全事故登记台账-导出")
    public void accidentExport(@Valid ReportLedgerQueryBo bo) {
        HttpServletResponse response = SpringMVCUtil.getResponse();
        java.util.List<AccidentLedgerVo> list = ledgerReportService.listAccident(bo);
        java.util.List<AccidentLedgerExportVo> exportList = new java.util.ArrayList<>();
        for (AccidentLedgerVo vo : list) {
            AccidentLedgerExportVo exportVo = new AccidentLedgerExportVo();
            exportVo.setAccidentTime(vo.getAccidentTime());
            exportVo.setPlateNumber(vo.getPlateNumber());
            exportVo.setDriverName(vo.getDriverName());
            // 解析事故等级
            if (org.dromara.common.core.utils.StringUtils.isNotEmpty(vo.getAccidentLevel())) {
                try {
                    String levelName = cn.hutool.json.JSONUtil.parseObj(vo.getAccidentLevel()).getStr("levelName");
                    exportVo.setAccidentLevel(levelName);
                } catch (Exception e) {
                    exportVo.setAccidentLevel(vo.getAccidentLevel());
                }
            }
            exportVo.setMainCause(vo.getMainCause());
            exportVo.setOccurLocation(vo.getOccurLocation());
            exportVo.setDeathToll(vo.getDeathToll());
            exportVo.setNumberSeriouslyInjured(vo.getNumberSeriouslyInjured());
            exportVo.setNumberMinorInjuries(vo.getNumberMinorInjuries());
            exportVo.setEconomicLoss(vo.getEconomicLoss());
            exportVo.setDivisionResponsibilities(vo.getDivisionResponsibilities());
            exportVo.setCaseClosingTime(vo.getCaseClosingTime());
            exportVo.setRemark(vo.getRemark());
            exportList.add(exportVo);
        }
        ExcelUtil.exportExcel(exportList, "安全事故登记台账", AccidentLedgerExportVo.class, response);
    }
}
