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 com.huatek.busi.vo.GisMechanicalPropertiesVo;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class GisYXSJCalculate {

    /**
     * 运行数据 手动
     *
     * @return
     */
    public static Map<String, String> calculateManualYXSJ(Map<String, BusiParamTransformerVo> busiParamGisMap, Map<String, String> map, int step) {
        String aHzbtq = map.get(GisAssessEnum.DIFFERENT_CLOSE_PERIODS_A.getKey());
        String bHzbtq = map.get(GisAssessEnum.DIFFERENT_CLOSE_PERIODS_B.getKey());
        String cHzbtq = map.get(GisAssessEnum.DIFFERENT_CLOSE_PERIODS_C.getKey());
        String hzbtqMax = GisAssessUtilsNew.getABCMax(aHzbtq, bHzbtq, cHzbtq).toString();

        String aFzbtq = map.get(GisAssessEnum.DIFFERENT_OPEN_PERIODS_A.getKey());
        String bFzbtq = map.get(GisAssessEnum.DIFFERENT_OPEN_PERIODS_B.getKey());
        String cFzbtq = map.get(GisAssessEnum.DIFFERENT_OPEN_PERIODS_C.getKey());
        String fzbtqMax = GisAssessUtilsNew.getABCMax(aFzbtq, bFzbtq, cFzbtq).toString();

        String aGhSpeed = map.get(GisAssessEnum.RIGID_CLOSE_SPEED_A.getKey());
        String bGhSpeed = map.get(GisAssessEnum.RIGID_CLOSE_SPEED_B.getKey());
        String cGhSpeed = map.get(GisAssessEnum.RIGID_CLOSE_SPEED_C.getKey());
        String ghSpeedMax = GisAssessUtilsNew.getABCMax(aGhSpeed, bGhSpeed, cGhSpeed).toString();

        String aGfSpeed = map.get(GisAssessEnum.RIGID_PARTIAL_VELOCITY_A.getKey());
        String bGfSpeed = map.get(GisAssessEnum.RIGID_PARTIAL_VELOCITY_B.getKey());
        String cGfSpeed = map.get(GisAssessEnum.RIGID_PARTIAL_VELOCITY_C.getKey());
        String gfSpeedMax = GisAssessUtilsNew.getABCMax(aGfSpeed, bGfSpeed, cGfSpeed).toString();

        String sjcsOther = map.get(GisAssessEnum.YXSJ_SJCS_QT.getKey());
        String sdtzOther = map.get(GisAssessEnum.YXSJ_SDTZ_QT.getKey());

        return calculateYXSJValue(busiParamGisMap, hzbtqMax, fzbtqMax, ghSpeedMax, gfSpeedMax, sjcsOther, sdtzOther, step);
    }

    /**
     * 运行数据 自动
     *
     * @return
     */
    public static Map<String, String> calculateAutoYXSJ(Map<String, BusiParamTransformerVo> busiParamGisMap, GisMechanicalPropertiesVo vo, int step) {
        if (StrUtil.isEmpty(vo.getAHzBtq()) && StrUtil.isEmpty(vo.getBHzBtq()) && StrUtil.isEmpty(vo.getCHzBtq()) &&
                "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DIFFERENT_CLOSE_PERIODS.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.DIFFERENT_CLOSE_PERIODS.getValue() + "未安装或未采集到数据");
            return map;
        }
        String aHzbtq = vo.getAHzBtq();
        String bHzbtq = vo.getBHzBtq();
        String cHzbtq = vo.getCHzBtq();
        String hzbtqMax = GisAssessUtilsNew.getABCMax(aHzbtq, bHzbtq, cHzbtq).toString();

        if (StrUtil.isEmpty(vo.getAFzBtq()) && StrUtil.isEmpty(vo.getBFzBtq()) && StrUtil.isEmpty(vo.getCFzBtq()) &&
                "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DIFFERENT_OPEN_PERIODS.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.DIFFERENT_OPEN_PERIODS.getValue() + "未安装或未采集到数据");
            return map;
        }
        String aFzbtq = vo.getAFzBtq();
        String bFzbtq = vo.getBFzBtq();
        String cFzbtq = vo.getCFzBtq();
        String fzbtqMax = GisAssessUtilsNew.getABCMax(aFzbtq, bFzbtq, cFzbtq).toString();

        if (StrUtil.isEmpty(vo.getAGhSpeed()) && StrUtil.isEmpty(vo.getBGhSpeed()) && StrUtil.isEmpty(vo.getCGhSpeed()) &&
                "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.STIFFNESS_AND_SPEED.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.STIFFNESS_AND_SPEED.getValue() + "未安装或未采集到数据");
            return map;
        }
        String aGhSpeed = vo.getAGhSpeed();
        String bGhSpeed = vo.getBGhSpeed();
        String cGhSpeed = vo.getCGhSpeed();
        String ghSpeedMax = GisAssessUtilsNew.getABCMax(aGhSpeed, bGhSpeed, cGhSpeed).toString();

        if (StrUtil.isEmpty(vo.getAGfSpeend()) && StrUtil.isEmpty(vo.getBGfSpeend()) && StrUtil.isEmpty(vo.getCGfSpeend()) &&
                "1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.RIGID_PARTIAL_VELOCITY.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.RIGID_PARTIAL_VELOCITY.getValue() + "未安装或未采集到数据");
            return map;
        }
        String aGfSpeed = vo.getAGfSpeend();
        String bGfSpeed = vo.getBGfSpeend();
        String cGfSpeed = vo.getCGfSpeend();
        String gfSpeedMax = GisAssessUtilsNew.getABCMax(aGfSpeed, bGfSpeed, cGfSpeed).toString();

        if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.TIME_PARAMETER_OTHER.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.TIME_PARAMETER_OTHER.getValue() + "未安装或未采集到数据");
            return map;
        }
        String sjcsOther = "";
