package com.njcn.advance.service.assess.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.advance.pojo.dto.assess.AssessTreeDTO;
import com.njcn.advance.pojo.dto.assess.HarmonicsIAndZDTO;
import com.njcn.advance.pojo.param.assess.AssessParam;
import com.njcn.advance.pojo.po.assess.*;
import com.njcn.advance.pojo.vo.assess.AssessResultVO;
import com.njcn.advance.service.assess.*;
import com.njcn.advance.utils.EasyExcelUtil;
import com.njcn.advance.utils.ExcelTemplateReplacer;
import com.njcn.advance.utils.assess.*;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.utils.COverlimitUtil;
import com.njcn.harmonic.api.HarmDataFeignClient;
import com.njcn.harmonic.pojo.vo.AssessEvaluation;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import lombok.RequiredArgsConstructor;
import org.apache.commons.math3.complex.Complex;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2025/06/06  下午 3:23【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class SecondaryEvaluationServiceImpl implements SecondaryEvaluationService {

    private final IAssessLoadParamService iAssessLoadParamService;
    private final IAssessDicHarmonicCharacterService iAssessDicHarmonicCharacterService;
    private final DicDataFeignClient dicDataFeignClient;
    private final IAssessDicTransformerService iAssessDicTransformerService;
    private final IAssessTransformerParamService iAssessTransformerParamService;

    private final IAssessCapacitorParamService iAssessCapacitorParamService;
    private final IAssessUserService iAssessUserService;
    private final IAssessDicCableTypeService iAssessDicCableTypeService;
    private final IAssessResultService iAssessResultService;
    private final IAssessResultAdditionService iAssessResultAdditionService;
    private final IAssessOverlimitService iAssessOverlimitService;
    private final IAssessShockLoadParamService iAssessShockLoadParamService;
    private final IAssessDicShockLoadService iAssessDicShockLoadService;

    private final IAssessWindLoadParamService iAssessWindLoadParamService;

    private final DeptFeignClient deptFeignClient;
    private final HarmDataFeignClient harmDataFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void evaluation(AssessParam param, MultipartFile file) {
        //解析文件取CP95值 2-50次谐波电流含有率，电压含有率，负序电流，三项不平衡度，A,B,C三项最大；
        String assessId = param.getAssessId();
        List<Double> iMax;
        Double unbalance;
        List<Double> uMax;
        Double iNeg;
        if (StrUtil.isNotBlank(param.getId())) {
            AssessEvaluation data = harmDataFeignClient.getHarmIAndHarmVData(param).getData();
            if (ObjUtil.isNotNull(data)) {
                iMax = data.getIMax();
                unbalance = data.getUnbalance();
                uMax = data.getUMax();
                iNeg = data.getINeg();
            } else {
                throw new BusinessException("评估原始数据有误！请检查数据是否为空");
            }
        } else {
            try {
                List<Map<Integer, String>> maps = EasyExcelUtil.syncRead(file.getInputStream(), 0, 0);
                //电流含有率
                List<Map<Integer, String>> maps1 = maps.subList(3, 52);
                //负序电流
                List<Map<Integer, String>> maps2 = maps.subList(52, 53);
                //电压含有率
                List<Map<Integer, String>> maps3 = maps.subList(56, 105);
                //三项不平衡度
                List<Map<Integer, String>> maps4 = maps.subList(105, 106);

                iMax = maps1.stream().map(temp -> {
                    double a = Double.valueOf(temp.get(5));
                    double b = Double.valueOf(temp.get(10));
                    double c = Double.valueOf(temp.get(15));
                    double v = a > b ? a : b;
                    double max = v > c ? v : c;
                    return max;

                }).collect(Collectors.toList());
                iNeg = maps2.stream().map(temp -> {
                    double a = Double.valueOf(temp.get(5));
                    double b = Double.valueOf(temp.get(10));
                    double c = Double.valueOf(temp.get(15));
                    double v = a > b ? a : b;
                    double max = v > c ? v : c;
                    return max;

                }).findFirst().get();

                uMax = maps3.stream().map(temp -> {
                    double a = Double.valueOf(temp.get(5));
                    double b = Double.valueOf(temp.get(10));
                    double c = Double.valueOf(temp.get(15));
                    double v = a > b ? a : b;
                    double max = v > c ? v : c;
                    return max;

                }).collect(Collectors.toList());

                unbalance = maps4.stream().map(temp -> {
                    double a = Double.valueOf(temp.get(5));
                    double b = Double.valueOf(temp.get(10));
                    double c = Double.valueOf(temp.get(15));
                    double v = a > b ? a : b;
                    double max = v > c ? v : c;
                    return max;

                }).findFirst().get();

            } catch (Exception e) {
                throw new BusinessException("解析文件失败，请按模版上传文件");

            }
        }

        AssessResult assessResult = new AssessResult();
        AssessResultAddition assessResultAddition = new AssessResultAddition();
        AssessOverlimit assessOverlimit = new AssessOverlimit();
        //取出电压字典
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.DEV_VOLTAGE_STAND.getName()).getData();
        /*************************************谐波评估算法**********************************/

        // 1. 计算单个负荷谐波阻抗和谐波电流
        //2. 计算配变谐波阻抗 并叠加
        List<HarmonicsIAndZDTO> loadList = new ArrayList<>();

        List<AssessLoadParam> assessLoadParamLista = iAssessLoadParamService.lambdaQuery().eq(AssessLoadParam::getAssessId, assessId).list();
        if (CollectionUtils.isEmpty(assessLoadParamLista)) {
            throw new BusinessException("评估用户负荷参数缺失");
        } else {
            assessLoadParamLista.forEach(temp -> {
                HarmonicsIAndZDTO zuphAndi = new HarmonicsIAndZDTO();
                String harmId = temp.getHarmId();
                AssessDicHarmonicCharacter byId = iAssessDicHarmonicCharacterService.getById(harmId);
                Double[] harmDict = changeAssessDicHarmonicCharacter(byId);
                Double uln = getVoltage(temp.getLinevoltageScale(), dictDataList);
                Double pln = getVoltage(temp.getTranslowScale(), dictDataList);
                Double uKr = temp.getTransImpedance().doubleValue();
                double sRt = temp.getTranshighCapacity().doubleValue();
                double pl = temp.getNonlinearloadPower().doubleValue();
                Double pf = temp.getPowerFactor().doubleValue();
                //1. 计算单个负荷谐波阻抗和谐波电流
                HarmonicsIAndZDTO zln = PowerQualityAlgorithmUtil.loadharmonicsCal(uln, pln, pl, pf, harmDict);
                //2. 计算配变谐波阻抗 并叠加
                HarmonicsIAndZDTO zph = PowerQualityAlgorithmUtil.transZCal(sRt, pln, uKr, 0.0);
                Complex[] zuph = new Complex[49];
                for (int i = 0; i < 49; i++) {
                    Complex complex = zln.getZ()[i];
                    Complex complex2 = zph.getZ()[i];
                    zuph[i] = complex.add(complex2).multiply(uln * uln / (pln * pln));
                }
                zuphAndi.setI(zln.getI());
                zuphAndi.setZ(zuph);
                loadList.add(zuphAndi);
            });
        }
        // 3. 各负荷并联叠加
        HarmonicsIAndZDTO loadIAndZDTO = PowerQualityAlgorithmUtil.loadShunt(loadList);


        // 4. 变压器谐波阻抗串联叠加
        HarmonicsIAndZDTO transIAndZDTO;
        Double transUl;
        Double transUh;
        AssessDicTransformer assessDicTransformer;
        AssessTransformerParam assessTransformerParam = iAssessTransformerParamService.lambdaQuery().eq(AssessTransformerParam::getAssessId, assessId).one();
        if(Objects.isNull(assessTransformerParam)){
            throw new BusinessException("变压器参数缺失");
        }
        assessDicTransformer = iAssessDicTransformerService.lambdaQuery().eq(AssessDicTransformer::getTranstypeId, assessTransformerParam.getTranstypeId()).one();
        transUl = getVoltage(assessDicTransformer.getTranslowScale(), dictDataList);
        transUh = getVoltage(assessDicTransformer.getTranshighScale(), dictDataList);
        Double sRt = assessDicTransformer.getTransCapa().doubleValue();
        Double uKr = assessDicTransformer.getShortCircuitImpedance().doubleValue();
        Double pKrt = assessDicTransformer.getLoadLoss().doubleValue();
        transIAndZDTO = PowerQualityAlgorithmUtil.transZCal(sRt, transUl, uKr, pKrt);


        // 5. 电容器组阻抗串联叠加
        AssessCapacitorParam assessCapacitorParam = iAssessCapacitorParamService.lambdaQuery().eq(AssessCapacitorParam::getAssessId, assessId).one();
        HarmonicsIAndZDTO capIAndZDTO;
        if (Objects.isNull(assessCapacitorParam)) {
            throw new BusinessException("评估用户电容器参数缺失");
        } else {
            Integer capacitorNum = assessCapacitorParam.getCapacitorNum();
            double capacitorCapacity = assessCapacitorParam.getCapacitorCapacity().doubleValue();
            double capacitorReactance = assessCapacitorParam.getCapacitorReactance().doubleValue();
            double capacitorScale = getVoltage(assessCapacitorParam.getCapacitorScale(), dictDataList);
            capIAndZDTO = PowerQualityAlgorithmUtil.capacitorZCal(capacitorScale, capacitorCapacity, capacitorReactance, capacitorNum);
        }

        // 6. 输电线路阻抗串联叠加
        AssessUser assessUser = iAssessUserService.lambdaQuery().eq(AssessUser::getAssessId, assessId).one();
        Double userScale = getVoltage(assessUser.getUserScale(), dictDataList);
        AssessDicCableType assessDicCableType = iAssessDicCableTypeService.lambdaQuery().eq(AssessDicCableType::getLinetypeId, assessUser.getLinetypeId()).one();
        Double r1 = assessDicCableType.getPosR1().doubleValue();
        Double x1 = assessDicCableType.getPosX1().doubleValue();
        Double lineLen = assessUser.getLineLen().doubleValue();
        HarmonicsIAndZDTO lineIAndZDTO = PowerQualityAlgorithmUtil.lineZCal(r1, x1, lineLen);

        // 7. 背景谐波阻抗串联叠加
        Double un = getVoltage(assessUser.getBusScale(), dictDataList);
        Double scc = assessUser.getMinshortcircuitCapacity().doubleValue();
        HarmonicsIAndZDTO bHarmIAndZDTO = PowerQualityAlgorithmUtil.bHarmonicCal(un, scc);
        // 8. 谐波电压计算
        //叠加主变压器,电容器, 输电线路,背景谐波阻
        //有电容器
        Complex[] zcph = new Complex[49];
        Double[] ipch = new Double[49];
        Double[] upch = new Double[49];
        //无电容器
        Complex[] zph = new Complex[49];
        Double[] iph = new Double[49];
        Double[] uph = new Double[49];


        if (Objects.nonNull(assessTransformerParam)) {
            Double m = transUh / transUl;
            for (int h = 0; h < 49; h++) {
                zcph[h] = loadIAndZDTO.getZ()[h].add(transIAndZDTO.getZ()[h]).multiply(m * m)
                        .add(capIAndZDTO.getZ()[h])
                        .add(lineIAndZDTO.getZ()[h])
                        .add(bHarmIAndZDTO.getZ()[h]);

                zph[h] = loadIAndZDTO.getZ()[h].add(transIAndZDTO.getZ()[h]).multiply(m * m)
                        .add(lineIAndZDTO.getZ()[h])
                        .add(bHarmIAndZDTO.getZ()[h]);
                ipch[h] = (loadIAndZDTO.getI()[h] / m) * (dMod(loadIAndZDTO.getZ()[h].divide(zcph[h])));
                iph[h] = (loadIAndZDTO.getI()[h] / m) * (dMod(loadIAndZDTO.getZ()[h].divide(zph[h])));

                upch[h] = ipch[h] * dMod(zcph[h]) / 1000;
                uph[h] = iph[h] * dMod(zph[h]) / 1000;
            }
        } else {
            for (int h = 0; h < 49; h++) {
                zcph[h] = loadIAndZDTO.getZ()[h].add(capIAndZDTO.getZ()[h])
                        .add(lineIAndZDTO.getZ()[h])
                        .add(bHarmIAndZDTO.getZ()[h]);
                zph[h] = loadIAndZDTO.getZ()[h]
                        .add(lineIAndZDTO.getZ()[h])
                        .add(bHarmIAndZDTO.getZ()[h]);

                ipch[h] = loadIAndZDTO.getI()[h] * (dMod(loadIAndZDTO.getZ()[h].divide(zcph[h])));
                iph[h] = loadIAndZDTO.getI()[h] * (dMod(loadIAndZDTO.getZ()[h].divide(zph[h])));

                upch[h] = ipch[h] * dMod(zcph[h]) / 1000;
                uph[h] = iph[h] * dMod(zph[h]) / 1000;
            }

        }

        // 9. 谐波电流叠加（背景谐波） // 10. 谐波电压叠加（背景谐波）
        Double[] ich = new Double[49];
        Double[] ih = new Double[49];

        Double[] urch = new Double[49];
        Double[] urh = new Double[49];

        for (int h = 0; h < 49; h++) {
            ich[h] = Math.sqrt(ipch[h] * ipch[h] + iMax.get(h) * iMax.get(h) + PowerQualityAlgorithmUtil.getK(h) * ipch[h] * iMax.get(h));
            ih[h] = Math.sqrt(iph[h] * iph[h] + iMax.get(h) * iMax.get(h) + PowerQualityAlgorithmUtil.getK(h) * iph[h] * iMax.get(h));

            urch[h] = Math.sqrt(upch[h] * upch[h] + uMax.get(h) * uMax.get(h) + PowerQualityAlgorithmUtil.getK(h) * upch[h] * uMax.get(h)) * 100 * 1.732 / userScale;
            urh[h] = Math.sqrt(uph[h] * uph[h] + uMax.get(h) * uMax.get(h) + PowerQualityAlgorithmUtil.getK(h) * uph[h] * uMax.get(h)) * 100 * 1.732 / userScale;


        }
        //限值计算
        Overlimit overlimit = COverlimitUtil.globalAssemble(un.floatValue(),
                assessUser.getAgreedCapacity().floatValue(),
                assessUser.getPowersupplyCapacity().floatValue(),
                assessUser.getMinshortcircuitCapacity().floatValue(), 1, 0);


        /************************************电压波动算法**********************************/
        //非风电场取冲击负荷最大的哪个
        List<AssessShockLoadParam> assessShockLoadParams = iAssessShockLoadParamService.lambdaQuery().eq(AssessShockLoadParam::getAssessId, assessId).list();
        assessShockLoadParams.forEach(temp -> {
            AssessDicShockLoad byId = iAssessDicShockLoadService.getById(temp.getInpactloadtypeId());
            if (Objects.isNull(byId)) {
                throw new BusinessException("冲击负荷字典缺失");
            }
            temp.setInpactloadMultiple(byId.getInpactloadMultiple());
        });
        AssessShockLoadParam assessShockLoadParam = assessShockLoadParams.stream().max(Comparator.comparing(e -> e.getInpactloadMultiple().multiply(e.getInpactloadPower()).doubleValue())).get();

        String isWindfarm = assessUser.getIsWindfarm();
        Double dMax;
        if (Objects.equals(isWindfarm, "01")) {
            //风电场
            dMax = VoltageFluctuationUtil.calculateWindFarmVoltageFluctuation(0.95,
                    assessUser.getWindfarmCapacity().doubleValue(),
                    assessUser.getLineGridresistance().doubleValue(),
                    assessUser.getLineGridreactance().doubleValue(),
                    un.doubleValue(),
                    true);
        } else if (Objects.equals(isWindfarm, "02")) {
            //光伏
            dMax = VoltageFluctuationUtil.calculateWindFarmVoltageFluctuation(0.95,
                    assessUser.getWindfarmCapacity().doubleValue(),
                    assessUser.getLineGridresistance().doubleValue(),
                    assessUser.getLineGridreactance().doubleValue(),
                    un.doubleValue(),
                    false);
        } else if (Objects.equals(isWindfarm, "03")) {
            dMax = VoltageFluctuationUtil.calculateImpactLoadVoltageFluctuation(assessShockLoadParam.getInpactloadPower().doubleValue(),
                    assessShockLoadParam.getInpactloadMultiple().doubleValue(), scc);
        } else {
            throw new BusinessException("用户负荷类型不正确");
        }
        double dmaxLimit = VoltageFluctuationUtil.getVoltageFluctuationLimit(un.doubleValue(), assessShockLoadParam.getInpactloadFreq());

        /************************************电压闪变算法**********************************/
        Double plt;
        if (Objects.equals(isWindfarm, "01")) {
            List<AssessWindLoadParam> assessWindLoadParams = iAssessWindLoadParamService.lambdaQuery().eq(AssessWindLoadParam::getAssessId, assessId).list();
            if (CollUtil.isEmpty(assessWindLoadParams)) {
                throw new BusinessException("风电参数缺失");
            }
            List<VoltageFlickerUtil.WindFarmLoads> windFarmLoadsList = assessWindLoadParams.stream().map(temp -> {
                VoltageFlickerUtil.WindFarmLoads windFarmLoads = new VoltageFlickerUtil.WindFarmLoads();
                windFarmLoads.setFlickerCoefficient(temp.getFlickerCoeff().doubleValue());
                windFarmLoads.setRatedApparentPower(temp.getS().doubleValue());
                return windFarmLoads;
            }).collect(Collectors.toList());
            plt = VoltageFlickerUtil.calculateWindFarmFlicker(scc, windFarmLoadsList);
        } else {
            plt = VoltageFlickerUtil.calculateNonWindFarmFlicker(0.30, dMax);
        }
        double flickerLimit = VoltageFlickerUtil.getFlickerLimit(un);

        /************************************电压偏差算法**********************************/
        Double transformerNoLoadCurrent, transformerShortCircuitVoltage, transformerRatedCapacity;
        if (Objects.nonNull(assessTransformerParam)) {
            transformerNoLoadCurrent = assessDicTransformer.getNoloadCur().doubleValue();
            transformerShortCircuitVoltage = assessDicTransformer.getShortCircuitImpedance().doubleValue();
            transformerRatedCapacity = assessDicTransformer.getTransCapa().doubleValue();
        } else {
            transformerNoLoadCurrent = 0.0;
            transformerShortCircuitVoltage = 0.0;
            transformerRatedCapacity = 1.0;
        }
        //用户投入，100% ，电网高峰
        double capOnH100Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, true, 1.0);
        //用户投入，60%，，电网高峰
        double capOnH60Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, true, 0.6);
        //用户投入，0% ，电网高峰
        double capOnH0Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, true, 0);

        //用户未投入，100% ，电网高峰
        double capOffH100Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, false, 1.0);

        //用户未投入，60%，，电网高峰
        double capOffH60Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, false, 0.6);

        //用户未投入，0%三个档估算 ，电网高峰
        double capOffH0Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.65, false, 0);

        //用户投入，100% ，电网低谷
        double capOnL100Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, true, 1.0);

        //用户投入，60%，电网低谷
        double capOnL60Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, true, 0.6);

        //用户投入，0%电网低谷
        double capOnL0Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, true, 0);


        //用户未投入，100% ，电网低谷
        double capOffL100Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, false, 1.0);

        //用户未投入，60% ，电网低谷
        double capOffL65Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, false, 0.6);

        //用户未投入，0% ，电网低谷
        double capOffL0Deltav = VoltageDeviationUtil.calculateVoltageDeviation(0.95, scc
                , assessCapacitorParam.getCapacitorCapacity().doubleValue(), assessUser.getPowersupplyCapacity().doubleValue(),
                assessUser.getWindfarmCapacity().doubleValue(), transformerNoLoadCurrent,
                transformerShortCircuitVoltage, transformerRatedCapacity, 0.15, false, 0);

        //限值
        VoltageDeviationUtil.VoltageDeviationLimits voltageDeviationLimits = VoltageDeviationUtil.getVoltageDeviationLimits(un);

        /************************************三相电压不平衡算法**********************************/
        //严格估算
        double accurUnblance = ThreePhaseUnbalanceUtil.calculateVoltageUnbalance(scc, assessUser.getPowersupplyCapacity().floatValue(),
                un, Objects.equals(assessUser.getSingleLoad(), "00"), iNeg, unbalance, true);
        //一般估算
        double approUnblance = ThreePhaseUnbalanceUtil.calculateVoltageUnbalance(scc, assessUser.getPowersupplyCapacity().floatValue(),
                un, Objects.equals(assessUser.getSingleLoad(), "00"), iNeg, unbalance, true);

        //限值
        double voltageUnbalanceLimit = ThreePhaseUnbalanceUtil.getVoltageUnbalanceLimit();
        //组装结果
        assembleRsult(assessResult, assessId,
                ih,
                urh,
                dMax,
                plt,
                capOnH100Deltav,
                capOnH60Deltav,
                capOnH0Deltav,
                capOffH100Deltav,
                capOffH60Deltav,
                capOffH0Deltav,
                capOnL100Deltav,
                capOnL60Deltav,
                capOnL0Deltav,
                capOffL100Deltav,
                capOffL65Deltav,
                capOffL0Deltav,
                accurUnblance,
                approUnblance);
        assembleRsultAddition(assessResultAddition, assessId, ich, urch, plt);
        assembleOverlimit(assessOverlimit, assessId, overlimit, dmaxLimit, flickerLimit, voltageDeviationLimits, voltageUnbalanceLimit);

        iAssessResultService.saveOrUpdate(assessResult);
        iAssessResultAdditionService.saveOrUpdate(assessResultAddition);
        iAssessOverlimitService.saveOrUpdate(assessOverlimit);
    }

    @Override
    public List<AssessTreeDTO> evaluationTree(String deptId) {
        List<AssessTreeDTO> result = new ArrayList<>();
        List<String> deptIds = deptFeignClient.getDepSonIdtByDeptId(deptId).getData();
        List<AssessUser> assessUserList = iAssessUserService.list(new LambdaQueryWrapper<AssessUser>().in(AssessUser::getDeptId, deptIds));
        Map<String, List<AssessUser>> listMap = assessUserList.stream().collect(Collectors.groupingBy(AssessUser::getDeptName));
        listMap.forEach((deptName, list) -> {
            AssessTreeDTO assessTreeDTO = new AssessTreeDTO();
            assessTreeDTO.setTreeId(list.get(0).getDeptId());
            assessTreeDTO.setTreeName(deptName);
            List<AssessTreeDTO> childrenList = new ArrayList<>();
            list.forEach(it -> {
                AssessTreeDTO children = new AssessTreeDTO();
                children.setTreeId(it.getAssessId());
                children.setTreeName(it.getAssessName());
                childrenList.add(children);
            });
            assessTreeDTO.setChildren(childrenList);
            result.add(assessTreeDTO);
        });
        return result;
    }

    @Override
    public AssessResultVO assessResult(String assessId) {
        AssessResultVO resultVO = null;
        AssessResult result = iAssessResultService.getById(assessId);
        AssessUser byId = iAssessUserService.getById(assessId);
        if (ObjUtil.isNotNull(result) && ObjUtil.isNotNull(byId)) {
            resultVO = new AssessResultVO();
            resultVO.setUser(byId);
            //取出电压字典,并获取电压系数
            List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeName(DicDataTypeEnum.DEV_VOLTAGE_STAND.getName()).getData();
            AssessUser assessUser = iAssessUserService.lambdaQuery().eq(AssessUser::getAssessId, assessId).one();
            Double userScale = getVoltage(assessUser.getUserScale(), dictDataList);

            AssessResultAddition resultAddition = iAssessResultAdditionService.getById(assessId);
            AssessOverlimit overLimit = iAssessOverlimitService.getById(assessId);
            List<AssessShockLoadParam> list = iAssessShockLoadParamService.list(new LambdaQueryWrapper<AssessShockLoadParam>()
                    .eq(AssessShockLoadParam::getAssessId, assessId));

            //冲击负荷数量
            Integer sum = list.stream().mapToInt(x -> x.getInpactloadNum().intValue()).sum();

            //电压波动评估结果
            AssessResultVO.EvaluationResult fluc = new AssessResultVO.EvaluationResult();
            fluc.setInPactLoadNum(sum);
            fluc.setData(result.getApproFluc());
            fluc.setLimitData(overLimit.getFluc());
            fluc.setIsQualified(NumberUtil.isLess(fluc.getData(), fluc.getLimitData()));
            resultVO.setFluc(fluc);
            //闪变评估结果
            AssessResultVO.EvaluationResult plt = new AssessResultVO.EvaluationResult();
            plt.setInPactLoadNum(sum);
            plt.setData(result.getPlt());
            plt.setLimitData(overLimit.getPlt());
            plt.setIsQualified(NumberUtil.isLess(plt.getData(), plt.getLimitData()));
            resultVO.setPlt(plt);

            //用户无功设备不运行
            resultVO.setCapOff(capOff(result, userScale, overLimit));

            //用户无功设备全运行
            resultVO.setCapOn(capOn(result, userScale, overLimit));

            //三项电压不平平衡
            List<AssessResultVO.EvaluationResult> unblance = new ArrayList<>();
            AssessResultVO.EvaluationResult evaluationResult = new AssessResultVO.EvaluationResult();
            evaluationResult.setData(result.getAccurUnblance());
            evaluationResult.setLimitData(overLimit.getUnblance());
            evaluationResult.setIsQualified(NumberUtil.isLess(evaluationResult.getData(), evaluationResult.getLimitData()));
            unblance.add(evaluationResult);

            evaluationResult = new AssessResultVO.EvaluationResult();
            evaluationResult.setData(result.getApproUnblance());
            evaluationResult.setLimitData(overLimit.getUnblance());
            evaluationResult.setIsQualified(NumberUtil.isLess(evaluationResult.getData(), evaluationResult.getLimitData()));
            unblance.add(evaluationResult);

            resultVO.setUnblance(unblance);

            resultVO.setHarmI(getHarms(result, resultAddition, overLimit, "i"));
            resultVO.setHarmV(getHarms(result, resultAddition, overLimit, "v"));
        }
        return resultVO;
    }

    @Override
    public void downAssessResult(HttpServletResponse response, String assessId) throws IOException {
        AssessResultVO resultVO = this.assessResult(assessId);
        if (ObjUtil.isNotNull(resultVO)) {
            Map<String, ExcelTemplateReplacer.ReplacementData> data = new HashMap<>();
            data.put("${name}", ExcelTemplateReplacer.ReplacementData.create(resultVO.getUser().getAssessName(), false));
            data.put("${subname}", ExcelTemplateReplacer.ReplacementData.create(resultVO.getUser().getPowerstationName(), false));
            data.put("${subvname}", ExcelTemplateReplacer.ReplacementData.create(resultVO.getUser().getBusName(), false));
            data.put("${linename}", ExcelTemplateReplacer.ReplacementData.create(resultVO.getUser().getLineName(), false));
            //电压
            AssessResultVO.EvaluationResult fluc = resultVO.getFluc();
            data.put("${num}", ExcelTemplateReplacer.ReplacementData.create(fluc.getInPactLoadNum(), false));
            data.put("${fluc}", ExcelTemplateReplacer.ReplacementData.create(fluc.getData(), !fluc.getIsQualified()));
            data.put("${fluclimt}", ExcelTemplateReplacer.ReplacementData.create(fluc.getLimitData(), false));
            data.put("${isfluc}", ExcelTemplateReplacer.ReplacementData.create(isData(fluc.getIsQualified()), false));

            //闪变
            AssessResultVO.EvaluationResult plt = resultVO.getPlt();
            data.put("${plt}", ExcelTemplateReplacer.ReplacementData.create(plt.getData(), !plt.getIsQualified()));
            data.put("${pltlimt}", ExcelTemplateReplacer.ReplacementData.create(plt.getLimitData(), false));
            data.put("${isplt}", ExcelTemplateReplacer.ReplacementData.create(isData(plt.getIsQualified()), false));

            List<AssessResultVO.EvaluationResult> unblance = resultVO.getUnblance();
            data.put("${app}", ExcelTemplateReplacer.ReplacementData.create(unblance.get(0).getData(), !unblance.get(0).getIsQualified()));
            data.put("${acc}", ExcelTemplateReplacer.ReplacementData.create(unblance.get(1).getData(), !unblance.get(1).getIsQualified()));
            data.put("${unblance}", ExcelTemplateReplacer.ReplacementData.create(unblance.get(0).getLimitData(), false));
            data.put("${isapp}", ExcelTemplateReplacer.ReplacementData.create(isData(unblance.get(0).getIsQualified()), false));
            data.put("${isacc}", ExcelTemplateReplacer.ReplacementData.create(isData(unblance.get(1).getIsQualified()), false));

            List<AssessResultVO.Useless> capOff = resultVO.getCapOff();
            data.put("${up}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(0).getCapUP(), false));
            data.put("${down}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(0).getCapDown(), false));

            data.put("${noth100}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(0).getPercent(), !capOff.get(0).getIsQualified()));
            data.put("${noth60}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(1).getPercent(), !capOff.get(1).getIsQualified()));
            data.put("${noth0}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(2).getPercent(), !capOff.get(2).getIsQualified()));
            data.put("${notl100}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(3).getPercent(), !capOff.get(3).getIsQualified()));
            data.put("${notl60}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(4).getPercent(), !capOff.get(4).getIsQualified()));
            data.put("${notl0}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(5).getPercent(), !capOff.get(5).getIsQualified()));
            data.put("${ynoth100}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(0).getVoltagePercent(), false));
            data.put("${ynoth60}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(1).getVoltagePercent(), false));
            data.put("${ynoth0}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(2).getVoltagePercent(), false));
            data.put("${ynotl100}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(3).getVoltagePercent(), false));
            data.put("${ynotl60}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(4).getVoltagePercent(), false));
            data.put("${ynotl0}", ExcelTemplateReplacer.ReplacementData.create(capOff.get(5).getVoltagePercent(), false));
            data.put("${isnoth100}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(0).getIsQualified()), false));
            data.put("${isnoth60}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(1).getIsQualified()), false));
            data.put("${isnoth0}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(2).getIsQualified()), false));
            data.put("${isnotl100}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(3).getIsQualified()), false));
            data.put("${isnotl60}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(4).getIsQualified()), false));
            data.put("${isnotl0}", ExcelTemplateReplacer.ReplacementData.create(isData(capOff.get(5).getIsQualified()), false));

            List<AssessResultVO.Useless> capOn = resultVO.getCapOn();
            data.put("${hash100}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(0).getPercent(), !capOn.get(0).getIsQualified()));
            data.put("${hash60}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(1).getPercent(), !capOn.get(1).getIsQualified()));
            data.put("${hash0}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(2).getPercent(), !capOn.get(2).getIsQualified()));
            data.put("${hasl100}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(3).getPercent(), !capOn.get(3).getIsQualified()));
            data.put("${hasl60}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(4).getPercent(), !capOn.get(4).getIsQualified()));
            data.put("${hasl0}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(5).getPercent(), !capOn.get(5).getIsQualified()));
            data.put("${yhash100}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(0).getVoltagePercent(), false));
            data.put("${yhash60}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(1).getVoltagePercent(), false));
            data.put("${yhash0}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(2).getVoltagePercent(), false));
            data.put("${yhasl100}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(3).getVoltagePercent(), false));
            data.put("${yhasl60}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(4).getVoltagePercent(), false));
            data.put("${yhasl0}", ExcelTemplateReplacer.ReplacementData.create(capOn.get(5).getVoltagePercent(), false));
            data.put("${ishash100}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(0).getIsQualified()), false));
            data.put("${ishash60}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(1).getIsQualified()), false));
            data.put("${ishash0}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(2).getIsQualified()), false));
            data.put("${ishasl100}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(3).getIsQualified()), false));
            data.put("${ishasl60}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(4).getIsQualified()), false));
            data.put("${ishasl0}", ExcelTemplateReplacer.ReplacementData.create(isData(capOn.get(5).getIsQualified()), false));
            List<AssessResultVO.Harm> harmI = resultVO.getHarmI();
            for (int i = 2; i < harmI.size() + 2; i++) {
                data.put("${noti" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmI.get(i - 2).getNotInvested(), !harmI.get(i - 2).getIsNotQualified()));
                data.put("${hasi" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmI.get(i - 2).getHasInvested(), !harmI.get(i - 2).getIsHasQualified()));
                data.put("${limi" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmI.get(i - 2).getLimitData(), false));
                data.put("${isi" + i + "}", ExcelTemplateReplacer.ReplacementData.create(isData(harmI.get(i - 2).getIsQualified()), false));
            }
            List<AssessResultVO.Harm> harmV = resultVO.getHarmV();
            for (int i = 2; i < harmV.size() + 2; i++) {
                data.put("${notv" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmV.get(i - 2).getNotInvested(), !harmV.get(i - 2).getIsNotQualified()));
                data.put("${hasv" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmV.get(i - 2).getHasInvested(), !harmV.get(i - 2).getIsHasQualified()));
                data.put("${limv" + i + "}", ExcelTemplateReplacer.ReplacementData.create(harmV.get(i - 2).getLimitData(), false));
                data.put("${isv" + i + "}", ExcelTemplateReplacer.ReplacementData.create(isData(harmV.get(i - 2).getIsQualified()), false));
            }

            ExcelTemplateReplacer.replaceDownTemplate("pgjg.xlsx",
                    resultVO.getUser().getAssessName() + "_评估结果.xlsx",
                    data,
                    ExcelTemplateReplacer.getCellStyleConfig(),
                    response);


        }

    }

    @Override
    public void downTemplateAssessResult(HttpServletResponse response) throws IOException {
        try (InputStream is = new ClassPathResource("pgjgmb.xlsx").getInputStream();
             Workbook workbook = new XSSFWorkbook(is)) {
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                response.setHeader("Content-Disposition", "attachment; filename= 评估模板");
                response.setContentType("application/octet-stream;charset=UTF-8");
                workbook.write(outputStream);
                outputStream.close();
            } catch (Exception e) {
                System.out.println("评估结果异常" + e);
            } finally {
                if (workbook != null) {
                    workbook.close();
                }
            }
        }
    }

    private String isData(Boolean b) {
        if (b) {
            return "合格";
        }
        return "不合格";
    }

    private List<AssessResultVO.Harm> getHarms(AssessResult result, AssessResultAddition resultAddition, AssessOverlimit overLimit, String type) {
        List<AssessResultVO.Harm> harmI = new ArrayList<>();
        try {
            AssessResultVO.Harm harm;
            for (int i = 2; i < 51; i++) {
                harm = new AssessResultVO.Harm();
                String fieldName = type + i;
                Field notField = result.getClass().getDeclaredField(fieldName);
                notField.setAccessible(true);
                harm.setNotInvested(new BigDecimal(notField.get(result).toString()));
                Field hasField = resultAddition.getClass().getDeclaredField(fieldName);
                hasField.setAccessible(true);
                harm.setHasInvested(new BigDecimal(hasField.get(resultAddition).toString()));
                if (ObjUtil.equals(type, "v")) {
                    if (i % 2 == 0) {
                        fieldName = "vevent";
                    } else {
                        fieldName = "vodd";
                    }
                    Field limitField = overLimit.getClass().getDeclaredField(fieldName);
                    limitField.setAccessible(true);
                    harm.setLimitData(new BigDecimal(limitField.get(overLimit).toString()));
                } else {
                    Field limitField = overLimit.getClass().getDeclaredField(fieldName);
                    limitField.setAccessible(true);
                    harm.setLimitData(new BigDecimal(limitField.get(overLimit).toString()));
                }
                harm.setIsNotQualified(NumberUtil.isLess(harm.getNotInvested(), harm.getLimitData()));
                harm.setIsHasQualified(NumberUtil.isLess(harm.getHasInvested(), harm.getLimitData()));
                harm.setIsQualified(harm.getIsNotQualified() && harm.getIsHasQualified());
                harmI.add(harm);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return harmI;
    }

    private List<AssessResultVO.Useless> capOn(AssessResult result, Double userScale, AssessOverlimit overLimit) {
        List<AssessResultVO.Useless> capOn = new ArrayList<>();
        capOn.add(getUseless(result.getCapOnH100Deltav(), userScale, overLimit));
        capOn.add(getUseless(result.getCapOnH60Deltav(), userScale, overLimit));
        capOn.add(getUseless(result.getCapOnH0Deltav(), userScale, overLimit));
        capOn.add(getUseless(result.getCapOnL100Deltav(), userScale, overLimit));
        capOn.add(getUseless(result.getCapOnL60Deltav(), userScale, overLimit));
        capOn.add(getUseless(result.getCapOnL0Deltav(), userScale, overLimit));
        return capOn;
    }

    private List<AssessResultVO.Useless> capOff(AssessResult result, Double userScale, AssessOverlimit overLimit) {
        List<AssessResultVO.Useless> capOff = new ArrayList<>();
        capOff.add(getUseless(result.getCapOffH100Deltav(), userScale, overLimit));
        capOff.add(getUseless(result.getCapOffH60Deltav(), userScale, overLimit));
        capOff.add(getUseless(result.getCapOffH0Deltav(), userScale, overLimit));
        capOff.add(getUseless(result.getCapOffL100Deltav(), userScale, overLimit));
        capOff.add(getUseless(result.getCapOffL60Deltav(), userScale, overLimit));
        capOff.add(getUseless(result.getCapOffL0Deltav(), userScale, overLimit));
        return capOff;
    }

    private AssessResultVO.Useless getUseless(BigDecimal result, Double userScale, AssessOverlimit overLimit) {
        AssessResultVO.Useless uselessOffH100;
        uselessOffH100 = new AssessResultVO.Useless();
        uselessOffH100.setPercent(result);
        uselessOffH100.setVoltagePercent(NumberUtil.round(uselessOffH100.getPercent().multiply(BigDecimal.valueOf(userScale)), 2));
        uselessOffH100.setCapUP(overLimit.getDltavUp());
        uselessOffH100.setCapDown(overLimit.getDltavDown());
        uselessOffH100.setIsQualified(NumberUtil.isLess(uselessOffH100.getPercent(), uselessOffH100.getCapUP()) &&
                NumberUtil.isGreater(uselessOffH100.getPercent(), uselessOffH100.getCapDown()));
        return uselessOffH100;
    }

    private AssessOverlimit assembleOverlimit(AssessOverlimit assessOverlimit, String assessId, Overlimit overlimit, double dmaxLimit, double flickerLimit, VoltageDeviationUtil.VoltageDeviationLimits voltageDeviationLimits, double voltageUnbalanceLimit) {
        assessOverlimit.setAssessId(assessId);
        assessOverlimit.setVodd(BigDecimal.valueOf(overlimit.getUharm3()).setScale(3, RoundingMode.HALF_UP));
        assessOverlimit.setVevent(BigDecimal.valueOf(overlimit.getUharm2()).setScale(3, RoundingMode.HALF_UP));
        assessOverlimit.setI2((BigDecimal.valueOf(overlimit.getIharm2()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI3((BigDecimal.valueOf(overlimit.getIharm3()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI4((BigDecimal.valueOf(overlimit.getIharm4()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI5((BigDecimal.valueOf(overlimit.getIharm5()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI6((BigDecimal.valueOf(overlimit.getIharm6()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI7((BigDecimal.valueOf(overlimit.getIharm7()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI8((BigDecimal.valueOf(overlimit.getIharm8()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI9((BigDecimal.valueOf(overlimit.getIharm9()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI10((BigDecimal.valueOf(overlimit.getIharm10()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI11((BigDecimal.valueOf(overlimit.getIharm11()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI12((BigDecimal.valueOf(overlimit.getIharm12()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI13((BigDecimal.valueOf(overlimit.getIharm13()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI14((BigDecimal.valueOf(overlimit.getIharm14()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI15((BigDecimal.valueOf(overlimit.getIharm15()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI16((BigDecimal.valueOf(overlimit.getIharm16()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI17((BigDecimal.valueOf(overlimit.getIharm17()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI18((BigDecimal.valueOf(overlimit.getIharm18()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI19((BigDecimal.valueOf(overlimit.getIharm19()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI20((BigDecimal.valueOf(overlimit.getIharm20()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI21((BigDecimal.valueOf(overlimit.getIharm21()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI22((BigDecimal.valueOf(overlimit.getIharm22()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI23((BigDecimal.valueOf(overlimit.getIharm23()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI24((BigDecimal.valueOf(overlimit.getIharm24()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI25((BigDecimal.valueOf(overlimit.getIharm25()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI26((BigDecimal.valueOf(overlimit.getIharm26()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI27((BigDecimal.valueOf(overlimit.getIharm27()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI28((BigDecimal.valueOf(overlimit.getIharm28()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI29((BigDecimal.valueOf(overlimit.getIharm29()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI30((BigDecimal.valueOf(overlimit.getIharm30()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI31((BigDecimal.valueOf(overlimit.getIharm31()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI32((BigDecimal.valueOf(overlimit.getIharm32()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI33((BigDecimal.valueOf(overlimit.getIharm33()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI34((BigDecimal.valueOf(overlimit.getIharm34()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI35((BigDecimal.valueOf(overlimit.getIharm35()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI36((BigDecimal.valueOf(overlimit.getIharm36()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI37((BigDecimal.valueOf(overlimit.getIharm37()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI38((BigDecimal.valueOf(overlimit.getIharm38()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI39((BigDecimal.valueOf(overlimit.getIharm39()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI40((BigDecimal.valueOf(overlimit.getIharm40()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI41((BigDecimal.valueOf(overlimit.getIharm41()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI42((BigDecimal.valueOf(overlimit.getIharm42()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI43((BigDecimal.valueOf(overlimit.getIharm43()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI44((BigDecimal.valueOf(overlimit.getIharm44()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI45((BigDecimal.valueOf(overlimit.getIharm45()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI46((BigDecimal.valueOf(overlimit.getIharm46()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI47((BigDecimal.valueOf(overlimit.getIharm47()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI48((BigDecimal.valueOf(overlimit.getIharm48()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI49((BigDecimal.valueOf(overlimit.getIharm49()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setI50((BigDecimal.valueOf(overlimit.getIharm50()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setPlt((BigDecimal.valueOf(flickerLimit).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setFluc((BigDecimal.valueOf(dmaxLimit).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setDltavUp((BigDecimal.valueOf(voltageDeviationLimits.getUpperLimit()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setDltavDown((BigDecimal.valueOf(voltageDeviationLimits.getLowerLimit()).setScale(3, RoundingMode.HALF_UP)));
        assessOverlimit.setUnblance((BigDecimal.valueOf(voltageUnbalanceLimit).setScale(3, RoundingMode.HALF_UP)));


        return assessOverlimit;
    }

    private AssessResultAddition assembleRsultAddition(AssessResultAddition assessResultAddition, String assessId, Double[] ich, Double[] urch, Double plt) {
        assessResultAddition.setAssessId(assessId);
        assessResultAddition.setDateTime(LocalDateTime.now());
        assessResultAddition.setV2(BigDecimal.valueOf(urch[0]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV3(BigDecimal.valueOf(urch[1]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV4(BigDecimal.valueOf(urch[2]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV5(BigDecimal.valueOf(urch[3]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV6(BigDecimal.valueOf(urch[4]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV7(BigDecimal.valueOf(urch[5]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV8(BigDecimal.valueOf(urch[6]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV9(BigDecimal.valueOf(urch[7]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV10(BigDecimal.valueOf(urch[8]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV11(BigDecimal.valueOf(urch[9]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV12(BigDecimal.valueOf(urch[10]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV13(BigDecimal.valueOf(urch[11]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV14(BigDecimal.valueOf(urch[12]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV15(BigDecimal.valueOf(urch[13]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV16(BigDecimal.valueOf(urch[14]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV17(BigDecimal.valueOf(urch[15]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV18(BigDecimal.valueOf(urch[16]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV19(BigDecimal.valueOf(urch[17]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV20(BigDecimal.valueOf(urch[18]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV21(BigDecimal.valueOf(urch[19]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV22(BigDecimal.valueOf(urch[20]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV23(BigDecimal.valueOf(urch[21]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV24(BigDecimal.valueOf(urch[22]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV25(BigDecimal.valueOf(urch[23]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV26(BigDecimal.valueOf(urch[24]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV27(BigDecimal.valueOf(urch[25]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV28(BigDecimal.valueOf(urch[26]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV29(BigDecimal.valueOf(urch[27]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV30(BigDecimal.valueOf(urch[28]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV31(BigDecimal.valueOf(urch[29]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV32(BigDecimal.valueOf(urch[30]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV33(BigDecimal.valueOf(urch[31]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV34(BigDecimal.valueOf(urch[32]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV35(BigDecimal.valueOf(urch[33]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV36(BigDecimal.valueOf(urch[34]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV37(BigDecimal.valueOf(urch[35]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV38(BigDecimal.valueOf(urch[36]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV39(BigDecimal.valueOf(urch[37]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV40(BigDecimal.valueOf(urch[38]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV41(BigDecimal.valueOf(urch[39]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV42(BigDecimal.valueOf(urch[40]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV43(BigDecimal.valueOf(urch[41]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV44(BigDecimal.valueOf(urch[42]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV45(BigDecimal.valueOf(urch[43]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV46(BigDecimal.valueOf(urch[44]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV47(BigDecimal.valueOf(urch[45]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV48(BigDecimal.valueOf(urch[46]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV49(BigDecimal.valueOf(urch[47]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setV50(BigDecimal.valueOf(urch[48]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI2(BigDecimal.valueOf(ich[0]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI3(BigDecimal.valueOf(ich[1]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI4(BigDecimal.valueOf(ich[2]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI5(BigDecimal.valueOf(ich[3]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI6(BigDecimal.valueOf(ich[4]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI7(BigDecimal.valueOf(ich[5]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI8(BigDecimal.valueOf(ich[6]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI9(BigDecimal.valueOf(ich[7]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI10(BigDecimal.valueOf(ich[8]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI11(BigDecimal.valueOf(ich[9]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI12(BigDecimal.valueOf(ich[10]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI13(BigDecimal.valueOf(ich[11]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI14(BigDecimal.valueOf(ich[12]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI15(BigDecimal.valueOf(ich[13]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI16(BigDecimal.valueOf(ich[14]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI17(BigDecimal.valueOf(ich[15]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI18(BigDecimal.valueOf(ich[16]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI19(BigDecimal.valueOf(ich[17]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI20(BigDecimal.valueOf(ich[18]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI21(BigDecimal.valueOf(ich[19]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI22(BigDecimal.valueOf(ich[20]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI23(BigDecimal.valueOf(ich[21]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI24(BigDecimal.valueOf(ich[22]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI25(BigDecimal.valueOf(ich[23]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI26(BigDecimal.valueOf(ich[24]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI27(BigDecimal.valueOf(ich[25]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI28(BigDecimal.valueOf(ich[26]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI29(BigDecimal.valueOf(ich[27]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI30(BigDecimal.valueOf(ich[28]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI31(BigDecimal.valueOf(ich[29]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI32(BigDecimal.valueOf(ich[30]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI33(BigDecimal.valueOf(ich[31]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI34(BigDecimal.valueOf(ich[32]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI35(BigDecimal.valueOf(ich[33]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI36(BigDecimal.valueOf(ich[34]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI37(BigDecimal.valueOf(ich[35]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI38(BigDecimal.valueOf(ich[36]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI39(BigDecimal.valueOf(ich[37]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI40(BigDecimal.valueOf(ich[38]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI41(BigDecimal.valueOf(ich[39]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI42(BigDecimal.valueOf(ich[40]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI43(BigDecimal.valueOf(ich[41]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI44(BigDecimal.valueOf(ich[42]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI45(BigDecimal.valueOf(ich[43]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI46(BigDecimal.valueOf(ich[44]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI47(BigDecimal.valueOf(ich[45]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI48(BigDecimal.valueOf(ich[46]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI49(BigDecimal.valueOf(ich[47]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setI50(BigDecimal.valueOf(ich[48]).setScale(3, RoundingMode.HALF_UP));
        assessResultAddition.setPlt(BigDecimal.valueOf(plt).setScale(3, RoundingMode.HALF_UP));


        return assessResultAddition;
    }

    private AssessResult assembleRsult(AssessResult assessResult, String assessId, Double[] ih, Double[] urh, Double dMax, Double plt, double capOnH100Deltav, double capOnH60Deltav, double capOnH0Deltav, double capOffH100Deltav, double capOffH60Deltav, double capOffH0Deltav, double capOnL100Deltav, double capOnL60Deltav, double capOnL0Deltav, double capOffL100Deltav, double capOffL65Deltav, double capOffL0Deltav, double accurUnblance, double approUnblance) {
        assessResult.setAssessId(assessId);
        assessResult.setDateTime(LocalDateTime.now());
        assessResult.setV2(BigDecimal.valueOf(urh[0]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV3(BigDecimal.valueOf(urh[1]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV4(BigDecimal.valueOf(urh[2]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV5(BigDecimal.valueOf(urh[3]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV6(BigDecimal.valueOf(urh[4]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV7(BigDecimal.valueOf(urh[5]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV8(BigDecimal.valueOf(urh[6]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV9(BigDecimal.valueOf(urh[7]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV10(BigDecimal.valueOf(urh[8]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV11(BigDecimal.valueOf(urh[9]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV12(BigDecimal.valueOf(urh[10]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV13(BigDecimal.valueOf(urh[11]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV14(BigDecimal.valueOf(urh[12]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV15(BigDecimal.valueOf(urh[13]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV16(BigDecimal.valueOf(urh[14]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV17(BigDecimal.valueOf(urh[15]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV18(BigDecimal.valueOf(urh[16]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV19(BigDecimal.valueOf(urh[17]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV20(BigDecimal.valueOf(urh[18]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV21(BigDecimal.valueOf(urh[19]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV22(BigDecimal.valueOf(urh[20]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV23(BigDecimal.valueOf(urh[21]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV24(BigDecimal.valueOf(urh[22]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV25(BigDecimal.valueOf(urh[23]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV26(BigDecimal.valueOf(urh[24]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV27(BigDecimal.valueOf(urh[25]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV28(BigDecimal.valueOf(urh[26]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV29(BigDecimal.valueOf(urh[27]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV30(BigDecimal.valueOf(urh[28]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV31(BigDecimal.valueOf(urh[29]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV32(BigDecimal.valueOf(urh[30]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV33(BigDecimal.valueOf(urh[31]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV34(BigDecimal.valueOf(urh[32]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV35(BigDecimal.valueOf(urh[33]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV36(BigDecimal.valueOf(urh[34]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV37(BigDecimal.valueOf(urh[35]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV38(BigDecimal.valueOf(urh[36]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV39(BigDecimal.valueOf(urh[37]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV40(BigDecimal.valueOf(urh[38]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV41(BigDecimal.valueOf(urh[39]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV42(BigDecimal.valueOf(urh[40]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV43(BigDecimal.valueOf(urh[41]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV44(BigDecimal.valueOf(urh[42]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV45(BigDecimal.valueOf(urh[43]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV46(BigDecimal.valueOf(urh[44]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV47(BigDecimal.valueOf(urh[45]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV48(BigDecimal.valueOf(urh[46]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV49(BigDecimal.valueOf(urh[47]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setV50(BigDecimal.valueOf(urh[48]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI2(BigDecimal.valueOf(ih[0]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI3(BigDecimal.valueOf(ih[1]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI4(BigDecimal.valueOf(ih[2]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI5(BigDecimal.valueOf(ih[3]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI6(BigDecimal.valueOf(ih[4]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI7(BigDecimal.valueOf(ih[5]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI8(BigDecimal.valueOf(ih[6]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI9(BigDecimal.valueOf(ih[7]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI10(BigDecimal.valueOf(ih[8]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI11(BigDecimal.valueOf(ih[9]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI12(BigDecimal.valueOf(ih[10]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI13(BigDecimal.valueOf(ih[11]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI14(BigDecimal.valueOf(ih[12]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI15(BigDecimal.valueOf(ih[13]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI16(BigDecimal.valueOf(ih[14]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI17(BigDecimal.valueOf(ih[15]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI18(BigDecimal.valueOf(ih[16]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI19(BigDecimal.valueOf(ih[17]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI20(BigDecimal.valueOf(ih[18]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI21(BigDecimal.valueOf(ih[19]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI22(BigDecimal.valueOf(ih[20]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI23(BigDecimal.valueOf(ih[21]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI24(BigDecimal.valueOf(ih[22]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI25(BigDecimal.valueOf(ih[23]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI26(BigDecimal.valueOf(ih[24]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI27(BigDecimal.valueOf(ih[25]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI28(BigDecimal.valueOf(ih[26]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI29(BigDecimal.valueOf(ih[27]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI30(BigDecimal.valueOf(ih[28]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI31(BigDecimal.valueOf(ih[29]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI32(BigDecimal.valueOf(ih[30]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI33(BigDecimal.valueOf(ih[31]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI34(BigDecimal.valueOf(ih[32]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI35(BigDecimal.valueOf(ih[33]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI36(BigDecimal.valueOf(ih[34]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI37(BigDecimal.valueOf(ih[35]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI38(BigDecimal.valueOf(ih[36]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI39(BigDecimal.valueOf(ih[37]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI40(BigDecimal.valueOf(ih[38]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI41(BigDecimal.valueOf(ih[39]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI42(BigDecimal.valueOf(ih[40]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI43(BigDecimal.valueOf(ih[41]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI44(BigDecimal.valueOf(ih[42]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI45(BigDecimal.valueOf(ih[43]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI46(BigDecimal.valueOf(ih[44]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI47(BigDecimal.valueOf(ih[45]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI48(BigDecimal.valueOf(ih[46]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI49(BigDecimal.valueOf(ih[47]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setI50(BigDecimal.valueOf(ih[48]).setScale(3, RoundingMode.HALF_UP));
        assessResult.setPlt(BigDecimal.valueOf(plt).setScale(3, RoundingMode.HALF_UP));
        assessResult.setApproFluc(BigDecimal.valueOf(dMax).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnH100Deltav(BigDecimal.valueOf(capOnH100Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnH60Deltav(BigDecimal.valueOf(capOnH60Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnH0Deltav(BigDecimal.valueOf(capOnH0Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffH100Deltav(BigDecimal.valueOf(capOffH100Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffH60Deltav(BigDecimal.valueOf(capOffH60Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffH0Deltav(BigDecimal.valueOf(capOffH0Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnL100Deltav(BigDecimal.valueOf(capOnL100Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnL60Deltav(BigDecimal.valueOf(capOnL60Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOnL0Deltav(BigDecimal.valueOf(capOnL0Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffL100Deltav(BigDecimal.valueOf(capOffL100Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffL60Deltav(BigDecimal.valueOf(capOffL65Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setCapOffL0Deltav(BigDecimal.valueOf(capOffL0Deltav).setScale(3, RoundingMode.HALF_UP));
        assessResult.setApproUnblance(BigDecimal.valueOf(approUnblance).setScale(3, RoundingMode.HALF_UP));
        assessResult.setAccurUnblance(BigDecimal.valueOf(accurUnblance).setScale(3, RoundingMode.HALF_UP));


        return assessResult;
    }

    private Double[] changeAssessDicHarmonicCharacter(AssessDicHarmonicCharacter byId) {
        Double[] doubles = new Double[49];
        for (int h = 0; h < 49; h++) {
            doubles[h] = ((BigDecimal) ReflectUtils.getValue(byId, "i" + (h + 2))).doubleValue();
        }
        return doubles;
    }

    /**
     * @Description: 根据电压字典获取电压等级
     * @Param: [id, dictDataList]
     * @return: java.lang.Double
     * @Author: clam
     * @Date: 2023/2/23
     */
    private Double getVoltage(String id, List<DictData> dictDataList) {
        for (DictData dictData : dictDataList) {
            if (dictData.getId().equals(id)) {
                Double result = Double.valueOf(dictData.getValue());
                return result;
            }
        }
        throw new BusinessException("电压等级有误");
    }

    private double dMod(Complex complex) {
        return Math.sqrt(complex.getReal() * complex.getReal() + complex.getImaginary() * complex.getImaginary());
    }

}
