package com.njcn.gather.tool.addledger.util;

import com.njcn.gather.tool.addledger.pojo.po.AddOverlimitPO;

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


/**
 * pqs
 * 限值计算工具类
 *
 * @author cdf
 * @date 2023/5/15
 */
public class COverlimitUtil {

    private static final float DEFAULT_LIMIT = 3.14159f;
    private static final float DEFAULT_CURRENT_LIMIT = -3.14159f;
    private static final int LINE_TYPE_DISTRIBUTION = 1;

    private static final float KV_0_22 = 0.22f;
    private static final float KV_0_6 = 0.6f;
    private static final float KV_1 = 1.0f;
    private static final float KV_6 = 6.0f;
    private static final float KV_10 = 10.0f;
    private static final float KV_20 = 20.0f;
    private static final float KV_35 = 35.0f;
    private static final float KV_66 = 66.0f;
    private static final float KV_110 = 110.0f;
    private static final float KV_220 = 220.0f;
    private static final float KV_330 = 330.0f;
    private static final float KV_500 = 500.0f;
    private static final float KV_750 = 750.0f;
    private static final float KV_1000 = 1000.0f;


    /**
     * 谐波电流系数
     */
    private static final double[][] ARR = {
            {78, 62, 39, 62, 26, 44, 19, 21, 16, 28, 13, 24, 11, 12, 9.7, 18, 8.6, 16, 7.8, 8.9, 7.1, 14, 6.5, 12, 6.0, 6.9, 5.6, 11, 5.2, 10, 4.9, 5.6, 4.6, 8.9, 4.3, 8.4, 4.1, 4.8, 3.9, 7.6, 3.7, 7.2, 3.5, 4.1, 3.4, 6.6, 3.3, 6.3, 3.1},
            {43, 34, 21, 34, 14, 24, 11, 11, 8.5, 16, 7.1, 13, 6.1, 6.8, 5.3, 10, 4.7, 9, 4.3, 4.9, 3.9, 7.4, 3.6, 6.8, 3.3, 3.8, 3.1, 5.9, 2.9, 5.5, 2.7, 3.1, 2.5, 4.9, 2.4, 4.6, 2.3, 2.6, 2.2, 4.1, 2.0, 4.0, 2.0, 2.3, 1.9, 3.6, 1.8, 3.5, 1.7},
            {26, 20, 13, 20, 8.5, 15, 6.4, 6.8, 5.1, 9.3, 4.3, 7.9, 3.7, 4.1, 3.2, 6, 2.8, 5.4, 2.6, 2.9, 2.3, 4.5, 2.1, 4.1, 2.0, 2.2, 1.9, 3.4, 1.7, 3.2, 1.6, 1.8, 1.5, 2.9, 1.4, 2.7, 1.4, 1.5, 1.3, 2.4, 1.2, 2.3, 1.2, 1.3, 1.1, 2.1, 1.1, 2.0, 1.0},
            {15, 12, 7.7, 12, 5.1, 8.8, 3.8, 4.1, 3.1, 5.6, 2.6, 4.7, 2.2, 2.5, 1.9, 3.6, 1.7, 3.2, 1.5, 1.8, 1.4, 2.7, 1.3, 2.5, 1.2, 1.3, 1.1, 2.1, 1.0, 1.9, 0.9, 1.1, 0.9, 1.7, 0.8, 1.6, 0.8, 0.9, 0.8, 1.5, 0.7, 1.4, 0.7, 0.8, 0.7, 1.3, 0.6, 1.2, 0.6},
            {16, 13, 8.1, 13, 5.4, 9.3, 4.1, 4.3, 3.3, 5.9, 2.7, 5, 2.3, 2.6, 2, 3.8, 1.8, 3.4, 1.6, 1.9, 1.5, 2.8, 1.4, 2.6, 1.2, 1.4, 1.1, 2.2, 1.1, 2.1, 1.0, 1.2, 0.9, 1.9, 0.9, 1.8, 0.8, 1.0, 0.8, 1.6, 0.8, 1.5, 0.7, 0.9, 0.7, 1.4, 0.7, 1.3, 0.6},
            {12, 9.6, 6, 9.6, 4, 6.8, 3, 3.2, 2.4, 4.3, 2, 3.7, 1.7, 1.9, 1.5, 2.8, 1.3, 2.5, 1.2, 1.4, 1.1, 2.1, 1, 1.9, 0.9, 1.1, 0.9, 1.7, 0.8, 1.5, 0.8, 0.9, 0.7, 1.4, 0.7, 1.3, 0.6, 0.7, 0.6, 1.2, 0.6, 1.1, 0.5, 0.6, 0.5, 1.0, 0.5, 1.0, 0.5}
    };


