package com.gyee.runeconomy.service.Application;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProEconAlarmTypeMapper;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.model.vo.YcdlVo;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.MathUtil;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ApplicationService {

    @Resource
    private IProEconPowerstationInfoDay5Service proEconPowerstationInfoDay5Service;

    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;

    @Resource
    private IProBasicProjectPlanService planService;

    @Resource
    private IProEconStationPowerService powerService;
    @Resource
    private IProEconAlarmTypeService alarmTypeService;

    @Resource
    private IProEconAlarmConfigurationService alarmConfigurationService;

    @Resource
    private IProEconShutdownEventService shutdownEventService;

    @Resource
    private ProEconAlarmTypeMapper proEconAlarmTypeMapper;
    @Resource
    private IEdosUtil edosUtil;


    public Map bulletin(String wpId) throws Exception {

        Map<String, Object> map = new HashMap<>();


        double rpjfs = 0.0;
        double rswdl = 0.0;
        double rgwdl = 0.0;
        double rfdl = 0.0;
        double rjhdl = 0.0;
        double rwcl = 0.0;

        double ypjfs = 0.0;
        double yswdl = 0.0;
        double ygwdl = 0.0;
        double yfdl = 0.0;
        double yjhdl = 0.0;
        double ywcl = 0.0;

        double npjfs = 0.0;
        double nswdl = 0.0;
        double ngwdl = 0.0;
        double nfdl = 0.0;
        double njhdl = 0.0;
        double nwcl = 0.0;

        //当前月份
        LocalDate currentDate = LocalDate.now();
        int currentMonth = currentDate.getMonthValue();

        //当前年份
        Calendar cal = Calendar.getInstance();
        int dqnf = cal.get(Calendar.YEAR);//本年

        //日发电量
        ProBasicPowerstationPoint rfdlvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.RFDL);
        rfdl = edosUtil.getRealData(rfdlvalue).getPointValueInDouble();


        List<ProEconPowerstationInfoDay5> getdrlist = proEconPowerstationInfoDay5Service.getdrlist(wpId);
        for (ProEconPowerstationInfoDay5 dr : getdrlist) {

            yfdl += dr.getYfdldb().doubleValue();
            nfdl += dr.getNfdldb().doubleValue();
        }

        List<ProBasicProjectPlan> plans = planService.getmonthList(String.valueOf(currentMonth));
        List<ProBasicProjectPlan> planyear = planService.getList(String.valueOf(dqnf));

        if (!wpId.endsWith("STA")) {

            yjhdl = plans.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            njhdl = planyear.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            rjhdl = yjhdl / 30;
        } else {
            List<ProBasicProjectPlan> collect = plans.stream().filter(p -> p.getWindpowerstationId().equals(wpId)).collect(Collectors.toList());
            List<ProBasicProjectPlan> year = planyear.stream().filter(p -> p.getWindpowerstationId().equals(wpId)).collect(Collectors.toList());
            yjhdl = collect.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            njhdl = year.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            rjhdl = yjhdl / 30;
        }
        List<ProEconPowerstationInfoDay5> getzrlist = proEconPowerstationInfoDay5Service.getzrlist(wpId);
        for (ProEconPowerstationInfoDay5 zr : getzrlist) {

        }

        map.put("日发电量", MathUtil.twoBit(rfdl / 10000));
        map.put("月发电量", MathUtil.twoBit(yfdl / 10000));
        map.put("年发电量", MathUtil.twoBit(nfdl / 10000));
        map.put("日计划发电量", MathUtil.twoBit(rjhdl));
        map.put("月计划发电量", MathUtil.twoBit(yjhdl));
        map.put("年计划发电量", MathUtil.twoBit(njhdl));
        map.put("日完成率", StringUtils.round(((rfdl / 10000) / rjhdl) * 100, 0) + "%");
        map.put("月完成率", StringUtils.round(((yfdl / 10000) / yjhdl) * 100, 0) + "%");
        map.put("年完成率", StringUtils.round(((nfdl / 10000) / njhdl) * 100, 0) + "%");

        return map;
    }

    public Map electricity() throws Exception {

        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);

        //当前月份
        LocalDate currentDate = LocalDate.now();
        int currentMonth = currentDate.getMonthValue();

        //当前年份
        Calendar cal = Calendar.getInstance();
        int dqnf = cal.get(Calendar.YEAR);//本年

        List<Double> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> zb = new HashMap<>();
        List<ProBasicPowerstation> wpls = CacheContext.wpls;

        List<ProBasicProjectPlan> plans = planService.getmonthList(String.valueOf(currentMonth));
        List<ProBasicProjectPlan> planyear = planService.getList(String.valueOf(dqnf));

        for (ProBasicPowerstation wp : wpls) {

            zb = new HashMap<>();
            double rfdl = 0.0;
            double yfdl = 0.0;
            double nfdl = 0.0;

            double rjhdl = 0.0;
            double yjhdl = 0.0;
            double njhdl = 0.0;
            //日发电量
            ProBasicPowerstationPoint rfdlvalue = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.RFDL);
            rfdl = edosUtil.getRealData(rfdlvalue).getPointValueInDouble();

            List<ProEconPowerstationInfoDay5> getdrlist = proEconPowerstationInfoDay5Service.getdrlist(wp.getId());
            for (ProEconPowerstationInfoDay5 dr : getdrlist) {

                yfdl += dr.getYfdldb().doubleValue();
                nfdl += dr.getNfdldb().doubleValue();
            }


            List<ProBasicProjectPlan> collect = plans.stream().filter(p -> p.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            List<ProBasicProjectPlan> year = planyear.stream().filter(p -> p.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            yjhdl = collect.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            njhdl = year.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            rjhdl = yjhdl / 30;

            zb.put("rfdl", MathUtil.twoBit(rfdl / 10000));
            zb.put("yfdl", MathUtil.twoBit(yfdl / 10000));
            zb.put("nfdl", MathUtil.twoBit(nfdl / 10000));

            zb.put("rjhdl", MathUtil.twoBit(rjhdl));
            zb.put("yjhdl", MathUtil.twoBit(yjhdl));
            zb.put("njhdl", MathUtil.twoBit(njhdl));

            map.put(wp.getId(), zb);
        }


        return map;
    }


    public Map<String, Object>[] electricityc() throws Exception {
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);

        //当前月份
        LocalDate currentDate = LocalDate.now();
        int currentMonth = currentDate.getMonthValue();

        //当前年份
        Calendar cal = Calendar.getInstance();
        int dqnf = cal.get(Calendar.YEAR);//本年

        List<Double> list = new ArrayList<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<ProBasicPowerstation> wpls = CacheContext.wpls;

        double rfdl = 0.0;
        double yfdl = 0.0;
        double nfdl = 0.0;
        double rjhdl = 0.0;
        double yjhdl = 0.0;
        double njhdl = 0.0;

        List<ProBasicProjectPlan> plans = planService.getmonthList(String.valueOf(currentMonth));
        List<ProBasicProjectPlan> planyear = planService.getList(String.valueOf(dqnf));
        @SuppressWarnings("unchecked")
        Map<String, Object>[] resultArray = new Map[wpls.size()];
        int i = 0;
        for (ProBasicPowerstation wp : wpls) {
            Map<String, Object> zb = new HashMap<>();

            //日发电量
            ProBasicPowerstationPoint rfdlvalue = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.RFDL);
            rfdl = edosUtil.getRealData(rfdlvalue).getPointValueInDouble();

            List<ProEconPowerstationInfoDay5> getdrlist = proEconPowerstationInfoDay5Service.getdrlist(wp.getId());
            for (ProEconPowerstationInfoDay5 dr : getdrlist) {
                yfdl += dr.getYfdldb().doubleValue();
                nfdl += dr.getNfdldb().doubleValue();
            }

            List<ProBasicProjectPlan> collect = plans.stream().filter(p -> p.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            List<ProBasicProjectPlan> year = planyear.stream().filter(p -> p.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            yjhdl = collect.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            njhdl = year.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum();
            rjhdl = yjhdl / 30;

            zb.put("wpid", wp.getId());
            zb.put("name", wp.getName());
            zb.put("rfdl", MathUtil.twoBit(rfdl / 10000));
            zb.put("yfdl", MathUtil.twoBit(yfdl / 10000));
            zb.put("nfdl", MathUtil.twoBit(nfdl / 10000));
            zb.put("rjhdl", rjhdl);
            zb.put("yjhdl", yjhdl);
            zb.put("njhdl", njhdl);

            resultArray[i++] = zb;
        }

        return resultArray;
    }

    public Map<Integer, Object> forecast(String type, String wpId) throws Exception {

        List<Map> list = new ArrayList<>();
        Map<Integer, Object> zb = new HashMap<>();
        Map<Integer, Object> zb2 = new HashMap<>();
        Map<Integer, Object> zb3 = new HashMap<>();
        // 基准日期
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);

        // 日期的月份和年份
        int baseMonth = calendar.get(Calendar.MONTH);
        int baseYear = calendar.get(Calendar.YEAR);

        // 同期
        calendar.set(Calendar.YEAR, baseYear - 1);
        calendar.set(Calendar.MONTH, baseMonth);
        Date sameMonthLastYear = calendar.getTime();

        // 上一期
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.YEAR, baseYear);
        Date previousMonth = calendar.getTime();

        List<ProEconStationPower> qwt = null;
        List<ProEconStationPower> qwt2 = null;
        List<ProEconStationPower> qwt3 = null;
        for (int i = 0; i < 3; i++) {
            //            zb = new HashMap<>();
            if (type.equals("0")) {
                QueryWrapper<ProEconStationPower> qw = new QueryWrapper<>();
                qw.lambda().eq(ProEconStationPower::getRecordDate, nowDate);
                qwt = powerService.list(qw);
                zb.put(i, qwt);
            } else {
                QueryWrapper<ProEconStationPower> qw = new QueryWrapper<>();
                QueryWrapper<ProEconStationPower> qw2 = new QueryWrapper<>();
                QueryWrapper<ProEconStationPower> qw3 = new QueryWrapper<>();
                qw.lambda().eq(ProEconStationPower::getRecordDate, nowDate);
                qw2.lambda().eq(ProEconStationPower::getRecordDate, previousMonth);
                qw3.lambda().eq(ProEconStationPower::getRecordDate, sameMonthLastYear);
                qw.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
                qw2.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
                qw3.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
                qwt = powerService.list(qw);
                qwt2 = powerService.list(qw2);
                qwt3 = powerService.list(qw3);
            }
            //            list.add(zb);
            nowDate = DateUtils.addDays(nowDate, +1);
        }
        return zb;
    }


    public List<YcdlVo> czforecast(String wpId) throws Exception {

        List<YcdlVo> list = new ArrayList<>();
        Map<Integer, Object> zb = new HashMap<>();

        List<ProBasicPowerstation> wpls = CacheContext.wpls.stream().filter(w -> wpId.equals(w.getId())).collect(Collectors.toList());
        // 基准日期
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);

        // 日期的月份和年份
        int baseMonth = calendar.get(Calendar.MONTH);
        int baseYear = calendar.get(Calendar.YEAR);

        // 同期
        calendar.set(Calendar.YEAR, baseYear - 1);
        calendar.set(Calendar.MONTH, baseMonth);
        Date sameMonthLastYear = calendar.getTime();

        // 上一期
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.YEAR, baseYear);
        Date previousMonth = calendar.getTime();

        List<ProEconStationPower> qwt = null;
        List<ProEconStationPower> qwt2 = null;
        List<ProEconStationPower> qwt3 = null;
        List<ProEconPowerstationInfoDay5> qwt4 = null;
        List<ProEconPowerstationInfoDay5> qwt5 = null;
        List<ProEconPowerstationInfoDay5> qwt6 = null;
        for (int i = 0; i < 3; i++) {
            //            list = new ArrayList<>();

            QueryWrapper<ProEconStationPower> qw = new QueryWrapper<>();
            QueryWrapper<ProEconStationPower> qw2 = new QueryWrapper<>();
            QueryWrapper<ProEconStationPower> qw3 = new QueryWrapper<>();
            qw.lambda().eq(ProEconStationPower::getRecordDate, nowDate);
            qw2.lambda().eq(ProEconStationPower::getRecordDate, previousMonth);
            qw3.lambda().eq(ProEconStationPower::getRecordDate, sameMonthLastYear);
            qw.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
            qw2.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
            qw3.lambda().eq(ProEconStationPower::getWindpowerstationId, wpId);
            qwt = powerService.list(qw);
            qwt2 = powerService.list(qw2);
            qwt3 = powerService.list(qw3);


            //            QueryWrapper<ProEconPowerstationInfoDay5> qw4 = new QueryWrapper<>();
            //            QueryWrapper<ProEconPowerstationInfoDay5> qw5 = new QueryWrapper<>();
            //            QueryWrapper<ProEconPowerstationInfoDay5> qw6 = new QueryWrapper<>();
            //            qw4.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, nowDate);
            //            qw5.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, previousMonth);
            //            qw6.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, sameMonthLastYear);
            //            qw4.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId,wpId);
            //            qw5.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId,wpId);
            //            qw6.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId,wpId);
            //            qwt4 = proEconPowerstationInfoDay5Service.list(qw4);
            //            qwt5 = proEconPowerstationInfoDay5Service.list(qw5);
            //            qwt6 = proEconPowerstationInfoDay5Service.list(qw6);

            //日发电量
            ProBasicPowerstationPoint rfdlvalue = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.RFDL);
            PointData bq = edosUtil.getSectionData(rfdlvalue, nowDate.getTime());
            PointData sq = edosUtil.getSectionData(rfdlvalue, previousMonth.getTime());
            PointData tq = edosUtil.getSectionData(rfdlvalue, sameMonthLastYear.getTime());

            YcdlVo vo = new YcdlVo();

            vo.setName(wpls.get(0).getName());
            vo.setWpid(wpls.get(0).getNemCode());
            if (qwt.size() > 0) {
                vo.setDate(qwt.get(0).getRecordDate());
            } else {
                vo.setDate(LocalDate.ofEpochDay(2023 - 12 - 12));
            }
            vo.setRfdl(bq.getPointValueInDouble());
            if (qwt.size() > 0) {
                vo.setYcdl(qwt.get(0).getDayPowerForecast().doubleValue());
            } else {
                vo.setYcdl(0.0);
            }

            vo.setSqrfdl(sq.getPointValueInDouble());
            if (qwt2.size() > 0) {
                vo.setSqycdl(qwt2.get(0).getDayPowerForecast().doubleValue());
            } else {
                vo.setSqycdl(0.0);
            }

            vo.setTqrfdl(tq.getPointValueInDouble());
            if (qwt3.size() > 0) {
                vo.setTqycdl(qwt3.get(0).getDayPowerForecast().doubleValue());
            } else {
                vo.setTqycdl(0.0);
            }

            list.add(vo);


            nowDate = DateUtils.addDays(nowDate, +1);
            previousMonth = DateUtils.addDays(previousMonth, +1);
            sameMonthLastYear = DateUtils.addDays(sameMonthLastYear, +1);

        }


        return list;
    }


    //公司故障
    public List<Map<String, Object>> shutdown(String type) throws Exception {

        List<Map<String, Object>> gz = new ArrayList<>();

        if (type.equals("day")) {
            List<Map<String, Object>> typeCounts = proEconAlarmTypeMapper.getAlarmTypeCounts();
            gz = typeCounts;
        } else if (type.equals("month")) {
            List<Map<String, Object>> alarmTypeCountsmonth = proEconAlarmTypeMapper.getAlarmTypeCountsmonth();
            gz = alarmTypeCountsmonth;
        } else if (type.equals("year")) {
            List<Map<String, Object>> alarmTypeCountsyear = proEconAlarmTypeMapper.getAlarmTypeCountsyear();
            gz = alarmTypeCountsyear;
        }

        return gz;


    }

    //场站故障

    public List<Map<String, Object>> shutdowncz(String type, String wpId) throws Exception {

        List<Map<String, Object>> gz = new ArrayList<>();

        if (type.equals("day")) {
            List<Map<String, Object>> typeCounts = proEconAlarmTypeMapper.getAlarmTypeCountscz(wpId);
            gz = typeCounts;
        } else if (type.equals("month")) {
            List<Map<String, Object>> alarmTypeCountsmonth = proEconAlarmTypeMapper.getAlarmTypeCountsmonthcz(wpId);
            gz = alarmTypeCountsmonth;
        } else if (type.equals("year")) {
            List<Map<String, Object>> alarmTypeCountsyear = proEconAlarmTypeMapper.getAlarmTypeCountsyearcz(wpId);
            gz = alarmTypeCountsyear;
        }

        return gz;
    }


    //公司机型
    public List<Map<String, Object>> shutdownjx(String type) throws Exception {

        List<Map<String, Object>> gz = new ArrayList<>();

        if (type.equals("day")) {
            List<Map<String, Object>> typeCounts = proEconAlarmTypeMapper.getResults();
            gz = typeCounts;
        } else if (type.equals("month")) {
            List<Map<String, Object>> alarmTypeCountsmonth = proEconAlarmTypeMapper.getResultsmonth();
            gz = alarmTypeCountsmonth;
        } else if (type.equals("year")) {
            List<Map<String, Object>> alarmTypeCountsyear = proEconAlarmTypeMapper.getResultsyear();
            gz = alarmTypeCountsyear;
        }

        return gz;


    }


    //场站机型
    public List<Map<String, Object>> shutdownczjx(String type, String wpId) throws Exception {

        List<Map<String, Object>> gz = new ArrayList<>();
        if (type.equals("day")) {
            List<Map<String, Object>> typeCounts = proEconAlarmTypeMapper.getResultscz(wpId);
            gz = typeCounts;
        } else if (type.equals("month")) {
            List<Map<String, Object>> alarmTypeCountsmonth = proEconAlarmTypeMapper.getResultsmonthcz(wpId);
            gz = alarmTypeCountsmonth;
        } else if (type.equals("year")) {
            List<Map<String, Object>> alarmTypeCountsyear = proEconAlarmTypeMapper.getResultsyearcz(wpId);
            gz = alarmTypeCountsyear;
        }


        return gz;
    }

}
