package com.ruoyi.caltools.controller;

import com.ruoyi.caltools.model.FlowProps;
import com.ruoyi.caltools.model.GasProps;
import com.ruoyi.caltools.service.DetailService;
import com.ruoyi.caltools.service.GBT11062Service;
import com.ruoyi.caltools.service.ThermService;
import com.ruoyi.caltools.utils.GasConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.controller.UnitConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/NGCalcTools")
public class GasController {


    public static ThermService thermService;
    public static DetailService detailService;
    public static GBT11062Service gbt11062Service;

    @Autowired
    private UnitConvert unitConvert=new UnitConvert();

    @PostMapping("/ngCalc")
    public AjaxResult ngCalc(@RequestBody FlowProps flowProps) {
        thermService = new ThermService();
        detailService = new DetailService();
        gbt11062Service = new GBT11062Service();
        GasProps gasProps = new GasProps();
        //大气压力转换成Pa
        double tempPatm = unitConvert.ConvertUniter("pressure", flowProps.getdPatm(), flowProps.getdPatmUnit(), 0);
        //压力转换成Pa
        double tempPf = unitConvert.ConvertUniter("pressure", flowProps.getdPf(), flowProps.getdPfUnit(), 0);
        //温度转换成K
        double tempTf = unitConvert.ConvertUniter("temperature", flowProps.getdTf(), flowProps.getdTfUnit(), 2);
        if (flowProps.getdPfType() == 0)  //0是表压
        {
            gasProps.dPf = tempPatm + tempPf;
        } else {
            gasProps.dPf = tempPf;
        }
        gasProps.dTf = tempTf;
        gasProps.dCbtj = flowProps.getdCbtj();

        switch (gasProps.dCbtj)
        {
            case 2:
                gasProps.setdPb(101325);
                gasProps.setdTb( 273.15);
                flowProps.setdPb_M(101325);
                flowProps.setdTb_M(273.15);
                break;
            case 1:

                gasProps.setdPb(101325);
                gasProps.setdTb( 288.15);
                flowProps.setdPb_M(101325);
                flowProps.setdTb_M(288.15);
                break;
            case 0:

                gasProps.setdPb(101325);
                gasProps.setdTb( 293.15);
                flowProps.setdPb_M(101325);
                flowProps.setdTb_M(293.15);
                break;
        }

        String[] stringArray = flowProps.getdngComponents().split("_");
        double[] doubleArray = new double[stringArray.length];        // 遍历字符串数组，将每个元素转换为 double 类型
        for (int i = 0; i < stringArray.length; i++) {
            try {
                doubleArray[i] = Double.parseDouble(stringArray[i]) / 100;
            } catch (NumberFormatException e) {
                // 处理转换异常
                System.err.println("无法将字符串 " + stringArray[i] + " 转换为 double 类型: " + e.getMessage());
            }
        }
        gasProps.adMixture = doubleArray;
        Crit(gasProps, 0); //计算临界流函数所有参数都计算了
        return AjaxResult.success(gasProps);
    }

    public static void ngCalcVoid(FlowProps flowProps, GasProps gasProps) {
        thermService = new ThermService();
        detailService = new DetailService();
        gbt11062Service = new GBT11062Service();
        Crit(gasProps, 0); //计算临界流函数所有参数都计算了
    }

    public static int NG_Cal_Init() {
        //create object for calculating density
        if (null == (detailService=new DetailService())) {
            return GasConstants.MEMORY_ALLOCATION_ERROR;
        }

        //create object for calculating thermodynamic properties
        if (null == (thermService=new ThermService())) {
            return GasConstants.MEMORY_ALLOCATION_ERROR;
        }

        //create object for calculating thermodynamic properties
        if (null == (gbt11062Service=new GBT11062Service())) {
            return GasConstants.MEMORY_ALLOCATION_ERROR;
        }

        return GasConstants.NG_Cal_INITIALIZED;

    }// NG_Cal_Init
    public static int NG_Cal_UnInit() {
        // delete the objects (if they exist)
        detailService = null;
        thermService = null;
        gbt11062Service=null;
        return 0;

    }

    public static double Crit(GasProps gasProps, double dPlenumVelocity)
    {
        thermService = new ThermService();
        detailService = new DetailService();
        gbt11062Service = new GBT11062Service();
        //variables local to function 
        double DH, DDH, S, H;
        double tolerance = 1.0;
        double R, P, T, Z;
        int i;
        //begin by calculating densities and thermodynamic properties
        thermService.Run(gasProps, detailService);

        //DH is enthalpy change from plenum to throat; this is our initial guess

        DH = (gasProps.dSOS * gasProps.dSOS - dPlenumVelocity * dPlenumVelocity) / 2.0;

        //trap plenum conditions before we alter the data stucture's contents
        S = gasProps.dS;

        H = gasProps.dH;

        R = gasProps.dRhof;
        P = gasProps.dPf;
        Z = gasProps.dZf;
        T = gasProps.dTf;

        DDH = 10.0;

        for (i = 1; i < GasConstants.MAX_NUM_OF_ITERATIONS; i++)
        {
            thermService.HS_Mode( gasProps, detailService, H - DH, S, true);
            thermService.Run( gasProps, detailService);
            DDH = DH;
            DH = (gasProps.dSOS * gasProps.dSOS - dPlenumVelocity * dPlenumVelocity) / 2.0;
            if (Math.abs(DDH - DH) < tolerance) break;
        }
        gasProps.dCstar = (gasProps.dRhof * gasProps.dSOS) / Math.sqrt(R * P * Z);
        gasProps.dPf = P;
        gasProps.dTf = T;
        thermService.Run(gasProps, detailService);
        gbt11062Service.Run(gasProps);
        return gasProps.dCstar;

    }

}




    
