package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.domain.effective.PcNationalCityRankingOther;
import cn.cecep.talroad.domain.leader.vo.*;
import cn.cecep.talroad.mapper.LeaderCockpitMapper;
import cn.cecep.talroad.mapper.effective.PcNationalCityRankingOtherMapper;
import cn.cecep.talroad.vo.EChartsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 领导驾驶舱服务层
 */
@Service
@Slf4j
public class LeaderCockpitService {

    @Resource
    private LeaderCockpitMapper leaderCockpitMapper;

    @Autowired
    private PcNationalCityRankingOtherMapper pcNationalCityRankingOtherMapper;

    /**
     * 地图站点类型复选展示
     * @return
     */
    public AjaxResults<List<StationTypeDictVO>> stationTypeList(){
        return AjaxResults.success(leaderCockpitMapper.selectStationTypeList());
    }

    /**
     * 地图上方三个数量展示
     * @return
     */
    public AjaxResults<MapUpVo> MapUpCount(){
        return AjaxResults.success(leaderCockpitMapper.selectMapUpCount());
    }

    /**
     *  获取融合数据所有数量的接口
     * @return
     */
    public AjaxResults<FusionCountDataVO> getFusionCountData(){
        FusionCountDataVO fusionCountDataVO = new FusionCountDataVO();
        fusionCountDataVO.setDatabaseDataCount(leaderCockpitMapper.getDatabaseDataCount());
        fusionCountDataVO.setFactoryManTypeCount(leaderCockpitMapper.getFactoryManTypeCount());
        fusionCountDataVO.setFactoryDataTypeCount(leaderCockpitMapper.getFactoryDataTypeCount());
        fusionCountDataVO.setDeviceCount(leaderCockpitMapper.selectDeviceCount());
        fusionCountDataVO.setOutletCount(leaderCockpitMapper.selectOutletCount());
        return AjaxResults.success(fusionCountDataVO);
    }

    /**
     * 关联分析数据接口
     * @param problemId 二级分类筛选的时候进行查询展示
     * @return
     */
    public AjaxResults<ProblemDataCountVO> getProblemDataCount(String problemId) {
        ProblemDataCountVO problemDataCountVO = new ProblemDataCountVO();
        problemDataCountVO.setProblemTypeCountVO(leaderCockpitMapper.selectProblemTypeCount());
        problemDataCountVO.setProblemDoingCountVO(leaderCockpitMapper.selectProblemDoingCount());
        problemDataCountVO.setLevelOneGroupCount(leaderCockpitMapper.selectProblemOneLevelGroupCount());
        problemDataCountVO.setLevelTwoGroupCount(leaderCockpitMapper.selectProblemTwoLevelGroupCount(problemId));
        return AjaxResults.success(problemDataCountVO);
    }

