package com.njcn.product.device.overlimit.util;



import com.njcn.product.device.overlimit.pojo.Overlimit;
import com.njcn.product.system.dict.enums.DicDataEnum;

import java.math.BigDecimal;
import java.math.RoundingMode;

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


    /**
     * 谐波电流系数
     */
    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 Overlimit globalAssemble(Float voltageLevel, Float protocolCapacity, Float devCapacity,
                                           Float shortCapacity, Integer powerFlag, Integer lineType) {
        Overlimit overlimit = new Overlimit();
        voltageDeviation(overlimit,voltageLevel);
        frequency(overlimit);
        voltageFluctuation(overlimit,voltageLevel);
        voltageFlicker(overlimit,voltageLevel);
        totalHarmonicDistortion(overlimit,voltageLevel);
        uHarm(overlimit,voltageLevel);
        threeVoltageUnbalance(overlimit);
        interharmonicCurrent(overlimit,voltageLevel);

        if(lineType == 1) {
          //配网
            Float[] iHarmTem = new Float[49];
            for (int i = 0; i <= 48; i++) {

                iHarmTem[i] = -3.14159f;
            }
            overlimit.buildIHarm(iHarmTem);
            overlimit.setINeg(-3.14159f);
        }else {
            //主网
            iHarm(overlimit, voltageLevel, protocolCapacity, devCapacity, shortCapacity);
            negativeSequenceCurrent(overlimit, voltageLevel, shortCapacity);
        }
        return overlimit;
    }


    /**
     * 电压偏差限值
     *
     */
    public static void voltageDeviation(Overlimit overlimit,Float voltageLevel) {
        float voltageDev = 3.14159f,uvoltageDev = 3.14159f;
        if(voltageLevel <= Float.parseFloat(DicDataEnum.V220.getCode())){
            voltageDev = 7.0f;
            uvoltageDev=-10.0f;
        }else if(voltageLevel>Float.parseFloat(DicDataEnum.V220.getCode())&&voltageLevel<Float.parseFloat(DicDataEnum.KV20.getCode())){
            voltageDev = 7.0f;
            uvoltageDev=-7.0f;
        }else if(voltageLevel>=Float.parseFloat(DicDataEnum.KV20.getCode())&&voltageLevel<Float.parseFloat(DicDataEnum.KV35.getCode())){
            voltageDev = 7.0f;
            uvoltageDev=-7.0f;
        }else if(voltageLevel>=Float.parseFloat(DicDataEnum.KV35.getCode())&&voltageLevel<Float.parseFloat(DicDataEnum.KV66.getCode())){
            voltageDev = 10.0f;
            uvoltageDev=-10.0f;
        }else if(voltageLevel>=Float.parseFloat(DicDataEnum.KV66.getCode())&&voltageLevel<=Float.parseFloat(DicDataEnum.KV110.getCode())){
            voltageDev = 7.0f;
            uvoltageDev=-3.0f;
        }else if(voltageLevel>Float.parseFloat(DicDataEnum.KV110.getCode())){
            voltageDev = 10.0f;
            uvoltageDev=-10.0f;
        }
        overlimit.setVoltageDev(voltageDev);
        overlimit.setUvoltageDev(uvoltageDev);
    }


    /**
     * 频率偏差
     * 默认限值：±0.2Hz（即：-0.2 Hz≤限值≤0.2 Hz）
     */
    public static void frequency(Overlimit 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(Overlimit overlimit, Float voltageLevel) {
        if (voltageLevel < Float.parseFloat(DicDataEnum.KV35.getCode())) {
            overlimit.setVoltageFluctuation(3.0f);
        } else {
            overlimit.setVoltageFluctuation(2.5f);
        }
    }



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


    /**
     * 总谐波电压畸变率
     *
     *
     */
    public static void totalHarmonicDistortion(Overlimit overlimit, Float voltageLevel) {
        float result = 3.14159f;
        if (voltageLevel < Float.parseFloat(DicDataEnum.KV6.getCode())) {
            result = 5.0f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV6.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV20.getCode())){
            result = 4.0f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV35.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV66.getCode())){
            result = 3.0f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV110.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV1000.getCode())){
            result = 2.0f;
        }
        overlimit.setUaberrance(result);
    }



    /**
     * 谐波电压含有率
     */
    public static void uHarm(Overlimit overlimit, Float voltageLevel) {
        float resultOdd = 3.14159f,resultEven = 3.14159f;
        if (voltageLevel < Float.parseFloat(DicDataEnum.KV6.getCode())) {
            resultOdd = 4.0f;
            resultEven = 2.0f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV6.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV20.getCode())){
            resultOdd = 3.2f;
            resultEven = 1.6f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV35.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV66.getCode())){
            resultOdd = 2.4f;
            resultEven = 1.2f;
        } else if(voltageLevel >= Float.parseFloat(DicDataEnum.KV110.getCode()) && voltageLevel <= Float.parseFloat(DicDataEnum.KV1000.getCode())){
            resultOdd = 1.6f;
            resultEven = 0.8f;
        }
        overlimit.buildUharm(resultEven,resultOdd);
    }


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


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

    /**
     * 谐波电流限值
     */
    public static void iHarm(Overlimit 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 < DicDataEnum.KV6.getValue()) {
            x = 0;
        } else if (voltageLevel<DicDataEnum.KV10.getValue()) {
            x = 1;
        } else if (voltageLevel<DicDataEnum.KV35.getValue()) {
            x = 2;
        } else if (voltageLevel<DicDataEnum.KV66.getValue()) {
            x = 3;
        } else if (voltageLevel<DicDataEnum.KV110.getValue()) {
            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< DicDataEnum.KV06.getValue()){
            capValue = 10;
        }else if(voltageLevel<DicDataEnum.KV20.getValue()){
            capValue = 100;
        }else if(voltageLevel<DicDataEnum.KV35.getValue()){
            capValue = 200;
        }else if(voltageLevel<DicDataEnum.KV66.getValue()){
            capValue = 250;
        }else if(voltageLevel<DicDataEnum.KV110.getValue()){
            capValue = 500;
        }else if(voltageLevel<DicDataEnum.KV220.getValue()){
            capValue = 750;
        }else if(voltageLevel<DicDataEnum.KV330.getValue()){
            capValue = 2000;
        }else if(voltageLevel<DicDataEnum.KV500.getValue()){
            capValue = 3000;
        }else if(voltageLevel<DicDataEnum.KV750.getValue()){
            capValue = 4500;
        }else if(voltageLevel<DicDataEnum.KV1000.getValue()){
            capValue = 7000;
        }else {
            capValue = 9000;
        }
        return capValue;
    }


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


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

    public static void interharmonicCurrent(Overlimit overlimit,Float voltageLevel){
        float aValue,bValue;
        if(voltageLevel <= DicDataEnum.KV1.getValue()){
            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(Overlimit 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<DicDataEnum.KV6.getValue()){
            value = 0.4f;
        }else if(voltageLevel<DicDataEnum.KV10.getValue()){
            value = 6.3f;
        }else if(voltageLevel<DicDataEnum.KV20.getValue()){
            value = 10.5f;
        }else if(voltageLevel<DicDataEnum.KV35.getValue()){
            value = 21.0f;
        }else if(voltageLevel<DicDataEnum.KV66.getValue()){
            value = 36.5f;
        }else if(voltageLevel<DicDataEnum.KV110.getValue()){
            value = 69.0f;
        }else if(voltageLevel<DicDataEnum.KV220.getValue()){
            value = 115.5f;
        }else if(voltageLevel<DicDataEnum.KV330.getValue()){
            value = 230.0f;
        }else {
            value = 345.0f;
        }
        return value;
    }


    public static void main(String[] args) {
        System.out.println("sss");
        float aa = iHarmCalculate(9,500f,10,10,0.002222222222f);

        System.out.println(aa);
    }
}
