package com.gyee.runeconomy.service.monitor;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.dto.DataVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProBasicPowerstationPoint;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.StringUtils;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
public class GroupRealtimeService {

    private static Logger logger = LoggerFactory.getLogger(MonitorService.class);
    @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;


    //首页实时数据
    public List<DataVo> GroupRealtimevalue(String wpId, String uniformCode) throws Exception {

        Date beginDate = com.gyee.common.util.DateUtils.getSamedayZero();   //当日零点
        Date endDate = com.gyee.common.util.DateUtils.getCurrentDate();   //当前时间
        List<DataVo> vos = new ArrayList<DataVo>();

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

        //        for (int i = 0; i < day; i++) {
        //            DataVo vo = new DataVo();
        //            vo.setValue1(0.0); //实际功率
        //            vo.setValue2(0.0); //风电实际功率
        //            vo.setValue3(0.0); //光伏实际功率
        //            vo.setValue4(0.0); //实时风速
        //            vo.setValue5(0.0); //理论功率
        //            vo.setValue6(0.0); //平均光照度
        //            vo.setValue7(0.0); //风能利用率
        //            vo.setHours(i);  //时间戳
        //            vo.setName(wpId);
        //            vos.add(vo);
        //        }


        //        if (uniformCode.contains("SSZGL")) {
        //            List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
        //            double temp1 = 0;
        //            for (int i = 0; i < sjglls.size(); i++) {
        //
        //                DataVo vo = new DataVo();
        //                temp1 = sjglls.get(i).getPointValueInDouble();
        //                vo.setValue1(StringUtils.round(temp1 / 1000, 2));
        //                vo.setTime(sjglls.get(i).getPointTime());
        //                vos.add(vo);
        //            }
        //        }
        if (wpId.contains(QS)) {
            ProBasicPowerstationPoint sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
            ProBasicPowerstationPoint fdsjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId.substring(0, wpId.indexOf("0")) + "-1", ContantXk.SSZGL);
            ProBasicPowerstationPoint gfsjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId.substring(0, wpId.indexOf("0")) + "-2", ContantXk.SSZGL);

            if (uniformCode.contains("SSZGL")) {
                List<PointData> fdsjglls = edosUtil.getHistoryDatasSnap(fdsjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
                List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
                List<PointData> gfsjglls = edosUtil.getHistoryDatasSnap(gfsjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);

                double temp1 = 0;
                double temp2 = 0;
                double temp3 = 0;
                for (int i = 0; i < fdsjglls.size(); i++) {
                    DataVo vo = new DataVo();
                    vo.setTime(sjglls.get(i).getPointTime());
                    temp1 = sjglls.get(i).getPointValueInDouble();
                    temp2 = fdsjglls.get(i).getPointValueInDouble();
                    temp3 = gfsjglls.get(i).getPointValueInDouble();
                    vo.setValue1(StringUtils.round(temp1 / 1000, 2));
                    vo.setValue2(StringUtils.round(temp2 / 1000, 2));
                    vo.setValue3(StringUtils.round(temp3 / 1000, 2));
                    vos.add(vo);
                }
            }
        } else if (uniformCode.contains("SSZGL")) {
            ProBasicPowerstationPoint sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
            List<PointData> sjglls = edosUtil.getHistoryDatasSnap(sjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
            double temp1 = 0;

            for (int i = 0; i < sjglls.size(); i++) {
                DataVo vo = new DataVo();
                vo.setTime(sjglls.get(i).getPointTime());
                temp1 = sjglls.get(i).getPointValueInDouble();
                vo.setValue1(StringUtils.round(temp1 / 1000, 2));
                vos.add(vo);
            }
        }
        //        if (wpId.contains(QS)) {
        //            ProBasicPowerstationPoint gfsjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId.substring(0, wpId.indexOf("0")) + "-2", ContantXk.SSZGL);
        //            if (uniformCode.contains("SSZGL")) {
        //                List<PointData> gfsjglls = edosUtil.getHistoryDatasSnap(gfsjgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
        //                double temp3 = 0;
        //                for (int i = 0; i < gfsjglls.size(); i++) {
        //                    DataVo vo = new DataVo();
        //                    temp3 = gfsjglls.get(i).getPointValueInDouble();
        //                    vo.setValue3(StringUtils.round(temp3 / 1000, 2));
        //                    vos.add(vo);
        //                }
        //            }
        //        }

        ProBasicPowerstationPoint ssfs = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSPJFS);
        ProBasicPowerstationPoint llgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.TPOINT_WP_YCGL); //预测功率
        //        ProBasicPowerstationPoint sjgl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.SSZGL);
        ProBasicPowerstationPoint gzzs = null;
        if (wpId.contains(QS) || wpId.contains(FD) || wpId.contains(GF)) {
            List<ProBasicPowerstation> wplsG = CacheContext.wplsG.stream().filter(c -> c.getOrderNum().equals(15)).collect(Collectors.toList());
            gzzs = proBasicPowerstationPointService.getPowerstationPoint(wplsG.get(0).getId(), ContantXk.GCGZQD);
        } else {
            gzzs = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.GCGZQD);
        }
        ProBasicPowerstationPoint fnlyl = proBasicPowerstationPointService.getPowerstationPoint(wpId, ContantXk.FNLYL);

        if (uniformCode.contains("SSPJFS")) {

            List<PointData> ssfsls = edosUtil.getHistoryDatasSnap(ssfs, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
            double temp4 = 0;
            for (int i = 0; i < ssfsls.size(); i++) {
                DataVo vo = new DataVo();
                temp4 = ssfsls.get(i).getPointValueInDouble();
                vo.setValue4(StringUtils.round(temp4, 2));
                vo.setTime(ssfsls.get(i).getPointTime());
                vos.add(vo);
            }
        }

        if (uniformCode.contains("SSZLLGL")) {
            List<PointData> llglls = edosUtil.getHistoryDatasSnap(llgl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
            double temp5 = 0;
            for (int i = 0; i < llglls.size(); i++) {
                DataVo vo = new DataVo();
                temp5 = llglls.get(i).getPointValueInDouble();
                vo.setValue5(StringUtils.round(temp5 / 1000, 2));
                vo.setTime(llglls.get(i).getPointTime());
                vos.add(vo);
            }
        }


        if (uniformCode.contains("RPJGZD")) {
            List<PointData> gzzsls = edosUtil.getHistoryDatasSnap(gzzs, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
            double temp6 = 0;
            for (int i = 0; i < gzzsls.size(); i++) {
                DataVo vo = new DataVo();
                temp6 = gzzsls.get(i).getPointValueInDouble();
                vo.setValue6(StringUtils.round(temp6, 2));
                vo.setTime(gzzsls.get(i).getPointTime());
                vos.add(vo);
            }
        }
        if (uniformCode.contains("FNLYL")) {
            List<PointData> fnlylls = edosUtil.getHistoryDatasSnap(fnlyl, beginDate.getTime() / 1000, endDate.getTime() / 1000, null, 900L);
            double temp7 = 0;
            for (int i = 0; i < fnlylls.size(); i++) {
                DataVo vo = new DataVo();
                temp7 = fnlylls.get(i).getPointValueInDouble();
                vo.setValue7(StringUtils.round(temp7, 2));
                vo.setTime(fnlylls.get(i).getPointTime());
                vos.add(vo);
            }
        }

        return vos;
    }
}
