package com.huatek.busi.util;


import com.huatek.busi.dto.cablecurrent.CableCurrentResultDto;

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

public class CableCurrentCal {

    public static BigDecimal f = new BigDecimal(50);

    public static BigDecimal T2=new BigDecimal(0.0212);

    public static BigDecimal T3=new BigDecimal(0.1123);

    public static BigDecimal ρT = new BigDecimal(3.5);

    public static BigDecimal Yp = new BigDecimal(0);

    public static BigDecimal λ1 = new BigDecimal(0.0098479);


    public static BigDecimal λ2 = new BigDecimal(0.0013465);


    public static BigDecimal e =new BigDecimal(3);

    public static BigDecimal ρ =new BigDecimal(1.7241);

    public static BigDecimal Diameter =new BigDecimal(35);

    public static BigDecimal DielectricLossFactor =new BigDecimal( 0.04);

    public static BigDecimal ExternalDiameterCC =new BigDecimal( 0.035);
    public static BigDecimal ExternalDiameterC =new BigDecimal( 0.0447);
    public static BigDecimal ExternalDiameterDC =new BigDecimal( 35);


    public static BigDecimal InsulationThick =new BigDecimal( 50);

    public static BigDecimal RatedVoltage =new BigDecimal( 500000);

    public static BigDecimal ResistanceTempCoefficient =new BigDecimal( 0.00393);

    public static BigDecimal t2 =new BigDecimal( 0.0212);
    public static String  t4 ="0.3247";
    public static BigDecimal t3 =new BigDecimal( 0.1123);

    /**
     * 计算电缆载流能力
     * @return Double
     */
    public static Double calcR(){
        double r2=(35/2)*0.001;
        double r1 = (1.7241 * (Math.pow(10d,-8))) * (1 + 0.00393 * (90 - 20))/(r2*r2*Math.PI);
        double x = ((8d * Math.PI * 50d)/r1) * (Math.pow(10d,-7)) * 1d;
        double ys = Math.pow(x,2)/(192 + 0.8 * Math.pow(x,2));
        double yp = 0d;
        double r = r1 * (1 + ys +yp);
        return new BigDecimal(r).setScale(16, RoundingMode.HALF_UP).doubleValue();
    }




    /**
     *
     * @param cablesNumber
     * @param r
     * @param wd
     * @param t1
     * @param t2
     * @param t3
     * @param t4
     * @param tempDiff
     * @return
     */
    public static BigDecimal calcCurrent(BigDecimal cablesNumber,BigDecimal r,BigDecimal wd,BigDecimal t1,BigDecimal t2,BigDecimal t3,BigDecimal t4,
                                  BigDecimal tempDiff) {
        double n = cablesNumber.doubleValue();
        double R = r.doubleValue();
        double Wd = wd.doubleValue();
        double T1 = t1.doubleValue();
        double T2 = t2.doubleValue();
        double T3 = t3.doubleValue();
        double T4 = t4.doubleValue();
        double rate1 = λ1.doubleValue();
        double rate2 = λ2.doubleValue();
        double up = tempDiff.doubleValue() - (Wd * ((0.5d * T1) + n * (T2 + T3 + T4)));
        if(up<=0){
            up=tempDiff.doubleValue();
        }

        double down = (R * T1) + (n * R * (1d + rate1) * T2) + (n * R * (1d + rate1 + rate2) * (T3 + T4));

        double result = Math.sqrt(up/down);
        return new BigDecimal(result).setScale(4, RoundingMode.HALF_UP);
    }

    /**
     * 计算电缆载流能力
     * @return Double
     */
    public static BigDecimal calcR(BigDecimal r1,BigDecimal ys){
        double r = r1.doubleValue() * (1 + ys.doubleValue() +Yp.doubleValue());
        return new BigDecimal(r).setScale(16, RoundingMode.HALF_UP);
    }
    /**
     * 计算一根导体和金属套之间单位长度的热阻
     * 计算公式 (eT/2π) * ln(1+2t1/dc)
     * @return BigDecimal
     */
    public static BigDecimal calcT1(BigDecimal diameter,BigDecimal insulationThick){
        double dc = diameter.doubleValue()*0.001;
        double t1 = insulationThick.doubleValue()*0.001;
        double T1 = (ρT.doubleValue()/(2 * Math.PI)) * Math.log(1 + (2 * (t1/dc)));
        return new BigDecimal(T1).setScale(4, RoundingMode.HALF_UP);
    }


    /**
     * 计算Ys
     * @param r1
     * @return
     */
    public static BigDecimal calYs(BigDecimal r1){

        double x = ((8d * Math.PI * 50d)/r1.doubleValue()) * (Math.pow(10d,-7)) * 1d;
        double ys = Math.pow(x,2)/(192 + 0.8 * Math.pow(x,2));
        return new BigDecimal(ys).setScale(16, RoundingMode.HALF_UP);
    }

    /**
     * Yp 集肤效应系数
     * @param r1
     * @return
     */
    public static BigDecimal calYp(BigDecimal r1){

        double x = ((8d * Math.PI * 50d)/r1.doubleValue()) * (Math.pow(10d,-7)) * 1d;
        double ys = Math.pow(x,2)/(192 + 0.8 * Math.pow(x,2));
        return new BigDecimal(ys).setScale(16, RoundingMode.HALF_UP);
    }

