electripper/electripper-common/src/main/java/com/ruoyi/common/utils/CommonUtil.java
2024-06-06 22:07:28 +08:00

166 lines
6.9 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.common.utils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* 业务工具类
*
* @author ruoyi
*/
@Slf4j
public class CommonUtil {
public static final String GEO_WEB_KEY = SpringUtils.getRequiredProperty("geo.key");
/**
* 格式化错误消息
* @author ruoyi
*/
public static String format(String status) {
String msg = "车辆";
// 检查status是否为null避免空指针异常
if (status == null) {
return "Status cannot be null.";
}
// 使用switch语句重构原来的if-else结构
switch (status) {
case ServiceConstants.VEHICLE_STATUS_NOT_LISTING:
return msg + ServiceConstants.VEHICLE_STATUS_NOT_LISTING_STR;
case ServiceConstants.VEHICLE_STATUS_IN_APPOINTMENT:
return msg + ServiceConstants.VEHICLE_STATUS_IN_APPOINTMENT_STR;
case ServiceConstants.VEHICLE_STATUS_IN_USING:
return msg + ServiceConstants.VEHICLE_STATUS_IN_USING_STR;
case ServiceConstants.VEHICLE_STATUS_TEMPORARILY_LOCK:
return msg + ServiceConstants.VEHICLE_STATUS_TEMPORARILY_LOCK_STR;
// case ServiceConstants.VEHICLE_STATUS_IN_REPAIR:
// return msg + ServiceConstants.VEHICLE_STATUS_IN_REPAIR_STR;
case ServiceConstants.VEHICLE_STATUS_IN_OFFLINE:
return msg + ServiceConstants.VEHICLE_STATUS_IN_CHANGING_STR;
case ServiceConstants.VEHICLE_STATUS_ABANDON:
return msg + ServiceConstants.VEHICLE_STATUS_ABANDON_STR;
default:
// 处理未知或新增的状态
return "Unknown vehicle status: " + status;
}
}
/**
* 根据定位获取地址
* @author ruoyi
*/
public static String getAddressByGeo(String location) {
String address = null;
try {
String url = StrUtil.format("https://restapi.amap.com/v3/geocode/regeo?key={}&location={}&&radius=1000&extensions=all", GEO_WEB_KEY, location);
String result = HttpUtils.sendGet(url);
log.info("【根据定位获取地址】请求结果result:{}",result);
//将json字符串转换为Object
JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
JSONObject regeocode1 = jsonObject.getJSONObject("regeocode");
address = regeocode1.getString("formatted_address");
log.info("【根据定位获取地址】address=:【{}】",result);
return address;
} catch (Exception e) {
log.error("【根据定位获取地址】转换地址报错", e);
}
return address;
}
/**
* 获取省市县
*
* @author ruoyi
*/
public static String getDistrictList() {
String list = null;
try {
String url = StrUtil.format("https://restapi.amap.com/v3/config/district?key={}&keywords={}&subdistrict=3", GEO_WEB_KEY, "中国");
String result = HttpUtils.sendGet(url);
// log.info("【获取省市县】请求结果result:{}",result);
//将json字符串转换为Object
JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
JSONArray districts = jsonObject.getJSONArray("districts");
JSONObject jsonArray = (JSONObject)districts.get(0);
JSONArray districts1 = jsonArray.getJSONArray("districts");
list = districts1.toString();
// log.info("【获取省市县】result=:【{}】",list);
return list;
} catch (Exception e) {
log.error("【获取省市县】失败", e);
}
return list;
}
/**
* 根据电压计算续航里程
*
* @param voltage 电压
* @param fullVoltage 满电电压
* @param lowVoltage 亏电电压
* @param fullEndurance 满电续航里程
* @author qzz
*/
public static Integer getRemainingMileage(String voltage,Double fullVoltage,Double lowVoltage,Integer fullEndurance) {
// 满电电压减去亏电电压 乘以 满电续航里程 除以 满电电压
log.info(" 电压--voltage:{},满电电压--fullVoltage:{},亏电电压--lowVoltage:{},满电续航--fullEndurance:{}",voltage,fullVoltage,lowVoltage,fullEndurance);
BigDecimal vol = new BigDecimal(voltage);
BigDecimal current = new BigDecimal(fullVoltage).subtract(vol);
BigDecimal full = new BigDecimal(fullVoltage).subtract(new BigDecimal(lowVoltage));
BigDecimal divide = full.subtract(current).divide(full,2, RoundingMode.HALF_UP);//当前电量百分百
log.info("当前电量百分百:{}%",divide.multiply(new BigDecimal(100)));
BigDecimal multiply = divide.multiply(new BigDecimal(fullEndurance));
log.info("当前剩余续航里程:{}km",multiply);
return multiply.intValue();
}
/**
* 根据电压计算电量百分比
*
* @param voltage 电压
* @param fullVoltage 满电电压
* @param lowVoltage 亏电电压
* @author qzz
*/
public static Integer getElectricQuantity(String voltage,Double fullVoltage,Double lowVoltage) {
// 满电电压减去亏电电压 乘以 满电续航里程 除以 满电电压
BigDecimal vol = new BigDecimal(voltage);
BigDecimal current = new BigDecimal(fullVoltage).subtract(vol);
BigDecimal full = new BigDecimal(fullVoltage).subtract(new BigDecimal(lowVoltage));
BigDecimal divide = full.subtract(current).divide(full,2, RoundingMode.HALF_UP);//当前电量百分百
BigDecimal multiply = divide.multiply(new BigDecimal(100));
// log.info("当前电量百分百:{}%",multiply);
return multiply.intValue();
}
/**
* 根据电压计算电量百分比
*
* 计算公示: ((高电压-低电压) * 百分比 / 100 ) + 低电压
*
* @param percentage 百分比
* @param fullVoltage 满电电压
* @param lowVoltage 亏电电压
* @author qzz
*/
public static Integer getElectricQuantityByPercentage(Integer percentage,Double fullVoltage,Double lowVoltage) {
BigDecimal lowVoltageBig = new BigDecimal(lowVoltage);
BigDecimal full = new BigDecimal(fullVoltage).subtract(lowVoltageBig);
BigDecimal multiply1 = full.multiply(new BigDecimal(percentage));
BigDecimal divide1 = multiply1.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
BigDecimal add = divide1.add(lowVoltageBig);
log.info("根据百分比计算出的电压:{}V",add);
return add.intValue();
}
}