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

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.huatek.busi.common.GisAssessEnum;
import com.huatek.busi.common.ParamEnum;
import com.huatek.busi.util.EvaluationNameUtil;
import com.huatek.busi.util.GisAssessUtilsNew;
import com.huatek.busi.util.GisCreateUtil;
import com.huatek.busi.vo.BusiParamTransformerVo;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class GisYXSJDQSYCalculate {

    /**
     * 自动、手动-运行数据-电气试验-全部采用离线数据
     *
     * @return
     */
    public static Map<String, String> calculateYXSJDQSY(Map<String, BusiParamTransformerVo> busiParamGisMap, Map<String, String> map, int step, String type) {
        // 运行数据-电气试验
        BusiParamTransformerVo yxsjDqsyVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATE_DATA_DQSY.getKey());
        // 电容量
        BusiParamTransformerVo electricCapacityVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_DRLANG.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_DRL_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_DRL_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_DRL_C.getKey())) &&
                "1".equals(electricCapacityVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_DRLANG.getValue() + "未安装或未采集到数据");
            return result;
        }
        String electricCapacityA = map.get(GisAssessEnum.YXSJ_DQSY_DRL_A.getKey());
        String electricCapacityB = map.get(GisAssessEnum.YXSJ_DQSY_DRL_B.getKey());
        String electricCapacityC = map.get(GisAssessEnum.YXSJ_DQSY_DRL_C.getKey());
        String electricCapacity = GisAssessUtilsNew.getABCMax(electricCapacityA, electricCapacityB, electricCapacityC).toString();
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(electricCapacity, yxsjDqsyVo, electricCapacityVo, step);
        BigDecimal electricCapacityResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));

        // 绝缘电阻
        BusiParamTransformerVo jydzVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_JYDZ.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_C.getKey())) &&
                "1".equals(jydzVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            result.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_JYDZ.getValue() + "未安装或未采集到数据");
            return result;
        }
        String jydzA = map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_A.getKey());
        String jydzB = map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_B.getKey());
        String jydzC = map.get(GisAssessEnum.YXSJ_DQSY_JYDZ_C.getKey());
        String jydz = GisAssessUtilsNew.getABCMax(jydzA, jydzB, jydzC).toString();
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(jydz, yxsjDqsyVo, jydzVo, step);
        BigDecimal jydzResult = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));

        // 介质损耗
        BusiParamTransformerVo dielectricLossVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_JZSH.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JZSH_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JZSH_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_JZSH_C.getKey())) &&
                "1".equals(dielectricLossVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            result.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_JZSH.getValue() + "未安装或未采集到数据");
            return result;
        }
        String dielectricLossA = map.get(GisAssessEnum.YXSJ_DQSY_JZSH_A.getKey());
        String dielectricLossB = map.get(GisAssessEnum.YXSJ_DQSY_JZSH_B.getKey());
        String dielectricLossC = map.get(GisAssessEnum.YXSJ_DQSY_JZSH_C.getKey());
        String dielectricLoss = GisAssessUtilsNew.getABCMax(dielectricLossA, dielectricLossB, dielectricLossC).toString();
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(dielectricLoss, yxsjDqsyVo, dielectricLossVo, step);
        BigDecimal dielectricLossResult = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        // 导电回路电阻
        BusiParamTransformerVo galvanicCircleVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_DDHLDZ.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_C.getKey())) &&
                "1".equals(galvanicCircleVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            result.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_DDHLDZ.getValue() + "未安装或未采集到数据");
            return result;
        }
        String galvanicCircleA = map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_A.getKey());
        String galvanicCircleB = map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_B.getKey());
        String galvanicCircleC = map.get(GisAssessEnum.CONDUCTIVE_LOOP_RESISTANCE_C.getKey());
        String galvanicCircle = GisAssessUtilsNew.getABCMax(galvanicCircleA, galvanicCircleB, galvanicCircleC).toString();
        Map<String, String> result4 = GisAssessmentCalculate.calculateResult(galvanicCircle, yxsjDqsyVo, galvanicCircleVo, step);
        BigDecimal galvanicCircleResult = new BigDecimal(result4.get("result"));
        step = Integer.parseInt(result4.get("step"));

        // 合闸线圈直流电阻
        BusiParamTransformerVo closingCoilResistanceVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_HZXQZLDZ.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_C.getKey())) &&
                "1".equals(closingCoilResistanceVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            result.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_HZXQZLDZ.getValue() + "未安装或未采集到数据");
            return result;
        }
        String closingCoilResistanceA = map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_A.getKey());
        String closingCoilResistanceB = map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_B.getKey());
        String closingCoilResistanceC = map.get(GisAssessEnum.YXSJ_DQSY_HZXQZLDZ_C.getKey());
        String closingCoilResistance = GisAssessUtilsNew.getABCMax(closingCoilResistanceA, closingCoilResistanceB, closingCoilResistanceC).toString();
        Map<String, String> result5 = GisAssessmentCalculate.calculateResult(closingCoilResistance, yxsjDqsyVo, closingCoilResistanceVo, step);
        BigDecimal closingCoilResistanceResult = new BigDecimal(result5.get("result"));
        step = Integer.parseInt(result5.get("step"));

        // 分闸线圈直流电阻
        BusiParamTransformerVo openingCoilResistanceVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GIS_DQSY_FZXQZLDZ.getKey());
        if ("2".equals(type) && StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_A.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_B.getKey())) &&
                StrUtil.isEmpty(map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_C.getKey())) &&
                "1".equals(openingCoilResistanceVo.getIsAttend())) {
            Map<String, String> result = new HashMap<>();
            result.put("warnMsg", "GIS评测-" + ParamEnum.GIS_DQSY_FZXQZLDZ.getValue() + "未安装或未采集到数据");
            return result;
        }
        String openingCoilResistanceA = map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_A.getKey());
        String openingCoilResistanceB = map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_B.getKey());
        String openingCoilResistanceC = map.get(GisAssessEnum.YXSJ_DQSY_FZXQZLDZ_C.getKey());
        String openingCoilResistance = GisAssessUtilsNew.getABCMax(openingCoilResistanceA, openingCoilResistanceB, openingCoilResistanceC).toString();
        Map<String, String> result6 = GisAssessmentCalculate.calculateResult(openingCoilResistance, yxsjDqsyVo, openingCoilResistanceVo, step);
        BigDecimal openingCoilResistanceResult = new BigDecimal(result6.get("result"));
        step = Integer.parseInt(result6.get("step"));

        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(yxsjDqsyVo.getIsAttend())) {
            finalResult = electricCapacityResult.add(jydzResult).add(dielectricLossResult).add(galvanicCircleResult).add(closingCoilResistanceResult).add(openingCoilResistanceResult).setScale(4, RoundingMode.HALF_UP);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(jydzVo.getIsAttend())) {
                jsonObject.put("绝缘电阻", jydzResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(dielectricLossVo.getIsAttend())) {
                jsonObject.put("介质损耗", dielectricLossResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(electricCapacityVo.getIsAttend())) {
                jsonObject.put("电容量", electricCapacityResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(galvanicCircleVo.getIsAttend())) {
                jsonObject.put("导电回路电阻", galvanicCircleResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(closingCoilResistanceVo.getIsAttend())) {
                jsonObject.put("合闸线圈直流电阻", closingCoilResistanceResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(openingCoilResistanceVo.getIsAttend())) {
                jsonObject.put("分闸线圈直流电阻", openingCoilResistanceResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("运行数据-电气试验-权重", yxsjDqsyVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, yxsjDqsyVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put(GisCreateUtil.OPERATION_PARAMETERS_DQSY, finalResult.toString());
        resultMap.put(GisCreateUtil.JYDZ, jydzResult.toString());
        resultMap.put(GisCreateUtil.Dielectric_Loss, dielectricLossResult.toString());
        resultMap.put(GisCreateUtil.Electric_Capacity, electricCapacityResult.toString());
        resultMap.put(GisCreateUtil.Galvanic_Circle, galvanicCircleResult.toString());
        resultMap.put(GisCreateUtil.Closing_Coil_Resistance, closingCoilResistanceResult.toString());
        resultMap.put(GisCreateUtil.Opening_Coil_Resistance, openingCoilResistanceResult.toString());
        resultMap.put("step", String.valueOf(step));
        // 运行数据-电气试验
        return resultMap;
    }
}
