package com.njcn.device.biz.utils;



import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.system.enums.DicDataEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


@Component
@Deprecated
public class COverlimit {


    private static int MAX_SCALE_NO = 9;
    private static int MAXOVERLIMITNUM = 55 + 16;//WW 2017-08-10 overlimit增加电压偏差的下偏差值,原来个数是53 WW2017-11-29 54->53  新增负序电流 间谐波电压含有率
    private static float[] fULimit = {
            2.0f, 1.6f, 0.8f, 10.0f,
            // 3.0f,2.4f,1.2f,10.0f,
            3.0f, 2.4f, 1.2f, 10.0f,
            4.0f, 3.2f, 1.6f, 7.0f,
            4.0f, 3.2f, 1.6f, 7.0f,
            5.0f, 4.0f, 2.0f, 7.0f,
            3.0f, 2.4f, 1.2f, 10.0f,
    };
    private static float[] fUDevL_Limit = {-3, -7, -10};


    //谐波电流幅值系数
    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}
    };



    //间谐波含有率
    private static float[] InharmLimit1 = {
            0.16f, 0.16f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f, 0.4f
    };
    private static float[] InharmLimit2 = {
            0.2f, 0.2f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f
    };

    private static float GetVoltageLimit(int nHarm, int iScaleSelect)//谐波含有率
    {
        if (nHarm < 0) {
            return 0.0f;
        }
        if (iScaleSelect >= MAX_SCALE_NO) {
            return 0.0f;
        }
        switch (iScaleSelect) {
            case 6:
                iScaleSelect = 4;
                break;
            case 7:
                iScaleSelect = 2;
                break;
            case 8:
                iScaleSelect = 0;
                break;
        }
        if (iScaleSelect < 2) {
            iScaleSelect = 0;
        }
        else {
            iScaleSelect--;
        }
        int nSelect = iScaleSelect * 4 + 1;
        if (nHarm % 2 == 0) {
            nSelect = iScaleSelect * 4 + 2;
        }

        return fULimit[nSelect];
    }

    private static float GetVoltageJBLimit(int iScaleSelect)//谐波畸变率
    {
        if (iScaleSelect >= MAX_SCALE_NO)
            return 0.0f;
        switch (iScaleSelect) {
            case 6:
                iScaleSelect = 4;
                break;
            case 7:
                iScaleSelect = 2;
                break;
            case 8:
                iScaleSelect = 0;
                break;
        }
        if (iScaleSelect < 2) {
            iScaleSelect = 0;
        }
        else {
            iScaleSelect--;
        }
        int nSelect = iScaleSelect * 4;
        return fULimit[nSelect];
    }

    private static float GetVoltageDevationLimit(int iScaleSelect)//电压偏差
    {
        if (iScaleSelect >= MAX_SCALE_NO) {
            return 0.0f;
        }
        switch (iScaleSelect) {
            case 6:
                iScaleSelect = 4;
                break;
            case 7:
                iScaleSelect = 2;
                break;
            case 8:
                iScaleSelect = 0;
                break;
        }
        if (iScaleSelect < 2) {
            iScaleSelect = 0;
        }
        else {
            iScaleSelect--;
        }
        int nSelect = iScaleSelect * 4 + 3;
        return fULimit[nSelect];
    }

    private static float GetVoltageDevationLimit_L(int iScaleSelect)//电压偏差负数
    {
        if (iScaleSelect >= MAX_SCALE_NO) {
            return 0.0f;
        }
        int nReturn = 0;
        if (iScaleSelect == 3 || iScaleSelect == 4 || iScaleSelect == 5 || iScaleSelect == 6) {
            nReturn = 1;
        }
        else if (iScaleSelect == -1) {
            nReturn = 2;
        }
        return fUDevL_Limit[nReturn];
    }

    private static float GetCurrentLimit(int nHarm, String voltageLevel, float fDLRL, float fJZRL, float fXYRL, float fSBRL) {
        Double tag = getHarmTag(nHarm,voltageLevel);
        Double limit =  getHarmonicLimit(nHarm,tag,new BigDecimal(String.valueOf(fSBRL)).doubleValue(),new BigDecimal(String.valueOf(fXYRL)).doubleValue());
        BigDecimal bigDecimal = new BigDecimal(limit).setScale(4,RoundingMode.HALF_UP);
        return bigDecimal.floatValue();
    }



    //电流谐波限值
    private static Double getHarmTag(Integer iCount, String voltageLevel) {
        int x, y;
        if (DicDataEnum.KV038.getName().equalsIgnoreCase(voltageLevel) || DicDataEnum.V380.getName().equalsIgnoreCase(voltageLevel) || DicDataEnum.V400.getName().equalsIgnoreCase(voltageLevel) || DicDataEnum.KV04.getName().equalsIgnoreCase(voltageLevel)) {
            x = 0;
        } else if (DicDataEnum.KV6.getName().equalsIgnoreCase(voltageLevel)) {
            x = 1;
        } else if (DicDataEnum.KV10.getName().equalsIgnoreCase(voltageLevel) || DicDataEnum.KV20.getName().equalsIgnoreCase(voltageLevel)) {
            x = 2;
        } else if (DicDataEnum.KV35.getName().equalsIgnoreCase(voltageLevel)) {
            x = 3;
        } else if (DicDataEnum.KV66.getName().equalsIgnoreCase(voltageLevel)) {
            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(2, RoundingMode.HALF_UP);
        return Double.parseDouble(bd.toString());
    }




    public static float GetFCurrentLimit(String strScale, float fDLRL) {
        float uL = 0.0f;
        switch (strScale) {
            case "0.38":
                uL = 0.4f;
                break;
            case "6":
                uL = 6.3f;
                break;
            case "10":
                uL = 10.5f;
                break;
            case "20":
                uL = 21.0f;
                break;
            case "35":
                uL = 36.5f;
                break;
            case "66":
                uL = 69.0f;
                break;
            case "110":
                uL = 115.0f;
                break;
            case "220":
                uL = 230.0f;
                break;
            case "330":
                uL = 345.0f;
                break;
            case "500":
                uL = 520.0f;
                break;
            case "515":
                uL = 535.0f;
                break;
            case "800":
                uL = 825.0f;
                break;
            default:
                return 0.0f;
        }

        return 2.6f / 100 * fDLRL * 1000 / ((float) Math.sqrt(3) * uL);
    }

    public static float GetInHarm(int i, int iScaleSelect) {
        if (iScaleSelect == 5) {
            return InharmLimit1[i];
        } else {
            return InharmLimit2[i];
        }
    }

    public static int TransStringScaleToInt(String strScale) {
        if (strScale.equals("110"))
            return 1;
        else if (strScale.equals("35") || strScale.equals("66")) {
            return 2;
        }
        else if (strScale.equals("10")) {
            return 3;
        }
        else if (strScale.equals("6")) {
            return 4;
        }
        else if (strScale.equals("0.38")) {
            return 5;
        }
        else if (strScale.equals("20")) {
            return 6;
        }
        else if (strScale.equals("220") || strScale.equals("500")) {
            return -1;
        }


        return 0;
    }


    /*单独处理闪变的*/
    public static float dealFlickerByScale(String strScale) {
        if (strScale.equals("110") || strScale.equals("35") || strScale.equals("66") || strScale.equals("10") || strScale.equals("6") || strScale.equals("0.38") || strScale.equals("20")) {
            return 1.0f;
        } else if (strScale.equals("220") || strScale.equals("500")) {
            return 0.8f;
        } else {
            return 1.0f;
        }
    }


    public static float[] GetOverLimit(String strScale, float fDLRL, float fJZRL, float fXYRL, float fSBRL) {
        int i = 0;

        int nScale = TransStringScaleToInt(strScale);

        float[] fLimit = new float[COverlimit.MAXOVERLIMITNUM];
        //频率偏差
        fLimit[0] = 0.2f;
        //电压偏差
        fLimit[1] = GetVoltageDevationLimit(nScale);
        //电压不平衡
        fLimit[2] = 2.0f;
        //闪变
        fLimit[3] = dealFlickerByScale(strScale);
        //畸变率
        fLimit[4] = GetVoltageJBLimit(nScale);
        //24谐波电压幅值
        for (i = 0; i < 24; i++) {
            fLimit[5 + i] = GetVoltageLimit(i, nScale);
        }
        //24谐波电流幅值
        for (i = 0; i < 24; i++) {
            fLimit[5 + 24 + i] = GetCurrentLimit(i + 2, strScale, fDLRL, fJZRL, fXYRL, fSBRL);
        }
        /****************************
         * Modify by yexb 20181015
         *此项为电压下偏差
         * 增加则MAXOVERLIMITNUM的值为54
         * 不增加则MAXOVERLIMITNUM的值为53
         ***************************/
        fLimit[COverlimit.MAXOVERLIMITNUM - 2 - 16] = GetVoltageDevationLimit_L(nScale);

        //负序电流
        fLimit[COverlimit.MAXOVERLIMITNUM - 1 - 16] = GetFCurrentLimit(strScale, fDLRL);

        //间谐波电压含有率
        for (i = 0; i < 16; i++) {
            fLimit[55 + i] = GetInHarm(i, nScale);
        }

        return fLimit;
    }


    //处理基准容量
    public static float getStandShortCap(String voltageLevel){
        if(DicDataEnum.V100.getCode().equalsIgnoreCase(voltageLevel)){
            return 10;
        } else if(DicDataEnum.V380.getCode().equalsIgnoreCase(voltageLevel)){
            return 10;
        }else if(DicDataEnum.KV038.getCode().equalsIgnoreCase(voltageLevel)){
            return 10;
        }else if(DicDataEnum.V220.getCode().equalsIgnoreCase(voltageLevel)){
            return 10;
        }else if(DicDataEnum.KV04.getCode().equalsIgnoreCase(voltageLevel)){
            return 100;
        }else if(DicDataEnum.V400.getCode().equalsIgnoreCase(voltageLevel)){
            return 100;
        }else if(DicDataEnum.KV6.getCode().equalsIgnoreCase(voltageLevel)){
            return 100;
        }else if(DicDataEnum.KV10.getCode().equalsIgnoreCase(voltageLevel)){
            return 100;
        }else if(DicDataEnum.KV20.getCode().equalsIgnoreCase(voltageLevel)){
            return 200;
        }else if(DicDataEnum.KV30.getCode().equalsIgnoreCase(voltageLevel)){
            //非标准
            return 250;
        }else if(DicDataEnum.KV35.getCode().equalsIgnoreCase(voltageLevel)){
            return 250;
        }else if(DicDataEnum.KV50.getCode().equalsIgnoreCase(voltageLevel)){
            return 500;
        }else if(DicDataEnum.KV66.getCode().equalsIgnoreCase(voltageLevel)){
            return 500;
        }else if(DicDataEnum.KV72_5.getCode().equalsIgnoreCase(voltageLevel)){
            return 500;
        }else if(DicDataEnum.KV110.getCode().equalsIgnoreCase(voltageLevel)){
            return 750;
        }else if(DicDataEnum.KV125.getCode().equalsIgnoreCase(voltageLevel)){
            return 750;
        }else if(DicDataEnum.KV200.getCode().equalsIgnoreCase(voltageLevel)){
            return 2000;
        }else if(DicDataEnum.KV220.getCode().equalsIgnoreCase(voltageLevel)){
            return 2000;
        }else if(DicDataEnum.KV320.getCode().equalsIgnoreCase(voltageLevel)){
            return 3000;
        }else if(DicDataEnum.KV330.getCode().equalsIgnoreCase(voltageLevel)){
            return 3000;
        }else if(DicDataEnum.KV500.getCode().equalsIgnoreCase(voltageLevel)){
            return 4500;
        }else if(DicDataEnum.KV600.getCode().equalsIgnoreCase(voltageLevel)){
            return 4500;
        }else if(DicDataEnum.KV660.getCode().equalsIgnoreCase(voltageLevel)){
            return 7000;
        }else if(DicDataEnum.KV750.getCode().equalsIgnoreCase(voltageLevel)){
            return 7000;
        }else if(DicDataEnum.KV1000.getCode().equalsIgnoreCase(voltageLevel)){
            return 7000;
        }else if(DicDataEnum.KV1100.getCode().equalsIgnoreCase(voltageLevel)){
            return 7000;
        }else {
            throw new BusinessException("没有对应电压等级,请先完善字典"+voltageLevel);
        }
    }


}
