package com.huatek.busi.task.transformer.utils.insulationtest;

import com.huatek.busi.task.transformer.utils.CommonUtils;
import com.huatek.busi.util.EvaluateConstant;
import com.huatek.busi.util.EvaluationNameUtil;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

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

/**
 * @version V1.0
 * @ClassName: InsulationPaperTest
 * @Description: 变压器绝缘试验-绝缘纸试验.
 * @Author: Arno
 * @Date: 2023/10/10 10:38
 */
@Slf4j
public class InsulationPaperTest {

    /**
     * @param transformerEvaluateDataVo 评测数据
     * @param secondLevelMap            二级分析参数
     * @param threeLevelMap             三级分析参数
     * @return Double
     * @Title: calcInsulationPaperWeight
     * @Description: 变压器绝缘试验-绝缘纸实验 权重计算
     * @createDate: Sep 4, 2019 6:24:47 PM
     */
    public static Map<String, Object> calcInsulationPaperWeight(TransformerEvaluateDataVo transformerEvaluateDataVo, Map<String, BusiParamTransformerVo> secondLevelMap,
                                                                Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        BusiParamTransformerVo secondVo = CommonUtils.getEvaluateObject(EvaluateConstant.JYZ, secondLevelMap);
        if (null != secondVo) {
            if (CommonUtils.checkAttend(secondVo)) {
                Map<String, Object> threeResultMap = calcThree(transformerEvaluateDataVo, threeLevelMap, jsonArray, step);
                return CommonUtils.calcSecond(secondVo, threeResultMap, jsonArray, "变压器绝缘试验-绝缘纸实验权重计算");
            }
        }
        log.error("-->>压器绝缘试验-绝缘纸实验未设置评估参数");
        return null;
    }


    public static Map<String, Object> calcThree(TransformerEvaluateDataVo vo, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        String degreeCardboard = vo.getDegreeCardboard();
        Double calcWeight = calc(degreeCardboard, EvaluateConstant.JYZ_ZBJHD, "纸板聚合度Dp", threeLevelMap, jsonArray, step, "变压器绝缘试验-绝缘纸试验-纸板聚合度Dp计算");
        log.info("->>变压器绝缘试验-绝缘纸试验-分项和计算结果: [{}]", calcWeight);
        step += 1;
        return CommonUtils.buildResult(step, calcWeight);

    }

    public static Double calc(String value, String type, String typeDesc, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step, String stepName) {
        Double calcWeight = 0d;
        BusiParamTransformerVo params = CommonUtils.getEvaluateObject(type, threeLevelMap);
        if (CommonUtils.checkParams(params)) {
            log.error( "-->>变压器绝缘试验-绝缘纸试验-{}未设置评估参数", typeDesc);
            return calcWeight;
        }
        if (CommonUtils.checkAttend(params)) {
            if (CommonUtils.checkValue(value)) {
                log.error("-->>变压器绝缘试验-绝缘纸试验-{}未设置评估值", typeDesc);
                calcWeight = params.getValue().doubleValue();
            } else {
                calcWeight = getCalcWeight(value, params);
            }
            step = step + 1;
          //  CommonUtils.setStep(jsonArray, step, stepName, CommonUtils.calculateModel(value, params), String.valueOf(calcWeight));
            EvaluationNameUtil.setStep(step, params, calcWeight.toString(), value);
            return calcWeight;
        }
        return calcWeight;
    }

    public static double getCalcWeight(String degreeCardboard, BusiParamTransformerVo param) {
        double jyz = 0d;
        double DP_A = param.getAValue().doubleValue();
        double DP_B = param.getBValue().doubleValue();
        double FFA = Double.valueOf(degreeCardboard);
        if ((0 < FFA || FFA == 0) && FFA < DP_A) {
            jyz = 0d;
        } else if ((DP_A < FFA || FFA == DP_A) && FFA < DP_B) {
            double d = (FFA - DP_A) / (DP_B - DP_A);
            jyz = new BigDecimal(d).setScale(4, RoundingMode.HALF_UP).doubleValue();
        } else if (DP_B < FFA || FFA == DP_B) {
            jyz = 1d;
        }
        return jyz;
    }
}
