package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.core.util.Arith;
import com.usky.common.core.util.DateUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.*;
import com.usky.fire.service.DemNewInspectRecordService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.vo.SiAeAllVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 新消监_消防监督检查记录 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-09-01
 */
@Service
public class DemNewInspectRecordServiceImpl extends AbstractCrudService<DemNewInspectRecordMapper, DemNewInspectRecord> implements DemNewInspectRecordService {
    @Autowired
    private DemNewDeadlineNoticeMapper demNewDeadlineNoticeMapper;
    @Autowired
    private DemNewOtherRecordMapper demNewOtherRecordMapper;
    @Autowired
    private DemNewImmediateNoticeMapper demNewImmediateNoticeMapper;
    @Autowired
    private DemNewPenaltyLetterMapper demNewPenaltyLetterMapper;
    @Autowired
    private DemNewSeizureLetterMapper demNewSeizureLetterMapper;

    @Override
    public List<SiAeAllVO> getAeAllCollect(String startDate, String endDate) {
        List<SiAeAllVO> allVoS = new ArrayList<>();
        Optional.ofNullable(startDate).orElseThrow(() -> new BusinessException("开始时间不能为空"));
        Optional.ofNullable(endDate).orElseThrow(() -> new BusinessException("结束时间不能为空"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date startTime = sdf.parse(startDate);
            Date endTime = sdf.parse(endDate);
            //获取同比
            Date upStartTime = DateUtils.addYears(startTime, -1);
            Date upEndTime = DateUtils.addYears(endTime, -1);
            //获取环比
            //获取两个时间天数之差
            int distanceOfTwoDate = DateUtils.getDistanceOfTwoDateNew(startTime, endTime);
            Date linkEndTime = startTime;
            Date linkStartTime = DateUtils.addDays(linkEndTime, -distanceOfTwoDate);
            Map<String, Object> dataMap = dataSelect(startTime,endTime);
            Map<String, Object> upDataMap = dataSelect(upStartTime,upEndTime);
            Map<String, Object> linkDataMap = dataSelect(linkStartTime,linkEndTime);
            SiAeAllVO siAeAllVO = countAeAll(dataMap.get("checkType").toString(),Double.parseDouble(dataMap.get(
                    "checkNum").toString()), Double.parseDouble(upDataMap.get("checkNum").toString()),
                    Double.parseDouble(linkDataMap.get("checkNum").toString()));
            allVoS.add(siAeAllVO);
            SiAeAllVO siAeAllVO1 = countAeAll(dataMap.get("rectifyType").toString(),Double.parseDouble(dataMap.get(
                    "rectifyNum").toString()), Double.parseDouble(upDataMap.get("rectifyNum").toString()),
                    Double.parseDouble(linkDataMap.get("rectifyNum").toString()));
            allVoS.add(siAeAllVO1);
            SiAeAllVO siAeAllVO2 = countAeAll(dataMap.get("findType").toString(),Double.parseDouble(dataMap.get(
                    "findNum").toString()), Double.parseDouble(upDataMap.get("findNum").toString()),
                    Double.parseDouble(linkDataMap.get("findNum").toString()));
            allVoS.add(siAeAllVO2);
            SiAeAllVO siAeAllVO3 = countAeAll(dataMap.get("punishType").toString(),Double.parseDouble(dataMap.get(
                    "punishNum").toString()), Double.parseDouble(upDataMap.get("punishNum").toString()),
                    Double.parseDouble(linkDataMap.get("punishNum").toString()));
            allVoS.add(siAeAllVO3);
            SiAeAllVO siAeAllVO4 = countAeAll(dataMap.get("sealUpType").toString(),Double.parseDouble(dataMap.get(
                    "sealUpNum").toString()), Double.parseDouble(upDataMap.get("sealUpNum").toString()),
                    Double.parseDouble(linkDataMap.get("sealUpNum").toString()));
            allVoS.add(siAeAllVO4);
            SiAeAllVO siAeAllVO5 = countAeAll(dataMap.get("stopType").toString(),Double.parseDouble(dataMap.get(
                    "stopNum").toString()), Double.parseDouble(upDataMap.get("stopNum").toString()),
                    Double.parseDouble(linkDataMap.get("stopNum").toString()));
            allVoS.add(siAeAllVO5);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return allVoS;
    }
    private Map<String, Object> dataSelect(Date startTime, Date endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("checkType", "检查家次(家)");
        LambdaQueryWrapper<DemNewInspectRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.between(DemNewInspectRecord::getCheckTime,startTime,endTime);
        Integer checkRadio1 = this.baseMapper.selectCount(queryWrapper);
        LambdaQueryWrapper<DemNewOtherRecord> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.between(DemNewOtherRecord::getCheckTime,startTime,endTime);
        Integer checkRadio2 = demNewOtherRecordMapper.selectCount(queryWrapper1);
        map.put("checkNum", checkRadio1+checkRadio2);
        LambdaQueryWrapper<DemNewImmediateNotice> queryWrapper2 = Wrappers.lambdaQuery();
        queryWrapper2.between(DemNewImmediateNotice::getCheckTime,startTime,endTime);
        List<DemNewImmediateNotice> demNewImmediateNoticeList = demNewImmediateNoticeMapper.selectList(queryWrapper2);
        long count = 0;
        if (CollectionUtils.isNotEmpty(demNewImmediateNoticeList)) {
            for (int j = 0; j < demNewImmediateNoticeList.size(); j++) {
                long count1 = demNewImmediateNoticeList.get(j).getContent4().chars().filter(ch -> ch == ',').count();
                count+=count1;
            }
        }
        map.put("rectifyType", "整改隐患(起)");
        map.put("rectifyNum", count+demNewImmediateNoticeList.size());
        LambdaQueryWrapper<DemNewDeadlineNotice> queryWrapper4 = Wrappers.lambdaQuery();
        queryWrapper4.between(DemNewDeadlineNotice::getCheckTime,startTime,endTime);
        List<DemNewDeadlineNotice> demNewDeadlineNoticeList = demNewDeadlineNoticeMapper.selectList(queryWrapper4);
        long deadlineCount = 0;
        if (CollectionUtils.isNotEmpty(demNewDeadlineNoticeList)) {
            for (int j = 0; j < demNewDeadlineNoticeList.size(); j++) {
                long count1 = demNewDeadlineNoticeList.get(j).getContent4().chars().filter(ch -> ch == ',').count();
                deadlineCount+=count1;
            }
        }
        map.put("findType", "发现隐患(起)");
        map.put("findNum", deadlineCount+demNewDeadlineNoticeList.size()+count+demNewImmediateNoticeList.size());
        QueryWrapper<DemNewPenaltyLetter> queryWrapper5 = Wrappers.query();
        queryWrapper5.select("sum(penalty_amount) as totalSum")
        .between("check_time",startTime,endTime);;
        DemNewPenaltyLetter demNewPenaltyLetter = demNewPenaltyLetterMapper.selectOne(queryWrapper5);
        map.put("punishType", "处罚金额(万元)");
        if (demNewPenaltyLetter != null) {
            map.put("punishNum", demNewPenaltyLetter.getTotalSum()/10000);
        }else {
            map.put("punishNum", 0);
        }
        LambdaQueryWrapper<DemNewSeizureLetter> queryWrapper6 = Wrappers.lambdaQuery();
        queryWrapper6.between(DemNewSeizureLetter::getCheckTime,startTime,endTime);
        Integer sealUp = demNewSeizureLetterMapper.selectCount(queryWrapper6);
        map.put("sealUpType", "临时查封(份)");
        map.put("sealUpNum", sealUp);
        LambdaQueryWrapper<DemNewPenaltyLetter> queryWrapper7 = Wrappers.lambdaQuery();
        queryWrapper7.between(DemNewPenaltyLetter::getCheckTime,startTime,endTime)
        .eq(DemNewPenaltyLetter::getTripleStop,1);
        Integer stopNum = demNewPenaltyLetterMapper.selectCount(queryWrapper7);
        map.put("stopType", "责令三停(家)");
        map.put("stopNum", stopNum);
        return map;
    }
    private SiAeAllVO countAeAll(String type,Double radio,Double upRadio,Double linkRadio) {
        SiAeAllVO siAeAllVO = new SiAeAllVO();
        siAeAllVO.setCheckType(type);
        siAeAllVO.setNumber(radio);
        if (0 != radio){
            siAeAllVO.setSameRatio(Arith.div(upRadio,radio));
            siAeAllVO.setLinkRelativeRatio(Arith.div(linkRadio,radio));
        }else {
            siAeAllVO.setSameRatio(0.00);
            siAeAllVO.setLinkRelativeRatio(0.00);
        }
        if (radio.equals(upRadio)){
            siAeAllVO.setSameStatus(0);
        }else if (radio > upRadio){
            siAeAllVO.setSameStatus(1);
        }else {
            siAeAllVO.setSameStatus(2);
        }
        if (radio.equals(linkRadio)){
            siAeAllVO.setLinkRelativeStatus(0);
        }else if (radio > linkRadio){
            siAeAllVO.setLinkRelativeStatus(1);
        }else {
            siAeAllVO.setLinkRelativeStatus(2);
        }
        return siAeAllVO;
    }
}