    /**
     * 计算监测点限值
     * @param voltageLevel 电压等级（10kV = 10 220kV = 220 ）
     * @param protocolCapacity 协议容量
     * @param devCapacity 设备容量
     * @param shortCapacity 短路容量
     * @param powerFlag 0.用户侧 1.电网侧
     * @param lineType 0.主网 1.配网 需要注意配网目前没有四种容量，谐波电流幅值限值,负序电流限值无法计算默认-3.14159
     */
    public static AddOverlimitPO globalAssemble(Float voltageLevel, Float protocolCapacity, Float devCapacity,
                                                Float shortCapacity, Integer powerFlag, Integer lineType) {
        if (voltageLevel == null) {
            throw new IllegalArgumentException("电压等级不能为空");
        }
        AddOverlimitPO overlimit = new AddOverlimitPO();
        voltageDeviation(overlimit,voltageLevel);
        frequency(overlimit);
        voltageFluctuation(overlimit,voltageLevel);
        voltageFlicker(overlimit,voltageLevel);
        totalHarmonicDistortion(overlimit,voltageLevel);
        uHarm(overlimit,voltageLevel);
        threeVoltageUnbalance(overlimit);
        interharmonicCurrent(overlimit,voltageLevel);

        if(isDistributionLine(lineType)) {
          //配网
            Float[] iHarmTem = new Float[49];
            for (int i = 0; i <= 48; i++) {
                //目前只处理了配网II类测点，III类测点暂未处理，III类测点参考主网
                iHarmTem[i] = getHarmTag(i+2,voltageLevel).floatValue();
            }
            overlimit.buildIHarm(iHarmTem);
            overlimit.setINeg(DEFAULT_CURRENT_LIMIT);
        }else if (hasMainNetworkCapacity(protocolCapacity, devCapacity, shortCapacity)) {
            //主网
            iHarm(overlimit, voltageLevel, protocolCapacity, devCapacity, shortCapacity);
            negativeSequenceCurrent(overlimit, voltageLevel, shortCapacity);
        } else {
            setDefaultCurrentLimit(overlimit);
        }
        return overlimit;
    }

    private static boolean isDistributionLine(Integer lineType) {
        return lineType != null && lineType == LINE_TYPE_DISTRIBUTION;
    }

    private static boolean hasMainNetworkCapacity(Float protocolCapacity, Float devCapacity, Float shortCapacity) {
        return protocolCapacity != null && devCapacity != null && shortCapacity != null
                && devCapacity > 0 && shortCapacity > 0;
    }

    private static void setDefaultCurrentLimit(AddOverlimitPO overlimit) {
        Float[] iHarmTem = new Float[49];
        Arrays.fill(iHarmTem, DEFAULT_CURRENT_LIMIT);
        overlimit.buildIHarm(iHarmTem);
        overlimit.setINeg(DEFAULT_CURRENT_LIMIT);
    }


    /**
     * 电压偏差限值
     *
     */
    public static void voltageDeviation(AddOverlimitPO overlimit,Float voltageLevel) {
        float voltageDev = DEFAULT_LIMIT,uvoltageDev = DEFAULT_LIMIT;
        if(voltageLevel <= KV_0_22){
            voltageDev = 7.0f;
            uvoltageDev=-10.0f;
        }else if(voltageLevel>KV_0_22&&voltageLevel<KV_20){
            voltageDev = 7.0f;
            uvoltageDev=-7.0f;
        }else if(voltageLevel>=KV_20&&voltageLevel<KV_35){
            voltageDev = 7.0f;
            uvoltageDev=-7.0f;
        }else if(voltageLevel>=KV_35&&voltageLevel<KV_66){
            voltageDev = 10.0f;
            uvoltageDev=-10.0f;
        }else if(voltageLevel>=KV_66&&voltageLevel<=KV_110){
            voltageDev = 7.0f;
            uvoltageDev=-3.0f;
        }else if(voltageLevel>KV_110){
            voltageDev = 10.0f;
            uvoltageDev=-10.0f;
        }
        overlimit.setVoltageDev(voltageDev);
        overlimit.setUvoltageDev(uvoltageDev);
    }


