package com.njcn.device.utils;



import com.njcn.device.pojo.po.LineDetail;
import com.njcn.device.pojo.po.Overlimit;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


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 float[] fILimit = {
            12.0f, 9.6f, 6.0f, 9.6f, 4.0f, 6.8f, 3.0f, 3.2f, 2.4f, 4.3f, 2.0f, 3.7f, 1.7f, 1.9f, 1.5f, 2.8f, 1.3f, 2.5f, 1.2f, 1.4f, 1.1f, 2.1f, 1.0f, 1.9f,
            //	16.0f,13.0f,8.1f,13.0f,5.4f,9.3f,4.1f,4.3f,3.3f,5.9f,2.7f,5.0f,2.3f,2.6f,2.0f,3.8f,1.8f,3.4f,1.6f,1.9f,1.5f,2.8f,1.4f,2.6f,
            15.0f, 12.0f, 7.7f, 12.0f, 5.1f, 8.8f, 3.8f, 4.1f, 3.1f, 5.6f, 2.6f, 4.7f, 2.2f, 2.5f, 1.9f, 3.6f, 1.7f, 3.2f, 1.5f, 1.8f, 1.4f, 2.7f, 1.3f, 2.5f,
            26.0f, 20.0f, 13.0f, 20.0f, 8.5f, 15.0f, 6.4f, 6.8f, 5.1f, 9.3f, 4.3f, 7.9f, 3.7f, 4.1f, 3.2f, 6.0f, 2.8f, 5.4f, 2.6f, 2.9f, 2.3f, 4.5f, 2.1f, 4.1f,
            43.0f, 34.0f, 21.0f, 34.0f, 14.0f, 24.0f, 11.0f, 11.0f, 8.5f, 16.0f, 7.1f, 13.0f, 6.1f, 6.8f, 5.3f, 10.0f, 4.7f, 9.0f, 4.3f, 4.9f, 3.9f, 7.4f, 3.6f, 6.8f,
            78.0f, 62.0f, 39.0f, 62.0f, 26.0f, 44.0f, 19.0f, 21.0f, 16.0f, 28.0f, 13.0f, 24.0f, 11.0f, 12.0f, 9.7f, 18.0f, 8.6f, 16.0f, 7.8f, 8.9f, 7.1f, 14.0f, 6.5f, 12.0f,
    };

    private static float[] fILimitCoe = {
            2.0f, 1.1f, 2.0f, 1.2f, 2.0f, 1.4f, 2.0f, 2.0f, 2.0f, 1.8f, 2.0f, 1.9f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f
    };

    //间谐波含有率
    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, int iScaleSelect, float fDLRL, float fJZRL, float fXYRL, float fSBRL) {
        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 nBaseSelect = nHarm + iScaleSelect * 24;
        float fRealLimit = fDLRL / fJZRL * fILimit[nBaseSelect];
        fRealLimit = fRealLimit * (float) Math.pow(fXYRL / fSBRL, 1.0f / fILimitCoe[nHarm]);
        return fRealLimit;
    }

    public static float GetFCurrentLimit(String strScale, float fDLRL){
    	float uL = 0.0f;
    	switch (strScale) {
        case "0.38kV":
        	uL = 0.4f;
            break;
        case "6kV":
        	uL = 6.3f;
            break;
        case "10kV":
        	uL = 10.5f;
            break;
        case "20kV":
        	uL = 21.0f;
            break;
        case "35kV":
        	uL = 36.5f;
            break;
        case "66kV":
        	uL = 69.0f;
            break;
        case "110kV":
        	uL = 115.0f;
            break;
        case "220kV":
        	uL = 230.0f;
            break;
        case "330kV":
        	uL = 345.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("110kV")) {
            return 1;
        }
        else if (strScale.equals("35kV") || strScale.equals("66kV")) {
            return 2;
        }
        else if (strScale.equals("10kV")) {
            return 3;
        }
        else if (strScale.equals("6kV")) {
            return 4;
        }
        else if (strScale.equals("0.38kV")) {
            return 5;
        }
        else if (strScale.equals("20kV")) {
            return 6;
        }
        else if (strScale.equals("220kV")) {
            return -1;
        }

        return 0;
    }

    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;
        //闪变
        if (nScale < 2) {
            fLimit[3] = 1.0f;
        }
        else {
            fLimit[3] = 0.8f;
        }
        //畸变率
        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, nScale, 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 void addOverLimit(String lineId, String scaTmp) {
        Overlimit overlimit = new Overlimit();
        LineDetail tmp = lineDetailMapper.selectById(lineId);
        float fDLRL = tmp.getShortCapacity();
        float fJZRL = tmp.getStandardCapacity();
        float fXYRL = tmp.getDealCapacity();
        float fSBRL = tmp.getDevCapacity();

        float[] fLimit = COverlimit.GetOverLimit(scaTmp, fDLRL, fJZRL, fXYRL, fSBRL);


        overLimitMapper.insert(overlimit);
    }*/
}
