package com.gyee.generation.service;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProBasicEquipmentPoint;
import com.gyee.generation.model.auto.ProBasicPowerstationPoint;
import com.gyee.generation.model.vo.StatData;
import com.gyee.generation.model.vo.WpType;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

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

@Service
public class LimitService {

    @Resource
    private IEdosUtil edosUtil;
    //AGC监视限电

    //限电
    static Map<String, ProBasicEquipmentPoint> WindTurbineE;

    //shishi
    static Map<String, ProBasicEquipmentPoint> WindturbineQXDic4;

    //限电状态
    static Map<String, Map<String, ProBasicEquipmentPoint>> WindTurbineD;


    //AGC限电指令
    static Map<String, ProBasicPowerstationPoint> WindPowerAGC;
    static Map<String, ProBasicPowerstationPoint> WindPowerCXYG;
    static Map<String, ProBasicPowerstationPoint> WindPowerZSGL;

    static Map<String, ProBasicPowerstationPoint> WindPowerXDZT;
    static Map<String, Double> powerCache = new HashMap<String, Double>();

    static Map<String, Date> timeCache = new HashMap<String, Date>();


    public void execStatusAGCxd() throws Exception {
        if (WindTurbineE == null || WindTurbineD == null || WindPowerAGC == null) {
            WindTurbineD = new HashMap<String, Map<String, ProBasicEquipmentPoint>>();

            List<ProBasicEquipment> wtls = CacheContext.wtls;
            Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
            wtls.stream().forEach(wt -> {
                Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = wtpAimap.get(wt.getId());
                if (windturbinetestingpointnewMap.containsKey(ContantXk.GPZT)) {
                    ProBasicEquipmentPoint xdslPoint = windturbinetestingpointnewMap.get(ContantXk.GPZT);
                    WindturbineXDSL.put(wt.getId(), xdslPoint);
                }

            });

            Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
            CacheContext.wpls.forEach(wp -> {
                Map<String, ProBasicPowerstationPoint> wppMap = wppointmap.get(wp.getId());
                if (wp.getId().contains(WpType.FDC.id)) {
                    if (wppMap.containsKey(ContantXk.TPOINT_WP_AGC)) {
                        ProBasicPowerstationPoint agcPoint = wppMap.get(ContantXk.TPOINT_WP_AGC);
                        WindPowerAGC.put(wp.getId(), agcPoint);
                    }

                    if (wppMap.containsKey(ContantXk.TPOINT_WP_CXGL)) {
                        ProBasicPowerstationPoint cxglPoint = wppMap.get(ContantXk.TPOINT_WP_CXGL);
                        WindPowerCXYG.put(wp.getId(), cxglPoint);
                    }

                    if (wppMap.containsKey(ContantXk.SSZNHGLZS)) {
                        ProBasicPowerstationPoint zsglPoint = wppMap.get(ContantXk.SSZNHGLZS);
                        WindPowerZSGL.put(wp.getId(), zsglPoint);
                    }

                    if (wppMap.containsKey(ContantXk.XDZT)) {
                        ProBasicPowerstationPoint xdztPoint = wppMap.get(ContantXk.XDZT);
                        WindPowerXDZT.put(wp.getId(), xdztPoint);
                    }
                }


            });


            for (String key : WindPowerAGC.keySet()) {
                Map<String, ProBasicEquipmentPoint> windD = new HashMap<>();
                Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = wtpAimap.get(key);
                if (windturbinetestingpointnewMap.containsKey(ContantXk.XDZT)) {
                    ProBasicEquipmentPoint xdztPoint = windturbinetestingpointnewMap.get(ContantXk.XDZT);
                    windD.put(key, xdztPoint);
                }

                if (windD != null) {
                    WindTurbineD.put(key, windD);
                }

                powerCache.put(key, -1.0);
            }

            //风机转速限值1.5MV && 有功控制给定值2MV

            wtls.stream().forEach(wt -> {
                if (wt.getWindpowerstationId().contains(WpType.FDC.id)) {
                    Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = wtpAimap.get(wt.getId());
                    if (windturbinetestingpointnewMap.containsKey(ContantXk.XDZT)) {
                        ProBasicEquipmentPoint xdclztPoint = windturbinetestingpointnewMap.get(ContantXk.XDZT);
                        WindTurbineE.put(wt.getId(), xdclztPoint);
                    }

                    if (windturbinetestingpointnewMap.containsKey(ContantXk.SSQFZT)) {
                        ProBasicEquipmentPoint rqfztPoint = windturbinetestingpointnewMap.get(ContantXk.SSQFZT);
                        WindturbineQXDic4.put(wt.getId(), rqfztPoint);
                    }
                }

            });


        } else {

            Date time = new Date();

            Date begin = DateUtils.addMinutes(time, -8);

            List<PointData> projectRT = new ArrayList<PointData>();


            for (String item : WindPowerAGC.keySet()) {


                Double capacity = CacheContext.wpmap.get(item).getCapacity();

                //读取AGC实时数据
                Double windpoweragc = 0.0;
                if (WindPowerAGC.containsKey(item)) {
                    windpoweragc = edosUtil.getRealData(WindPowerAGC.get(item).getNemCode()).getPointValueInDouble();
                    ;
                }
                //读取出线有功实时数据
                Double windpowercxyg = 0.0;
                if (WindPowerCXYG.containsKey(item)) {
                    windpowercxyg = edosUtil.getRealData(WindPowerCXYG.get(item).getNemCode()).getPointValueInDouble();
                }

                //   #region 读取历史数据
                Double windpoweragcHis = 0.0;
                if (WindPowerAGC.containsKey(item)) {
                    long pried = DateUtils.secondsDiff(begin, time);

                    List<PointData> hsls = edosUtil.getHistStat(WindPowerAGC.get(item).getNemCode(), begin.getTime() / 1000, time.getTime() / 1000, 1l, pried, StatData.AVG.getValue());
                    if (StringUtils.notEmp(hsls) && hsls.size() > 0) {
                        windpoweragcHis = hsls.get(0).getPointValueInDouble();
                    }

                }
                //读取出线有功历史数据
                Double windpowercxygHis = 0.0;
                if (WindPowerCXYG.containsKey(item)) {
                    long pried = DateUtils.secondsDiff(begin, time);

                    List<PointData> hsls = edosUtil.getHistStat(WindPowerCXYG.get(item).getNemCode(), begin.getTime() / 1000, time.getTime() / 1000, 1l, pried, StatData.AVG.getValue());
                    if (StringUtils.notEmp(hsls) && hsls.size() > 0) {
                        windpowercxygHis = hsls.get(0).getPointValueInDouble();
                    }

                }

                Double windpowerzsglHis = 0.0;
                if (WindPowerZSGL.containsKey(item)) {
                    long pried = DateUtils.secondsDiff(begin, time);
                    List<PointData> hsls = edosUtil.getHistStat(WindPowerZSGL.get(item).getNemCode(), begin.getTime() / 1000, time.getTime() / 1000, 1l, pried, StatData.AVG.getValue());
                    if (StringUtils.notEmp(hsls) && hsls.size() > 0) {
                        windpowerzsglHis = hsls.get(0).getPointValueInDouble();
                    }


                }


                boolean k = false;  //agc 硬限电
                // 判断缓存有没有这个ID点
                if (!powerCache.containsKey(item)) {
                    continue;
                }
                //第一次初始化复制实时数据到缓存
                if (powerCache.get(item) == -1) {
                    powerCache.put(item, windpoweragc);

                } else if (powerCache.get(item) == windpoweragc) {
                    //continue;
                    k = true;
                } else {
                    powerCache.put(item, windpoweragc);
                }

                //查看风场缓存中是否存在AGC信息
                if (!CacheContext.wpmap.containsKey(item)) {
                    continue;
                }

                if (!WindTurbineD.containsKey(item)) {
                    continue;
                }

                windpoweragc = StringUtils.round(windpoweragc, 1);
                windpowercxyg = StringUtils.round(windpowercxyg, 1);
                capacity = StringUtils.round(capacity, 1);


                if (windpoweragc >= capacity) {
                    for (String key : WindTurbineD.get(item).keySet()) {
                        //String id = WindTurbineD[item][key].ID;
                        //Double d = edosUtil.getRealData(id);
                        //if (d == 1d)
                        //{

                        //限电受累
                        String id2 = WindturbineXDSL.get(key).getNemCode();
                        Double xdsl = edosUtil.getRealData(id2).getPointValueInDouble();
                        Double val = 0.0;
                        if (xdsl == 1) {
                            val = 1.0;
                        }
                        PointData rt = new PointData();


                        rt.setEdnaId(WindTurbineD.get(item).get(key).getNemCode());
                        rt.setPointTime(time.getTime());
                        rt.setPointValueInDouble(val);
                        projectRT.add(rt);
                        //}

                    }

                    PointData fcrt = new PointData();

                    fcrt.setEdnaId(WindPowerXDZT.get(item).getNemCode());
                    fcrt.setPointTime(time.getTime());
                    fcrt.setPointValueInDouble(0.0);
                    projectRT.add(fcrt);

                } else {
                    boolean b = false;
                    if (windpowerzsglHis > capacity * 0.4) {
                        if (windpowerzsglHis / windpoweragc > 1.5) {
                            b = true;
                        }
                        if (windpoweragcHis <= windpowercxygHis) {
                            b = true;
                        }
                        if (windpoweragcHis <= windpowerzsglHis * 0.75 && windpowercxygHis >= windpoweragcHis * 0.85) {
                            b = true;
                        }
                        if (windpoweragcHis <= windpowerzsglHis * 0.85 && windpowercxygHis >= windpoweragcHis * 0.94) {
                            b = true;
                        }
                        if (windpoweragcHis <= windpowerzsglHis && windpowercxygHis >= windpoweragcHis * 0.95) {
                            b = true;
                        }
                    } else {
                        if (k) {
                            if (windpoweragcHis <= windpowercxygHis) {
                                b = true;
                            }
                            if (windpoweragcHis <= windpowerzsglHis * 0.75 && windpowercxygHis >= windpoweragcHis * 0.94) {
                                b = true;
                            }
                        }
                    }

                    //     #region 新增短时间来回变化状态
                    if (b) {
                        if (timeCache.containsKey(item)) {
                            timeCache.put(item, time);

                        } else {
                            timeCache.put(item, time);

                        }
                    } else {
                        if (timeCache.containsKey(item)) {
                            Double minutes = DateUtils.minutesDiff2(time, timeCache.get(item));
                            if (minutes < 3) {
                                if (windpoweragc - windpowercxyg < 1.5) {
                                    b = true;
                                }
                            }
                        }
                    }
                    //       #endregion

                    if (!b) {
                        for (String key : WindTurbineD.get(item).keySet()) {
                            //String id = WindTurbineD[item][key].ID;
                            //Double d = edosUtil.getRealData(id);
                            //if (d == 1d)
                            //{
                            //限电受累
                            String id2 = WindturbineXDSL.get(key).getNemCode();
                            Double xdsl = edosUtil.getRealData(id2).getPointValueInDouble();
                            Double val = 0.0;
                            if (xdsl == 1) {
                                val = 1.0;
                            }
                            PointData rt = new PointData();

                            rt.setEdnaId(WindTurbineD.get(item).get(key).getNemCode());
                            rt.setPointTime(time.getTime());
                            rt.setPointValueInDouble(0.0);
                            projectRT.add(rt);


                            //}

                        }
                        PointData fcrt = new PointData();


                        fcrt.setEdnaId(WindPowerXDZT.get(item).getNemCode());
                        fcrt.setPointTime(time.getTime());
                        fcrt.setPointValueInDouble(0.0);
                        projectRT.add(fcrt);
                    } else {
                        PointData fcrt = new PointData();

                        fcrt.setEdnaId(WindPowerXDZT.get(item).getNemCode());
                        fcrt.setPointTime(time.getTime());
                        fcrt.setPointValueInDouble(1.0);
                        projectRT.add(fcrt);

                        for (String key : WindTurbineD.get(item).keySet()) {
                            //限电状态
                            //String id = WindTurbineD[item][key].ID;
                            //Double xd = edosUtil.getRealData(id);

                            boolean tempb = false;


                            if (CacheContext.wtmap.containsKey(key) && WindturbineQXDic4.containsKey(key)) {
                                String modelid = CacheContext.wtmap.get(key).getModelId();

                                Double xz = 0.0;
                                Double xz2 = 0.0;
                                if (modelid.contains("UP77-1500") || modelid.contains("UP82-1500")) {
                                    xz = 17.3;
                                    xz2 = 1500.0;
                                } else if (modelid.contains("UP100-3000")) {
                                    xz = 3000.0;
                                    xz2 = 3000.0;
                                } else {
                                    xz = 2000.0;
                                    xz2 = 2000.0;
                                }

                                if (WindturbineQXDic4.containsKey(key)) {
                                    Double tempk = edosUtil.getRealData(WindturbineQXDic4.get(key).getNemCode()).getPointValueInDouble();

                                    if (tempk >= 3) {
                                        tempb = true;
                                    } else {
                                        tempb = false;
                                    }
                                }
                                //     #region 新增加

                                Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
                                Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = wtpAimap.get(key);
                                if (windturbinetestingpointnewMap.containsKey(ContantXk.CJ_SSFS)) {
                                    ProBasicEquipmentPoint xdztPoint = windturbinetestingpointnewMap.get(ContantXk.CJ_SSFS);
                                    Double rtbbb = edosUtil.getRealData(xdztPoint.getNemCode()).getPointValueInDouble();
                                    if (rtbbb >= xz2 * 0.9) {
                                        tempb = false;
                                    }
                                }

                            }


                            //限出力状态
                            String id1 = WindTurbineE.get(key).getNemCode();
                            Double xcl = edosUtil.getRealData(id1).getPointValueInDouble();


                            //限电受累
                            String id2 = WindturbineXDSL.get(key).getNemCode();
                            Double xdsl = edosUtil.getRealData(id2).getPointValueInDouble();
                            Double val = 0.0;
                            if (xdsl == 1 || xcl == 1 || tempb) {
                                val = 1.0;
                            }
                            PointData rt = new PointData();


                            rt.setEdnaId(WindTurbineD.get(item).get(key).getNemCode());
                            rt.setPointTime(time.getTime());
                            rt.setPointValueInDouble(val);
                            projectRT.add(rt);


                        }
                    }
                }

            }
            if (projectRT.size() > 0) {
                edosUtil.sendMultiPoint(projectRT);

            }
        }
    }