    /**
     * 频率偏差
     * 默认限值：±0.2Hz（即：-0.2 Hz≤限值≤0.2 Hz）
     */
    public static void frequency(AddOverlimitPO overlimit) {
        overlimit.setFreqDev(0.2f);
    }


    /**
     * 电压波动
     * 对LV、MV：0≤限值≤3%；对HV：0≤限值≤2.5%。
     * LV、MV、HV的定义：
     * 低压（LV）                 UN≤1kV
     * 中压（MV）          1kV＜UN≤35kV
     * 高压（HV）         35kV＜UN≤220kV
     * 超高压（EHV），220kV＜UN，参照HV执行
     */
    public static void voltageFluctuation(AddOverlimitPO overlimit, Float voltageLevel) {
        if (voltageLevel < KV_35) {
            overlimit.setVoltageFluctuation(3.0f);
        } else {
            overlimit.setVoltageFluctuation(2.5f);
        }
    }



    /**
     * 电压闪变
     * ≤110kV	1
     * ＞110kV	0.8
     */
    public static void voltageFlicker(AddOverlimitPO overlimit, Float voltageLevel) {
        if (voltageLevel <= KV_110) {
            overlimit.setFlicker(1.0f);
        } else {
            overlimit.setFlicker(0.8f);
        }
    }


    /**
     * 总谐波电压畸变率
     *
     *
     */
    public static void totalHarmonicDistortion(AddOverlimitPO overlimit, Float voltageLevel) {
        float result = DEFAULT_LIMIT;
        if (voltageLevel < KV_6) {
            result = 5.0f;
        } else if(voltageLevel >= KV_6 && voltageLevel <= KV_20){
            result = 4.0f;
        } else if(voltageLevel >= KV_35 && voltageLevel <= KV_66){
            result = 3.0f;
        } else if(voltageLevel >= KV_110 && voltageLevel <= KV_1000){
            result = 2.0f;
        }
        overlimit.setUaberrance(result);
    }



    /**
     * 谐波电压含有率
     */
    public static void uHarm(AddOverlimitPO overlimit, Float voltageLevel) {
        float resultOdd = DEFAULT_LIMIT,resultEven = DEFAULT_LIMIT;
        if (voltageLevel < KV_6) {
            resultOdd = 4.0f;
            resultEven = 2.0f;
        } else if(voltageLevel >= KV_6 && voltageLevel <= KV_20){
            resultOdd = 3.2f;
            resultEven = 1.6f;
        } else if(voltageLevel >= KV_35 && voltageLevel <= KV_66){
            resultOdd = 2.4f;
            resultEven = 1.2f;
        } else if(voltageLevel >= KV_110 && voltageLevel <= KV_1000){
            resultOdd = 1.6f;
            resultEven = 0.8f;
        }
        overlimit.buildUharm(resultEven,resultOdd);
    }


    /**
     * 负序电压不平衡（三相电压不平衡度）
     *
     */
    public static void threeVoltageUnbalance(AddOverlimitPO overlimit) {
        overlimit.setUbalance(2.0f);
        overlimit.setShortUbalance(4.0f);
    }


    /*---------------------------------谐波电流限值start-----------------------------------*/

    /**
     * 谐波电流限值
     */
    public static void iHarm(AddOverlimitPO overlimit, Float voltageLevel,Float protocolCapacity,Float devCapacity,Float shortCapacity) {
        float calCap = shortCapacity/getDlCapByVoltageLevel(voltageLevel);
        //24谐波电流幅值
        Float[] iHarmTem = new Float[49];
        for (int i = 0; i <= 48; i++) {
            float inHarm = iHarmCalculate(i+2,voltageLevel,protocolCapacity,devCapacity,calCap);
            iHarmTem[i] = inHarm;
        }
        overlimit.buildIHarm(iHarmTem);
    }
    /**
    * @Description: iHarmCalculate
    * @Param:  protocolCapacity 协议容量 devCapacity设备容量 calCap 短路容量
    * @return: float
    * @Author: clam
    * @Date: 2024/2/4
    */
    private static float iHarmCalculate(int nHarm, Float voltageLevel,float protocolCapacity, float devCapacity,float calCap) {
        double tag = calCap*getHarmTag(nHarm,voltageLevel);
        Double limit =  getHarmonicLimit(nHarm,tag,new BigDecimal(String.valueOf(devCapacity)).doubleValue(),new BigDecimal(String.valueOf(protocolCapacity)).doubleValue());
        BigDecimal bigDecimal = BigDecimal.valueOf(limit).setScale(4,RoundingMode.HALF_UP);
        return bigDecimal.floatValue();
    }