    /**
     * 三屏联动
     * @return
     */
    public AjaxResults<ThreeMonitorCountVO> getThreeMonitorData() {
        ThreeMonitorCountVO threeMonitorCountVO = new ThreeMonitorCountVO();
        threeMonitorCountVO.setTaskCountVO(new TaskCountVO(66,"50",22,22,22,33,20,8,5));
        threeMonitorCountVO.setQuestionCountVO(new QuestionCountVO(50,"53.4",90,"28"));
        List<QuestionTop10CountVO> top10CountVOList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            QuestionTop10CountVO questionTop10CountVO = new QuestionTop10CountVO();
            questionTop10CountVO.setQuestionName("问题"+Math.abs(new Random().nextInt())%100);
            questionTop10CountVO.setQuestionCount(Math.abs(new Random().nextInt())%100+(10-i));
            top10CountVOList.add(questionTop10CountVO);
        }
        threeMonitorCountVO.setQuestionTop10CountVO(top10CountVOList);
        return AjaxResults.success(threeMonitorCountVO);
    }

    /**
     * 监督执法
     * @return
     */
    public AjaxResults getMonitorEnforcementData(Integer selectType) {
        int rateNum = 100;
        if(selectType != null && selectType.compareTo(1) == 0){
            rateNum = 50;
        }
        MonitorEnforcementCountVO monitorEnforcementCountVO = new MonitorEnforcementCountVO();
        RegulatoryCapacityCountVO regulatoryCapacityCountVO = new RegulatoryCapacityCountVO();
        regulatoryCapacityCountVO.setImplementPersonCount(20);
        regulatoryCapacityCountVO.setImplementCarCount(5);
        regulatoryCapacityCountVO.setImplementClientCount(15);
        EChartsVO eChartsVO = new EChartsVO();
        List<String> strings = Arrays.asList("丰润区", "丰南区", "古冶区", "曹妃甸区", "开平区", "玉田县", "迁安市", "滦州市", "遵化市", "乐亭县");
        List<String> xTypes = Arrays.asList("监管企业", "执法人员", "人均监管");
        eChartsVO.setLegendData(strings);
        List<EChartsVO.SeriesObj> seriesObjList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            EChartsVO.SeriesObj seriesObj = new EChartsVO.SeriesObj();
            List<Object> listValue = new ArrayList<>();
            for (int j = 0; j < strings.size(); j++) {
                listValue.add(Math.abs(new Random().nextInt())%rateNum);
            }
            seriesObj.setValues(listValue);
            seriesObj.setName(xTypes.get(i));
            seriesObjList.add(seriesObj);
        }
        eChartsVO.setSeries(seriesObjList);
        regulatoryCapacityCountVO.setCountData(eChartsVO);
        monitorEnforcementCountVO.setRegulatoryCapacityCountVO(regulatoryCapacityCountVO);
        monitorEnforcementCountVO.setMonitorTaskCountVO(new MonitorTaskCountVO(55,37,41,65));
        monitorEnforcementCountVO.setFactoryColorTagCountVO(new FactoryColorTagCountVO(100,20,32,48));
        List<AreaTaskCountVO> list = new ArrayList<>();
        for (int i = 0; i < strings.size(); i++) {
            AreaTaskCountVO areaTaskCountVO = new AreaTaskCountVO();
            areaTaskCountVO.setAreaName(strings.get(i));
            areaTaskCountVO.setEnforcementCount(Math.abs(new Random().nextInt())%rateNum);
            areaTaskCountVO.setEnforcementPersonCount(Math.abs(new Random().nextInt())%rateNum);
            areaTaskCountVO.setQuestionCount(Math.abs(new Random().nextInt())%rateNum);
            list.add(areaTaskCountVO);
        }
        monitorEnforcementCountVO.setAreaTaskCountVO(list);
        return AjaxResults.success(monitorEnforcementCountVO);
    }

    public AjaxResults<AirDataVO> getAirData() {

        AirDataVO airDataVO = new AirDataVO();
        AirQualityDataVO cityAirQualityData = leaderCockpitMapper.getCityAirQualityData();

        // 排名用年数据
        PcNationalCityRankingOther cityRankingOther = pcNationalCityRankingOtherMapper.selectOne(new QueryWrapper<PcNationalCityRankingOther>()
                .select("index_337", "index_168", "index_province_city")
                .eq("date_type", "Y")
                .eq("\"cityName\"", "唐山市")
                .orderByDesc("date_time", "id")
                .last("limit 1")
        );
        if (cityAirQualityData != null && cityRankingOther != null) {
            cityAirQualityData.setIndex337(cityRankingOther.getIndex337());
            cityAirQualityData.setIndex168(cityRankingOther.getIndex168());
            cityAirQualityData.setIndexProvinceCity(cityRankingOther.getIndexProvinceCity());
        }

        airDataVO.setAirQualityDataVO(cityAirQualityData);
        List<AirForecastDataVO> airForecastDataVOList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            String[] pollutantArray = {"Pm2.5","Pm10","So2","CO","NO","O3"};
            String[] pollutantLevelArray = {"优","良","中","差"};
            String timeFormat = LocalDateTime.now().plus(i, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00"));
            AirForecastDataVO airForecastDataVO = new AirForecastDataVO();
            airForecastDataVO.setForecastDate(timeFormat);
            airForecastDataVO.setAqiMax(""+(Math.abs(new Random().nextInt())%20+30));
            airForecastDataVO.setAqiMin(""+(Math.abs(new Random().nextInt())%20+10));
            airForecastDataVO.setPrimaryPollutant(pollutantArray[Math.abs(new Random().nextInt())%pollutantArray.length]);
            int indexLevel = Math.abs(new Random().nextInt())%pollutantLevelArray.length;
            airForecastDataVO.setLevelNameMin(pollutantLevelArray[indexLevel]);
            airForecastDataVO.setLevelNameMax(pollutantLevelArray[indexLevel+1>=pollutantLevelArray.length?indexLevel:indexLevel+1]);
            airForecastDataVOList.add(airForecastDataVO);
        }
        airDataVO.setAirForecastData(airForecastDataVOList);
        PolluteControlDataVO polluteControlDataVO = new PolluteControlDataVO();
        polluteControlDataVO.setYearEmergencyControlCount(Math.abs(new Random().nextInt())%10);
        polluteControlDataVO.setDetectFactoryQuestionCount(Math.abs(new Random().nextInt())%50);
        polluteControlDataVO.setQuestionList(Arrays.asList(new DetectFactoryQuestionVO("迁安中化煤化工有限责任公司",Math.abs(new Random().nextInt())%20),
                new DetectFactoryQuestionVO("华润电力唐山丰润有限公司",Math.abs(new Random().nextInt())%20),
                new DetectFactoryQuestionVO("唐山文丰特钢有限公司",Math.abs(new Random().nextInt())%20)));
        airDataVO.setPolluteControlData(polluteControlDataVO);
        return AjaxResults.success(airDataVO);
    }
}
