package com.huatek.busi.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.huatek.busi.common.GisAssessEnum;
import com.huatek.busi.entity.*;
import com.huatek.busi.vo.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.*;

/**
 * gis评测工具类
 *
 * @author aaly_lei
 */
@Slf4j
public class GisAssessUtilsNew {

    public static GisFormulaCal gisFormulaCalInstance = GisFormulaCal.getInstance();

    /**
     * gis的权重系数转为Map
     *
     * @param busiParamGisList
     * @return
     */
    public static Map<String, BusiParamTransformerVo> busiParamGisList2Map(List<BusiParamTransformerVo> busiParamGisList) {
        Map<String, BusiParamTransformerVo> resuMap = new HashMap<>();
        for (int i = 0; i < busiParamGisList.size(); i++) {
            resuMap.put(busiParamGisList.get(i).getName(), busiParamGisList.get(i));
        }
        return resuMap;
    }

//    /**
//     * String 转 BigDecimal
//     * @param str
//     * @return
//     */
//    public static BigDecimal str2BigDecimal(String str) {
//        BigDecimal val = createBigD("0");
//        if(null != str && !"".equals(str)) {
//            val = new BigDecimal(str).setScale(5,BigDecimal.ROUND_HALF_UP);
//        }
//        return val;
//    }

    /**
     * Long 转 BigDecimal
     *
     * @param value
     * @return
     */
    public static BigDecimal long2BigDecimal(Long value) {
        BigDecimal val = Convert.createBigD("0");
        if (null != value) {
            val = new BigDecimal(value.toString());
        }
        return val;
    }

