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

import cn.hutool.core.collection.CollectionUtil;
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.BusiOfflineGisBrandVo;
import com.huatek.busi.vo.BusiOfflineGisRepairVo;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.SubstationMicGisVo;
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 GisYXCSCalculate {

    /**
     * 手动-运行参数
     *
     * @return
     */
    public static Map<String, String> calculateManualYXCS(Map<String, BusiParamTransformerVo> busiParamGisMap, Map<String, String> map, BusiOfflineGisBrandVo vo, int step) {
        // 环境温度 OPERATING_AMBIENT_TEMPERATURE
        String temperature = map.get(GisAssessEnum.OPERATING_AMBIENT_TEMPERATURE.getKey());
        // 环境湿度 OPERATE_AMBIENT_HUMIDITY
        String humidity = map.get(GisAssessEnum.OPERATE_AMBIENT_HUMIDITY.getKey());
        // 尘埃腐蚀 ENVIRONMENT_COEFFICIENT
        String environment = map.get(GisAssessEnum.ENVIRONMENT_COEFFICIENT.getKey());
        // 操作机构润滑 OPERATE_MECHANISM_LUBRICATION
        String operateMechanismLubrication = map.get(GisAssessEnum.OPERATE_MECHANISM_LUBRICATION.getKey());
        // 主体、机构外观 MAIN_BODY_APPEARANCE
        String mainBodyAppearance = map.get(GisAssessEnum.MAIN_BODY_APPEARANCE.getKey());
        // 支持绝缘部分 SUPPORT_INSULATION_PART
        String supportInsulationPart = map.get(GisAssessEnum.SUPPORT_INSULATION_PART.getKey());
        // 主回路端子 MAIN_CIRCUIT_TERMINAL
        String mainCircuitTerminal = map.get(GisAssessEnum.MAIN_CIRCUIT_TERMINAL.getKey());
        // 检修次数 MAINTENANCE_TIME
        String maintenanceTime = map.get(GisAssessEnum.MAINTENANCE_TIME.getKey());
        //气压、日照、雨量
        String psrDou = map.get(GisAssessEnum.YXCS_GZHJ_QYRZYL.getKey());

        return calculateYXCSValue(busiParamGisMap, temperature, humidity, environment, operateMechanismLubrication, mainBodyAppearance, supportInsulationPart, mainCircuitTerminal, maintenanceTime, psrDou, vo, step);
    }

    /**
     * 自动-运行参数
     *
     * @return
     */
    public static Map<String, String> calculateAutoYXCS(Map<String, BusiParamTransformerVo> busiParamGisMap, SubstationMicGisVo substationMicGis, List<BusiOfflineGisRepairVo> gisRepair, Map<String, String> map, BusiOfflineGisBrandVo vo, int step) {
        // 环境温度 OPERATING_AMBIENT_TEMPERATURE
        if (StrUtil.isEmpty(substationMicGis.getAirTemperature().toString()) &&
                GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATING_AMBIENT_TEMPERATURE.getKey()).getIsAttend().equals("1")) {
            Map<String, String> result = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.OPERATING_AMBIENT_TEMPERATURE.getValue() + "未安装或未采集到数据");
            return result;
        }
        String temperature = substationMicGis.getAirTemperature() == null ? "0.00" : substationMicGis.getAirTemperature().toString();
        // 环境湿度 OPERATE_AMBIENT_HUMIDITY
        if (StrUtil.isEmpty(substationMicGis.getHumidity().toString()) &&
                GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATING_AMBIENT_HUMIDITY.getKey()).getIsAttend().equals("1")) {
            Map<String, String> result = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.OPERATING_AMBIENT_HUMIDITY.getValue() + "未安装或未采集到数据");
            return result;
        }
        String humidity = substationMicGis.getHumidity() == null ? "0.00" : substationMicGis.getHumidity().toString();
        // 尘埃腐蚀 ENVIRONMENT_COEFFICIENT
        String environment = "0.00";
        String operateMechanismLubrication = "0.00";
        String mainBodyAppearance = "0.00";
        String supportInsulationPart = "0.00";
        String mainCircuitTerminal = "0.00";
        String maintenanceTime = "0.00";
        if (CollectionUtil.isNotEmpty(gisRepair)) {
            // 尘埃腐蚀 ENVIRONMENT_COEFFICIENT
            environment = gisRepair.get(0).getDustCorrosion() == null ? "0.00" : gisRepair.get(0).getDustCorrosion();
            // 操作机构润滑 OPERATE_MECHANISM_LUBRICATION
            operateMechanismLubrication = gisRepair.stream().map(item -> new BigDecimal(item.getCzjgLubrication() == null ? "0.00" : item.getCzjgLubrication())).reduce(BigDecimal.ONE, BigDecimal::add).toString();
            // 主体、机构外观 MAIN_BODY_APPEARANCE
            mainBodyAppearance = gisRepair.stream().map(item -> new BigDecimal(item.getZtjgAppearance() == null ? "0.00" : item.getZtjgAppearance())).reduce(BigDecimal.ONE, BigDecimal::add).toString();
            // 支持绝缘部分 SUPPORT_INSULATION_PART
            supportInsulationPart = gisRepair.stream().map(item -> new BigDecimal(item.getSupportInsulation() == null ? "0.00" : item.getSupportInsulation())).reduce(BigDecimal.ONE, BigDecimal::add).toString();
            // 主回路端子 MAIN_CIRCUIT_TERMINAL
            mainCircuitTerminal = gisRepair.stream().map(item -> new BigDecimal(item.getMainLoopTerminal() == null ? "0.00" : item.getMainLoopTerminal())).reduce(BigDecimal.ONE, BigDecimal::add).toString();
            // 检修次数 MAINTENANCE_TIME
            maintenanceTime = String.valueOf(gisRepair.size());
        }
        //气压、日照、雨量
        String psrDou = map.get(GisAssessEnum.YXCS_GZHJ_QYRZYL.getKey());
        return calculateYXCSValue(busiParamGisMap, temperature, humidity, environment, operateMechanismLubrication, mainBodyAppearance, supportInsulationPart, mainCircuitTerminal, maintenanceTime, psrDou, vo, step);
    }

    private static Map<String, String> calculateYXCSValue(Map<String, BusiParamTransformerVo> busiParamGisMap,
                                                          String temperature, String humidity,
                                                          String environment, String operateMechanismLubrication,
                                                          String mainBodyAppearance, String supportInsulationPart,
                                                          String mainCircuitTerminal, String maintenanceTime, String psrDou,
                                                          BusiOfflineGisBrandVo vo, int step) {
        // 运行参数
        BusiParamTransformerVo yxcsVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATE_PARAMETER.getKey());

        Map<String, String> map1 = calculateGZHJ(busiParamGisMap, temperature, humidity, environment, psrDou, step);
        BigDecimal result = new BigDecimal(map1.get("result"));
        step = Integer.parseInt(map1.get("step"));

        Map<String, String> map2 = calculateWG(busiParamGisMap, operateMechanismLubrication, mainBodyAppearance, supportInsulationPart, mainCircuitTerminal, step);
        BigDecimal result2 = new BigDecimal(map2.get("result"));
        step = Integer.parseInt(map2.get("step"));

        Map<String, String> map3 = calculateCJ(busiParamGisMap, maintenanceTime, vo, step);
        BigDecimal result3 = new BigDecimal(map3.get("result"));
        step = Integer.parseInt(map3.get("step"));

        Map<String, String> map4 = calculateDLGWH(busiParamGisMap, maintenanceTime, step);
        BigDecimal result4 = new BigDecimal(map4.get("result"));
        step = Integer.parseInt(map4.get("step"));

        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(yxcsVo.getIsAttend())) {
            finalResult = result.add(result2).add(result3).add(result4).multiply(yxcsVo.getValue());
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.APPEARANCE.getKey()).getIsAttend())) {
                jsonObject.put("外观情况", result2.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MANUFACTURER.getKey()).getIsAttend())) {
                jsonObject.put("生产厂家", result3.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.WORK_ENVIRONMENT.getKey()).getIsAttend())) {
                jsonObject.put("工作环境", result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.CIRCUIT_BREAKER_MAINTENANCE.getKey()).getIsAttend())) {
                jsonObject.put("断路器维护", result4.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("运行参数权重", yxcsVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, yxcsVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put(GisCreateUtil.OPERATION_PARAMETERS, finalResult.toString());
        resultMap.put(GisCreateUtil.WORK_ENVIRONMENT, result.toString());
        resultMap.put(GisCreateUtil.APPEARANCE, result2.toString());
        resultMap.put(GisCreateUtil.Manufacturer, result3.toString());
        resultMap.put(GisCreateUtil.MAINTENANCE_RECORD, result4.toString());
        resultMap.put("step", String.valueOf(step));
        return resultMap;
    }

    /**
     * 工作环境
     *
     * @return
     */
    private static Map<String, String> calculateGZHJ(Map<String, BusiParamTransformerVo> busiParamGisMap, String operatingAmbientTemperatures, String operateAmbientHumidity, String environmentCoefficient, String psrDou, int step) {
        // 工作环境
        BusiParamTransformerVo gzhjVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.WORK_ENVIRONMENT.getKey());
        //使用环境温度
        BusiParamTransformerVo operatingAmbientTemperaturesVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATING_AMBIENT_TEMPERATURE.getKey());
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(operatingAmbientTemperatures, gzhjVo, operatingAmbientTemperaturesVo, step);
        BigDecimal operatingAmbientTemperaturesResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));

        //使用环境湿度
        BusiParamTransformerVo operateAmbientHumidityVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATING_AMBIENT_HUMIDITY.getKey());
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(operateAmbientHumidity, gzhjVo, operateAmbientHumidityVo, step);
        BigDecimal operateAmbientHumidityResult = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));

        //尘埃腐蚀
        BusiParamTransformerVo environmentCoefficientVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DUST_CORROSION.getKey());
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(environmentCoefficient, gzhjVo, environmentCoefficientVo, step);
        BigDecimal environmentCoefficientResult = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        //气压、日照、雨量
        BusiParamTransformerVo psrDouVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.ALTITUDE.getKey());
        Map<String, String> result4 = GisAssessmentCalculate.calculateResult(psrDou, gzhjVo, psrDouVo, step);
        BigDecimal psrDouResult = new BigDecimal(result4.get("result"));
        step = Integer.parseInt(result4.get("step"));

        // 运行参数-其他因素-工作环境总结果
        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(gzhjVo.getIsAttend())) {
            finalResult = operatingAmbientTemperaturesResult.add(operateAmbientHumidityResult).add(environmentCoefficientResult).add(psrDouResult);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(operatingAmbientTemperaturesVo.getIsAttend())) {
                jsonObject.put("使用环境温度", operatingAmbientTemperaturesResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(operateAmbientHumidityVo.getIsAttend())) {
                jsonObject.put("使用环境湿度", operateAmbientHumidityResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(environmentCoefficientVo.getIsAttend())) {
                jsonObject.put("尘埃、腐蚀", environmentCoefficientResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(psrDouVo.getIsAttend())) {
                jsonObject.put("气压、日照、雨量", psrDouResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("工作环境权重", gzhjVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, gzhjVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", finalResult.toString());
        return map;
    }

    /**
     * 外观情况
     *
     * @return
     */
    private static Map<String, String> calculateWG(Map<String, BusiParamTransformerVo> busiParamGisMap,
                                                   String operateMechanismLubrication,
                                                   String mainBodyAppearance,
                                                   String supportInsulationPart,
                                                   String mainCircuitTerminal,
                                                   int step) {
        // 外观情况
        BusiParamTransformerVo wgbVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.APPEARANCE.getKey());
        //修改操作机构润滑
        BusiParamTransformerVo operateMechanismLubricationVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATE_MECHANISM_LUBRICATION.getKey());
        String czjgLubrication = GisAssessUtilsNew.getValFromString(operateMechanismLubrication);
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(czjgLubrication, wgbVo, operateMechanismLubricationVo, step);
        BigDecimal operateMechanismLubricationResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));

        //修改主体、机构外观
        BusiParamTransformerVo mainBodyAppearanceVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MAIN_APPEARANCE_MECHANISM.getKey());
        String ztjgAppearance = GisAssessUtilsNew.getValFromString(mainBodyAppearance);
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(ztjgAppearance, wgbVo, mainBodyAppearanceVo, step);
        BigDecimal mainBodyAppearanceResult = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));

        //修改支持绝缘部分
        BusiParamTransformerVo supportInsulationPartVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SUPPORT_INSULATION_PART.getKey());
        String supportInsulation = GisAssessUtilsNew.getValFromString(supportInsulationPart);
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(supportInsulation, wgbVo, supportInsulationPartVo, step);
        BigDecimal supportInsulationPartResult = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        //修改主回路端子
        BusiParamTransformerVo mainCircuitTerminalVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MAIN_CIRCUIT_TERMINAL.getKey());
        String mainLoopTerminal = GisAssessUtilsNew.getValFromString(mainCircuitTerminal);
        Map<String, String> result4 = GisAssessmentCalculate.calculateResult(mainLoopTerminal, wgbVo, mainCircuitTerminalVo, step);
        BigDecimal mainCircuitTerminalResult = new BigDecimal(result4.get("result"));
        step = Integer.parseInt(result4.get("step"));

        // 运行参数-其他因素-外观情况总结果
        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(wgbVo.getIsAttend())) {
            finalResult = operateMechanismLubricationResult.add(mainBodyAppearanceResult).add(supportInsulationPartResult).add(mainCircuitTerminalResult);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(operateMechanismLubricationVo.getIsAttend())) {
                jsonObject.put("操作机构润滑", operateMechanismLubricationResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(mainBodyAppearanceVo.getIsAttend())) {
                jsonObject.put("主体、机构外观", mainBodyAppearanceResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(supportInsulationPartVo.getIsAttend())) {
                jsonObject.put("支持绝缘部分", supportInsulationPartResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(mainCircuitTerminalVo.getIsAttend())) {
                jsonObject.put("主回路端子", mainCircuitTerminalResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("外观情况权重", wgbVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, wgbVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", finalResult.toString());
        return map;
    }

    /**
     * 生产厂家
     *
     * @return
     */
    private static Map<String, String> calculateCJ(Map<String, BusiParamTransformerVo> busiParamGisMap, String maintenanceTime, BusiOfflineGisBrandVo vo, int step) {
        // 厂家
        BusiParamTransformerVo cjcVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MANUFACTURER.getKey());
        //产品成熟度
        BusiParamTransformerVo productMaturityVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.PRODUCT_MATURITY.getKey());
        String productMaturity = GisAssessUtilsNew.getCpcsd(vo).toString();
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(productMaturity, cjcVo, productMaturityVo, step);
        BigDecimal productMaturityResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));

        //知名度
        BusiParamTransformerVo popularityVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.POPULARITY.getKey());
        String popularity = GisAssessUtilsNew.getZmd(vo).toString();
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(popularity, cjcVo, popularityVo, step);
        BigDecimal popularityResult = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));

        //厂家设备遗传
        BusiParamTransformerVo cjsbycVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MANUFACTURER_EQUIPMENT_GENETICS.getKey());
        double a = Double.parseDouble(maintenanceTime);
        String cjyc = GisAssessUtilsNew.getCjyc((int) a).toString();
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(cjyc, cjcVo, cjsbycVo, step);
        BigDecimal cjycResult = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        // 运行参数-生产厂家
        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(cjcVo.getIsAttend())) {
            finalResult = productMaturityResult.add(popularityResult).add(cjycResult);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(productMaturityVo.getIsAttend())) {
                jsonObject.put("产品成熟度", productMaturityResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(popularityVo.getIsAttend())) {
                jsonObject.put("知名度", popularityResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(cjsbycVo.getIsAttend())) {
                jsonObject.put("厂家设备遗传", cjycResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("生产厂家权重", cjcVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, cjcVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", finalResult.toString());
        return map;
    }

    private static Map<String, String> calculateDLGWH(Map<String, BusiParamTransformerVo> busiParamGisMap, String maintenanceTime, int step) {
        // 断路器维护
        BusiParamTransformerVo dlqwhVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.CIRCUIT_BREAKER_MAINTENANCE.getKey());
        String dlgwh = GisAssessUtilsNew.getValFromString(maintenanceTime);
        // 运行参数-生产厂家
        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(dlqwhVo.getIsAttend())) {
            Map<String, String> result = GisAssessmentCalculate.calculateResult(dlgwh, new BusiParamTransformerVo(), dlqwhVo, step);
            finalResult = new BigDecimal(result.get("result"));
            step = Integer.parseInt(result.get("step"));
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", finalResult.toString());
        return map;
    }
}
