package com.nantian.cloud.dbcapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.dbcapp.entity.BigScreenModel;
import com.nantian.cloud.dbcapp.mapper.record.BigScreenMapper;
import com.nantian.cloud.dbcapp.service.BigScreenService;
import com.nantian.cloud.dbcapp.service.EnergyService;
import com.nantian.cloud.dbcapp.unit.DateUtil;
import com.nantian.cloud.dbcapp.vo.AreaAndIndustryVo;
import com.nantian.scheduling.core.context.XxlJobHelper;
import com.nantian.scheduling.core.handler.annotation.XxlJob;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 大屏显示 服务实现类
 * 只需结算2021年开始的数据，不用考虑历史数据（历史结果已在表中）
 * 大屏显示的企业数据范围为已接入完成的企业
 */
@Service("bigScreenService")
public class BigScreenNewImpl implements BigScreenService {

    @Autowired
    private BigScreenMapper bigScreenMapper;
    
    @Autowired
    private EnergyService energyService;

    private static final ArrayList<String> eleEnergyList = new ArrayList<>(); //归为电的能源品种
    private static final ArrayList<String> coalEnergyList = new ArrayList<>(); //归为煤的能源品种
    static {
        Collections.addAll(eleEnergyList, "3300");
        Collections.addAll(coalEnergyList, "0100", "0101", "0102", "0103", "0104", "0105", "0600", "0700", "0800");
    }
    

    //   =============================在线监测企业、重点用能单位企业、电力关口企业、上送国家企业============================
    

