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.entity.RptGisMonitorSf6Charts;
import com.huatek.busi.entity.TransformerPdStatistics;
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.*;


@Slf4j
public class GisSf6Calculate {

    /**
     * 手动SF6绝缘实验
     *
     * @return
     */
    public static Map<String, String> calculateManualSf6(Map<String, BusiParamTransformerVo> busiParamGisMap, Map<String, String> map, int step) {
        // 气体泄露（气室压力）
        String gasLeakA = map.get(GisAssessEnum.CHAMBER_PRESSURE_VALUE_A.getKey());
        String gasLeakB = map.get(GisAssessEnum.CHAMBER_PRESSURE_VALUE_B.getKey());
        String gasLeakC = map.get(GisAssessEnum.CHAMBER_PRESSURE_VALUE_C.getKey());
        String gasLeakMin = GisAssessUtilsNew.getABCMin(gasLeakA, gasLeakB, gasLeakC).toString();
        // 微水含量(灭弧室)
        String microWaterA = map.get(GisAssessEnum.MICRO_WATER_CONTENT_VALUE_A.getKey());
        String microWaterB = map.get(GisAssessEnum.MICRO_WATER_CONTENT_VALUE_B.getKey());
        String microWaterC = map.get(GisAssessEnum.MICRO_WATER_CONTENT_VALUE_C.getKey());
        String microWaterMax = GisAssessUtilsNew.getABCMax(microWaterA, microWaterB, microWaterC).toString();
        // 局放量
        String partialDischarge = map.get(GisAssessEnum.PARTIAL_DISCHARGE_VALUE.getKey());
        // 局放次数
        String partialDischargeCount = map.get(GisAssessEnum.PARTIAL_DISCHARGE_TIME.getKey());
        // 其他
        String other = map.get(GisAssessEnum.YXSJ_JYJZ_QT.getKey());
        return calculateSf6Value(busiParamGisMap, gasLeakMin, microWaterMax, partialDischarge, partialDischargeCount, other, step);
    }

