package com.ruoyi.system.controller;


import com.ruoyi.system.domain.SysUnitConvert;
import com.ruoyi.system.service.ISysUnitConvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class UnitConvert {

    @Autowired
    private ISysUnitConvertService sysUnitConvertService;

    public  double ConvertUniter(String unitType, double oldValue, int oldUnit, int newUnit) {
        // 查询旧单位信息
        if ("temperature".equalsIgnoreCase(unitType)) {
            return handleTemperatureConversion(BigDecimal.valueOf(oldValue), (long) oldUnit, (long) newUnit).doubleValue();
        } else {

            SysUnitConvert oldUnitInfo =sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder(unitType, (long) oldUnit,null);
            if (oldUnitInfo == null) {
                throw new IllegalArgumentException("旧单位 '" + oldUnit + "' 不存在或不可用");
            }


            SysUnitConvert baseUnitInfo = sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder (unitType,null,"Y");
            if (baseUnitInfo == null) {
                throw new IllegalArgumentException("基准单位 不存在或不可用");
            }

            // 查询新单位信息
            SysUnitConvert newUnitInfo = sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder(unitType, (long) newUnit,null);
            if (newUnitInfo == null) {
                throw new IllegalArgumentException("新单位 '" + newUnit + "' 不存在或不可用");
            }

            BigDecimal oldFactor = oldUnitInfo.getConversionFactor();
            BigDecimal newFactor = newUnitInfo.getConversionFactor();

            // 检查旧单位转换因子是否为零
            if (oldFactor.compareTo(BigDecimal.ZERO) == 0) {
                throw new ArithmeticException("旧单位 '" + oldUnit + "' 的转换因子为零，无法进行转换");
            }

            // 计算基准值：oldValue / oldFactor
            int scale = 20; // 设置足够大的精度以避免精度丢失
            BigDecimal baseValue = BigDecimal.valueOf(oldValue).divide(oldFactor, scale, RoundingMode.HALF_UP);

            // 计算新值：baseValue * newFactor
            BigDecimal newValue = baseValue.multiply(newFactor);

            // 四舍五入到合理的小数位数（例如10位）
            newValue = newValue.setScale(10, RoundingMode.HALF_UP);

            return newValue.doubleValue();
        }
    }

    // 温度转换方法
    public static BigDecimal handleTemperatureConversion(BigDecimal oldValue, Long oldUnit, Long newUnit) {
        final BigDecimal THIRTY_TWO = BigDecimal.valueOf(32);
        final BigDecimal FIVE = BigDecimal.valueOf(5);
        final BigDecimal NINE = BigDecimal.valueOf(9);
        final BigDecimal TWO_HUNDRED_SEVENTY_THREE_POINT_ONE_FIVE = BigDecimal.valueOf(273.15);
        // 使用原始值计算
        BigDecimal celsius;
        if (oldUnit == 0) {
            celsius = oldValue;
        } else if (oldUnit == 1) {
            celsius = oldValue.subtract(THIRTY_TWO).multiply(FIVE).divide(NINE, 10, RoundingMode.HALF_UP);
        } else if (oldUnit == 2) {
            celsius = oldValue.subtract(TWO_HUNDRED_SEVENTY_THREE_POINT_ONE_FIVE);
        } else {
            throw new IllegalArgumentException("无效温度单位");
        }

        if (newUnit == 0) {
            return celsius;
        } else if (newUnit == 1) {
            return celsius.multiply(NINE).divide(FIVE, 10, RoundingMode.HALF_UP).add(THIRTY_TWO);
        } else if (newUnit == 2) {
            return celsius.add(TWO_HUNDRED_SEVENTY_THREE_POINT_ONE_FIVE);
        }
        throw new IllegalArgumentException("无效温度单位");
    }
}