    /**
     * 电流谐波限值
     */
    private static Double getHarmTag(Integer iCount, Float voltageLevel) {
        int x, y;
        if (voltageLevel < KV_6) {
            x = 0;
        } else if (voltageLevel<KV_10) {
            x = 1;
        } else if (voltageLevel<KV_35) {
            x = 2;
        } else if (voltageLevel<KV_66) {
            x = 3;
        } else if (voltageLevel<KV_110) {
            x = 4;
        } else {
            x = 5;
        }
        y = iCount - 2;
        return ARR[x][y];
    }

    /**
     * 相位叠加系数的取值
     */
    public static Double getHarmonicLimit(Integer times, double iTag, double supply, double user) {
        if (supply == 0) {
            return 0.0;
        }
        double coefficient = 2.0;
        if (times == 3) {
            coefficient = 1.1;
        } else if (times == 5) {
            coefficient = 1.2;
        } else if (times == 7) {
            coefficient = 1.4;
        } else if (times == 11) {
            coefficient = 1.8;
        } else if (times == 13) {
            coefficient = 1.9;
        }
        BigDecimal bd = new BigDecimal(iTag * Math.pow((user / supply), (1 / coefficient)));
        bd = bd.setScale(6, RoundingMode.HALF_UP);
        return Double.parseDouble(bd.toString());
    }

    /**
     * 根据电压等级获取基准短路容量
     */
    public static float getDlCapByVoltageLevel(Float voltageLevel){
        float capValue;
        if(voltageLevel< KV_0_6){
            capValue = 10;
        }else if(voltageLevel<KV_20){
            capValue = 100;
        }else if(voltageLevel<KV_35){
            capValue = 200;
        }else if(voltageLevel<KV_66){
            capValue = 250;
        }else if(voltageLevel<KV_110){
            capValue = 500;
        }else if(voltageLevel<KV_220){
            capValue = 750;
        }else if(voltageLevel<KV_330){
            capValue = 2000;
        }else if(voltageLevel<KV_500){
            capValue = 3000;
        }else if(voltageLevel<KV_750){
            capValue = 4500;
        }else if(voltageLevel<KV_1000){
            capValue = 7000;
        }else {
            capValue = 9000;
        }
        return capValue;
    }


    /*---------------------------------谐波电流限值end-----------------------------------*/


    /**
     * 间谐波电压含有率
     */

    public static void interharmonicCurrent(AddOverlimitPO overlimit,Float voltageLevel){
        float aValue,bValue;
        if(voltageLevel <= KV_1){
            aValue = 0.2f;bValue = 0.5f;
        }else {
            aValue = 0.16f;bValue = 0.4f;
        }
        overlimit.setInuharm1(aValue);
        overlimit.setInuharm2(aValue);

        overlimit.setInuharm3(bValue);
        overlimit.setInuharm4(bValue);
        overlimit.setInuharm5(bValue);
        overlimit.setInuharm6(bValue);
        overlimit.setInuharm7(bValue);
        overlimit.setInuharm8(bValue);
        overlimit.setInuharm9(bValue);
        overlimit.setInuharm10(bValue);
        overlimit.setInuharm11(bValue);
        overlimit.setInuharm12(bValue);
        overlimit.setInuharm13(bValue);
        overlimit.setInuharm14(bValue);
        overlimit.setInuharm15(bValue);
        overlimit.setInuharm16(bValue);
    }


    /**
     * 负序电流限值
     */
    public static void negativeSequenceCurrent(AddOverlimitPO overlimit,Float voltageLevel,Float shortCapacity){
        double v = (0.013*shortCapacity*1000)/(getUl(voltageLevel)*Math.sqrt(3));
        overlimit.setINeg((float) v);
    }

    /**
     * 获取额定线电压
     */
    private static float getUl(Float voltageLevel){
        float value;
        if(voltageLevel<KV_6){
            value = 0.4f;
        }else if(voltageLevel<KV_10){
            value = 6.3f;
        }else if(voltageLevel<KV_20){
            value = 10.5f;
        }else if(voltageLevel<KV_35){
            value = 21.0f;
        }else if(voltageLevel<KV_66){
            value = 36.5f;
        }else if(voltageLevel<KV_110){
            value = 69.0f;
        }else if(voltageLevel<KV_220){
            value = 115.5f;
        }else if(voltageLevel<KV_330){
            value = 230.0f;
        }else {
            value = 345.0f;
        }
        return value;
    }

}
