package com.huatek.busi.service.impl.gis.gisAssessment;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.huatek.busi.common.GisAssessEnum;
import com.huatek.busi.common.ParamEnum;
import com.huatek.busi.entity.GisAssessmentResultData;
import com.huatek.busi.entity.IecBreakerParam;
import com.huatek.busi.util.*;
import com.huatek.busi.vo.BusiOfflineGisBrandVo;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.BusiSemiAutomaticEvaluateDataVo;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GisKDMSCalculate {

    public static GisFormulaCal gisFormulaCalInstance = GisFormulaCal.getInstance();

    /**
     * 手动断开磨损
     *
     * @return
     */
    public static Map<String, String> calculateManualKDMS(Map<String, BusiParamTransformerVo> busiParamGisMap, BusiOfflineGisBrandVo busiOfflineGisBrandVo, Map<String, String> map, int step) {
        // 触头额定断开电流
        BigDecimal eddkdl = getEDDKDL(busiOfflineGisBrandVo);
        // 100%负荷的断开次数
        String shortCircuitBreakCurrents = map.get(GisAssessEnum.SHORT_CIRCUIT_BREAK_CURRENT.getKey());

        BigDecimal dkcsFor100_val = GisAssessUtilsNew.getDkcsesFor100(shortCircuitBreakCurrents, eddkdl);

        BigDecimal xdmscd_val = GisAssessUtilsNew.getMslsForFh(shortCircuitBreakCurrents, eddkdl);
        return calculateKDMSValue(busiParamGisMap, busiOfflineGisBrandVo, dkcsFor100_val, xdmscd_val, step);
    }

    /**
     * 自动断开磨损
     *
     * @return
     */
    public static Map<String, String> calculateAutoKDMS(Map<String, BusiParamTransformerVo> busiParamGisMap, BusiOfflineGisBrandVo busiOfflineGisBrandVo, List<IecBreakerParam> iecBreakerParams, GisAssessmentResultData gisAssessmentResultData, int step) {
        // 触头额定断开电流
        BigDecimal eddkdl = getEDDKDL(busiOfflineGisBrandVo);
        BigDecimal dkcsFor100_val = new BigDecimal("0");
        BigDecimal xdmscd_val = new BigDecimal("0");

        //评测中间结果、参数表中前一天评测数据都不为空，评测数据+断开参数表作为新评测数据。
        if (ObjectUtil.isNotEmpty(gisAssessmentResultData) && CollectionUtil.isNotEmpty(iecBreakerParams)) {
            // 100%负荷的断开次数
            dkcsFor100_val = new BigDecimal(gisAssessmentResultData.getDkcses()).add(GisAssessUtilsNew.getDkcsesFor100(iecBreakerParams, eddkdl));
            // 触头磨损率
            xdmscd_val = new BigDecimal(gisAssessmentResultData.getContactWearRate()).add(GisAssessUtilsNew.getMslsForFh(iecBreakerParams, eddkdl));

            //评测中间结果如果为空取全部数据进行评测
        } else if (ObjectUtil.isEmpty(gisAssessmentResultData) && CollectionUtil.isNotEmpty(iecBreakerParams)) {
            // 100%负荷的断开次数
            dkcsFor100_val = GisAssessUtilsNew.getDkcsesFor100(iecBreakerParams, eddkdl);
            // 触头磨损率
            xdmscd_val = GisAssessUtilsNew.getMslsForFh(iecBreakerParams, eddkdl);

            //评测中间结果不为空，参数表取得前一天评测数据为空，取评测中间结果数据评测
        } else if (ObjectUtil.isNotEmpty(gisAssessmentResultData) && CollectionUtil.isEmpty(iecBreakerParams)) {
            // 100%负荷的断开次数
            dkcsFor100_val = new BigDecimal(gisAssessmentResultData.getDkcses());
            // 触头磨损率
            xdmscd_val = new BigDecimal(gisAssessmentResultData.getContactWearRate());

        } else if (ObjectUtil.isEmpty(gisAssessmentResultData) && CollectionUtil.isEmpty(iecBreakerParams)) {
            dkcsFor100_val = null;
            xdmscd_val = null;
        }
        if (dkcsFor100_val == null && "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.CUMULATIVE_BREAKING_ACTION_TIME.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.CUMULATIVE_BREAKING_ACTION_TIME.getValue() + "未安装或未采集到数据");
            return map;
        }
        if (busiOfflineGisBrandVo.getTransportTime() == null && "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SERVICE_LIFE.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.SERVICE_LIFE.getValue() + "未安装或未采集到数据");
            return map;
        }
        if (xdmscd_val == null && "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.RELATIVE_ELECTRICAL_WEAR.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.RELATIVE_ELECTRICAL_WEAR.getValue() + "未安装或未采集到数据");
            return map;
        }
        return calculateKDMSValue(busiParamGisMap, busiOfflineGisBrandVo, dkcsFor100_val, xdmscd_val, step);
    }


    private static BigDecimal getEDDKDL(BusiOfflineGisBrandVo busiOfflineGisBrandVo) {
        BigDecimal eddkdl = GisAssessUtilsNew.getEddkdl(busiOfflineGisBrandVo);
        eddkdl = eddkdl.multiply(new BigDecimal("1000"));
        log.info("触头额定断开电流(A)-标牌：{}", eddkdl);
        return eddkdl;
    }

    private static Map<String, String> calculateKDMSValue(Map<String, BusiParamTransformerVo> busiParamGisMap, BusiOfflineGisBrandVo busiOfflineGisBrandVo, BigDecimal dkcsFor100_val, BigDecimal xdmscd_val, int step) {
        BusiParamTransformerVo dkmsVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DISCONNECTION_WEAR.getKey());
        BusiParamTransformerVo ljdkcsVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.CUMULATIVE_BREAKING_ACTION_TIME.getKey());
        BusiParamTransformerVo synxVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SERVICE_LIFE.getKey());
        BusiParamTransformerVo xdmsdVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.RELATIVE_ELECTRICAL_WEAR.getKey());
        // 累计断开次数-总得分
        Map<String, String> result1 = calculateLJDKCS(ljdkcsVo, dkmsVo, dkcsFor100_val, step);
        BigDecimal leijidongzuocishu_total = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));
        // 使用年限得分
        Map<String, String> result2 = calculateSYNX(synxVo, dkmsVo, busiOfflineGisBrandVo, step);
        BigDecimal shiyongnianxian_total = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));
        // 相对电磨损程度得分
        Map<String, String> result3 = calculateXDMSD(xdmsdVo, dkmsVo, xdmscd_val, step);
        BigDecimal xiangduidianmosun_total = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(dkmsVo.getIsAttend())) {
            // 开断磨损-总得分
            finalResult = leijidongzuocishu_total.add(shiyongnianxian_total).add(xiangduidianmosun_total);
            log.info("-- >>  GIS-开断磨损计算-计算结果: [{}]", finalResult);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(ljdkcsVo.getIsAttend())) {
                jsonObject.put("累计断开动作次数", leijidongzuocishu_total.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(synxVo.getIsAttend())) {
                jsonObject.put("使用年限", shiyongnianxian_total.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(xdmsdVo.getIsAttend())) {
                jsonObject.put("相对电磨损", xiangduidianmosun_total.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("断开磨损权重", dkmsVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, dkmsVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> result = new HashMap<>();
        result.put(GisCreateUtil.BREAKING_WEAR, finalResult.toString());
        result.put(GisCreateUtil.CUMULATIVE_ACTION_TIMES, leijidongzuocishu_total.toString());
        result.put(GisCreateUtil.SERVICE_LIFE, shiyongnianxian_total.toString());
        result.put(GisCreateUtil.RELATIVE_WEAR_DEGREE, xiangduidianmosun_total.toString());
        result.put(GisCreateUtil.CUMULATIVE_ACTION_TIMES_VALUE, dkcsFor100_val.toString());
        result.put(GisCreateUtil.RELATIVE_WEAR_DEGREE_VALUE, xdmscd_val.toString());
        result.put("step", String.valueOf(step));
        return result;
    }

    /**
     * 累计开断(动作)次数
     *
     * @return
     */
    private static Map<String, String> calculateLJDKCS(BusiParamTransformerVo vo, BusiParamTransformerVo parentVo, BigDecimal dkcsFor100_val, int step) {
        BigDecimal leijidongzuocishu_total = new BigDecimal(0);
        if ("1".equals(vo.getIsAttend())) {
            BigDecimal dkcsFor100 = gisFormulaCalInstance.formulaForUpAndDown(dkcsFor100_val, vo.getModel(), vo.getAValue(), vo.getBValue());
            leijidongzuocishu_total = gisFormulaCalInstance.calcValue(dkcsFor100, vo.getValue(), parentVo.getValue());
            log.info("-- >>  GIS-累计开断(动作)次数-计算结果: [{}]", leijidongzuocishu_total.toString());
            step += 1;
            EvaluationNameUtil.setStep(step, vo, leijidongzuocishu_total.toString(), dkcsFor100_val.toString());
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", leijidongzuocishu_total.toString());
        return map;
    }

    /**
     * 使用年限时间
     *
     * @return
     */
    private static Map<String, String> calculateSYNX(BusiParamTransformerVo vo, BusiParamTransformerVo parentVo, BusiOfflineGisBrandVo busiOfflineGisBrandVo, int step) {
        BigDecimal shiyongnianxian_total = new BigDecimal(0);
        if ("1".equals(vo.getIsAttend())) {
            BigDecimal synx_val = GisAssessUtilsNew.getSynx(busiOfflineGisBrandVo);
            log.info("使用年限时间：{}", synx_val);
            // synx升降后的计算使用年限
            BigDecimal synx = gisFormulaCalInstance.formulaForUpAndDown(synx_val, vo.getModel(), vo.getAValue(), vo.getBValue());

            shiyongnianxian_total = gisFormulaCalInstance.calcValue(synx, vo.getValue(), parentVo.getValue());
            log.info("-- >>  GIS-使用年限时间-计算结果: [{}]", shiyongnianxian_total.toString());
            step += 1;
            EvaluationNameUtil.setStep(step, vo, shiyongnianxian_total.toString(), synx_val.toString());
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", shiyongnianxian_total.toString());
        return map;
    }

    /**
     * 相对磨损程度
     *
     * @return
     */
    private static Map<String, String> calculateXDMSD(BusiParamTransformerVo vo, BusiParamTransformerVo parentVo, BigDecimal xdmscd_val, int step) {
        BigDecimal xiangduidianmosun_total = new BigDecimal(0);
        if ("1".equals(vo.getIsAttend())) {
            // 触头磨损率
            BigDecimal xdmscd = gisFormulaCalInstance.formulaForUpAndDown(xdmscd_val, vo.getModel(), vo.getAValue(), vo.getBValue());
            // 相对磨损程度-总得分
            xiangduidianmosun_total = gisFormulaCalInstance.calcValue(xdmscd, vo.getValue(), parentVo.getValue());
            log.info("-- >>  GIS-相对磨损程度-计算结果: [{}]", xiangduidianmosun_total.toString());
            step += 1;
            EvaluationNameUtil.setStep(step, vo, xiangduidianmosun_total.toString(), xdmscd_val.toString());
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", xiangduidianmosun_total.toString());
        return map;
    }
    /**
     * 半自动断开磨损
     *
     * @return
     */
    public static Map<String, String> calculateSemiAutoKDMS(Map<String, BusiParamTransformerVo> busiParamGisMap, BusiOfflineGisBrandVo busiOfflineGisBrandVo,GisAssessmentResultData gisAssessmentResultData, int step, BusiSemiAutomaticEvaluateDataVo latestSemiAutomaticEvaluateData) {
        // 触头额定断开电流
        BigDecimal eddkdl = getEDDKDL(busiOfflineGisBrandVo);
        BigDecimal dkcsFor100_val = new BigDecimal("0");
        BigDecimal xdmscd_val = new BigDecimal("0");
        //评测中间结果不为空,最新半自动录入数据不为空，评测数据+最新半自动录入数据作为新评测数据。
        if (ObjectUtil.isNotEmpty(gisAssessmentResultData) && ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)) {
            // 100%负荷的断开次数
            dkcsFor100_val = new BigDecimal(gisAssessmentResultData.getDkcses()).add(GisAssessUtilsNew.getSemiAutoDkcsesFor100(eddkdl,latestSemiAutomaticEvaluateData));
            // 触头磨损率
            xdmscd_val = new BigDecimal(gisAssessmentResultData.getContactWearRate()).add(GisAssessUtilsNew.getSemiAutoMslsForFh(eddkdl,latestSemiAutomaticEvaluateData));

            //评测中间结果为空，最新半自动录入数据不为空，取最新半自动录入数据进行评测
        } else if (ObjectUtil.isEmpty(gisAssessmentResultData) && ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)) {
            // 100%负荷的断开次数
            dkcsFor100_val = GisAssessUtilsNew.getSemiAutoDkcsesFor100(eddkdl,latestSemiAutomaticEvaluateData);
            // 触头磨损率
            xdmscd_val = GisAssessUtilsNew.getSemiAutoMslsForFh(eddkdl,latestSemiAutomaticEvaluateData);

            //评测中间结果不为空，最新半自动录入数据为空，取评测中间结果数据评测
        } else if (ObjectUtil.isNotEmpty(gisAssessmentResultData) && ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)) {
            // 100%负荷的断开次数
            dkcsFor100_val = new BigDecimal(gisAssessmentResultData.getDkcses());
            // 触头磨损率
            xdmscd_val = new BigDecimal(gisAssessmentResultData.getContactWearRate());

        } else if (ObjectUtil.isEmpty(gisAssessmentResultData) && ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)) {
            dkcsFor100_val = null;
            xdmscd_val = null;
        }
        String isAttend = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.CUMULATIVE_BREAKING_ACTION_TIME.getKey()).getIsAttend();
        if (dkcsFor100_val == null && "1".equals(isAttend)) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.CUMULATIVE_BREAKING_ACTION_TIME.getValue() + "未安装或未采集到数据");
            return map;
        }
        if (busiOfflineGisBrandVo.getTransportTime() == null && "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SERVICE_LIFE.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.SERVICE_LIFE.getValue() + "未安装或未采集到数据");
            return map;
        }
        if (xdmscd_val == null && "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.RELATIVE_ELECTRICAL_WEAR.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.RELATIVE_ELECTRICAL_WEAR.getValue() + "未安装或未采集到数据");
            return map;
        }
        return calculateKDMSValue(busiParamGisMap, busiOfflineGisBrandVo, dkcsFor100_val, xdmscd_val, step);
    }
}
