package com.gyee.alarm.service.realtimelibrary;
/*
@author   谢生杰
@date   2022/11/29-9:56
秒刷新
*/

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.util.PointUtil;
import com.gyee.alarm.util.realtimesource.IEdosUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service
public class AccessStatusService {
    @Resource
    private IEdosUtil edosUtil;
    @Value("${second}")
    private String second;

    /**
     * 场站接入状态,总预测功率
     * @throws Exception
     */
    public void accessStatus() throws Exception {

        List<PointData> resultList = new ArrayList<>();
        List<ProBasicMeterPoint> meterpoints = CacheContext.meterpoints;
        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        List<ProBasicSubStation> subwpls = CacheContext.subwpls;
        List<ProBasicWeatherStation> wswpls = CacheContext.wswpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> subwppointmap = CacheContext.subwppointmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> wswppointmap = CacheContext.wswppointmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
        Date currentDate = DateUtils.getCurrentDate();

        wpls.stream().forEach(wp->{
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            ProBasicPowerstationPoint djlpoint = powerstationPointMap.get(ContantXk.DQJRZT);
            ProBasicPowerstationPoint agcpoint = powerstationPointMap.get(ContantXk.AGCJRZT);
            ProBasicPowerstationPoint glycpoint = powerstationPointMap.get(ContantXk.GLYCJRZT);
            ProBasicPowerstationPoint cftpoint = powerstationPointMap.get(ContantXk.QXZJRZT);
            ProBasicPowerstationPoint sbjrpoint = powerstationPointMap.get(ContantXk.SBJRZT);
            ProBasicPowerstationPoint czztpoint = powerstationPointMap.get(ContantXk.CZZT);

            ProBasicPowerstationPoint qcztPoint = powerstationPointMap.get(ContantXk.QCZT);

            ProBasicPowerstationPoint zycglpoint = powerstationPointMap.get(ContantXk.ZYCGL);
            double djlzt = 0;
            double fjzt = 0;
            double agczt = 0;
            double glyczt = 0;
            double cftzt = 0;
            double czzt = 0;

            double zycgl = 0;
            Optional<ProBasicMeterPoint> swdlOptional = meterpoints.stream().filter(me -> me.getWindpowerstationId().equals(wp.getId()) && me.getUniformCode().equals("ZXYG") && me.getMeterType().equals("出线") && me.getMeterSort().equals("主")).findFirst();
            Optional<ProBasicSubStation> subStationOptional = subwpls.stream().filter(sub -> sub.getWindpowerstationId().equals(wp.getId())).findFirst();
            Optional<ProBasicWeatherStation> wsOptional = wswpls.stream().filter(ws -> ws.getWindpowerstationId().equals(wp.getId())).findFirst();
            List<ProBasicSubStation> stations = subwpls.stream().filter(sub -> sub.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
            List<String> pointList = new ArrayList<>();
            stations.stream().forEach(station->{
                pointList.add(subwppointmap.get(station.getId()).get(ContantXk.TPOINT_WP_YCGL).getNemCode());
            });
            try {
                zycgl = edosUtil.getRealData(pointList).stream().mapToDouble(PointData::getPointValueInDouble).sum();
            } catch (Exception e) {
                e.printStackTrace();
            }


            try {
                double qczt = edosUtil.getSectionData(qcztPoint, currentDate.getTime()).getPointValueInDouble();
                if (qczt != 6){
                    fjzt = 1;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            if (swdlOptional.isPresent()){
                ProBasicMeterPoint proBasicMeterPoint = swdlOptional.get();
                try {
                    Long pointTime = edosUtil.getRealData(proBasicMeterPoint.getNemCode()).getPointTime();
                    if ((currentDate.getTime() - pointTime) / 1000 < Integer.parseInt(second)){
                        djlzt = 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (wsOptional.isPresent()){
                Map<String, ProBasicPowerstationPoint> basicPowerstationPointMap = wswppointmap.get(wsOptional.get().getId());

                ProBasicPowerstationPoint proBasicPowerstationPoint = null;
                if (wp.getSpare4().equals("1")){
                    proBasicPowerstationPoint = basicPowerstationPointMap.get(ContantXk.FCCFTWD);
                }else if(wp.getSpare4().equals("2")){
                    proBasicPowerstationPoint = basicPowerstationPointMap.get(ContantXk.QXZWD);
                }

//                ProBasicPowerstationPoint proBasicPowerstationPoint = basicPowerstationPointMap.get(ContantXk.FCCFTWD);
                try {
                    Long pointTime = edosUtil.getRealData(proBasicPowerstationPoint.getNemCode()).getPointTime();
                    if ((currentDate.getTime() - pointTime) / 1000 < Integer.parseInt(second)){
                        cftzt = 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (subStationOptional.isPresent()){
                Map<String, ProBasicPowerstationPoint> basicPowerstationPointMap = subwppointmap.get(subStationOptional.get().getId());
                ProBasicPowerstationPoint agcpowerstationPoint = basicPowerstationPointMap.get(ContantXk.TPOINT_WP_CXGL);
                try {
                    Long pointTime = edosUtil.getRealData(agcpowerstationPoint.getNemCode()).getPointTime();
                    if ((currentDate.getTime() - pointTime) / 1000 < Integer.parseInt(second)){
                        agczt = 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ProBasicPowerstationPoint glycpowerstationPoint = basicPowerstationPointMap.get(ContantXk.TPOINT_WP_YCGL);
                try {
                    Long pointTime = edosUtil.getRealData(glycpowerstationPoint.getNemCode()).getPointTime();
                    if ((currentDate.getTime() - pointTime) / 1000 < Integer.parseInt(second)){
                        glyczt = 1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (djlzt==1 || agczt==1 || glyczt==1 || fjzt==1 || cftzt==1){
                czzt = 1;
            }
            resultList.add(PointUtil.createPointData(currentDate,zycgl,zycglpoint.getNemCode(),zycglpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,djlzt,djlpoint.getNemCode(),djlpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,agczt,agcpoint.getNemCode(),agcpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,glyczt,glycpoint.getNemCode(),glycpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,cftzt,cftpoint.getNemCode(),cftpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,fjzt,sbjrpoint.getNemCode(),sbjrpoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate,czzt,czztpoint.getNemCode(),czztpoint.getName()));
        });
        edosUtil.sendMultiPoint(resultList);

    }

    /**
     * 全场状态
     */
    public void fullFieldState() throws Exception {
        List<PointData> resultList = new ArrayList<>();
        Date currentDate = DateUtils.getCurrentDate();
        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        Map<String, List<ProBasicEquipment>> wpwtmap = CacheContext.wpwtmap;
        wpls.stream().forEach(wp->{
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            ProBasicPowerstationPoint powerstationPoint = powerstationPointMap.get(ContantXk.QCZT);
            List<ProBasicEquipment> proBasicEquipments = wpwtmap.get(wp.getId());
            List<String> pointIds = new ArrayList<>();
            proBasicEquipments.stream().forEach(wt->{
                ProBasicEquipmentPoint equipmentPoint = wtpAimap.get(wt.getId()).get(ContantXk.SBZT);
                pointIds.add(equipmentPoint.getNemCode());
            });
            double zt = 1;
            try {
                List<PointData> realData = edosUtil.getRealData(pointIds);
                List<Double> doubles = realData.stream().map(real -> real.getPointValueInDouble()).collect(Collectors.toList());
                if (Collections.max(doubles).equals(Collections.min(doubles))){
                    zt = Collections.max(doubles);
                }else if(doubles.stream().filter(i->i==1).collect(Collectors.toList()).size()>0){
                    zt = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            resultList.add(PointUtil.createPointData(currentDate,zt,powerstationPoint.getNemCode(),powerstationPoint.getName()));
        });
        edosUtil.sendMultiPoint(resultList);
    }




}
