package com.njcn.advance.utils.assess;



import com.njcn.advance.pojo.dto.assess.HarmonicsIAndZDTO;
import org.apache.commons.math3.complex.Complex;

import java.util.List;

/**
 * Description:
 * Date: 2024/8/7  11:08【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
public class PowerQualityAlgorithmUtil {
   /**
   * @Description: loadharmonicsCal
   * @Param:  uln负荷电压 pf功率因数 pl非线性负荷功率
   * @return: java.util.List<java.util.List<java.util.List<java.lang.Double>>>
   * @Author: clam
   * @Date: 2024/8/15
   */
    public static HarmonicsIAndZDTO loadharmonicsCal(Double uln, Double pln, Double pl, Double pf, Double[] harmDict){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Double[] i = new Double[49];
        Complex[] z = new Complex[49];
        //①计算负荷视在功率S_L：
        Double sl = pl/pf;
        //②计算负荷无功功率Q_L
        Double ql = Math.sqrt(1-pf*pf)*sl;
        //③计算负荷基波电流
        Double il1 = sl/(Math.sqrt(3)*uln);

        //计算低压侧基波电流
        Double ill1 =sl/(Math.sqrt(3)*pln);
        //④计算基波阻抗：
        Complex zl1 = new Complex(pl*1000/(3*ill1*ill1),ql*1000/(3*ill1*ill1));
        for (int h = 0; h < 49; h++) {
            i[h] = il1*harmDict[h]/100.00;
            z[h] = new Complex(Math.sqrt(h+2)*pl*1000/(3*ill1*ill1),(h+2)*ql*1000/(3*ill1*ill1));

        }
        harmonicsIAndZDTO.setI(i);
        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }
    /**
    * @Description: transZCal 变压器谐波阻抗
    * @Param:  sRt额定容量 uRtl低压侧电压 短路阻抗uKr 负载损耗 pKrt
    * @return: HarmonicsIAndZDTO
    * @Author: clam
    * @Date: 2024/8/15
    */
    public static HarmonicsIAndZDTO transZCal(Double sRt, Double uRtl , Double uKr, Double pKrt){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Complex[] z = new Complex[49];
        //①计算变压器的低压侧的额定电流
        Double iRt = sRt/(Math.sqrt(3)*uRtl);
        //计算变压器的基波阻抗
        Double zT1 = (uKr/100.00) *(uRtl*uRtl*1000.00/sRt);
        Double rT1 = (pKrt*1000.00)/(3*iRt*iRt);
        if(zT1<rT1){
            throw  new RuntimeException("变压器参数存在问题");
        }
        Double xT1 = Math.sqrt(zT1*zT1 -rT1*rT1);
        //③计算变压器的各次阻抗
        for (int h = 0; h < 49; h++) {
            z[h] = new Complex(Math.sqrt(h+2)*rT1,(h+2)*xT1);
        }

        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }
    /**
    * @Description: capacitorZCal 电容器阻抗
    * @Param:
    * @return: HarmonicsIAndZDTO
    * @Author: clam
    * @Date: 2024/8/15
    */
    public static HarmonicsIAndZDTO capacitorZCal(Double uRc, Double qRc , Double kCl, Integer cNum){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Complex[] z = new Complex[49];
        //计算不含电抗率的基本的阻抗
        Double z_b = uRc*uRc*1000/(cNum*qRc);
        for (int h = 0; h < 49; h++) {
            if (kCl<=1){
                z[h] = new Complex(Math.sqrt(h+2)*0.02*z_b,-z_b*((1.00/(h+2))-((h+2)*kCl/100.00)));
            }else {
                z[h] = new Complex(0.00,-z_b*((1.00/(h+2))-((h+2)*kCl/100.00)));
            }
        }

        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }
    /**
    * @Description: lineZCal 电路
    * @Param:
    * @return: HarmonicsIAndZDTO
    * @Author: clam
    * @Date: 2024/8/15
    */
    public static HarmonicsIAndZDTO lineZCal(Double r1, Double x1, Double lineLen){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Complex[] z = new Complex[49];
        //计算不含电抗率的基本的阻抗
        Complex z_w1 = new Complex(r1*lineLen,x1*lineLen);
        for (int h = 0; h < 49; h++) {
           z[h] = new Complex(0.288*r1+0.138*Math.sqrt((h+2)*r1),(h+2)*x1);
        }

        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }


    public static HarmonicsIAndZDTO loadShunt(List<HarmonicsIAndZDTO> harmonicsIAndZDTOList){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Double[] i = new Double[50];
        Complex[] z = new Complex[50];
        for (int h = 0; h < 49; h++) {
            int finalH = h;
            i[h] = harmonicsIAndZDTOList.stream().map(temp->{
                return  temp.getI()[finalH];
            }).reduce((a,b)->Math.sqrt(a*a+b*b+getK(finalH))*a*b).get();
            z[h] = harmonicsIAndZDTOList.stream().map(temp->{
                return temp.getZ()[finalH].reciprocal();
            }).reduce((a,b)->a.add(b)).get().reciprocal();
        }

        harmonicsIAndZDTO.setI(i);
        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }
    //背景谐波阻抗
    public static HarmonicsIAndZDTO bHarmonicCal(double un , double ssc){
        HarmonicsIAndZDTO harmonicsIAndZDTO = new HarmonicsIAndZDTO();
        Complex[] z = new Complex[49];
        //计算不含电抗率的基本的阻抗
        Complex zppc1 = new Complex(0.00,un*un/ssc);
        for (int h = 0; h < 49; h++) {
            z[h] = new Complex(0.0,(h+2)*un*un/ssc);
        }

        harmonicsIAndZDTO.setZ(z);
        return harmonicsIAndZDTO;
    }
    public static double getK(int h) {
        if(h>13||h==9||h%2==0){
            return 0.0;
        } else if (h==3) {
            return 1.62;

        }
        else if (h==3) {
            return 1.62;

        }
        else if (h==5) {
            return 1.28;

        }
        else if (h==7) {
            return 0.72;

        }
        else if (h==11) {
            return 0.18;

        }
        else if (h==13) {
            return 0.08;

        }else if (h==1) {
            return 2.00;

        }
        return 0.00;
    }

}