//
        if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SPEED_PARAMETER_OTHER_ASPECTS.getKey()).getIsAttend())) {
            Map<String, String> map = new HashMap<>();
            map.put("warnMsg", "GIS评测-" + ParamEnum.SPEED_PARAMETER_OTHER_ASPECTS.getValue() + "未安装或未采集到数据");
            return map;
        }
        String sdtzOther = "";

        return calculateYXSJValue(busiParamGisMap, hzbtqMax, fzbtqMax, ghSpeedMax, gfSpeedMax, sjcsOther, sdtzOther, step);
    }

    private static Map<String, String> calculateYXSJValue(Map<String, BusiParamTransformerVo> busiParamGisMap, String hzbtqMax, String fzbtqMax, String ghSpeedMax, String gfSpeedMax, String sjcsOther, String sdtzOther, int step) {
        BusiParamTransformerVo yxsjVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.OPERATE_DATA.getKey());
        Map<String, String> result1 = calculateSJCS(busiParamGisMap, hzbtqMax, fzbtqMax, sjcsOther, step);
        BigDecimal finalResult = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));

        Map<String, String> result2 = calculateSDTZ(busiParamGisMap, ghSpeedMax, gfSpeedMax, sdtzOther, step);
        BigDecimal finalResult2 = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));

        // 运行数据-机械实验数据
        BigDecimal h2 = new BigDecimal(0);
        if ("1".equals(yxsjVo.getIsAttend())) {
            h2 = (finalResult.add(finalResult2)).multiply(yxsjVo.getValue());
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.TIME_PARAMETER.getKey()).getIsAttend())) {
                jsonObject.put("时间参数", finalResult.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.VELOCITY_CHARACTERISTICS.getKey()).getIsAttend())) {
                jsonObject.put("速度特征", finalResult2.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("运行数据权重", yxsjVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, yxsjVo, String.valueOf(h2), jsonObject, null);
        }
        Map<String, String> result = new HashMap<>();
        result.put(GisCreateUtil.OPERATIONAL_DATA, h2.toString());
        result.put(GisCreateUtil.TIME_PARAMETER, finalResult.toString());
        result.put(GisCreateUtil.VELOCITY_CHARACTERISTICS, finalResult2.toString());
        result.put("step", String.valueOf(step));
        return result;
    }

    /**
     * 运行数据-机械实验数据-时间参数
     *
     * @return
     */
    private static Map<String, String> calculateSJCS(Map<String, BusiParamTransformerVo> busiParamGisMap, String hzbtqMax, String fzbtqMax, String sjcsOther, int step) {
        BusiParamTransformerVo sjcsVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.TIME_PARAMETER.getKey());
        BusiParamTransformerVo hzbtqVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DIFFERENT_CLOSE_PERIODS.getKey());
        BusiParamTransformerVo fzbtqVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.DIFFERENT_OPEN_PERIODS.getKey());
        BusiParamTransformerVo otherVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.TIME_PARAMETER_OTHER.getKey());
        //合闸不同期
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(hzbtqMax, sjcsVo, hzbtqVo, step);
        BigDecimal hzbtq_result = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));
        // 分闸不同期
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(fzbtqMax, sjcsVo, fzbtqVo, step);
        BigDecimal fzbtq_result = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));
        // 其他
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(sjcsOther, sjcsVo, otherVo, step);
        BigDecimal other_result = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));
        // 运行数据-机械实验数据-时间参数
        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(sjcsVo.getIsAttend())) {
            finalResult = hzbtq_result.add(fzbtq_result).add(other_result);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(hzbtqVo.getIsAttend())) {
                jsonObject.put("合闸不同期", hzbtq_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(fzbtqVo.getIsAttend())) {
                jsonObject.put("分闸不同期", fzbtq_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(otherVo.getIsAttend())) {
                jsonObject.put("其他", other_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("时间参数权重", sjcsVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, sjcsVo, 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> calculateSDTZ(Map<String, BusiParamTransformerVo> busiParamGisMap, String ghSpeedMax, String gfSpeedMax, String sdtzOther, int step) {
        BusiParamTransformerVo sdtzVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.VELOCITY_CHARACTERISTICS.getKey());
        BusiParamTransformerVo ghsdVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.STIFFNESS_AND_SPEED.getKey());
        BusiParamTransformerVo gfsdVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.RIGID_PARTIAL_VELOCITY.getKey());
        BusiParamTransformerVo otherVo = GisAssessUtilsNew.getVo(busiParamGisMap, ParamEnum.SPEED_PARAMETER_OTHER_ASPECTS.getKey());
        // 刚合速度
        Map<String, String> result1 = GisAssessmentCalculate.calculateResult(ghSpeedMax, sdtzVo, ghsdVo, step);
        BigDecimal ghSpeend_result = new BigDecimal(result1.get("result"));
        step = Integer.parseInt(result1.get("step"));
        // 刚分速度
        Map<String, String> result2 = GisAssessmentCalculate.calculateResult(gfSpeedMax, sdtzVo, gfsdVo, step);
        BigDecimal gfSpeend_result = new BigDecimal(result2.get("result"));
        step = Integer.parseInt(result2.get("step"));
        // 其他
        Map<String, String> result3 = GisAssessmentCalculate.calculateResult(sdtzOther, sdtzVo, otherVo, step);
        BigDecimal other_result = new BigDecimal(result3.get("result"));
        step = Integer.parseInt(result3.get("step"));

        BigDecimal finalResult = new BigDecimal(0);
        if ("1".equals(sdtzVo.getIsAttend())) {
            finalResult = ghSpeend_result.add(gfSpeend_result).add(other_result);
            step += 1;
            JSONObject jsonObject = new JSONObject();
            if ("1".equals(ghsdVo.getIsAttend())) {
                jsonObject.put("刚合速度", ghSpeend_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(gfsdVo.getIsAttend())) {
                jsonObject.put("刚分速度", gfSpeend_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            if ("1".equals(otherVo.getIsAttend())) {
                jsonObject.put("其他", other_result.setScale(4, RoundingMode.HALF_UP).toString());
            }
            jsonObject.put("速度特征权重", sdtzVo.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            EvaluationNameUtil.setSpecialStep(step, sdtzVo, String.valueOf(finalResult), jsonObject, null);
        }
        Map<String, String> map = new HashMap<>();
        map.put("step", String.valueOf(step));
        map.put("result", finalResult.toString());
        return map;
    }
}
