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: FurfuralTndexTest
 * @Description: 变压器绝缘试验-糠醛指标 权重计算.
 * @Author: Arno
 * @Date: 2023/10/9 18:29
 */
@Slf4j
public class FurfuralTndexTest {


    /**
     * @param transformerEvaluateDataVo 评测数据
     * @param secondLevelMap            二级分析参数
     * @param threeLevelMap             三级分析参数
     * @return Double
     * @Title: calcFurIndexWeight
     * @Description: 变压器绝缘试验-糠醛指标 权重计算
     * @createDate: Sep 4, 2019 6:24:47 PM
     */
    public static Map<String, Object> calcFurIndexWeight(TransformerEvaluateDataVo transformerEvaluateDataVo, Map<String, BusiParamTransformerVo> secondLevelMap,
                                                         Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        BusiParamTransformerVo secondVo = CommonUtils.getEvaluateObject(EvaluateConstant.KQZB, 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 transformerEvaluateDataVo, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        String furfural = transformerEvaluateDataVo.getFurfural();
        Double kqzb = calc(furfural, EvaluateConstant.KQZB_KQHL, "糠醛含量", threeLevelMap, jsonArray, step, "变压器绝缘试验-糠醛指标-糠醛含量计算");
        log.info("->>变压器绝缘试验-糠醛指标-分项和计算结果: [{}]", kqzb);
        step += 1;
        return CommonUtils.buildResult(step, kqzb);
    }

    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;
    }

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