    /**
     * 自动SF6绝缘实验
     *
     * @return
     */
    public static Map<String, String> calculateAutoSf6(Map<String, BusiParamTransformerVo> busiParamGisMap, List<RptGisMonitorSf6Charts> rptGisMonitorSf6Chartses, TransformerPdStatistics transformerGisPdStatistics, int step) {
        // 气体泄露（气室压力）
        if (!rptGisMonitorSf6Chartses.stream().map(item -> new BigDecimal(item.getPressure() == null ? "0.00" : item.getPressure())).findAny().isPresent()) {
            if(isAttend(busiParamGisMap,ParamEnum.GAS_LEAKAGE_ARC_EXTINGUISH_CHAMBER)){
                Map<String, String> map = new HashMap<>();
                map.put("warnMsg", "GIS评测-" + ParamEnum.GAS_LEAKAGE_ARC_EXTINGUISH_CHAMBER.getValue() + "未安装或未采集到数据");
                return map;
            }
        }

        String gasLeakMin = rptGisMonitorSf6Chartses.stream()
                .map(item -> new BigDecimal(item.getPressure() == null ? "0.00" : item.getPressure()))
                .min(BigDecimal::compareTo).get().toString();
        // 微水含量(灭弧室)
        if (!rptGisMonitorSf6Chartses.stream().map(item -> new BigDecimal(item.getMoisture() == null ? "0.00" : item.getMoisture())).findAny().isPresent()) {
            if( isAttend(busiParamGisMap,ParamEnum.MICRO_WATER_CONTENT_ARC_EXTINGUISH_CHAMBER)){
                Map<String, String> map = new HashMap<>();
                map.put("warnMsg", "GIS评测-" + ParamEnum.MICRO_WATER_CONTENT_ARC_EXTINGUISH_CHAMBER.getValue() + "未安装或未采集到数据");
                return map;
            }
        }
        String microWaterMax = rptGisMonitorSf6Chartses.stream()
                .map(item -> new BigDecimal(item.getMoisture() == null ? "0.00" : item.getMoisture()))
                .max(BigDecimal::compareTo).get().toString();

        // 局放量
        if (StrUtil.isEmpty(transformerGisPdStatistics.getElectricQuantity())){
            if (isAttend(busiParamGisMap,ParamEnum.LOCAL_DISCHARGE)){
                Map<String, String> map = new HashMap<>();
                map.put("warnMsg", "GIS评测-" + ParamEnum.LOCAL_DISCHARGE.getValue() + "未安装或未采集到数据");
                return map;
            }
        }
        String partialDischarge = transformerGisPdStatistics.getElectricQuantity();

        // 局放次数
        if (StrUtil.isEmpty(transformerGisPdStatistics.getCount().toString()) && isAttend(busiParamGisMap,ParamEnum.PARTIAL_DISCHARGE_TIMES)) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.PARTIAL_DISCHARGE_TIMES.getValue() + "未安装或未采集到数据");
            return map;
        }
        String partialDischargeCount = transformerGisPdStatistics.getCount().toString();
        if (isAttend(busiParamGisMap,ParamEnum.INSULATING_MEDIUM_OTHER)) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.INSULATING_MEDIUM_OTHER.getValue() + "未安装或未采集到数据");
            return map;
        }
        String other = "";
        // 局放量
        return calculateSf6Value(busiParamGisMap, gasLeakMin, microWaterMax, partialDischarge, partialDischargeCount, other, step);
    }
    private static boolean isAttend(Map<String, BusiParamTransformerVo> busiParamGisMap, ParamEnum paramEnum){
        BusiParamTransformerVo busiParamTransformerVo = GisAssessUtilsNew.getVo(busiParamGisMap, paramEnum.getKey());
        return busiParamTransformerVo != null && "1".equals(busiParamTransformerVo.getIsAttend());
    }

    private static Map<String, String> calculateSf6Value(Map<String, BusiParamTransformerVo> busiParamGisMap, String gasLeakMin, String microWaterMax, String partialDischarge, String partialDischargeCount, String other, int step) {
        // 运行数据-电气试验
        BusiParamTransformerVo sf6Vo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.INSULATION_MEDIUM.getKey());
        // 气体泄露（气室压力）
        BusiParamTransformerVo gasLeakVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.GAS_LEAKAGE_ARC_EXTINGUISH_CHAMBER.getKey());
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(gasLeakMin, sf6Vo, gasLeakVo, step);
        BigDecimal gasLeakResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));
        // 微水含量(灭弧室)
        BusiParamTransformerVo microWaterVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.MICRO_WATER_CONTENT_ARC_EXTINGUISH_CHAMBER.getKey());
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(microWaterMax, sf6Vo, microWaterVo, step);
        BigDecimal microWaterResult = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));
        // 局放量
        BusiParamTransformerVo partialDischargeVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.LOCAL_DISCHARGE.getKey());
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(partialDischarge, sf6Vo, partialDischargeVo, step);
        BigDecimal partialDischargeResult = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));
        // 局放次数
        BusiParamTransformerVo partialDischargeCountVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.PARTIAL_DISCHARGE_TIMES.getKey());
        Map<String, String> result4 = GisAssessmentCalculate.calculateResult(partialDischargeCount, sf6Vo, partialDischargeCountVo, step);
        BigDecimal partialDischargeCountResult = new BigDecimal(result4.get("result"));
        step = Integer.parseInt(result4.get("step"));
        // 其他
        BusiParamTransformerVo otherVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.INSULATING_MEDIUM_OTHER.getKey());
        Map<String, String> result5 = GisAssessmentCalculate.calculateResult(other, sf6Vo, otherVo, step);
        BigDecimal otherResult = new BigDecimal(result5.get("result"));
        step = Integer.parseInt(result5.get("step"));

        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(sf6Vo.getIsAttend())) {
            finalResult = gasLeakResult.add(microWaterResult).add(partialDischargeResult).add(partialDischargeCountResult).add(otherResult).setScale(4, RoundingMode.HALF_UP);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(gasLeakVo.getIsAttend())) {
                jsonObject.put("气体泄露（气室压力）", gasLeakResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(microWaterVo.getIsAttend())) {
                jsonObject.put("微水含量(灭弧室)", microWaterResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(partialDischargeVo.getIsAttend())) {
                jsonObject.put("局放量", partialDischargeResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(partialDischargeCountVo.getIsAttend())) {
                jsonObject.put("局放次数", partialDischargeCountResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(otherVo.getIsAttend())) {
                jsonObject.put("其他", otherResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("SF6绝缘实验权重", sf6Vo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, sf6Vo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> result = new HashMap<>();
        result.put(GisCreateUtil.INSULATING_MEDIUM, finalResult.toString());
        result.put(GisCreateUtil.GAS_PRESSURE, gasLeakResult.toString());
        result.put(GisCreateUtil.MICRO_WATER_CONTENT, microWaterResult.toString());
        result.put(GisCreateUtil.PARTIAL_DISCHARGE, partialDischargeResult.toString());
        result.put(GisCreateUtil.PARTIAL_DISCHARGE_COUNT, partialDischargeCountResult.toString());
        result.put("step", String.valueOf(step));
        // 运行数据-电气试验
        return result;
    }
}