    /**
     * 获取系数库  a值
     *
     * @param busiParamGisMap 系数库
     * @param key
     * @return
     */
    public static BigDecimal getaVal(Map<String, BusiParamTransformerVo> busiParamGisMap, String key) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiParamGisMap && null != busiParamGisMap.get(key)) {
            BusiParamTransformerVo busiParamGis = busiParamGisMap.get(key);
            val = busiParamGis.getAValue();
        }
        return val;
    }

    /**
     * 获取系数库  b值
     *
     * @param busiParamGisMap 系数库
     * @param key
     * @return
     */
    public static BigDecimal getbVal(Map<String, BusiParamTransformerVo> busiParamGisMap, String key) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiParamGisMap && null != busiParamGisMap.get(key)) {
            BusiParamTransformerVo busiParamGis = busiParamGisMap.get(key);
            val = busiParamGis.getBValue();
        }
        return val;
    }

    /**
     * 获取系数库  权重值
     *
     * @param busiParamGisMap 系数库
     * @param key
     * @return
     */
    public static BigDecimal getVal(Map<String, BusiParamTransformerVo> busiParamGisMap, String key) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiParamGisMap && null != busiParamGisMap.get(key)) {
            BusiParamTransformerVo busiParamGis = busiParamGisMap.get(key);
            val = busiParamGis.getValue();
        }
        return val;
    }

    public static BusiParamTransformerVo getVo(Map<String, BusiParamTransformerVo> busiParamGisMap, String key) {
        BigDecimal val = Convert.createBigD("0");
        BusiParamTransformerVo busiParamGis = new BusiParamTransformerVo();
        if (null != busiParamGisMap && null != busiParamGisMap.get(key)) {
            busiParamGis = busiParamGisMap.get(key);
        }
        return busiParamGis;
    }

    /**
     * 获取导电回路电阻
     *
     * @param busiOutlineGisExperimentdata 例行试验
     * @return
     */
    public static BigDecimal getDdhldz(BusiOutlineGisExperimentData busiOutlineGisExperimentdata) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiOutlineGisExperimentdata) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.getADdhlResistance()),
                    GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.getBDdhlResistance()),
                    GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.getCDdhlResistance()));
        }
        return val;
    }

    public static BigDecimal getDdhldzManual(Map<String, String> busiOutlineGisExperimentdata) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiOutlineGisExperimentdata) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_A)),
                    GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_B)),
                    GisAssessUtils.str2BigDecimal(busiOutlineGisExperimentdata.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_C)));
        }
        return val;
    }

    /**
     * 获取合闸不同期
     *
     * @param gisMechanical 获取GIS机械特性信息
     * @return
     */
    public static BigDecimal getHzbtq(GisMechanicalPropertiesVo gisMechanical) {
        BigDecimal val = Convert.createBigD("0");
        if (null != gisMechanical) {
            // 合闸不同期
            BigDecimal aHzBtq = Convert.str2BigDecimal(gisMechanical.getAHzBtq());
            // B相合闸不同期
            BigDecimal bHzBtq = Convert.str2BigDecimal(gisMechanical.getBHzBtq());
            // C相合闸不同期
            BigDecimal cHzBtq = Convert.str2BigDecimal(gisMechanical.getCHzBtq());
            val = gisFormulaCalInstance.compareABCMax(aHzBtq, bHzBtq, cHzBtq);
        }
        return val;
    }

    /**
     * 获取分闸不同期
     *
     * @param gisMechanical 获取GIS机械特性信息
     * @return
     */
    public static BigDecimal getFzbtq(GisMechanicalPropertiesVo gisMechanical) {
        BigDecimal val = Convert.createBigD("0");
        if (null != gisMechanical) {
            // 分闸不同期
            BigDecimal aFzBtq = Convert.str2BigDecimal(gisMechanical.getAFzBtq());
            // B相分闸不同期
            BigDecimal bHzBtq = Convert.str2BigDecimal(gisMechanical.getBFzBtq());
            // C相分闸不同期
            BigDecimal cFzBtq = Convert.str2BigDecimal(gisMechanical.getCFzBtq());
            val = gisFormulaCalInstance.compareABCMax(aFzBtq, bHzBtq, cFzBtq);
        }
        return val;
    }

    /**
     * 获取刚分速度
     *
     * @param gisMechanical 获取GIS机械特性信息
     * @return
     */
    public static BigDecimal getGfSpeend(GisMechanicalPropertiesVo gisMechanical) {
        BigDecimal val = Convert.createBigD("0");
        if (null != gisMechanical) {
            // A相刚分速度
            BigDecimal aGfSpeend = Convert.str2BigDecimal(gisMechanical.getAGfSpeend());
            // B相刚分速度
            BigDecimal bGfSpeend = Convert.str2BigDecimal(gisMechanical.getBGfSpeend());
            // C相刚分速度
            BigDecimal cGfSpeend = Convert.str2BigDecimal(gisMechanical.getCGfSpeend());
            val = gisFormulaCalInstance.compareABCMax(aGfSpeend, bGfSpeend, cGfSpeend);
        }
        return val;
    }

    /**
     * 获取刚和速度
     *
     * @param gisMechanical 获取GIS机械特性信息
     * @return
     */
    public static BigDecimal getGhSpeend(GisMechanicalPropertiesVo gisMechanical) {
        BigDecimal val = Convert.createBigD("0");
        if (null != gisMechanical) {
            // A相刚和速度
            BigDecimal aGhSpeed = Convert.str2BigDecimal(gisMechanical.getAGhSpeed());
            // B相刚和速度
            BigDecimal bGhSpeed = Convert.str2BigDecimal(gisMechanical.getBGhSpeed());
            // C相刚和速度
            BigDecimal cGfSpeend = Convert.str2BigDecimal(gisMechanical.getCGhSpeed());
            val = gisFormulaCalInstance.compareABCMax(aGhSpeed, bGhSpeed, cGfSpeend);
        }
        return val;
    }

    public static BigDecimal getABCMax(String aValue, String bValue, String cValue) {
        BigDecimal aResult = new BigDecimal(0);
        BigDecimal bResult = new BigDecimal(0);
        BigDecimal cResult = new BigDecimal(0);
        if (StrUtil.isNotBlank(aValue)) {
            aResult = Convert.str2BigDecimal(aValue);
        }
        if (StrUtil.isNotBlank(bValue)) {
            bResult = Convert.str2BigDecimal(bValue);
        }
        if (StrUtil.isNotBlank(cValue)) {
            cResult = Convert.str2BigDecimal(cValue);
        }
        return gisFormulaCalInstance.compareABCMax(aResult, bResult, cResult);
    }

    /**
     * 获取ABC三相最小值
     *
     * @param aValue A相导电回路电阻(其他)
     * @param bValue B相导电回路电阻(其他)
     * @param cValue C相导电回路电阻(其他)
     * @return BigDecimal
     */
    public static BigDecimal getABCMin(String aValue, String bValue, String cValue) {
        BigDecimal aResult = new BigDecimal(0);
        BigDecimal bResult = new BigDecimal(0);
        BigDecimal cResult = new BigDecimal(0);
        if (StrUtil.isNotBlank(aValue)) {
            aResult = Convert.str2BigDecimal(aValue);
        }
        if (StrUtil.isNotBlank(bValue)) {
            bResult = Convert.str2BigDecimal(bValue);
        }
        if (StrUtil.isNotBlank(cValue)) {
            cResult = Convert.str2BigDecimal(cValue);
        }
        return gisFormulaCalInstance.compareMinABC(aResult, bResult, cResult);
    }


    /**
     * 获取气体泄漏数据
     * 取所有在线监测SF6的数据（气体压力值），计算比较出每个监测点气体泄露率/天，要<0.1%,超过此值，我们
     * 认为有绝缘问题，要加以注意。（有不同温度的，全部折算到同一的温度下，气体的压力是多少。20度），同时选
     * 出一个压力最不足的监测数据，作为计算使用的数据。所有开关注意值=0.5，道闸、套管、注意值=0.44，主道
     * 闸权重=0.35，其他权重均分。每个监测点评估一次，合计评价。
     *
     * @return
     */
    public static BigDecimal getQtxl(List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses) {
        JSONObject jsonObject = getVal(rptGisMonitorSf6Chartses);
        BigDecimal aVal = (BigDecimal) jsonObject.get("aVal");
        BigDecimal bVal = (BigDecimal) jsonObject.get("bVal");
        BigDecimal cVal = (BigDecimal) jsonObject.get("cVal");
        return gisFormulaCalInstance.compareMinABC(aVal, bVal, cVal);
    }

    public static JSONObject getVal(List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses) {
        BigDecimal aVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartA = getNewRptGisMonitorSf6Charts("A相", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartA) {
            aVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartA.getPressure());
        }

        BigDecimal bVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartB = getNewRptGisMonitorSf6Charts("B相", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartB) {
            bVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartB.getPressure());
        }

        BigDecimal cVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartC = getNewRptGisMonitorSf6Charts("C相", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartC) {
            cVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartC.getPressure());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aVal", aVal);
        jsonObject.put("bVal", bVal);
        jsonObject.put("cVal", cVal);
        return jsonObject;
    }

    /**
     * 微水含量-灭弧室（0.6），其他气室（0.4）
     * 选出微水含量最大的一个数据，作为计算数据。
     *
     * @param rptGisMonitorSf6Chartses GIS  SF6压力微水温度-统计数据
     * @return
     */
    public static BigDecimal getWshl(List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses) {
        JSONObject jsonObject = getWshlVal(rptGisMonitorSf6Chartses);
        BigDecimal aVal = (BigDecimal) jsonObject.get("aVal");
        BigDecimal bVal = (BigDecimal) jsonObject.get("bVal");
        BigDecimal cVal = (BigDecimal) jsonObject.get("cVal");
        return gisFormulaCalInstance.compareABCMax(aVal, bVal, cVal);
    }

    public static JSONObject getWshlVal(List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses) {
        BigDecimal aVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartA = getNewRptGisMonitorSf6Charts("A", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartA) {
            aVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartA.getMoisture());
        }

        BigDecimal bVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartB = getNewRptGisMonitorSf6Charts("B", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartB) {
            bVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartB.getMoisture());
        }

        BigDecimal cVal = Convert.createBigD("0");
        RptGisMonitorSf6Charts rptGisMonitorSf6ChartC = getNewRptGisMonitorSf6Charts("C", rptGisMonitorSf6Chartses);
        if (null != rptGisMonitorSf6ChartC) {
            cVal = Convert.str2BigDecimal(rptGisMonitorSf6ChartC.getMoisture());
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aVal", aVal);
        jsonObject.put("bVal", bVal);
        jsonObject.put("cVal", cVal);
        return jsonObject;
    }

    /**
     * 获取A、B、C三相中最新的SF6数据
     *
     * @param position
     * @param rptGisMonitorSf6Chartses
     * @return
     */
    private static RptGisMonitorSf6Charts getNewRptGisMonitorSf6Charts(String position, List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses) {
        RptGisMonitorSf6Charts rptGisMonitorSf6Charts = null;
        if (CollectionUtil.isNotEmpty(rptGisMonitorSf6Chartses)) {
            for (RptGisMonitorSf6Charts gisMonitorSf6Charts : rptGisMonitorSf6Chartses) {
                rptGisMonitorSf6Charts = gisMonitorSf6Charts;
                if (Objects.equals(position, rptGisMonitorSf6Charts.getPosition())) {
                    rptGisMonitorSf6Charts = gisMonitorSf6Charts;
                    break;
                }
            }
        }
        return rptGisMonitorSf6Charts;
    }

    /**
     * 局放量
     *
     * @param transformerGisPdStatistics 变压器/GIS 局部放电统计
     * @return
     */
    public static BigDecimal getJfl(List<TransformerPdStatistics> transformerGisPdStatistics) {
        BigDecimal val = Convert.createBigD("0");
        if (CollectionUtil.isNotEmpty(transformerGisPdStatistics)) {
            val = Convert.str2BigDecimal(transformerGisPdStatistics.get(0).getElectricQuantity());
        }
        return val;
    }

    /**
     * 局放次数
     *
     * @param transformerGisPdStatistics transformerGisPdStatistics 变压器/GIS 局部放电统计
     * @return
     */
    public static BigDecimal getJfcs(List<TransformerPdStatistics> transformerGisPdStatistics) {
        BigDecimal val = Convert.createBigD("0");
        if (CollectionUtil.isNotEmpty(transformerGisPdStatistics)) {
            val = Convert.str2BigDecimal(transformerGisPdStatistics.get(0).getCount().toString());
        }
        return val;
    }

    /**
     * 局放次数
     *
     * @param count
     * @return
     */
    public static BigDecimal getJfcs(String count) {
        BigDecimal val = Convert.createBigD("0");
        if (StrUtil.isNotEmpty(count)) {
            val = Convert.str2BigDecimal(count);
        }
        return val;
    }

    /**
     * 累计开断次数
     *
     * @param rptGisMonitor GIS开关--断路器(机械特性)信息
     * @return
     */
    public static BigDecimal getLjdkcs(RptGisMonitor rptGisMonitor) {
        BigDecimal val = Convert.createBigD("0");
        if (null != rptGisMonitor) {
            val = long2BigDecimal(rptGisMonitor.getTurnoffCount());
        }
        return val;
    }

    /**
     * 触头额定断开电流
     *
     * @param busiOfflineGisBrandVo 标牌信息
     * @return
     */
    public static BigDecimal getEddkdl(BusiOfflineGisBrandVo busiOfflineGisBrandVo) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiOfflineGisBrandVo) {
            val = Convert.str2BigDecimal(busiOfflineGisBrandVo.getElectricCurrent());
        }
        return val;
    }

    /**
     * 触头累计磨损率(7天)
     *
     * @param iecBreakerParams 触头所有断开记录（断开一次会有一条断开记录）
     * @param eddkdl           额定断开电流
     * @return
     */
    public static BigDecimal getMslsForFh(List<IecBreakerParam> iecBreakerParams, BigDecimal eddkdl) {
        BigDecimal val = Convert.createBigD("0");
        IecBreakerParam iecBreakerParam;
        // 负荷
        BigDecimal fh;
        // 触头可断开次数
        BigDecimal n;
        // 触头断开一次的磨损量
        BigDecimal thisMsl;
        if (CollectionUtil.isNotEmpty(iecBreakerParams)) {
            for (int i = 0; i < iecBreakerParams.size(); i++) {
                iecBreakerParam = iecBreakerParams.get(i);
                BigDecimal abortCurrent = new BigDecimal(iecBreakerParam.getAbortCurrent()).setScale(5, BigDecimal.ROUND_HALF_UP);
                fh = gisFormulaCalInstance.getFh(abortCurrent, eddkdl);

                log.info("Ib断开时的电流A：" + abortCurrent);
                log.info("In触头额定电流A：" + eddkdl);
                log.info("P负荷(%)：" + fh + ",计算公式：(Ib/In)*100% =(" + abortCurrent + "/" + eddkdl + ")*100% = " + fh);
                log.info("本次负荷(%)：" + fh + ",  断开电流(A)：" + abortCurrent + ",  额定断开电流（A）：" + eddkdl);

                BigDecimal tmpCurrent = abortCurrent.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
                BigDecimal tmpEddkdl = eddkdl.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
                //公式计算
                n = gisFormulaCalInstance.getEdKdkcs(tmpCurrent, tmpEddkdl, fh);
                thisMsl = gisFormulaCalInstance.getMslForFh(fh, n);

                log.info("当前断开电流(A)：" + iecBreakerParam.getAbortCurrent() + "， 触头相对断磨损量C:" + thisMsl);

                val = val.add(thisMsl);
            }
        }
        return val;
    }

    /**
     * 半自动-触头累计磨损率(7天)
     * @param eddkdl
     * @param latestSemiAutomaticEvaluateData
     * @return
     */
    public static BigDecimal getSemiAutoMslsForFh(BigDecimal eddkdl,BusiSemiAutomaticEvaluateDataVo latestSemiAutomaticEvaluateData) {
        BigDecimal val = Convert.createBigD("0");
        if(ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)){

            BigDecimal abortCurrent = new BigDecimal(latestSemiAutomaticEvaluateData.getCurrent()).setScale(5, BigDecimal.ROUND_HALF_UP);
            // 负荷
            BigDecimal fh = gisFormulaCalInstance.getFh(abortCurrent, eddkdl);

            log.info("Ib断开时的电流A：" + abortCurrent);
            log.info("In触头额定电流A：" + eddkdl);
            log.info("P负荷(%)：" + fh + ",计算公式：(Ib/In)*100% =(" + abortCurrent + "/" + eddkdl + ")*100% = " + fh);
            log.info("本次负荷(%)：" + fh + ",  断开电流(A)：" + abortCurrent + ",  额定断开电流（A）：" + eddkdl);

            BigDecimal tmpCurrent = abortCurrent.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
            BigDecimal tmpEddkdl = eddkdl.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
            //公式计算
            // 触头可断开次数
            BigDecimal n = gisFormulaCalInstance.getEdKdkcs(tmpCurrent, tmpEddkdl, fh);
            // 触头断开一次的磨损量
            BigDecimal thisMsl = gisFormulaCalInstance.getMslForFh(fh, n);

            log.info("当前断开电流(A)：" + latestSemiAutomaticEvaluateData.getCurrent() + "， 触头相对断磨损量C:" + thisMsl);

            val = thisMsl.multiply(latestSemiAutomaticEvaluateData.getBreakTimes());
        }
        return val;
    }

    /**
     * 触头累计磨损率(7天)
     *
     * @param abortCurrents 触头所有断开记录（断开一次会有一条断开记录）
     * @param eddkdl        额定断开电流
     * @return
     */
    public static BigDecimal getMslsForFh(String abortCurrents, BigDecimal eddkdl) {
        BigDecimal val = Convert.createBigD("0");
        // 负荷
        BigDecimal fh;
        // 触头可断开次数
        BigDecimal n;
        // 触头断开一次的磨损量
        BigDecimal thisMsl;
        if (StrUtil.isNotEmpty(abortCurrents)) {
            BigDecimal abortCurrent = new BigDecimal(abortCurrents).setScale(5, BigDecimal.ROUND_HALF_UP);
            fh = gisFormulaCalInstance.getFh(abortCurrent, eddkdl);

            log.info("Ib断开时的电流A：" + abortCurrent);
            log.info("In触头额定电流A：" + eddkdl);
            log.info("P负荷(%)：" + fh + ",计算公式：(Ib/In)*100% =(" + abortCurrent + "/" + eddkdl + ")*100% = " + fh);
            log.info("本次负荷(%)：" + fh + ",  断开电流(A)：" + abortCurrent + ",  额定断开电流（A）：" + eddkdl);

            BigDecimal tmpCurrent = abortCurrent.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
            BigDecimal tmpEddkdl = eddkdl.divide(new BigDecimal("1000"), 4, BigDecimal.ROUND_CEILING);
            //公式计算
            n = gisFormulaCalInstance.getEdKdkcs(tmpCurrent, tmpEddkdl, fh);
            thisMsl = gisFormulaCalInstance.getMslForFh(fh, n);

            log.info("当前断开电流(A)：" + abortCurrents + "， 触头相对断磨损量C:" + thisMsl);
            log.info("当前断开电流(A)：" + abortCurrents + "， 触头相对断磨损量C:" + thisMsl);
            log.info("当前断开电流(A)：" + abortCurrents + "， 触头相对断磨损量C:" + thisMsl);

            val = val.add(thisMsl);
        }
        return val;
    }

    /**
     * 100%负荷的断开次数
     *
     * @param abortCurrent 触头所有断开记录
     * @param eddkdl       额定断开电流
     * @return
     */
    public static BigDecimal getDkcsesFor100(String abortCurrent, BigDecimal eddkdl) {
        BigDecimal fh;
        BigDecimal dkcs;
        BigDecimal val = new BigDecimal("0");
        if (StrUtil.isNotEmpty(abortCurrent)) {
            log.info("断开电流(A):" + abortCurrent + " 额定断开电流A(标牌):" + eddkdl);
            fh = gisFormulaCalInstance.getFh(Convert.str2BigDecimal(abortCurrent), eddkdl);
            dkcs = gisFormulaCalInstance.getDkcsFor100(fh);
            val = val.add(dkcs);
        }
        return val;
    }

    /**
     * 100%负荷的断开次数
     *
     * @param iecBreakerParams 触头所有断开记录
     * @param eddkdl           额定断开电流
     * @return
     */
    public static BigDecimal getDkcsesFor100(List<IecBreakerParam> iecBreakerParams, BigDecimal eddkdl) {
        BigDecimal fh;
        BigDecimal dkcs;
        IecBreakerParam iecBreakerParam;
        BigDecimal val = new BigDecimal("0");
        if (CollectionUtil.isNotEmpty(iecBreakerParams)) {
            for (int i = 0; i < iecBreakerParams.size(); i++) {
                iecBreakerParam = iecBreakerParams.get(i);
                log.info("断开电流(A):" + iecBreakerParam.getAbortCurrent() + " 额定断开电流A(标牌):" + eddkdl);
                fh = gisFormulaCalInstance.getFh(Convert.str2BigDecimal(iecBreakerParam.getAbortCurrent()), eddkdl);
                dkcs = gisFormulaCalInstance.getDkcsFor100(fh);
                val = val.add(dkcs);
            }
        }
        log.info("累计断开次数：" + iecBreakerParams.size() + "次， 合计：" + val);
        return val;
    }


    /**
     * 半自动-100%负荷的断开次数
     * @param eddkdl
     * @param latestSemiAutomaticEvaluateData
     * @return
     */
    public static BigDecimal getSemiAutoDkcsesFor100(BigDecimal eddkdl, BusiSemiAutomaticEvaluateDataVo latestSemiAutomaticEvaluateData) {
        BigDecimal val = new BigDecimal("0");
        if (ObjectUtil.isNotEmpty(latestSemiAutomaticEvaluateData)) {
            String dkdl = latestSemiAutomaticEvaluateData.getCurrent();
            log.info("断开电流(A):" + dkdl + " 额定断开电流A(标牌):" + eddkdl);
            BigDecimal fh = gisFormulaCalInstance.getFh(Convert.str2BigDecimal(dkdl), eddkdl);
            BigDecimal dkcs = gisFormulaCalInstance.getDkcsFor100(fh);
            val = dkcs.multiply(latestSemiAutomaticEvaluateData.getBreakTimes());
        }
        return val;
    }

    /**
     * 使用年限
     *
     * @param busiOfflineGisBrand GIS 离线标牌数据
     * @return
     */
    public static BigDecimal getSynx(BusiOfflineGisBrandVo busiOfflineGisBrand) {
        BigDecimal val = Convert.createBigD("0");
        if (null != busiOfflineGisBrand && null != busiOfflineGisBrand.getTransportTime()) {
            Date startDate = Date.from(busiOfflineGisBrand.getTransportTime().atZone(ZoneId.systemDefault()).toInstant());
            Date now = new Date();
            long day = timeDifference(startDate, now);
            val = new BigDecimal(day).divide(new BigDecimal("365"), 5, BigDecimal.ROUND_HALF_UP);
        }
        return val;
    }

    /**
     * 使用环境温度
     *
     * @param substationMicGis 变电站微气象暂存数据
     * @return
     */
    public static BigDecimal getSyhjwd(SubstationMicGisVo substationMicGis) {
        BigDecimal val = Convert.createBigD("0");
        if (null != substationMicGis) {
            val = substationMicGis.getAirTemperature();
        }
        return val;
    }

    /**
     * 使用环境湿度
     *
     * @param eqmCode
     * @param eqmId
     * @return
     */
    public static BigDecimal getSyhjsd(String eqmCode, String eqmId, SubstationMicGisVo substationMicGis) {
        BigDecimal val = Convert.createBigD("0");
        if (null != substationMicGis) {
            val = substationMicGis.getHumidity();
        }
        return val;
    }

    /**
     * 尘埃、腐蚀
     *
     * @param gisRepair 检修记录
     * @return
     */
    public static BigDecimal getCafs(BusiOfflineGisRepairVo gisRepair) {
        BigDecimal val = Convert.createBigD("1");
        if (null != gisRepair) {
            val = Convert.str2BigDecimal(gisRepair.getDustCorrosion());
        }
        return val;
    }

    /**
     * 尘埃、腐蚀
     *
     * @param environmentCoefficient
     * @return
     */
    public static BigDecimal getCafs(String environmentCoefficient) {
        BigDecimal val = Convert.createBigD("1");
        if (StrUtil.isNotEmpty(environmentCoefficient)) {
            val = Convert.str2BigDecimal(environmentCoefficient);
        }
        return val;
    }

    /**
     * 其他因素
     *
     * @param busiOfflineGisBrand
     * @return
     */
    public static BigDecimal getQt(BusiOfflineGisBrandVo busiOfflineGisBrand) {
        BigDecimal val = Convert.createBigD("1");
        if (null != busiOfflineGisBrand) {
            val = Convert.str2BigDecimal(busiOfflineGisBrand.getYxEnvironment());
        }
        return val;
    }

    /**
     * 操作机构润滑
     *
     * @param list
     * @return
     */
    public static BigDecimal getCzjgrh(List<BusiOfflineGisRepairVo> list, net.sf.json.JSONArray jsonArray) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (BusiOfflineGisRepairVo busiOfflineGisRepairVo : list) {
                if (null != busiOfflineGisRepairVo.getCzjgLubrication() && !"".equals(busiOfflineGisRepairVo.getCzjgLubrication())) {
                    val.add(Convert.str2BigDecimal(busiOfflineGisRepairVo.getCzjgLubrication()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
            setStep(jsonArray, 49, "操作机构润滑计算", String.valueOf(val), String.valueOf(val));
        }
        return val;
    }

    /**
     * 操作机构润滑
     *
     * @param list
     * @return
     */
    public static BigDecimal getCzjgrh(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getCzjgLubrication() && !"".equals(list.get(i).getCzjgLubrication())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getCzjgLubrication()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
        }
        return val;
    }

    /**
     * 根据外观系数读表查询结果数据
     *
     * @param value
     * @return
     */
    public static String getValFromString(String value) {
        BigDecimal val = Convert.createBigD("1");
        if (StrUtil.isNotEmpty(value)) {
            val = gisFormulaCalInstance.getWgxs(Convert.str2BigDecimal(value));
        }
        return val.toString();
    }

    /**
     * 主体、机构外观
     *
     * @param list
     * @return
     */
    public static BigDecimal getZtjgwg(List<BusiOfflineGisRepairVo> list, net.sf.json.JSONArray jsonArray) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (BusiOfflineGisRepairVo busiOfflineGisRepairVo : list) {
                if (null != busiOfflineGisRepairVo.getZtjgAppearance() && !"".equals(busiOfflineGisRepairVo.getZtjgAppearance())) {
                    val.add(Convert.str2BigDecimal(busiOfflineGisRepairVo.getZtjgAppearance()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
            setStep(jsonArray, 50, "主体、机构外观计算", String.valueOf(val), String.valueOf(val));
        }
        return val;
    }

    /**
     * 主体、机构外观
     *
     * @param list
     * @return
     */
    public static BigDecimal getZtjgwg(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getZtjgAppearance() && !"".equals(list.get(i).getZtjgAppearance())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getZtjgAppearance()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
        }
        return val;
    }

    /**
     * 支持绝缘部分
     *
     * @param list
     * @return
     */
    public static BigDecimal getZcjybf(List<BusiOfflineGisRepairVo> list, net.sf.json.JSONArray jsonArray) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getSupportInsulation() && !"".equals(list.get(i).getSupportInsulation())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getSupportInsulation()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
            setStep(jsonArray, 51, "支持绝缘部分计算", String.valueOf(val), String.valueOf(val));
        }
        return val;
    }

    /**
     * 支持绝缘部分
     *
     * @param list
     * @return
     */
    public static BigDecimal getZcjybf(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getSupportInsulation() && !"".equals(list.get(i).getSupportInsulation())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getSupportInsulation()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
        }
        return val;
    }

    /**
     * 主回路端子
     *
     * @param list
     * @return
     */
    public static BigDecimal getZhldz(List<BusiOfflineGisRepairVo> list, JSONArray jsonArray) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getMainLoopTerminal() && !"".equals(list.get(i).getMainLoopTerminal())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getMainLoopTerminal()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
            setStep(jsonArray, 52, "主回路端子计算", String.valueOf(val), String.valueOf(val));
        }
        return val;
    }

    /**
     * 主回路端子
     *
     * @param list
     * @return
     */
    public static BigDecimal getZhldz(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getMainLoopTerminal() && !"".equals(list.get(i).getMainLoopTerminal())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getMainLoopTerminal()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
        }
        return val;
    }

    /**
     * 同类断路器维护次数(检修记录)
     *
     * @param list 检修记录
     * @return
     */
    public static BigDecimal getTldlqwhcs(List<BusiOfflineGisRepairVo> list, JSONArray jsonArray) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getTldlqMaintain() && !"".equals(list.get(i).getTldlqMaintain())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getTldlqMaintain()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
            setStep(jsonArray, 53, "同类断路器维护次数(检修记录)计算", String.valueOf(val), String.valueOf(val));
        }
        return val;
    }

    /**
     * 同类断路器维护次数(检修记录)
     *
     * @param list 检修记录
     * @return
     */
    public static BigDecimal getTldlqwhcs(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (null != list.get(i).getTldlqMaintain() && !"".equals(list.get(i).getTldlqMaintain())) {
                    val.add(Convert.str2BigDecimal(list.get(i).getTldlqMaintain()));
                }
            }
            val = gisFormulaCalInstance.getWgxs(val);
        }
        return val;
    }

    /**
     * 同类断路器维护次数(检修记录)
     *
     * @param circuitBreakerMaintenanceTime
     * @return
     */
    public static BigDecimal getTldlqwhcs(String circuitBreakerMaintenanceTime) {
        BigDecimal val = Convert.createBigD("1");
        if (StrUtil.isNotEmpty(circuitBreakerMaintenanceTime)) {
            val = gisFormulaCalInstance.getWgxs(Convert.str2BigDecimal(circuitBreakerMaintenanceTime));
        }
        return val;
    }

    /**
     * 断路器维护
     *
     * @return
     */
    public static BigDecimal getDlqwh(int maintenanceTime) {
        return new BigDecimal(Math.pow(0.9, maintenanceTime)).setScale(5, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 断路器维护
     *
     * @param list
     * @return
     */
    public static BigDecimal getDlqwh(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = Convert.createBigD("1");
        if (null != list && list.size() > 0) {
            val = new BigDecimal(Math.pow(0.9, 25)).setScale(5, BigDecimal.ROUND_HALF_UP);
        }
        return val;
    }

    /**
     * 厂家设备遗传
     *
     * @param list 检修记录
     * @return
     */
    public static BigDecimal getCjyc(List<BusiOfflineGisRepairVo> list) {
        BigDecimal val = new BigDecimal("1");
        BigDecimal valFz = new BigDecimal("1");//分子
        if (null != list) {
            val = valFz.subtract(valFz.divide(new BigDecimal(Math.pow(3, list.size())), 5, BigDecimal.ROUND_HALF_UP));
        }
        return val;
    }

    /**
     * 厂家设备遗传
     *
     * @param maintenanceTime
     * @return
     */
    public static BigDecimal getCjyc(int maintenanceTime) {
        BigDecimal valFz = new BigDecimal("1");//分子
        BigDecimal val = valFz.subtract(valFz.divide(new BigDecimal(Math.pow(3, maintenanceTime)), 5, BigDecimal.ROUND_HALF_UP));
        return val;
    }

    /**
     * 产品成熟度
     *
     * @param busiOfflineGisBrand
     * @return
     */
    public static BigDecimal getCpcsd(BusiOfflineGisBrandVo busiOfflineGisBrand, JSONArray jsonArray) {
        BigDecimal val = new BigDecimal("1");
        if (null != busiOfflineGisBrand) {
            String productMaturity = busiOfflineGisBrand.getProductMaturity();
            if (!StringUtils.isEmpty(productMaturity)) {
                val = gisFormulaCalInstance.getCalMaturity(productMaturity);
            }
        }
        return val;
    }

    /**
     * 产品成熟度
     *
     * @param busiOfflineGisBrand
     * @return
     */
    public static BigDecimal getCpcsd(BusiOfflineGisBrandVo busiOfflineGisBrand) {
        BigDecimal val = new BigDecimal("1");
        if (null != busiOfflineGisBrand) {
            String productMaturity = busiOfflineGisBrand.getProductMaturity();
            if (!StringUtils.isEmpty(productMaturity)) {
                val = gisFormulaCalInstance.getCalMaturity(productMaturity);
            }
        }
        return val;
    }

    /**
     * 知名度（手动录入）标牌
     *
     * @param busiOfflineGisBrand
     * @return
     */
    public static BigDecimal getZmd(BusiOfflineGisBrandVo busiOfflineGisBrand) {
        BigDecimal val = new BigDecimal("1");
        if (null != busiOfflineGisBrand) {
            val = Convert.str2BigDecimal(busiOfflineGisBrand.getPopularity());
        }
        return val;
    }

    /**
     * 获取两个时间的相差天数
     *
     * @return
     */
    public static long timeDifference(Date start, Date end) {
        long between = end.getTime() - start.getTime();
        long day = between / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
     * 数字类型格式化
     *
     * @param val
     * @return
     */
    public static BigDecimal formatNum(BigDecimal val) {
        val = val.setScale(5, BigDecimal.ROUND_HALF_UP);
        return val;
    }

    /**
     * 字符串数字类型格式化
     *
     * @param valStr
     * @return
     */
    public static BigDecimal formatNumStr(String valStr) {
        BigDecimal val = Convert.str2BigDecimal(valStr);
        return formatNum(val);
    }

    /**
     * 放电次数计算
     *
     * @param v1
     * @param v2
     * @param v3
     * @param v4
     * @param v5
     * @return
     */
    public static BigDecimal calcfdcs(BigDecimal v1, BigDecimal v2, BigDecimal v3, BigDecimal v4, BigDecimal v5) {
        BigDecimal hzbtq = v1.multiply(v2).setScale(4, BigDecimal.ROUND_HALF_UP);
        BigDecimal fzbtq = v3.multiply(v4).setScale(4, BigDecimal.ROUND_HALF_UP);
        BigDecimal tmp = hzbtq.add(fzbtq);
        return tmp.multiply(v5).setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal calcMultiply(BigDecimal v1, BigDecimal v2) {
        return v1.multiply(v2).setScale(4, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算步骤
     *
     * @param jsonArray
     * @param stepId          步骤编号->第几步
     * @param stepName        每个步骤名称
     * @param inputParam      入参
     * @param calculateResult 计算结果
     */
    public static void setStep(net.sf.json.JSONArray jsonArray, int stepId, String stepName, String inputParam, String calculateResult) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stepId", stepId);
        jsonObject.put("stepName", stepName);
        jsonObject.put("inputParam", inputParam);
        jsonObject.put("calculateResult", calculateResult);
        jsonArray.add(jsonObject);
    }

    /**
     * 计算刚分合速度ABC最大值
     *
     * @param obj
     * @param jsonObject
     * @return
     */
    public static String compareABCMax(Object obj, com.alibaba.fastjson.JSONObject jsonObject, String type) {
        BigDecimal a = null, b = null, c = null;
        if (obj instanceof GisMechanicalPropertiesVo) {
            switch (type) {
                case "0":
                    // A相刚分速度
                    a = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getAGfSpeend());
                    // B相刚分速度
                    b = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getBGfSpeend());
                    // C相刚分速度
                    c = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getCGfSpeend());
                    break;
                case "1":
                    // A相刚和速度
                    a = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getAGhSpeed());
                    // B相刚和速度
                    b = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getBGhSpeed());
                    // C相刚和速度
                    c = Convert.str2BigDecimal(((GisMechanicalPropertiesVo) obj).getCGhSpeed());
                    break;
                default:
                    throw new IllegalStateException("计算ABC最大值 value: " + type);
            }
        }
        jsonObject.clear();
        jsonObject.put("aSpeed", a);
        jsonObject.put("bSpeed", b);
        jsonObject.put("cSpeend", c);
        return jsonObject.toString();
    }

    /**
     * 升降计算
     *
     * @param jsonObject
     * @param v
     * @param upAndDown
     * @param aVal
     * @param bVal
     * @return
     */
    public static String formulaForUpAndDown(com.alibaba.fastjson.JSONObject jsonObject, BigDecimal v, String upAndDown, BigDecimal aVal, BigDecimal bVal) {
        jsonObject.clear();
        jsonObject.put("v", v);
        jsonObject.put("upAndDown", upAndDown);
        jsonObject.put("aVal", aVal);
        jsonObject.put("bVal", bVal);
        return jsonObject.toJSONString();
    }

    /**
     * 导电回路电阻计算
     *
     * @return
     */
    public static String calcValue(com.alibaba.fastjson.JSONObject jsonObject, BigDecimal v1, BigDecimal v2, BigDecimal v3) {
        jsonObject.clear();
        jsonObject.put("v1", v1);
        jsonObject.put("v2", v2);
        jsonObject.put("v3", v3);
        return jsonObject.toString();
    }

    public static BigDecimal getInsulationResistanceResult(Map<String, String> map) {
        BigDecimal val = Convert.createBigD("0");
        if (!CollectionUtils.isEmpty(map)) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(map.get("insulationResistanceA")), GisAssessUtils.str2BigDecimal(map.get("insulationResistanceB")), GisAssessUtils.str2BigDecimal(map.get("insulationResistanceC")));
        }
        return val;
    }

    public static BigDecimal getDielectricLossResult(Map<String, String> map) {
        BigDecimal val = Convert.createBigD("0");
        if (!CollectionUtils.isEmpty(map)) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(map.get("dielectricLossA")), GisAssessUtils.str2BigDecimal(map.get("dielectricLossB")), GisAssessUtils.str2BigDecimal(map.get("dielectricLossC")));
        }
        return val;
    }

    public static BigDecimal getElectricCapacityResult(Map<String, String> map) {
        BigDecimal val = Convert.createBigD("0");
        if (!CollectionUtils.isEmpty(map)) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(map.get("electricCapacityA")), GisAssessUtils.str2BigDecimal(map.get("electricCapacityB")), GisAssessUtils.str2BigDecimal(map.get("electricCapacityC")));
        }
        return val;
    }

    public static BigDecimal getClosingCoilResistanceResult(Map<String, String> map) {
        BigDecimal val = Convert.createBigD("0");
        if (!CollectionUtils.isEmpty(map)) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(map.get("closingCoilResistanceA")), GisAssessUtils.str2BigDecimal(map.get("closingCoilResistanceB")), GisAssessUtils.str2BigDecimal(map.get("closingCoilResistanceC")));
        }
        return val;
    }

    public static BigDecimal getOpeningCoilResistanceResult(Map<String, String> map) {
        BigDecimal val = Convert.createBigD("0");
        if (!CollectionUtils.isEmpty(map)) {
            val = gisFormulaCalInstance.compareABCMax(GisAssessUtils.str2BigDecimal(map.get("openingCoilResistanceA")), GisAssessUtils.str2BigDecimal(map.get("openingCoilResistanceB")), GisAssessUtils.str2BigDecimal(map.get("openingCoilResistanceC")));
        }
        return val;
    }
}