    /**
     * 在线监测企业数
     */
    @Override
    //@Scheduled(cron = "0 0 5 * * ?")
    @XxlJob("getAllEnergyZxjcYear")
    public Integer getAllEnergyZxjcYear() {
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 2));
        bigScreenMapper.insertEnterpriseByTmp(2,"");
        return null;
    }

    /**
     * 重点用能单位
     */
    @Override
    //@Scheduled(cron = "0 2 5 * * ?")
    @XxlJob("getAllEnergyNameYear")
    public Integer getAllEnergyNameYear() {
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 8));
        bigScreenMapper.insertEnterpriseByTmp(8,"1");
        return null;
    }

    /**
     * 电力关口企业
     */
    @Override
    //@Scheduled(cron = "0 4 5 * * ?")
    @XxlJob("getAllEnergyDlgkYear")
    public Integer getAllEnergyDlgkYear() {
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 9));
        bigScreenMapper.insertEnterpriseByTmp(9,"2");
        return null;
    }
    

    /**
     * 对接国家平台企业
     */
    @Override
    //@Scheduled(cron = "0 6 5 * * ?")
    @XxlJob("getAllEnergyDjgjYear")
    public Integer getAllEnergyDjgjYear() {
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 10));
        bigScreenMapper.insertEnterpriseByTmp(10,"3");
        return null;
    }


    //   ======================================================年度能耗数据============================================
    

    /**
     * 综合能耗
     */
    @Override
    //@Scheduled(cron = "0 8 5 * * ?")
    @XxlJob("getAllTce")
    public List<AreaAndIndustryVo> getAllTce() {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 0).eq(BigScreenModel.YEAR, time.substring(0,4)));
        bigScreenMapper.insertAllTce( 0, time.substring(0,4), time.split("&")[0], time.split("&")[1]);
        return null;
    }
    
    /***
     * 查询所有地区, 提供大屏地区能耗提供接口
     */
    @Override
    //@Scheduled(cron = "0 10 5 * * ?")
    @XxlJob("getAllAreaByYear")
    public List<AreaAndIndustryVo> getAllAreaByYear() {
        String date = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(date);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 1).eq(BigScreenModel.YEAR, time.substring(0,4)));
        bigScreenMapper.insertRegionTce( 1, time.substring(0,4), time.split("&")[0], time.split("&")[1]);
        return null;
    }

    /***
     * 查询所有行业, 提供大屏行业能耗提供接口
     */
    @Override
    //@Scheduled(cron = "0 12 5 * * ?")
    @XxlJob("getAllHyByYear")
    public List<AreaAndIndustryVo> getAllHyByYear() {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 3).eq(BigScreenModel.YEAR, time.substring(0,4)));
        bigScreenMapper.insertIndustryTce( 3, time.substring(0,4), time.split("&")[0], time.split("&")[1]);
        return null;
    }

    /***
    * 煤消耗
    */
    @Override
    //@Scheduled(cron = "0 14 5 * * ?")
    @XxlJob("getAllMeiYear")
    public List<AreaAndIndustryVo> getAllMeiYear() throws ParseException {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 5).eq(BigScreenModel.YEAR, time.substring(0,4)));
        getEnergyTceByFormula(time, coalEnergyList, 5, time.substring(0,4));
        return null;
    }

    /**
    * 水消耗
    */
    @Override
    //@Scheduled(cron = "0 16 5 * * ?")
    @XxlJob("getAllWaterYear")
    public List<AreaAndIndustryVo> getAllWaterYear() {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 6).eq(BigScreenModel.YEAR, time.substring(0,4)));
        List<String> list = DateUtil.getMonthBetween(time.split("&")[0], time.split("&")[1]);
        for (String month : list) {
            bigScreenMapper.insertWaterValue( 6, time.substring(0,4), month);
        }
        return null;
    }

    /**
    * 电力消耗
    */
    @Override
    //@Scheduled(cron = "0 18 5 * * ?")
    @XxlJob("getAllElectricityYear")
    public List<AreaAndIndustryVo> getAllElectricityYear() throws ParseException {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 7).eq(BigScreenModel.YEAR, time.substring(0,4)));
        getEnergyTceByFormula(time, eleEnergyList, 7, time.substring(0,4));
        return null;
    }

    /**
     * 能源分类
     */
    @Override
    //@Scheduled(cron = "0 20 5 * * ?")
    @XxlJob("getAllXfjgYear")
    public Integer getAllXfjgYear() throws ParseException {
        String year = XxlJobHelper.getJobParam();
        String time = getLastMonthAgo(year);
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 4).eq(BigScreenModel.YEAR, time.substring(0,4)));
        List<String> formula = energyService.parseFormulToSimpleAndParamsMap("云南", "全部", time, time.split("&")[0], time.split("&")[1], null);
        bigScreenMapper.insertConsumptionByFormula( 4, time.substring(0,4), time.split("&")[0], time.split("&")[1], formula);
        return 0;
    }

    /**
     * 获取时间内各地区单个能源实物量
     */
    private void getEnergyTceByFormula(String time, ArrayList<String> energyList, int bigScreenType, String year) throws ParseException {
        List<String> formula = energyService.parseFormulToSimpleAndParamsMap("云南", "全部", time, time.split("&")[0], time.split("&")[1], energyList);
        List<String> list = DateUtil.getMonthBetween(time.split("&")[0], time.split("&")[1]);
        for (String month : list) {
            bigScreenMapper.insertEnergyTceByFormula(bigScreenType, year, month, formula, "0.0001");
        }
    }

 //   ======================================================近六个月能耗数据============================================


    /**
     * 综合能耗
     */
    @Override
    //@Scheduled(cron = "0 22 5 * * ?")
    @XxlJob("getAllTce6")
    public List<AreaAndIndustryVo> getAllTce6() {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 0).eq(BigScreenModel.YEAR, "近六个月"));
        bigScreenMapper.insertAllTce( 0, "近六个月", time.split("&")[0], time.split("&")[1]);
        return null;
    }

    /**
     * 查询所有地区, 提供大屏地区能耗提供接口
     */
    @Override
    //@Scheduled(cron = "0 24 5 * * ?")
    @XxlJob("getAllAreaByYear6")
    public List<AreaAndIndustryVo> getAllAreaByYear6() {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 1).eq(BigScreenModel.YEAR, "近六个月"));
        bigScreenMapper.insertRegionTce( 1, "近六个月", time.split("&")[0], time.split("&")[1]);
        return null;
    }

    /**
     * 查询所有行业, 提供大屏行业能耗提供接口
     */
    @Override
    //@Scheduled(cron = "0 26 5 * * ?")
    @XxlJob("getAllHyByYear6")
    public List<AreaAndIndustryVo> getAllHyByYear6() {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 3).eq(BigScreenModel.YEAR, "近六个月"));
        bigScreenMapper.insertIndustryTce( 3, "近六个月", time.split("&")[0], time.split("&")[1]);
        return null;
    }

    /**
     * 煤消耗
     */
    @Override
    //@Scheduled(cron = "0 28 5 * * ?")
    @XxlJob("getAllMeiYear6")
    public List<AreaAndIndustryVo> getAllMeiYear6() throws ParseException {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 5).eq(BigScreenModel.YEAR, "近六个月"));
        getEnergyTceByFormula(time, coalEnergyList, 5, "近六个月");
        return null;
    }

    /**
    * 水消耗
    */
    @Override
    //@Scheduled(cron = "0 30 5 * * ?")
    @XxlJob("getAllWaterYear6")
    public List<AreaAndIndustryVo> getAllWaterYear6() {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 6).eq(BigScreenModel.YEAR, "近六个月"));
        List<String> list = DateUtil.getMonthBetween(time.split("&")[0], time.split("&")[1]);
        for (String month : list) {
            bigScreenMapper.insertWaterValue( 6, "近六个月", month);
        }
        return null;
    }

    /**
     * 电力消耗
     */
    @Override
    //@Scheduled(cron = "0 32 5 * * ?")
    @XxlJob("getAllElectricityYear6")
    public List<AreaAndIndustryVo> getAllElectricityYear6() throws ParseException {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 7).eq(BigScreenModel.YEAR, "近六个月"));
        getEnergyTceByFormula(time, eleEnergyList, 7, "近六个月");
        return null;
    }

    /**
     * 能源分类
     */
    //@Scheduled(cron = "0 34 5 * * ?")
    @XxlJob("getAllXfjgYear6")
    public Integer getAllXfjgYear6() throws ParseException {
        String time = getSixMonthAgo();
        bigScreenMapper.delete(new QueryWrapper<BigScreenModel>().eq(BigScreenModel.TYPE, 4).eq(BigScreenModel.YEAR, "近六个月"));
        List<String> formula = energyService.parseFormulToSimpleAndParamsMap("云南", "全部", time, time.split("&")[0], time.split("&")[1], null);
        bigScreenMapper.insertConsumptionByFormula( 4, "近六个月", time.split("&")[0], time.split("&")[1], formula);
        return 0;
    }


    /**
     * 得到前6个月时间区间值 现在为2022-11 得到时间2022-05&2022-10
     */
    public static String getSixMonthAgo(){
        //得到当前的年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MONTH,-6);
        String l = sdf.format(now.getTime());
        Calendar now1 = Calendar.getInstance();
        now1.add(Calendar.MONTH,-1);
        String l1 = sdf.format(now1.getTime());
        return l+"&"+l1;
    }

    /**
     * 得到年度时间 若未指定年份，将获取前一个月年度时间 现在时间为2023-01 得到时间为2022-01&2022-12
     */
    public static String getLastMonthAgo(String year){
        if(StringUtils.isNotBlank(year)){ //指定年份不为空
            return year+"-01&"+year+"-12";
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -1);  //获取前一个月
            String tmpYear = String.valueOf(calendar.get(Calendar.YEAR)); //获取年度
            String date = sdf.format(calendar.getTime());
            return tmpYear+"-01&"+date;
        }
    }
    
}