ruoyi-api/ruoyi-system/src/main/java/com/ruoyi/system/controller/UnitConvert.java

100 lines
4.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
private final ISysUnitConvertService sysUnitConvertService;
@Autowired
public UnitConvert(ISysUnitConvertService sysUnitConvertService) {
this.sysUnitConvertService = 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 tempUnit = new SysUnitConvert();
tempUnit.setUnitType(unitType);
tempUnit.setUnitOrder((long) oldUnit);
SysUnitConvert oldUnitInfo = sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder(tempUnit);
if (oldUnitInfo == null) {
throw new IllegalArgumentException("旧单位 '" + oldUnit + "' 不存在或不可用");
}
tempUnit=new SysUnitConvert();;
tempUnit.setUnitType(unitType);
tempUnit.setBaseUnit("Y");
SysUnitConvert baseUnitInfo = sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder(tempUnit);
if (baseUnitInfo == null) {
throw new IllegalArgumentException("基准单位 不存在或不可用");
}
tempUnit=new SysUnitConvert();
tempUnit.setUnitType(unitType);
tempUnit.setUnitOrder((long) newUnit);
// 查询新单位信息
SysUnitConvert newUnitInfo = sysUnitConvertService.selectSysUnitConvertUnitByTypeOrder(tempUnit);
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 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("无效温度单位");
}
}