package com.gyee.generation.service;

import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.algorithm.Underdelivery;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.util.SpringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.redis.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName : StatusService
 * @Author : xieshengjie
 * @Date: 2022/3/28 14:22
 * @Description : 状态计算
 */
@Service
public class Status_HF_Service {

    @Resource
    private IEdosUtil edosUtil;


    /**
     * 判断故障状态
     *
     * @throws Exception
     */
    public void calculateGzStatus() throws Exception {


        RedisService redisService = SpringUtils.getBean("redisService");
        Date date = new Date();
        Map<String, Map<String, Object>> pipelinedmap = new HashMap<>();
        if (!CacheContext.wtls.isEmpty()) {

            Map<Integer, List<String>> pointmap = new LinkedHashMap<>();
            for (ProBasicEquipment wt : CacheContext.wtls) {

                List<ProBasicStatusPoint> disLs = CacheContext.pointdis.stream().filter(i -> i.getWindturbineId().equals(wt.getId()) && i.getUniformCode().equals("MX006")).collect(Collectors.toList());

                if (!disLs.isEmpty()) {
                    ProBasicStatusPoint key = disLs.get(0);
                    if (CacheContext.redisDbMap.containsKey(key.getId())) {
                        int dbnum = CacheContext.redisDbMap.get(key.getId());

                        if (pointmap.containsKey(dbnum)) {
                            List<String> ls = pointmap.get(dbnum);
                            ls.add(key.getId());
                        } else {
                            List<String> ls = new ArrayList<>();
                            ls.add(key.getId());
                            pointmap.put(dbnum, ls);
                        }

                    }


                }

                disLs = CacheContext.pointdis.stream().filter(i -> i.getWindturbineId().equals(wt.getId()) && i.getUniformCode().equals("MX002")).collect(Collectors.toList());


                if (!disLs.isEmpty()) {
                    ProBasicStatusPoint key = disLs.get(0);
                    if (CacheContext.redisDbMap.containsKey(key.getId())) {
                        int dbnum = CacheContext.redisDbMap.get(key.getId());


                        if (pointmap.containsKey(dbnum)) {
                            List<String> ls = pointmap.get(dbnum);
                            ls.add(key.getId());
                        } else {
                            List<String> ls = new ArrayList<>();
                            ls.add(key.getId());
                            pointmap.put(dbnum, ls);
                        }


                    }

                }
            }

            Map<String, Map<String, String>> valuemap = redisService.hgetall(pointmap);
            for (ProBasicEquipment wt : CacheContext.wtls) {


                if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                    Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());
                    if (map.containsKey(ContantXk.SBZT)) {
                        ProBasicEquipmentPoint pt = map.get(ContantXk.SBZT);
                        // redisService.select(0);

                        Map<String, Object> pomap = new LinkedHashMap<>();

                        pomap.put("pointid", pt.getId());

                        pomap.put("value", 0);
                        pomap.put("timestamp", date.getTime() / 1000);
                        pomap.put("datetime", DateUtils.toDate(date));
                        pomap.put("status", 0);
                        pipelinedmap.put(pt.getId(), pomap);
                        // redisService.putHashAllObj(pt.getId(),pomap);

                    }
                }

                if (wt.getModelId().equals("SL1500-77") || wt.getModelId().equals("SL1500-82")) {
                    List<ProBasicStatusPoint> disLs = CacheContext.pointdis.stream().filter(i -> i.getWindturbineId().equals(wt.getId()) && i.getUniformCode().equals("ZT001")).collect(Collectors.toList());

                    if (!disLs.isEmpty()) {
                        ProBasicStatusPoint key = disLs.get(0);
                        if (valuemap.containsKey(key.getId())) {

                            double value = 0.0;

                            if (valuemap.containsKey(key.getId())) {
                                Map<String, String> tempmap = valuemap.get(key.getId());
                                if (tempmap.containsKey("value")) {
                                    value = Double.valueOf((String) tempmap.get("value"));
                                }
                            }


                            if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                                Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());
                                if (map.containsKey(ContantXk.SBZT)) {
                                    ProBasicEquipmentPoint pt = map.get(ContantXk.SBZT);

                                    if (StringUtils.notEmp(value) && value == 3) {
                                        redisService.select(0);


                                        Map<String, Object> pomap = new LinkedHashMap<>();

                                        pomap.put("pointid", pt.getId());
                                        pomap.put("value", 1);
                                        pomap.put("timestamp", date.getTime() / 1000);
                                        pomap.put("datetime", DateUtils.toDate(date));
                                        pomap.put("status", 0);

                                        pipelinedmap.put(pt.getId(), pomap);
                                        // redisService.putHashAllObj(pt.getId(),pomap);


                                    }
                                }
                            }

                        }


                    }
                } else {
                    List<ProBasicStatusPoint> disLs = CacheContext.pointdis.stream().filter(i -> i.getWindturbineId().equals(wt.getId()) && i.getUniformCode().equals("ZT002")).collect(Collectors.toList());


                    if (!disLs.isEmpty()) {
                        ProBasicStatusPoint key = disLs.get(0);
                        if (valuemap.containsKey(key.getId())) {
                            double value = 0.0;
                            if (valuemap.containsKey(key.getId())) {
                                Map<String, String> tempmap = valuemap.get(key.getId());
                                if (tempmap.containsKey("value")) {
                                    value = Double.valueOf((String) tempmap.get("value"));
                                }
                            }


                            if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                                Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());
                                if (map.containsKey(ContantXk.SBZT)) {
                                    ProBasicEquipmentPoint pt = map.get(ContantXk.SBZT);

                                    if (StringUtils.notEmp(value) && value == 1) {
                                        // redisService.select(0);


                                        Map<String, Object> pomap = new LinkedHashMap<>();

                                        pomap.put("pointid", pt.getId());
                                        pomap.put("value", 1);
                                        pomap.put("timestamp", date.getTime() / 1000);
                                        pomap.put("datetime", DateUtils.toDate(date));
                                        pomap.put("status", 0);
                                        pipelinedmap.put(pt.getId(), pomap);
                                        //redisService.putHashAllObj(pt.getId(),pomap);


                                    }
                                }
                            }
                        }

                    }
                }
            }
            redisService.select(0);
            redisService.putHashAllPipeObj(pipelinedmap);


        }

    }


    /**
     * 判断欠发状态
     *
     * @throws Exception
     */
    public void calculateQfStatus() throws Exception {
        RedisService redisService = SpringUtils.getBean("redisService");
        Date date = new Date();
        Map<String, Map<String, Object>> pipelinedmap = new HashMap<>();
        if (!CacheContext.wtls.isEmpty()) {
            Map<Integer, List<String>> pointmap = new LinkedHashMap<>();

            for (ProBasicEquipment wt : CacheContext.wtls) {

                if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                    Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());


                    if (map.containsKey(ContantXk.CJ_SSGL) && map.containsKey(ContantXk.CJ_SSFS)) {
                        ProBasicEquipmentPoint powerpoint = map.get(ContantXk.CJ_SSGL);
                        ProBasicEquipmentPoint speedpoint = map.get(ContantXk.CJ_SSFS);
                        if (CacheContext.redisDbMap.containsKey(powerpoint.getId()) && CacheContext.redisDbMap.containsKey(speedpoint.getId())) {

                            int dbnum = CacheContext.redisDbMap.get(powerpoint.getId());
                            if (pointmap.containsKey(dbnum)) {
                                List<String> ls = pointmap.get(dbnum);
                                ls.add(powerpoint.getId());
                            } else {
                                List<String> ls = new ArrayList<>();
                                ls.add(powerpoint.getId());
                                pointmap.put(dbnum, ls);
                            }


                            dbnum = CacheContext.redisDbMap.get(speedpoint.getId());
                            if (pointmap.containsKey(dbnum)) {
                                List<String> ls = pointmap.get(dbnum);
                                ls.add(speedpoint.getId());
                            } else {
                                List<String> ls = new ArrayList<>();
                                ls.add(speedpoint.getId());
                                pointmap.put(dbnum, ls);
                            }


                        }
                    }
                }
            }

            Map<String, Map<String, String>> valuemap = redisService.hgetall(pointmap);

            for (ProBasicEquipment wt : CacheContext.wtls) {

                if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                    Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());


                    if (map.containsKey(ContantXk.SSQFZT)) {
                        ProBasicEquipmentPoint qfpt = map.get(ContantXk.SSQFZT);
                        // redisService.select(0);
                        Map<String, Object> pomap = new LinkedHashMap<>();

                        pomap.put("pointid", qfpt.getId());

                        pomap.put("value", 0);
                        pomap.put("timestamp", date.getTime() / 1000);
                        pomap.put("datetime", DateUtils.toDate(date));
                        pomap.put("status", 0);
                        pipelinedmap.put(qfpt.getId(), pomap);
                        //redisService.putHashAllObj(qfpt.getId(),pomap);
                    }
                    if (map.containsKey(ContantXk.CJ_SSGL) && map.containsKey(ContantXk.CJ_SSFS)) {
                        ProBasicEquipmentPoint powerpoint = map.get(ContantXk.CJ_SSGL);
                        ProBasicEquipmentPoint speedpoint = map.get(ContantXk.CJ_SSFS);
                        double power = 0.0;

                        if (valuemap.containsKey(powerpoint.getId())) {
                            Map<String, String> tempmap = valuemap.get(powerpoint.getId());

                            if (tempmap.containsKey("value")) {
                                power = Double.valueOf((String) tempmap.get("value"));
                            }
                        }


                        double speed = 0.0;
                        if (valuemap.containsKey(speedpoint.getId())) {
                            Map<String, String> tempmap2 = valuemap.get(speedpoint.getId());
                            if (tempmap2.containsKey("value")) {
                                speed = Double.valueOf((String) tempmap2.get("value"));
                            }
                        }


                        if ((speed != 0 && power != 0) && CacheContext.curveFittingPowerMap.containsKey(wt.getId()) && StringUtils.notEmp(power) && StringUtils.notEmp(speed)) {
                            Map<Double, ProEconWtPowerCurveFitting> wtcurvemap = CacheContext.curveFittingPowerMap.get(wt.getId());

                            if (wtcurvemap.containsKey(StringUtils.round(speed, 2))) {
                                ProEconWtPowerCurveFitting wtcurve = wtcurvemap.get(StringUtils.round(speed, 2));
                                //拟合功率
                                Double actualpower = wtcurve.getActualPower();

                                double qfzt = Underdelivery.underdeliveryStatusReal(power, actualpower);
                                if (map.containsKey(ContantXk.SSQFZT)) {
                                    ProBasicEquipmentPoint qfpt = map.get(ContantXk.SSQFZT);
                                    //                                        redisService.select(0);


                                    Map<String, Object> pomap = new LinkedHashMap<>();

                                    pomap.put("pointid", qfpt.getId());

                                    pomap.put("value", qfzt);
                                    pomap.put("timestamp", date.getTime() / 1000);
                                    pomap.put("datetime", DateUtils.toDate(date));
                                    pomap.put("status", 0);
                                    pipelinedmap.put(qfpt.getId(), pomap);
                                    //redisService.putHashAllObj(qfpt.getId(),pomap);


                                }
                            }

                        }
                    }
                }
            }
            redisService.select(0);
            redisService.putHashAllPipeObj(pipelinedmap);

        }
    }


    /**
     * 判断理论功率
     *
     * @throws Exception
     */
    public void calculateLlgl() throws Exception {
        RedisService redisService = SpringUtils.getBean("redisService");
        Date date = new Date();
        Map<String, Map<String, Object>> pipelinedmap = new HashMap<>();
        if (!CacheContext.wtls.isEmpty()) {

            Map<Integer, List<String>> pointmap = new LinkedHashMap<>();
            for (ProBasicEquipment wt : CacheContext.wtls) {

                if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                    Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());


                    if (map.containsKey(ContantXk.CJ_SSFS)) {
                        ProBasicEquipmentPoint speedpoint = map.get(ContantXk.CJ_SSFS);
                        if (CacheContext.redisDbMap.containsKey(speedpoint.getId())) {

                            int dbnum = CacheContext.redisDbMap.get(speedpoint.getId());
                            if (pointmap.containsKey(dbnum)) {
                                List<String> ls = pointmap.get(dbnum);
                                ls.add(speedpoint.getId());
                            } else {
                                List<String> ls = new ArrayList<>();
                                ls.add(speedpoint.getId());
                                pointmap.put(dbnum, ls);
                            }

                        }
                    }
                }


            }

            Map<String, Map<String, String>> valuemap = redisService.hgetall(pointmap);
            for (ProBasicEquipment wt : CacheContext.wtls) {

                if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                    Map<String, ProBasicEquipmentPoint> map = CacheContext.wtpAimap.get(wt.getId());
                    if (map.containsKey(ContantXk.LLGL)) {
                        ProBasicEquipmentPoint qfpt = map.get(ContantXk.LLGL);
                        //redisService.select(8);


                        Map<String, Object> pomap = new LinkedHashMap<>();

                        pomap.put("pointid", qfpt.getId());

                        pomap.put("value", 0);
                        pomap.put("timestamp", date.getTime() / 1000);
                        pomap.put("datetime", DateUtils.toDate(date));
                        pomap.put("status", 0);
                        pipelinedmap.put(qfpt.getId(), pomap);
                        //  redisService.putHashAllObj(qfpt.getId(),pomap);

                    }

                    if (map.containsKey(ContantXk.CJ_SSFS)) {
                        ProBasicEquipmentPoint speedpoint = map.get(ContantXk.CJ_SSFS);
                        double speed = 0.0;
                        if (valuemap.containsKey(speedpoint.getId())) {
                            Map<String, String> tempmap = valuemap.get(speedpoint.getId());
                            if (tempmap.containsKey("value")) {
                                speed = Double.valueOf((String) tempmap.get("value"));
                            }
                        }


                        if (speed != 0 && CacheContext.curveFittingPowerMap.containsKey(wt.getId()) && StringUtils.notEmp(speed)) {
                            Map<Double, ProEconWtPowerCurveFitting> wtcurvemap = CacheContext.curveFittingPowerMap.get(wt.getId());

                            if (wtcurvemap.containsKey(StringUtils.round(speed, 2))) {
                                ProEconWtPowerCurveFitting wtcurve = wtcurvemap.get(StringUtils.round(speed, 2));
                                //拟合功率
                                Double actualpower = wtcurve.getOptimalPower();

                                if (CacheContext.wtpAimap.containsKey(wt.getId())) {

                                    if (map.containsKey(ContantXk.LLGL)) {
                                        ProBasicEquipmentPoint qfpt = map.get(ContantXk.LLGL);
                                        // redisService.select(0);


                                        Map<String, Object> pomap = new LinkedHashMap<>();

                                        pomap.put("pointid", qfpt.getId());

                                        pomap.put("value", actualpower);
                                        pomap.put("timestamp", date.getTime() / 1000);
                                        pomap.put("datetime", DateUtils.toDate(date));
                                        pomap.put("status", 0);
                                        pipelinedmap.put(qfpt.getId(), pomap);
                                        //redisService.putHashAllObj(qfpt.getId(),pomap);


                                    }
                                }
                            }
                        }
                    }
                }


            }
            redisService.select(0);
            redisService.putHashAllPipeObj(pipelinedmap);

        }
    }


    /**
     * 转存场站计算指标
     *
     * @throws Exception
     */
    public void calculateWpPoint() throws Exception {
        RedisService redisService = SpringUtils.getBean("redisService");
        Date date = new Date();
        Map<String, Map<String, Object>> pipelinedmap = new HashMap<>();
        if (!CacheContext.wpls.isEmpty()) {
            for (ProBasicPowerstation wp : CacheContext.wpls) {


                if (CacheContext.wpwtmap.containsKey(wp.getId())) {
                    List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(wp.getId());

                    List<String> fsls = new ArrayList<>();
                    List<String> glls = new ArrayList<>();
                    List<String> fdlls = new ArrayList<>();
                    for (ProBasicEquipment wt : wtls) {
                        if (CacheContext.wtpAimap.containsKey(wt.getId())) {
                            Map<String, ProBasicEquipmentPoint> pointmap = CacheContext.wtpAimap.get(wt.getId());

                            if (pointmap.containsKey(ContantXk.CJ_SSFS)) {
                                fsls.add(pointmap.get(ContantXk.CJ_SSFS).getNemCode());
                            }

                            if (pointmap.containsKey(ContantXk.CJ_SSGL)) {
                                glls.add(pointmap.get(ContantXk.CJ_SSGL).getNemCode());
                            }

                            if (pointmap.containsKey(ContantXk.CJ_FDL)) {
                                fdlls.add(pointmap.get(ContantXk.CJ_FDL).getNemCode());
                            }
                        }

                    }


                    List<PointData> fsvos = edosUtil.getRealData(fsls);

                    List<PointData> glvos = edosUtil.getRealData(glls);

                    List<PointData> fdlvos = edosUtil.getRealData(fdlls);
                    if (!fsvos.isEmpty()) {

                        double fs = 0.0;
                        for (PointData vo : fsvos) {

                            fs = fs + vo.getPointValueInDouble();
                            fs = new BigDecimal(fs).divide(new BigDecimal(fsvos.size()), 2, RoundingMode.HALF_EVEN).doubleValue();

                        }

                        if (CacheContext.wppointmap.containsKey(wp.getId())) {

                            Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.wppointmap.get(wp.getId());

                            if (pointMap.containsKey(ContantXk.SSPJFS)) {
                                ProBasicPowerstationPoint point = pointMap.get(ContantXk.SSPJFS);

                                Map<String, Object> pomap = new LinkedHashMap<>();

                                pomap.put("pointid", point.getNemCode());
                                pomap.put("value", fs);
                                pomap.put("timestamp", date.getTime() / 1000);
                                pomap.put("datetime", DateUtils.toDate(date));
                                pomap.put("status", 0);
                                pipelinedmap.put(point.getNemCode(), pomap);
                            }
                        }
                    }


                    if (!glvos.isEmpty()) {

                        double gl = 0.0;
                        for (PointData vo : glvos) {

                            gl = gl + vo.getPointValueInDouble();
                            gl = StringUtils.round(gl, 2);

                        }

                        if (CacheContext.wppointmap.containsKey(wp.getId())) {

                            Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.wppointmap.get(wp.getId());

                            if (pointMap.containsKey(ContantXk.SSZGL)) {
                                ProBasicPowerstationPoint point = pointMap.get(ContantXk.SSZGL);

                                Map<String, Object> pomap = new LinkedHashMap<>();

                                pomap.put("pointid", point.getNemCode());
                                pomap.put("value", gl);
                                pomap.put("timestamp", date.getTime() / 1000);
                                pomap.put("datetime", DateUtils.toDate(date));
                                pomap.put("status", 0);
                                pipelinedmap.put(point.getNemCode(), pomap);
                            }
                        }
                    }


                    if (!fdlvos.isEmpty()) {

                        double fdl = 0.0;
                        for (PointData vo : fdlvos) {

                            fdl = fdl + vo.getPointValueInDouble();
                            fdl = new BigDecimal(fdl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                        }

                        if (CacheContext.wppointmap.containsKey(wp.getId())) {

                            Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.wppointmap.get(wp.getId());

                            if (pointMap.containsKey(ContantXk.RFDL)) {
                                ProBasicPowerstationPoint point = pointMap.get(ContantXk.RFDL);

                                Map<String, Object> pomap = new LinkedHashMap<>();

                                pomap.put("pointid", point.getNemCode());
                                pomap.put("value", fdl);
                                pomap.put("timestamp", date.getTime() / 1000);
                                pomap.put("datetime", DateUtils.toDate(date));
                                pomap.put("status", 0);
                                pipelinedmap.put(point.getNemCode(), pomap);
                            }

                            if (pointMap.containsKey(ContantXk.RLLFDL)) {
                                ProBasicPowerstationPoint point = pointMap.get(ContantXk.RLLFDL);

                                Map<String, Object> pomap = new LinkedHashMap<>();

                                pomap.put("pointid", point.getNemCode());
                                pomap.put("value", fdl * (1 + Math.random()));
                                pomap.put("timestamp", date.getTime() / 1000);
                                pomap.put("datetime", DateUtils.toDate(date));
                                pomap.put("status", 0);
                                pipelinedmap.put(point.getNemCode(), pomap);
                            }
                        }
                    }

                }
            }
            redisService.select(0);
            redisService.putHashAllPipeObj(pipelinedmap);

        }
    }
}