    /**
     * 计算R’
     * @param conductivity  铜导体常温电导率ρ
     * @param conductivity  铜导体常温电导率ρ
     * @param resistanceTempCoefficient  线芯的电阻温度系数
     * @return
     */
    public static BigDecimal calR1(BigDecimal diameter ,BigDecimal conductivity,BigDecimal resistanceTempCoefficient){
        double r=diameter.doubleValue()/2*0.001;
        double p=conductivity.doubleValue();
        double r0=p * (Math.pow(10d,-8))*1/(r*r*Math.PI);
        double resistance=resistanceTempCoefficient.doubleValue();
        double r1 = r0 * (1 + resistance * (90 - 20));

        return new BigDecimal(r1).setScale(16, RoundingMode.HALF_UP);
    }

    /**
         * 周围媒质热阻取值==0.3247
         * 计算周围媒质热阻取值
         * 计算公式 T4' + T4''
         * T4‘‘=（ρT /2π）{ln[u+（u2-1）1/2]+ln[1+(2L/s1)2]}===取值==0 由于电缆不埋入地下，此值=0
         * @return Double
         */
    public static BigDecimal calcT4( BigDecimal dc) {
        double a = 5.2d;
        double b = 1.1d;
        double c = 0.011d;
        double temp = 27d;
        double down = 1 + (0.1 * (b + c * temp) * dc.doubleValue());
        double T41 = a / down;
        double T42 = 0d;
        double T4 = T41 + T42;
        return new BigDecimal(T4).setScale(6, RoundingMode.HALF_UP);
    }

    /**
     * 单位长度电缆电容 (F/m）
     *
     * 计算公式 (e/18ln(Di/Dc)) *10^-9
     * @return Double
     */
    public static BigDecimal calcC(BigDecimal dielectricConstant,BigDecimal externalDiameterCI ,BigDecimal externalDiameterCC){
        double e = dielectricConstant.doubleValue();
        double di = externalDiameterCI.doubleValue();
        double dc = externalDiameterCC.doubleValue();
        double x = Math.log(di/dc);
        double c = (e/(18d * x)) * (Math.pow(10d,-9));
        return new BigDecimal(c).setScale(16, RoundingMode.HALF_UP);
    }


    /**
     * 单位长度电缆电容 (F/m）
     * 计算公式 2πf * c * u^2 * tan\delta
     * @return Double
     */
    public static BigDecimal calcWd(BigDecimal ratedVoltage ,BigDecimal dielectricLossFactor,BigDecimal c){
        BigDecimal u = ratedVoltage;
        BigDecimal tan = dielectricLossFactor;
        BigDecimal wd = new BigDecimal("2").multiply(new BigDecimal(Math.PI)).multiply(f).multiply(c)
                .multiply(new BigDecimal(Math.pow(u.doubleValue(),2))).multiply(tan).multiply(new BigDecimal(0.00001));
        return wd.setScale(8, RoundingMode.HALF_UP);
    }

    /**
     * 单位长度电缆电容 (F/m）
     * 计算公式 2πf * c * u^2 * tan\delta
     * @return Double
     */
    public static Double calcWd(){
        double f = 50d;
        double u = 500000d;
//        double u = 500000d;
        double tan = 0.04d;
        double c = calcC();
        double wd = 2d * Math.PI * f * c * u*u * tan;
        return new BigDecimal(wd).setScale(8, RoundingMode.HALF_UP).doubleValue();
    }

    /**
     * 计算一根导体和金属套之间单位长度的热阻
     * 计算公式 (eT/2π) * ln(1+2t1/dc)
     * @return Double
     */
    public static Double calcT1(){
        double eT = 3.5d;
        double dc = 40;
        double t1 = 26.6;
        double T1 = (eT/(2 * Math.PI)) * Math.log(1 + (2 * (t1/dc)));
        return new BigDecimal(T1).setScale(4, RoundingMode.HALF_UP).doubleValue();
    }
    /**
     * 单位长度电缆电容 (F/m）
     * 计算公式 (e/18ln(Di/Dc)) *10^-9
     * @return Double
     */
    public static Double calcC(){
        double e = 3d;
        double di = 0.0447d;
//        double di = 0.058d;
        double dc = 0.0342d;
//        double dc = 0.035d;
        double x = Math.log(di/dc);
        double c = (e/(18d * x)) * (Math.pow(10d,-9));
        return new BigDecimal(c).setScale(16, RoundingMode.HALF_UP).doubleValue();
    }
    public static void main(String[] args) {
        double n = 1d;
        double R = calcR();
        double Wd = calcWd();
        double T1 = calcT1();
        double T2 = Constant.T2;
        double T3 = Constant.T3;
        double T4 = Constant.T4;
        double rate1 = Constant.RATE_1;
        double rate2 = Constant.RATE_2;
        double up = 63 - Wd * ((0.5d * T1) + n * (T2 + T3 + T4));

        double down = (R * T1) + (n * R * (1d + rate1) * T2) + (n * R * (1d + rate1 + rate2) * (T3 + T4));
        double result = Math.sqrt(up/down);

    }
}
