package com.huatek.busi.service.impl.transformerCalUtils;

import com.huatek.busi.util.TransformerFormulaCal;
import com.huatek.busi.util.TransformerFormulaSingleton;
import com.huatek.busi.vo.BusiOfflineTransformerBrandVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import com.huatek.torch.frame.exception.OwlException;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.dubbo.common.utils.StringUtils;

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

@Slf4j
public class AgingHealthIndexUtil {

    public static TransformerFormulaCal transformerFormulaCalInstance = TransformerFormulaCal.getInstance();

    public static TransformerFormulaSingleton instance = TransformerFormulaSingleton.getInstance();

    /**
     * @param brand 变压器
     * @return Map<String, Object> map
     * @Title: getTransformerAgingHealthIndex
     * @Description: 变压器主绝缘老化状态健康指数
     * @createDate: Aug 30, 2019 6:27:18 PM
     */
    public static Map<String, Double> getTransformerAgingHealthIndex(BusiOfflineTransformerBrandVo brand, int step, double load) {
        Map<String, Double> map = new HashMap<>();
        String name = brand.getTransformerName();
        String environmentLevel = brand.getEnvironmentLevel();
        LocalDate deliveryTime = brand.getEquipmentDeliveryTime();

        log.info("--->  变压器:{}, 环境等级：{}", name, environmentLevel);
        double FE = transformerFormulaCalInstance.calcEnvironmentCoefficient(environmentLevel);
        log.info("--->  变压器:{}, 环境等级查表系数：{}", name, FE);

        log.info("--->  变压器:{}, 变压器负荷率：{}", name, load);
        load = load * 100;
        double FL = transformerFormulaCalInstance.calcLoadCoefficient(load);
        log.info("--->  变压器:{}, 变压器负荷率查表系数：{}", name, FL);

        // 预期运行寿命 T = TD/(FL*FE)
        double TD = Double.parseDouble(brand.getDesignLife() == null || "".equals(brand.getDesignLife()) ? "30" : brand.getDesignLife());
        log.info("--->  变压器:{}, 变压器设计寿命：{}", name, TD);
        double expectlife = transformerFormulaCalInstance.calcExpectRunningLife(FL, FE, TD);
        log.info("--->  变压器:{}, 预期使用寿命：{}", name, expectlife);
        // 老化系数
        double B0 = transformerFormulaCalInstance.calcInitAgingCoefficient(expectlife);
//        double B0 = transformerFormulaCalInstance.calcInitAgingCoefficient(TD);
        log.info("--->  变压器:{}, 初始老化系数：{}", name, B0);

        log.info("--->  变压器:{}, 投运时间：{}", name, deliveryTime);
        long dayDiff = instance.calcDateDifference(deliveryTime, LocalDate.now(), "YEARS");
        log.info("--->  变压器:{}, 运行时间：{}", name, dayDiff);

        Double agingHealthIndex = transformerFormulaCalInstance.calcAgingHealthIndex(B0, dayDiff);
        log.info("---> 变压器主绝缘老化状态健康指数  ：{}", agingHealthIndex);

        map.put("expectLife", expectlife);
        map.put("B0", B0);
        map.put("agingHealthIndex", agingHealthIndex);
        map.put("step", (double) step);
        return map;
    }

    public static double getTransformerLoad(BusiOfflineTransformerBrandVo brand, TransformerEvaluateDataVo evaluateData) {
        BigDecimal ratedCapacity = new BigDecimal(brand.getRatedCapacity());
        BigDecimal totalCapacity = ratedCapacity.multiply(new BigDecimal("24000000"));
        log.info("----------------标牌额定容量：" + ratedCapacity);
        log.info("----------------转化公式 ratedCapacity*1000d*1000d*24d");
        log.info("----------------转化成秒后的容量：" + totalCapacity);
        if(StringUtils.isEmpty(evaluateData.getWorkingVoltage()) &&
                StringUtils.isEmpty(evaluateData.getWorkingCurrent())&&
                StringUtils.isEmpty(evaluateData.getWorkingTime())) {
            throw new OwlException("缺少计算负荷率相关数据");
        }

        BigDecimal workingVoltage = new BigDecimal(evaluateData.getWorkingVoltage()).multiply(new BigDecimal("1000"));
        BigDecimal workingCurrent = new BigDecimal(evaluateData.getWorkingCurrent());
        BigDecimal workingTime = new BigDecimal(evaluateData.getWorkingTime());

        BigDecimal workingLoad = workingVoltage.multiply(workingCurrent).multiply(workingTime);
        BigDecimal pumpingLoad = new BigDecimal(0);
        if(StringUtils.isNotEmpty(evaluateData.getPumpingVoltage()) &&
                StringUtils.isNotEmpty(evaluateData.getPumpingCurrent())&&
                StringUtils.isNotEmpty(evaluateData.getPumpingTime())){
            BigDecimal pumpingVoltage = new BigDecimal(evaluateData.getPumpingVoltage()).multiply(new BigDecimal("1000"));
            BigDecimal pumpingCurrent = new BigDecimal(evaluateData.getPumpingCurrent());
            BigDecimal pumpingTime = new BigDecimal(evaluateData.getPumpingTime());
            pumpingLoad = pumpingVoltage.multiply(pumpingCurrent).multiply(pumpingTime);
        }
        BigDecimal totalLoad = workingLoad.add(pumpingLoad);
        log.info("----------------发电负荷：" + workingLoad);
        log.info("----------------抽水负荷：" + pumpingLoad);
        log.info("----------------总负荷：" + totalLoad);
        BigDecimal load = totalLoad.divide(totalCapacity, 4, RoundingMode.HALF_UP);
        log.info("----------------负荷率：" + load);
        return load.doubleValue();
    }
}
