package com.hotwj.platform.reportStatistics.controller;

import com.hotwj.platform.reportStatistics.domain.bo.ReportLedgerQueryBo;
import com.hotwj.platform.reportStatistics.domain.vo.*;
import com.hotwj.platform.reportStatistics.service.ILedgerReportService;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.domain.vo.HotGovEnterpriseUnitVo;
import com.hotwj.platform.resourceManagement.govEnterpriseUnit.service.IHotGovEnterpriseUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
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.PathVariable;
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/personManagement")
@Tag(name = "从业人员信息管理台账（政府端）", description = "从业人员信息管理台账（政府端）")
public class PersonManagementController extends BaseController {

    private final ILedgerReportService ledgerReportService;
    private final IHotGovEnterpriseUnitService hotGovEnterpriseUnitService;

    //@SaCheckPermission("personManagement:driverInfo:list")
    @GetMapping("/driverInfo/list")
    @Operation(summary = "从业人员信息管理台账-分页（政府端）")
    public TableDataInfo<DriverInfoLedgerVo> driverInfoList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        return ledgerReportService.queryDriverInfo(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:driverHealth:list")
    @GetMapping("/driverHealth/list")
    @Operation(summary = "驾驶员身体健康台账-分页（政府端）")
    public TableDataInfo<DriverHealthLedgerVo> driverHealthList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        return ledgerReportService.queryDriverHealth(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:profCertScore:list")
    @GetMapping("/profCertScore/list")
    @Operation(summary = "从业资格证记分登记台账-分页（政府端）")
    public TableDataInfo<ProfCertScoreLedgerVo> profCertScoreList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        //离职人员不显示
        bo.setStatus(1L);
        return ledgerReportService.queryProfCertScore(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:driverIntegrity:list")
    @GetMapping("/driverIntegrity/list")
    @Operation(summary = "驾驶员诚信考核等级台账-分页（政府端）")
    public TableDataInfo<DriverIntegrityLedgerVo> driverIntegrityList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        //离职人员不显示
        bo.setStatus(1L);
        return ledgerReportService.queryDriverIntegrity(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:preJobTraining:list")
    @GetMapping("/preJobTraining/list")
    @Operation(summary = "从业人员岗前培训考核记录-分页（政府端）")
    public TableDataInfo<GovPreJobTrainingLedgerVo> preJobTrainingList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        // 仅统计审核通过的人员，保持与明细列表一致
        bo.setAuditStatus(1);
        return ledgerReportService.queryGovPreJobTraining(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:preJobTraining:detail")
    @GetMapping("/preJobTraining/detail")
    @Operation(summary = "从业人员岗前培训考核记录-明细列表（政府端）")
    public TableDataInfo<GovPreJobTrainingDetailVo> preJobTrainingDetail(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        if (bo.getCompanyId() == null) {
            return TableDataInfo.build();
        }
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        //离职人员不显示
        bo.setStatus(1L);
        bo.setAuditStatus(1);
        return ledgerReportService.queryGovPreJobTrainingDetail(unitVo.getDistrictCode(), bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:safetyEducationTraining:list")
    @GetMapping("/safetyEducationTraining/list")
    @Operation(summary = "安全生产教育培训记录-分页（政府端）")
    public TableDataInfo<GovSafetyEducationTrainingVo> safetyEducationTrainingList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        if (bo.getCompanyId() == null) {
            return TableDataInfo.build();
        }
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        return ledgerReportService.queryGovSafetyEducationTraining(bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:safetyEducationTraining:query")
    @GetMapping("/safetyEducationTraining/detail/{id}")
    @Operation(summary = "安全生产教育培训记录-详情（政府端）")
    public R<GovSafetyEducationTrainingDetailVo> safetyEducationTrainingDetail(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(ledgerReportService.queryGovSafetyEducationTrainingDetail(id));
    }

    //@SaCheckPermission("personManagement:violationInfo:list")
    @GetMapping("/violationInfo/list")
    @Operation(summary = "违法违章信息排查记录-分页（政府端）")
    public TableDataInfo<GovViolationInfoLedgerVo> violationInfoList(@Validated ReportLedgerQueryBo bo, PageQuery pageQuery) {
        if (bo.getCompanyId() == null) {
            throw new IllegalArgumentException("CompanyId不能为空");
        }
        //离职人员不显示
        bo.setStatus(1L);
        bo.setAuditStatus(1);
        return ledgerReportService.queryGovViolationInfoLedger(bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:violationInfo:list")
    @GetMapping("/violationInfo/detail/list")
    @Operation(summary = "违法违章信息排查记录-详情分页（政府端）")
    public TableDataInfo<GovViolationInfoDetailVo> violationInfoDetailList(@Validated ReportLedgerQueryBo bo, PageQuery pageQuery) {
        if (bo.getCompanyId() == null) {
            throw new IllegalArgumentException("CompanyId不能为空");
        }
        if (bo.getStartDate() == null || bo.getEndDate() == null) {
            throw new IllegalArgumentException("开始时间和结束时间不能为空");
        }
        //离职人员不显示
        bo.setStatus(1L);
        bo.setAuditStatus(1);
        return ledgerReportService.queryGovViolationInfoDetail(bo, pageQuery);
    }

    //@SaCheckPermission("personManagement:violationHandling:list")
    @GetMapping("/violationHandling/list")
    @Operation(summary = "违章处理记录表-分页（政府端）")
    public TableDataInfo<GovViolationHandlingLedgerVo> violationHandlingList(ReportLedgerQueryBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null || !ISysUserLoginPortService.GOVERNMENT_PORT.equals(loginUser.getLoginPort())) {
            return TableDataInfo.build();
        }
        HotGovEnterpriseUnitVo unitVo = hotGovEnterpriseUnitService.queryById(loginUser.getCompanyId());
        if (unitVo == null || unitVo.getDistrictCode() == null) {
            return TableDataInfo.build();
        }
        //离职人员不显示
        bo.setStatus(1L);
        bo.setAuditStatus(1);
        return ledgerReportService.queryGovViolationHandlingLedger(unitVo.getDistrictCode(), bo, pageQuery);
    }
}