    Map<String, ProBasicEquipmentPoint> WindturbineXDSL;


    Map<String, List<String>> WindturbineQXDic;
    List<ProBasicEquipmentPoint> WindTurbineQX;
    Map<String, ProBasicEquipmentPoint> WindturbineQXDic2;

    public void execStatus() throws Exception {


        if (WindTurbineQX == null || WindturbineQXDic == null) {

            WindturbineQXDic = new HashMap<String, List<String>>();

            //var disArr = entity.ProBasicEquipmentPoint.Where(it => it.UNIFORMCODE == "AI178" || it.UNIFORMCODE == "CJ_SCADA_YGSD").ToList();

            //叶轮转速给定           AI134   限值17.4
            //转矩给定               CJ_ZSGD    限值 95   （2MW 叫 变频器转矩给定  无用）
            //发电状态下的转速给定   AI178    限值17.4


            //scada有功设定值           CJ_SCADA_YGSD   2MW 限值2000   3MW 限值3000
            //实时欠发状态    SSQFZT

            List<ProBasicEquipmentPoint> wtpointls = new ArrayList<>();
            List<ProBasicEquipment> wtls = CacheContext.wtls;
            Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
            wtls.stream().forEach(wt -> {
                Map<String, ProBasicEquipmentPoint> windturbinetestingpointnewMap = wtpAimap.get(wt.getId());
                if (windturbinetestingpointnewMap.containsKey(ContantXk.CJ_YLZSGD)) {
                    ProBasicEquipmentPoint point = windturbinetestingpointnewMap.get(ContantXk.CJ_YLZSGD);
                    wtpointls.add(point);
                }
                if (windturbinetestingpointnewMap.containsKey(ContantXk.CJ_FDZTXZSGD)) {
                    ProBasicEquipmentPoint point = windturbinetestingpointnewMap.get(ContantXk.CJ_FDZTXZSGD);
                    wtpointls.add(point);
                }
                if (windturbinetestingpointnewMap.containsKey(ContantXk.CJ_SCADA_YGSD)) {
                    ProBasicEquipmentPoint point = windturbinetestingpointnewMap.get(ContantXk.CJ_SCADA_YGSD);
                    wtpointls.add(point);
                }


                if (windturbinetestingpointnewMap.containsKey(ContantXk.XDZT)) {
                    ProBasicEquipmentPoint point = windturbinetestingpointnewMap.get(ContantXk.XDZT);
                    WindTurbineQX.add(point);
                }
            });


            for (ProBasicEquipmentPoint dis : wtpointls) {
                if (!WindturbineQXDic.containsKey(dis.getWindturbineId())) {
                    WindturbineQXDic.put(dis.getWindturbineId(), new ArrayList<String>());
                }
                WindturbineQXDic.get(dis.getWindturbineId()).add(dis.getNemCode());
            }

        } else {
            Date dt = new Date();
            List<PointData> insertRt = new ArrayList<PointData>();
            for (ProBasicEquipmentPoint item : WindTurbineQX) {
                String key = item.getWindturbineId();
                String windows = item.getWindpowerstationId();

                String point = item.getNemCode();//重点注意！！！！
                Double val = 0.0;
                if (CacheContext.wtmap.containsKey(key) && WindturbineQXDic.containsKey(key)) {
                    String modelid = CacheContext.wtmap.get(key).getModelId();

                    Double xz = 0.0;
                    Double xz2 = 0.0;
                    if (modelid.contains("UP77-1500") || modelid.contains("UP82-1500")) {
                        xz = 17.3;
                        xz2 = 1500.0;
                    } else if (modelid.contains("UP100-3000")) {
                        xz = 3000.0;
                        xz2 = 3000.0;
                    } else {
                        xz = 2000.0;
                        xz2 = 2000.0;
                    }

                    List<String> lxIds = WindturbineQXDic.get(key);

                    List<Date> dts = new ArrayList<>();
                    List<PointData> vals1 = edosUtil.getRealData(lxIds);

                    boolean b = true;

                    for (PointData va : vals1) {
                        b = b && (va.getPointValueInDouble() >= xz);
                    }

                    if (WindturbineQXDic2.containsKey(key)) {
                        PointData k = edosUtil.getRealData(WindturbineQXDic2.get(key).getNemCode());

                        b = b && (k.getPointValueInDouble() == 0);
                    }


                    val = b ? 0.0 : 1.0;

                    PointData rt = new PointData();

                    rt.setEdnaId(point);
                    rt.setPointTime(dt.getTime());
                    rt.setPointValueInDouble(val);
                    insertRt.add(rt);


                }
            }

            edosUtil.sendMultiPoint(insertRt);
            this.execStatusAGCxd();
            //this.execStatuSL();

            System.out.println(DateUtils.getFormat1().format(new Date()));

            Thread.sleep(1000);
        }

    }
}
