package com.gyee.runeconomy.service.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.dto.PowercurveVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerPoint;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.model.auto.ProBasicProject;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.MathUtil;
import com.gyee.runeconomy.util.StringUtils;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

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

@Service
public class HourpowerService {

    @Resource
    private IProBasicProjectPlanService proBasicProjectPlanService;
    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;
    @Resource
    private IProBasicEquipmentPointService proBasicEquipmentPointService;
    @Resource
    private IProBasicCompanyService proBasicCompanyService;
    @Resource
    private IProEconTargetdataService proEconTargetdataService;
    @Resource
    private IProEconTargetdataFristService proEconTargetdataFristService;

    @Resource
    private IProBasicPowerPointService powerPointService;

    @Resource
    private IProEconPowerstationInfoDay5Service proEconPowerstationInfoDay5Service;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";

    @Resource
    private IEdosUtil edosUtil;
    Map<String, Object> map = new HashMap<>();
    @Resource
    private TokenService tokenService;

    //24小时功率曲线
    public List<PowercurveVo> Powercurve24(String wpId) throws Exception {

        Date beginDate = DateUtils.geteleven();   //当日零点前一刻
        Date endDate = DateUtils.getCurrentDate();   //当前时间


        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 设置时间为当前时间
        calendar.add(Calendar.DAY_OF_YEAR, 1); // 将日期加一天

        // 设置时、分、秒、毫秒为0，即获取当天后一天的0点时间值
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date nextDayZeroTime = calendar.getTime();

        int day = DateUtils.getCurrentHour();//4

        List<PowercurveVo> vos = new ArrayList<PowercurveVo>();
        List<PowercurveVo> vos1 = new ArrayList<PowercurveVo>();
        for (int i = 0; i < 24; i++) {
            PowercurveVo vo = new PowercurveVo();
            //            vo.setLlgl(0.0); //理论功率
            //            vo.setSjgl(0.0); //实际功率
            //            vo.setSsfs(0.0); //实时风速
            vo.setBzgl(0.0); //保证功率
            vo.setHours(i);  //时间戳
            vo.setName(wpId);
            vos.add(vo);
        }
        for (int i = 0; i < 24; i++) {
            PowercurveVo vo1 = new PowercurveVo();
            //            vo.setLlgl(0.0); //理论功率
            //            vo.setSjgl(0.0); //实际功率
            //            vo.setSsfs(0.0); //实时风速
            vo1.setBzgl(0.0); //保证功率
            vo1.setHours(i);  //时间戳
            vo1.setName(wpId);
            vos1.add(vo1);
        }

        List<ProBasicProject> pjlist = new ArrayList<>();

        for (ProBasicProject pj : CacheContext.pjls) {
            if (wpId.equals(pj.getWindpowerstationId())) {
                pjlist.add(pj);
            }
        }


        ProBasicPowerstationPoint llgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZLLGL);
        ProBasicPowerstationPoint sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);

        QueryWrapper<ProBasicPowerPoint> qw = new QueryWrapper<>();
        List<ProBasicPowerPoint> powerls = new ArrayList<>();
        List<ProBasicPowerPoint> qyls = null;
        powerls = powerPointService.getBaseMapper().selectList(qw);

        List<ProBasicPowerPoint> sxfd = powerls.stream().filter(i -> i.getTypeId().endsWith(FD)).collect(Collectors.toList());
        List<ProBasicPowerPoint> sxgf = powerls.stream().filter(i -> i.getTypeId().endsWith(GF)).collect(Collectors.toList());

        if (wpId.endsWith(QS)) {
            qyls = powerls;
        } else if (wpId.endsWith(FD)) {
            qyls = sxfd;
        } else if (wpId.endsWith(GF)) {
            qyls = sxgf;
        }
        if (wpId.endsWith(QS) || wpId.endsWith(FD) || wpId.endsWith(GF)) {

            List<List<PowercurveVo>> ls = new ArrayList<>();
            for (ProBasicPowerPoint pp : qyls) {
                if (pp.getWindpowerstationId().contains("MLJ_FDC")) continue;
                ProBasicPowerstationPoint ycgl = proBasicPowerstationPointService.getPowerstationPoint(pp.getWindpowerstationId(), ContantXk.GLYC);
                List<PointData> ycglls = edosUtil.getHistoryDatasSnap(ycgl, beginDate.getTime() / 1000, nextDayZeroTime.getTime() / 1000, null, 3600L);
                vos = new ArrayList<>();
                double temp1 = 0;
                for (int i = 0; i < ycglls.size(); i++) {
                    temp1 = ycglls.get(i).getPointValueInDouble();
                    PowercurveVo vo = new PowercurveVo();
                    vo.setHours(i);
                    vo.setBzgl(StringUtils.round(temp1, 2));
                    vos.add(vo);
                }
                ls.add(vos);
            }

            List<PowercurveVo> voList = ls.get(0);
            for (int i = 1; i < ls.size(); i++) {
                for (int j = 0; j < voList.size(); j++) {
                    if (i < ls.size() && j < ls.get(i).size()) {
                        voList.get(j).setBzgl(voList.get(j).getBzgl() + ls.get(i).get(j).getBzgl());
                    }
                }
            }


            double temp5 = 0;
            for (int i = 0; i < voList.size(); i++) {
                temp5 = voList.get(i).getBzgl();
                if (i < vos.size()) {
                    vos.get(i).setBzgl(StringUtils.round(temp5, 2));
                }
            }

            List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 3600L);
            double temp2 = 0;
            for (int i = 0; i < llglls.size(); i++) {
                temp2 = llglls.get(i).getPointValueInDouble();
                if (i < vos.size()) {
                    vos.get(i).setLlgl(StringUtils.round(temp2 / 1000, 2));
                }
            }


            List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 3600L);
            double temp4 = 0;
            for (int i = 0; i < sjglls.size(); i++) {
                temp4 = sjglls.get(i).getPointValueInDouble();
                if (i < vos.size()) {
                    vos.get(i).setSjgl(StringUtils.round(temp4 / 1000, 2));
                }

            }

        } else {
            //单场请求特殊场站进行处理
            if (wpId.contains("GJY") || wpId.contains("PTZ") || wpId.contains("NJL") || wpId.contains("TZ_GDC_STA") || wpId.contains("SY_GDC_STA")) {
                Map<String, Object> map1 = new HashMap<>();

                for (ProBasicProject pj : pjlist) {
                    if (pj.getId().contains("GJYF02") || pj.getId().contains("TZG02") || pj.getId().contains("TZG03") || pj.getId().contains("TZG04"))
                        continue;
                    ProBasicPowerstationPoint bzgl = proBasicPowerstationPointService.getPowerstationPoint(pj.getId(), ContantXk.GLYC);
                    List<PointData> bzglls = edosUtil.getHistoryDatasSnap(bzgl, beginDate.getTime() / 1000, nextDayZeroTime.getTime() / 1000, null, 3600L);
                    if (bzglls != null && !bzglls.isEmpty() && bzglls.get(0).getPointName() != null) {
                        double temp1 = 0;
                        for (int i = 0; i < bzglls.size(); i++) {

                            temp1 = bzglls.get(i).getPointValueInDouble();
                            if (!pj.getId().contains("01")) {
                                if (i < vos.size()) {
                                    vos.get(i).setBzgl(StringUtils.round(temp1, 2));
                                }
                            } else {
                                if (i < vos.size()) {
                                    vos1.get(i).setBzgl(StringUtils.round(temp1, 2));
                                }
                            }
                        }

                    }
                    if (map1.containsKey(wpId)) {
                        List<PowercurveVo> previousVos1 = (List<PowercurveVo>) map1.get(wpId);
                        for (int i = 0; i < vos.size(); i++) {
                            PowercurveVo previousVo1 = previousVos1.get(i);
                            PowercurveVo currentVo1 = vos.get(i);
                            if (previousVo1 != null && currentVo1 != null) {
                                if (previousVo1.getBzgl() != null && currentVo1.getBzgl() != null) {
                                    currentVo1.setBzgl(MathUtil.twoBit((previousVo1.getBzgl() + currentVo1.getBzgl())));
                                }
                            }
                        }
                    }
                    map1.put(wpId, vos1);
                }

            } else {
                //正常场站请求
                ProBasicPowerstationPoint bzgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.GLYC);

                List<PointData> bzglls = edosUtil.getHistoryDatasSnap(bzgl, beginDate.getTime() / 1000, nextDayZeroTime.getTime() / 1000, null, 3600L);
                double temp1 = 0;
                for (int i = 0; i < bzglls.size(); i++) {
                    temp1 = bzglls.get(i).getPointValueInDouble();
                    if (i < vos.size()) {
                        vos.get(i).setBzgl(StringUtils.round(temp1, 2));
                    }
                }
            }

            List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 3600L);
            double temp2 = 0;
            for (int i = 0; i < llglls.size(); i++) {
                temp2 = llglls.get(i).getPointValueInDouble();
                if (i < vos.size()) {
                    vos.get(i).setLlgl(StringUtils.round(temp2 / 1000, 2));
                }
            }


            List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 3600L);
            double temp4 = 0;
            for (int i = 0; i < sjglls.size(); i++) {
                temp4 = sjglls.get(i).getPointValueInDouble();
                if (i < vos.size()) {
                    vos.get(i).setSjgl(StringUtils.round(temp4 / 1000, 2));
                }

            }
        }
        return vos;
    }

}
