package org.jeecg.modules.stm.project.chart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.domain.vo.StDomainVo;
import org.jeecg.modules.stm.project.chart.mapper.StProjectChartMapper;
import org.jeecg.modules.stm.project.chart.service.ProjectChartService;
import org.jeecg.modules.stm.project.chart.vo.DomainFindVo;
import org.jeecg.modules.stm.project.chart.vo.DomainLevelVo;
import org.jeecg.modules.stm.project.chart.vo.DomainScoreVo;
import org.jeecg.modules.stm.project.chart.vo.QueryDomainLevelVo;
import org.jeecg.modules.stm.project.create.entity.StProjectCheck;
import org.jeecg.modules.stm.project.create.service.IStProjectCheckService;
import org.jeecg.modules.stm.project.exec.service.IStProjectExecService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yzh
 * @Date 2023/8/27 13:38
 * @Description:
 */
@Service
public class ProjectChartServiceImpl implements ProjectChartService {

    @Autowired
    private IStProjectExecService iStProjectExecService;

    @Autowired
    private IStProjectCheckService iStProjectCheckService;

    @Autowired
    private IStDomainService iStDomainService;

    @Autowired
    private IStCaMainService iStCaMainService;

    @Autowired
    private StProjectChartMapper stProjectChartMapper;

    private String queryDomainCodeByCaNo(String caNo){
        QueryWrapper<StCaMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CA_NO",caNo);
        StCaMain caMain = iStCaMainService.getOne(queryWrapper);
        if(caMain!=null){
            return caMain.getDomainCode();
        }
        return null;
    }

