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.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import com.huatek.busi.vo.epa.EpaPdDataVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

import java.util.Map;

/**
 * @version V1.0
 * @ClassName: RoutineTest
 * @Description: 变压器绝缘试验-例行试验.
 * @Author: Arno
 * @Date: 2023/10/10 13:18
 */
@Slf4j
public class RoutineTest {



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


    public static Map<String, Object> calcThree(TransformerEvaluateDataVo vo, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step,EpaPdDataVo pdEvaluateData) {
        //极化指数
        String polarizatioIndex = vo.getPolarizatioIndex();
        //局放放电量
        //String partialDischarge = vo.getPartialDischarge();

        //绝缘电阻(MΩ)
        String insulationResistance = vo.getInsulationResistance();
        //绝缘电阻吸收比
        String insulationResistanceAbsorptionRatio = vo.getInsulationResistanceAbsorptionRatio();
        //绕组直流电阻相间差(%)
        String phaseDifferenceDcResistance = vo.getPhaseDifferenceDcResistance();
        //绕组直流电阻线间差(%)
        String lineDifferenceDcResistance = vo.getLineDifferenceDcResistance();
        //绕组绝缘介损
        String windingInsulationDielectricLoss = vo.getWindingInsulationDielectricLoss();
        //铁芯绝缘电阻（MΩ）
        String coreInsulationResistance = vo.getCoreInsulationResistance();

        Double calcWeight = calc(polarizatioIndex, EvaluateConstant.LXSY_JHZS, "极化指数", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-极化指数计算结果");

        //局放放电量 在线数据
        Double calcWeight2 = 0d;
        if(null != pdEvaluateData){
            String partialDischarge =  pdEvaluateData.getElectricQuantity();
            calcWeight2 = calc(partialDischarge, EvaluateConstant.LXSY_JFFDL, "局放放电量", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-局放放电量计算结果");
        }

        Double calcWeight3 = calc(insulationResistance, EvaluateConstant.LXSY_JYDZ, "绝缘电阻(MΩ)", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-绝缘电阻(MΩ)计算结果");
        Double calcWeight4 = calc(insulationResistanceAbsorptionRatio, EvaluateConstant.LXSY_JYDZ_SHB, "绝缘电阻吸收比", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-绝缘电阻吸收比计算结果");

        Double calcWeight5 = calc(phaseDifferenceDcResistance, EvaluateConstant.LXSY_RZZL_DZXJC, "绕组直流电阻相间差(%)", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-绕组直流电阻相间差(%)计算结果");
        Double calcWeight6 = calc(lineDifferenceDcResistance, EvaluateConstant.LXSY_RZZL_DZXJC2, "绕组直流电阻线间差(%)", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-绕组直流电阻线间差(%)计算结果");
        Double calcWeight7 = calc(windingInsulationDielectricLoss, EvaluateConstant.LXSY_RZJYJS, "绕组绝缘介损(tgδ)", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-绕组绝缘介损(tgδ)计算结果");
        Double calcWeight8 = calc(coreInsulationResistance, EvaluateConstant.LXSY_TXJDDZ, "铁芯接地电阻（MΩ）", threeLevelMap, jsonArray, step, "变压器绝缘试验-例行试验-铁芯接地电阻（MΩ）计算结果");

        Double lxsy = calcWeight + calcWeight2 + calcWeight3 + calcWeight4 + calcWeight5 + calcWeight6
                + calcWeight7 + calcWeight8;
        log.info("- >>变压器绝缘试验-例行试验-分项和计算结果: [{}]", lxsy);
        step += 1;
        return CommonUtils.buildResult(step, lxsy);
    }


    public static Double calc(String value, String type, String typeDesc, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step, String stepName) {
        Double aDouble = CommonUtils.calcItemResult(value, type, typeDesc, threeLevelMap, jsonArray, step, stepName);
        log.info("-->>变压器绝缘试验-例行试验-{}计算结果{}", typeDesc, aDouble);
        return aDouble;
    }
}
