package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.domain.leader.vo.ProblemGroupCountVO;
import cn.cecep.talroad.domain.vo.ProblemGroupTimeCountVO;
import cn.cecep.talroad.mapper.AmProblemFiMapper;
import cn.cecep.talroad.vo.analysis.FactoryIndustryProblemCountVO;
import cn.cecep.talroad.vo.analysis.FactoryTop10ProblemCountVO;
import cn.cecep.talroad.vo.query.analysis.SREStatisticsQuery;
import cn.cecep.talroad.vo.EChartsVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cecep.talroad.domain.analysis.SRaEmissionsStatistics;
import cn.cecep.talroad.mapper.analysis.SRaEmissionsStatisticsMapper;
import cn.cecep.talroad.service.analysis.SRaEmissionsStatisticsService;

import javax.annotation.Resource;

@Service
public class SRaEmissionsStatisticsServiceImpl extends ServiceImpl<SRaEmissionsStatisticsMapper, SRaEmissionsStatistics> implements SRaEmissionsStatisticsService{

    @Resource
    private AmProblemFiMapper amProblemFiMapper;

    @Resource
    private SRaEmissionsStatisticsMapper sRaEmissionsStatisticsMapper;


    @Override
    public AjaxResults<EChartsVO> selectChartData(SREStatisticsQuery query) {
        query.setType("trend");
        EChartsVO eChartsVO = new EChartsVO();
        List<String> dateList = CollUtil.map(DateUtil.rangeToList(query.getProblemTimeStart(),
                        query.getProblemTimeEnd(), DateField.DAY_OF_MONTH),
                x -> DateUtil.format(x,"yyyy-MM-dd"), true);
        eChartsVO.setXData(dateList);
        //List<SREStatisticsQuery> list = baseMapper.selectListAll(query);
        //获取问题大类
        List<String> problemTypeList = sRaEmissionsStatisticsMapper.selectProblemType();
        //获取各个日期与问题大类的分组数量
        List<ProblemGroupTimeCountVO> problemGroupTimeCountVOS = sRaEmissionsStatisticsMapper.selectFactoryProblemGroup(query);
        List<EChartsVO.SeriesObj> collect = new ArrayList<>();
        //数据分拣,一层包装,日期key，内层map为问题分类key
        Map<String,Map<String,Integer>> map = new HashMap();
        for (ProblemGroupTimeCountVO problemGroupTimeCountVO : problemGroupTimeCountVOS) {
            Map<String,Integer> mapTemp;
            if (!map.containsKey(problemGroupTimeCountVO.getDateTime())) {
                mapTemp= new HashMap();
                map.put(problemGroupTimeCountVO.getDateTime(),mapTemp);
            }
            mapTemp = map.get(problemGroupTimeCountVO.getDateTime());
            mapTemp.put(problemGroupTimeCountVO.getName(), problemGroupTimeCountVO.getTotalCount());
        }
        for (String typeName : problemTypeList) {
            EChartsVO.SeriesObj seriesObj = new EChartsVO.SeriesObj();
            seriesObj.setName(typeName);
            //格式化日期数据，如果空则添加0
            List<Integer> dataValues = new ArrayList<>();
            for (String time : dateList) {
                //该日期不存在数据，直接赋0
                if (!map.containsKey(time)) {
                    dataValues.add(0);
                    continue;
                }
                Integer value = map.get(time).get(typeName);
                dataValues.add(value==null?0:value);
            }
            seriesObj.setValues((List)dataValues);
            collect.add(seriesObj);
        }
        eChartsVO.setSeries(collect);
        eChartsVO.setName("企业异常行为发生趋势");
        return AjaxResults.success(eChartsVO);
    }

    @Override
    public List<FactoryTop10ProblemCountVO> selectChartDataTop(SREStatisticsQuery query) {
        query.setType("top");
        List<FactoryTop10ProblemCountVO> factoryTop10ProblemCountVOS = sRaEmissionsStatisticsMapper.selectTop10Factory(query);
        return factoryTop10ProblemCountVOS;
    }

    @Override
    public EChartsVO selectChartDataIndustry(SREStatisticsQuery query) {
        query.setType("industry");
        EChartsVO eChartsVO=new EChartsVO();
        //获取问题大类
        List<FactoryIndustryProblemCountVO> tempIndustry = sRaEmissionsStatisticsMapper.selectTop10FactoryIndustry(query);
        List<String> industryType = tempIndustry.stream().map((e) -> e.getIndustryPermissionName()).collect(Collectors.toList());

        List<FactoryIndustryProblemCountVO> factoryIndustryProblemCountVOS = sRaEmissionsStatisticsMapper.selectFactoryIndustryProblemGroup(query);
        //数据分拣,一层包装,日期key，内层map为问题分类key
        Map<String,Map<String,Integer>> map = new HashMap();
        for (FactoryIndustryProblemCountVO factoryIndustryProblemCountVO : factoryIndustryProblemCountVOS) {
            Map<String,Integer> mapTemp;
            if (!map.containsKey(factoryIndustryProblemCountVO.getName())) {
                mapTemp= new HashMap();
                map.put(factoryIndustryProblemCountVO.getName(),mapTemp);
            }
            mapTemp = map.get(factoryIndustryProblemCountVO.getName());
            mapTemp.put(factoryIndustryProblemCountVO.getIndustryPermissionName(), factoryIndustryProblemCountVO.getTotalCount());
        }
        //组装数据
        List<EChartsVO.SeriesObj> collect = new ArrayList<>();
        for (Map.Entry<String, Map<String, Integer>> stringMapEntry : map.entrySet()) {
            EChartsVO.SeriesObj seriesObj = new EChartsVO.SeriesObj();
            seriesObj.setName(stringMapEntry.getKey());
            //格式化行业类别数据，如果空则添加0
            List<Integer> dataValues = new ArrayList<>();
            for (String type : industryType) {
                //该行业类别不存在数据，直接赋0
                if (!stringMapEntry.getValue().containsKey(type)) {
                    dataValues.add(0);
                    continue;
                }
                Integer value = stringMapEntry.getValue().get(type);
                dataValues.add(value==null?0:value);
            }
            seriesObj.setValues((List)dataValues);
            collect.add(seriesObj);
        }
        eChartsVO.setName("行业异常行为排名");
        eChartsVO.setXData(industryType);
        eChartsVO.setSeries(collect);
        return eChartsVO;
    }
}