    @Override
    public DomainLevelVo domainlevel() {
        // 查询所有的检查项目
        List<StProjectCheck> stProjectCheckList = iStProjectCheckService.list();
        if(CollectionUtils.isEmpty(stProjectCheckList)){
            return new DomainLevelVo();
        }
        Map<String,Boolean> caContinueMap = new HashMap<>();
        // 处理活动编号
        Map<String,List<Integer>> checkIdMap = new HashMap<>();
        for(StProjectCheck stProjectCheck:stProjectCheckList){
            String caNo = stProjectCheck.getCaNo();
            if(caContinueMap.get(caNo)!=null){
                continue;
            }
            String domainCode = queryDomainCodeByCaNo(caNo);
            if(!StringUtils.hasText(domainCode)){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            // 如果存在该控制活动不在计算领域名称
            StDomainVo stDomainVo = iStDomainService.queryEntityByCode(domainCode);
            if(!StringUtils.hasText(stDomainVo.getFirstDomainName())){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            String names = stDomainVo.getFirstDomainName();
            List<Integer> ids = checkIdMap.get(names);
            if (CollectionUtils.isEmpty(ids)) {
                ids = new ArrayList<>();
                checkIdMap.put(names, ids);
            }
            ids.add(stProjectCheck.getId());
        }
        // 统计sql
        DomainLevelVo domainLevelVo = new DomainLevelVo();
        for(String name:checkIdMap.keySet()){
            domainLevelVo.getNames().add(name);
            List<Integer> ids = checkIdMap.get(name);
            List<QueryDomainLevelVo> queryDomainLevelVoList = stProjectChartMapper.staticByckIds(ids);
            long hLevel = 0L;
            long level = 0L;
            long llevel = 0L;
            if(!CollectionUtils.isEmpty(queryDomainLevelVoList)){
                for(QueryDomainLevelVo queryDomainLevelVo:queryDomainLevelVoList){
                    if("高".equals(queryDomainLevelVo.getDamageLevel())){
                        hLevel = queryDomainLevelVo.getCount();
                    }
                    if("中".equals(queryDomainLevelVo.getDamageLevel())){
                        level = queryDomainLevelVo.getCount();
                    }
                    if("低".equals(queryDomainLevelVo.getDamageLevel())){
                        llevel = queryDomainLevelVo.getCount();
                    }
                }
            }
            domainLevelVo.getHightLevel().add(hLevel);
            domainLevelVo.getLevel().add(level);
            domainLevelVo.getLowLevel().add(llevel);
        }
        return domainLevelVo;
    }

    @Override
    public DomainScoreVo domainScore() {
        // 查询所有的检查项目
        List<StProjectCheck> stProjectCheckList = iStProjectCheckService.list();
        if(CollectionUtils.isEmpty(stProjectCheckList)){
            return new DomainScoreVo();
        }
        Map<String,Boolean> caContinueMap = new HashMap<>();
        // 处理活动编号
        Map<String,List<Integer>> checkIdMap = new HashMap<>();
        for(StProjectCheck stProjectCheck:stProjectCheckList){
            String caNo = stProjectCheck.getCaNo();
            if(caContinueMap.get(caNo)!=null){
                continue;
            }
            String domainCode = queryDomainCodeByCaNo(caNo);
            if(!StringUtils.hasText(domainCode)){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            // 如果存在该控制活动不在计算领域名称
            StDomainVo stDomainVo = iStDomainService.queryEntityByCode(domainCode);
            if(!StringUtils.hasText(stDomainVo.getFirstDomainName())){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            String names = stDomainVo.getFirstDomainName();
            List<Integer> ids = checkIdMap.get(names);
            if (CollectionUtils.isEmpty(ids)) {
                ids = new ArrayList<>();
                checkIdMap.put(names, ids);
            }
            ids.add(stProjectCheck.getId());
        }
        // 统计sql
        DomainScoreVo domainScoreVo = new DomainScoreVo();
        for(String name:checkIdMap.keySet()){
            domainScoreVo.getNames().add(name);
            List<Integer> ids = checkIdMap.get(name);
            long score = stProjectChartMapper.sumScoreByIds(ids);
            domainScoreVo.getScores().add(score);
        }
        return domainScoreVo;
    }

    @Override
    public DomainFindVo domainFind() {
        // 查询所有的检查项目
        List<StProjectCheck> stProjectCheckList = iStProjectCheckService.list();
        if(CollectionUtils.isEmpty(stProjectCheckList)){
            return new DomainFindVo();
        }
        Map<String,Boolean> caContinueMap = new HashMap<>();
        // 处理活动编号
        Map<String,List<Integer>> checkIdMap = new HashMap<>();
        for(StProjectCheck stProjectCheck:stProjectCheckList){
            String caNo = stProjectCheck.getCaNo();
            if(caContinueMap.get(caNo)!=null){
                continue;
            }
            String domainCode = queryDomainCodeByCaNo(caNo);
            if(!StringUtils.hasText(domainCode)){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            // 如果存在该控制活动不在计算领域名称
            StDomainVo stDomainVo = iStDomainService.queryEntityByCode(domainCode);
            if(!StringUtils.hasText(stDomainVo.getFirstDomainName())){
                caContinueMap.put(caNo,Boolean.TRUE);
                continue;
            }
            String names = stDomainVo.getFirstDomainName();
            List<Integer> ids = checkIdMap.get(names);
            if (CollectionUtils.isEmpty(ids)) {
                ids = new ArrayList<>();
                checkIdMap.put(names, ids);
            }
            ids.add(stProjectCheck.getId());
        }
        // 统计sql
        DomainFindVo domainFindVo = new DomainFindVo();
        for(String name:checkIdMap.keySet()){
            domainFindVo.getNames().add(name);
            List<Integer> ids = checkIdMap.get(name);
            List<QueryDomainLevelVo> queryDomainFindVoList = stProjectChartMapper.staticSeverityByckIds(ids);
            long fzyzCount = 0L;
            long yzCount = 0L;
            long count = 0L;
            long byzCount = 0L;
            if(!CollectionUtils.isEmpty(queryDomainFindVoList)){
                for(QueryDomainLevelVo queryDomainLevelVo:queryDomainFindVoList){
                    if("非常严重".equals(queryDomainLevelVo.getDamageLevel())){
                        fzyzCount = queryDomainLevelVo.getCount();
                    }
                    if("严重".equals(queryDomainLevelVo.getDamageLevel())){
                        yzCount = queryDomainLevelVo.getCount();
                    }
                    if("一般".equals(queryDomainLevelVo.getDamageLevel())){
                        count = queryDomainLevelVo.getCount();
                    }
                    if("不严重".equals(queryDomainLevelVo.getDamageLevel())) {
                        byzCount =queryDomainLevelVo.getCount();
                    }
                }
            }
            domainFindVo.getFcyzValues().add(fzyzCount);
            domainFindVo.getYzValues().add(yzCount);
            domainFindVo.getValues().add(count);
            domainFindVo.getByzValues().add(byzCount);
        }
        return domainFindVo;
    }
}
