package cn.iocoder.yudao.module.pms.controller.admin.qhse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.cert.vo.IotMeasureCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.hazard.vo.IotHazardPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.ptw.vo.QhsePtwPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.cert.IotMeasureCertDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.hazard.IotHazardDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.ptw.QhsePtwDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.accident.IotAccidentReportMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.cert.IotMeasureCertMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.danger.IotDangerSourceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.hazard.IotHazardMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureBookMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.ptw.QhsePtwMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.google.common.collect.ImmutableMap;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;

@Tag(name = "QHSE统计分析")
@RestController
@RequestMapping("/rq/qhse/stat")
@Validated
public class QhseStatController {


    private final IotDangerSourceMapper iotDangerSourceMapper;
    private final IotHazardMapper iotHazardMapper;
    private final DeptService deptService;
    private final QhsePtwMapper qhsePtwMapper;
    private final IotMeasureBookMapper iotMeasureBookMapper;
    private final IotMeasureCertMapper iotMeasureCertMapper;
    private final IotAccidentReportMapper iotAccidentReportMapper;

    public QhseStatController(IotDangerSourceMapper iotDangerSourceMapper, IotDangerSourceMapper iotDangerSourceMapper1, IotHazardMapper iotHazardMapper, DeptService deptService, QhsePtwMapper qhsePtwMapper, IotMeasureBookMapper iotMeasureBookMapper, IotMeasureCertMapper iotMeasureCertMapper, IotAccidentReportMapper iotAccidentReportMapper) {
        this.iotDangerSourceMapper = iotDangerSourceMapper1;
        this.iotHazardMapper = iotHazardMapper;
        this.deptService = deptService;
        this.qhsePtwMapper = qhsePtwMapper;
        this.iotMeasureBookMapper = iotMeasureBookMapper;
        this.iotMeasureCertMapper = iotMeasureCertMapper;
        this.iotAccidentReportMapper = iotAccidentReportMapper;
    }

    @GetMapping("/number")
    public CommonResult<ImmutableMap> getStatNumber() {
        Long danger = iotDangerSourceMapper.selectCount();
        LocalDateTime monthBegin = LocalDateTimeUtils.getMonth();
        IotHazardPageReqVO pageReqVO = new IotHazardPageReqVO();
        Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(loginUserDeptId);
            ids.add(loginUserDeptId);
        }
        pageReqVO.setCreateTime(new LocalDateTime[]{monthBegin, LocalDateTime.now()});
        List<IotHazardDO> iotHazardDOS = iotHazardMapper.selectListByDept(pageReqVO, ids);
        int monthHazard = iotHazardDOS.size();


        IotHazardPageReqVO reqVO = new IotHazardPageReqVO();
        List<IotHazardDO> dos = iotHazardMapper.selectListByDept(reqVO, ids);
        int total = dos.size();
        reqVO.setStatus("todo");
        int todoSize = iotHazardMapper.selectListByDept(reqVO, ids).size();

        QhsePtwPageReqVO qhsePtwPageReqVO = new QhsePtwPageReqVO();
        qhsePtwPageReqVO.setCreateTime(new LocalDateTime[]{monthBegin, LocalDateTime.now()});
        List<QhsePtwDO> qhsePtwDOS = qhsePtwMapper.selectListByDept(qhsePtwPageReqVO, ids);
        int ptwCount = qhsePtwDOS.size();

        IotMeasureCertPageReqVO iotMeasureCertPageReqVO = new IotMeasureCertPageReqVO();
        List<IotMeasureCertDO> iotMeasureCertDOS = iotMeasureCertMapper.selectListByDept(iotMeasureCertPageReqVO, ids);
        int totalCert = iotMeasureCertDOS.size();
        long expiredCount = iotMeasureCertDOS.stream().filter(IotMeasureCertDO::getExpired).count();
        long warnCount = iotMeasureCertDOS.stream().filter(e -> Objects.nonNull(e.getExpired())&&Objects.nonNull(e.getAlertWarn())&&!e.getExpired() && e.getAlertWarn()).count();

        DateTime beforeYear = DateUtil.offsetYear(new Date(), -1);
        IotAccidentReportPageReqVO iotAccidentReportPageReqVO = new IotAccidentReportPageReqVO();
        iotAccidentReportPageReqVO.setCreateTime(new LocalDateTime[]{beforeYear.toLocalDateTime(), LocalDateTime.now()});
        int accidentSize = iotAccidentReportMapper.selectListByDept(iotAccidentReportPageReqVO, ids).size();
        return CommonResult.success(ImmutableMap.of("danger", danger, "monthHazard", monthHazard, "totalHazard", total, "todoHazard", todoSize, "ptwCount", ptwCount,
                "totdalCert", totalCert,"expiredCert", expiredCount,"warnCert", warnCount, "accident", accidentSize));
    }
}
