package com.gyee.runeconomy.service.Forecast;

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.ForecastVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerPoint;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
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.IProBasicPowerPointService;
import com.gyee.runeconomy.service.auto.IProBasicPowerstationPointService;
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 NewForecastService {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IProBasicPowerPointService powerPointService;
    @Resource
    private TokenService tokenService;
    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;

    public Map<String, Object> getcockpit(String wpId, String beginDate, String endDate) throws Exception {

        Date starttime = DateUtils.parseDate(beginDate);
        Date endtime = DateUtils.parseDate(endDate);
        Date currentDate = new Date();
        List<ProBasicPowerstation> station = CacheContext.wpls.stream().filter(w -> wpId.equals(w.getId())).collect(Collectors.toList());

        List<ProBasicProject> pjls = CacheContext.pjls;

        QueryWrapper<ProBasicPowerPoint> qw = new QueryWrapper<>();
        List<ProBasicPowerPoint> powerls = new ArrayList<>();

        qw.lambda().eq(ProBasicPowerPoint::getWindpowerstationId, wpId);
        powerls = powerPointService.getBaseMapper().selectList(qw);

        if (powerls.size() < 0 || powerls.isEmpty()) {
            powerls = powerPointService.getBaseMapper().selectList(null);
            List<ProBasicProject> collect = pjls.stream().filter(p -> wpId.equals(p.getWindpowerstationId())).collect(Collectors.toList());
            List<ProBasicPowerPoint> filteredPowerls = powerls.stream().filter(p -> collect.stream().anyMatch(c -> c.getId().equals(p.getWindpowerstationId()))).
                    collect(Collectors.toList());
            powerls = filteredPowerls;
        }


        List<ForecastVo> vos = null;
        Map<String, Object> map = new LinkedHashMap<>();
        ProBasicPowerstationPoint pjfs = null;
        ProBasicPowerstationPoint fsyc = null;
        ProBasicPowerstationPoint ycgl = null;
        ProBasicPowerstationPoint sjgl = null;
        ProBasicPowerstationPoint llgl = null;

        List<ProBasicProject> projects = null;
        for (ProBasicPowerPoint wp : powerls) {

            projects = pjls.stream().filter(j -> wp.getWindpowerstationId().equals(j.getId())).collect(Collectors.toList());

            vos = new ArrayList<>();
            if (wpId.endsWith("FDC_STA")) {
                pjfs = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSPJFS);
                fsyc = proBasicPowerstationPointService.getPowerstationPoint(wp.getWindpowerstationId(), ContantXk.FSYC);
                ycgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getWindpowerstationId(), ContantXk.GLYC);
                sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
                llgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZLLGL);

            }
            if (wpId.endsWith("GDC_STA")) {
                pjfs = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.GCGZQD);
                fsyc = proBasicPowerstationPointService.getPowerstationPoint(wp.getWindpowerstationId(), ContantXk.GZYC);
                ycgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getWindpowerstationId(), ContantXk.GLYC);
                sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
                llgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZLLGL);
            }


            List<PointData> pjfsls = edosUtil.getHistoryDatasSnap(pjfs, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
            List<PointData> fsycls = edosUtil.getHistoryDatasSnap(fsyc, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
            List<PointData> ycglls = edosUtil.getHistoryDatasSnap(ycgl, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
            List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
            List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);

            double temp1 = 0;
            double temp2 = 0;
            double temp3 = 0;
            double temp4 = 0;
            double temp5 = 0;
            for (int i = 0; i < fsycls.size(); i++) {

                ForecastVo vo = new ForecastVo();
                //                    vo.setPjfs(0.0); //平均风速
                vo.setYcfs(0.0); //预测风速
                //                    vo.setSjgl(0.0); //实际功率
                vo.setYcgl(0.0); //功率预测
                //                    vo.setLlgl(0.0); //理论功率
                vo.setHours(2023L);  //时间戳
                vo.setName(wpId);

                if (pjfsls != null && !pjfsls.isEmpty() && i < pjfsls.size() && pjfsls.get(i).getPointName() != null) {
                    temp1 = pjfsls.get(i).getPointValueInDouble();
                    vo.setPjfs(StringUtils.round(temp1, 2));
                }

                if (fsycls != null && !fsycls.isEmpty() && fsycls.get(0).getPointName() != null) {
                    temp2 = fsycls.get(i).getPointValueInDouble();
                    vo.setYcfs(StringUtils.round(temp2, 2));
                }
                if (ycglls != null && !ycglls.isEmpty() && ycglls.get(0).getPointName() != null) {
                    temp3 = ycglls.get(i).getPointValueInDouble();
                    vo.setYcgl(StringUtils.round(temp3, 2));
                }

                if (sjglls != null && !sjglls.isEmpty() && i < sjglls.size() && sjglls.get(i).getPointName() != null) {
                    temp4 = sjglls.get(i).getPointValueInDouble();
                    vo.setSjgl(StringUtils.round(temp4 / 1000, 2));
                }
                if (llglls != null && !llglls.isEmpty() && i < llglls.size() && llglls.get(i).getPointName() != null) {
                    temp5 = llglls.get(i).getPointValueInDouble();
                    vo.setLlgl(StringUtils.round(temp5 / 1000, 2));
                }
                vo.setHours(fsycls.get(i).getPointTime());

                if (!projects.isEmpty() || projects.size() > 0) {
                    vo.setName(projects.get(0).getName());
                } else {
                    vo.setName(station.get(0).getName());
                }
                vo.setId(wpId);

                vos.add(vo);
            }
            map.put(wp.getWindpowerstationId(), vos);

        }
        return map;
    }


    public Map<String, Object> getcockpittype(String beginDate, String endDate, String type) throws Exception {

        Date starttime = DateUtils.parseDate(beginDate);
        Date endtime = DateUtils.parseDate(endDate);
        Date currentDate = new Date();

        List<ProBasicProject> pjls = CacheContext.pjls;

        List<ProBasicPowerstation> wpls = CacheContext.wpls;

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

        List<ForecastVo> vos = null;
        Map<String, Object> map = new LinkedHashMap<>();
        ProBasicPowerstationPoint pjfs = null;
        ProBasicPowerstationPoint fsyc = null;
        ProBasicPowerstationPoint ycgl = null;
        ProBasicPowerstationPoint sjgl = null;
        ProBasicPowerstationPoint llgl = null;

        List<ProBasicProject> projects = null;
        for (ProBasicPowerstation wp : wpls) {
            powerls.stream().filter(p -> wp.getId().equals(p.getWindpowerstationId()));
            if (!powerls.isEmpty() || powerls.size() > 0) {
                vos = new ArrayList<>();
                if (wp.getId().endsWith("FDC_STA")) {
                    pjfs = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSPJFS);
                    fsyc = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.FSYC);
                    ycgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.GLYC);
                    sjgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZGL);
                    llgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZLLGL);

                }
                if (wp.getId().endsWith("GDC_STA")) {
                    pjfs = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.GCGZQD);
                    fsyc = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.GZYC);
                    ycgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.GLYC);
                    sjgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZGL);
                    llgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZLLGL);
                }


                List<PointData> pjfsls = edosUtil.getHistoryDatasSnap(pjfs, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
                List<PointData> fsycls = edosUtil.getHistoryDatasSnap(fsyc, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
                List<PointData> ycglls = edosUtil.getHistoryDatasSnap(ycgl, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
                List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
                List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);

                double temp1 = 0;
                double temp2 = 0;
                double temp3 = 0;
                double temp4 = 0;
                double temp5 = 0;
                for (int i = 0; i < fsycls.size(); i++) {

                    ForecastVo vo = new ForecastVo();
                    //                    vo.setPjfs(0.0); //平均风速
                    vo.setYcfs(0.0); //预测风速
                    //                    vo.setSjgl(0.0); //实际功率
                    vo.setYcgl(0.0); //功率预测
                    //                    vo.setLlgl(0.0); //理论功率
                    vo.setHours(2023L);  //时间戳
                    vo.setName(wp.getId());

                    if (pjfsls != null && !pjfsls.isEmpty() && i < pjfsls.size() && pjfsls.get(i).getPointName() != null) {
                        temp1 = pjfsls.get(i).getPointValueInDouble();
                        vo.setPjfs(StringUtils.round(temp1, 2));
                    }

                    if (fsycls != null && !fsycls.isEmpty() && fsycls.get(0).getPointName() != null) {
                        temp2 = fsycls.get(i).getPointValueInDouble();
                        vo.setYcfs(StringUtils.round(temp2, 2));
                    }
                    if (ycglls != null && !ycglls.isEmpty() && ycglls.get(0).getPointName() != null) {
                        temp3 = ycglls.get(i).getPointValueInDouble();
                        vo.setYcgl(StringUtils.round(temp3, 2));
                    }

                    if (sjglls != null && !sjglls.isEmpty() && i < sjglls.size() && sjglls.get(i).getPointName() != null) {
                        temp4 = sjglls.get(i).getPointValueInDouble();
                        vo.setSjgl(StringUtils.round(temp4 / 1000, 2));
                    }
                    if (llglls != null && !llglls.isEmpty() && i < llglls.size() && llglls.get(i).getPointName() != null) {
                        temp5 = llglls.get(i).getPointValueInDouble();
                        vo.setLlgl(StringUtils.round(temp5 / 1000, 2));
                    }
                    vo.setHours(fsycls.get(i).getPointTime());

                    if (!projects.isEmpty() || projects.size() > 0) {
                        vo.setName(projects.get(0).getName());
                    }
                    vo.setId(wp.getId());

                    vos.add(vo);
                }
                map.put(wp.getId(), vos);

            } else {
                for (ProBasicProject pj : pjls) {
                    powerls.stream().filter(p -> pj.getId().equals(p.getWindpowerstationId()));

                    vos = new ArrayList<>();
                    if (wp.getId().endsWith("FDC_STA")) {
                        pjfs = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSPJFS);
                        fsyc = proBasicPowerstationPointService.getPowerstationPoint(pj.getId(), ContantXk.FSYC);
                        ycgl = proBasicPowerstationPointService.getPowerstationPoint(pj.getId(), ContantXk.GLYC);
                        sjgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZGL);
                        llgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZLLGL);

                    }
                    if (wp.getId().endsWith("GDC_STA")) {
                        pjfs = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.GCGZQD);
                        fsyc = proBasicPowerstationPointService.getPowerstationPoint(pj.getId(), ContantXk.GZYC);
                        ycgl = proBasicPowerstationPointService.getPowerstationPoint(pj.getId(), ContantXk.GLYC);
                        sjgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZGL);
                        llgl = proBasicPowerstationPointService.getPowerstationPoint(wp.getId(), ContantXk.SSZLLGL);
                    }


                    List<PointData> pjfsls = edosUtil.getHistoryDatasSnap(pjfs, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
                    List<PointData> fsycls = edosUtil.getHistoryDatasSnap(fsyc, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
                    List<PointData> ycglls = edosUtil.getHistoryDatasSnap(ycgl, starttime.getTime() / 1000, endtime.getTime() / 1000, null, 900L);
                    List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);
                    List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, starttime.getTime() / 1000, currentDate.getTime() / 1000, null, 900L);

                    double temp1 = 0;
                    double temp2 = 0;
                    double temp3 = 0;
                    double temp4 = 0;
                    double temp5 = 0;
                    for (int i = 0; i < fsycls.size(); i++) {

                        ForecastVo vo = new ForecastVo();
                        //                    vo.setPjfs(0.0); //平均风速
                        vo.setYcfs(0.0); //预测风速
                        //                    vo.setSjgl(0.0); //实际功率
                        vo.setYcgl(0.0); //功率预测
                        //                    vo.setLlgl(0.0); //理论功率
                        vo.setHours(2023L);  //时间戳
                        vo.setName(wp.getId());

                        if (pjfsls != null && !pjfsls.isEmpty() && i < pjfsls.size() && pjfsls.get(i).getPointName() != null) {
                            temp1 = pjfsls.get(i).getPointValueInDouble();
                            vo.setPjfs(StringUtils.round(temp1, 2));
                        }

                        if (fsycls != null && !fsycls.isEmpty() && fsycls.get(0).getPointName() != null) {
                            temp2 = fsycls.get(i).getPointValueInDouble();
                            vo.setYcfs(StringUtils.round(temp2, 2));
                        }
                        if (ycglls != null && !ycglls.isEmpty() && ycglls.get(0).getPointName() != null) {
                            temp3 = ycglls.get(i).getPointValueInDouble();
                            vo.setYcgl(StringUtils.round(temp3, 2));
                        }

                        if (sjglls != null && !sjglls.isEmpty() && i < sjglls.size() && sjglls.get(i).getPointName() != null) {
                            temp4 = sjglls.get(i).getPointValueInDouble();
                            vo.setSjgl(StringUtils.round(temp4 / 1000, 2));
                        }
                        if (llglls != null && !llglls.isEmpty() && i < llglls.size() && llglls.get(i).getPointName() != null) {
                            temp5 = llglls.get(i).getPointValueInDouble();
                            vo.setLlgl(StringUtils.round(temp5 / 1000, 2));
                        }
                        vo.setHours(fsycls.get(i).getPointTime());

                        if (!projects.isEmpty() || projects.size() > 0) {
                            vo.setName(projects.get(0).getName());
                        }
                        vo.setId(wp.getId());

                        vos.add(vo);
                    }
                    map.put(wp.getId(), vos);
                }
            }
        }
        return map;
    }
}
