工具类修改 #131

Merged
p95fco63j merged 1 commits from junmao_branch into develop 2 weeks ago

@ -0,0 +1,557 @@
package com.campus.water.util;
import java.time.LocalDate;
import lombok.extern.slf4j.Slf4j;
import com.campus.water.entity.Device;
import com.campus.water.entity.Device.DeviceType;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
*
*
*/
@Slf4j
public class CommonUtils {
// ========== 设备字符串处理方法 ==========
/**
* IDWM/WS + 3
*/
public static boolean validateDeviceId(String deviceId) {
if (deviceId == null || deviceId.length() != 5) {
return false;
}
String prefix = deviceId.substring(0, 2);
String suffix = deviceId.substring(2);
if (!("WM".equals(prefix) || "WS".equals(prefix))) {
return false;
}
try {
Integer.parseInt(suffix);
return true;
} catch (NumberFormatException e) {
log.warn("设备ID后缀非数字{}", deviceId);
return false;
}
}
/**
* IDWM001 WM*01
*/
public static String desensitizeDeviceId(String deviceId) {
if (!validateDeviceId(deviceId)) {
return deviceId;
}
return deviceId.substring(0, 2) + "*" + deviceId.substring(3);
}
/**
*
*/
public static String formatInstallLocation(String location) {
if (location == null) {
return "未配置安装位置";
}
// 去除首尾空格、替换全角空格、多个空格合并为一个
String formatted = location.trim().replace(" ", " ").replaceAll("\\s+", " ");
return formatted.isEmpty() ? "未配置安装位置" : formatted;
}
/**
* 50
*/
public static String limitRemarkLength(String remark) {
if (remark == null) {
return "";
}
if (remark.length() <= 50) {
return remark;
}
return remark.substring(0, 50) + "...";
}
/**
* +ID+
*/
public static String concatDeviceFullName(Device device) {
if (device == null) {
return "";
}
String typeName = DeviceType.water_maker.equals(device.getDeviceType()) ? "制水机" : "供水机";
return String.format("[%s]%s-%s", typeName, device.getDeviceId(), device.getDeviceName());
}
/**
*
*/
public static boolean isBlankWithFullWidth(String str) {
if (str == null) {
return true;
}
return str.replace(" ", "").trim().isEmpty();
}
/**
*
*/
public static boolean isNotBlankWithFullWidth(String str) {
return !isBlankWithFullWidth(str);
}
/**
*
*/
public static String convertDeviceStatusToCn(String status) {
if (status == null) {
return "未知状态";
}
return switch (status) {
case "online" -> "在线";
case "offline" -> "离线";
case "fault" -> "故障";
default -> "未知状态";
};
}
/**
*
*/
public static String convertDeviceTypeToCn(DeviceType type) {
if (type == null) {
return "未知类型";
}
return DeviceType.water_maker.equals(type) ? "制水机" : "供水机";
}
/**
*
*/
public static DeviceType convertStrToDeviceType(String typeStr) {
if (typeStr == null) {
return null;
}
return switch (typeStr.toLowerCase()) {
case "water_maker", "制水机" -> DeviceType.water_maker;
case "water_supply", "供水机" -> DeviceType.water_supply;
default -> null;
};
}
// ========== 设备数字处理方法 ==========
/**
*
*/
public static boolean validateSensorValue(BigDecimal value) {
return value != null && value.compareTo(BigDecimal.ZERO) >= 0;
}
/**
* TDS1
*/
public static BigDecimal formatTdsValue(BigDecimal tdsValue) {
if (!validateSensorValue(tdsValue)) {
return BigDecimal.ZERO;
}
return tdsValue.setScale(1, RoundingMode.HALF_UP);
}
/**
* 2
*/
public static BigDecimal formatWaterPress(BigDecimal press) {
if (!validateSensorValue(press)) {
return BigDecimal.ZERO;
}
return press.setScale(2, RoundingMode.HALF_UP);
}
/**
* 0-100
*/
public static BigDecimal formatWaterLevel(BigDecimal level) {
if (level == null) {
return BigDecimal.ZERO;
}
if (level.compareTo(BigDecimal.ZERO) < 0) {
return BigDecimal.ZERO;
}
if (level.compareTo(new BigDecimal(100)) > 0) {
return new BigDecimal(100);
}
return level.setScale(1, RoundingMode.HALF_UP);
}
/**
* 0-100
*/
public static BigDecimal formatTemperature(BigDecimal temp) {
if (temp == null) {
return BigDecimal.ZERO;
}
if (temp.compareTo(BigDecimal.ZERO) < 0) {
return BigDecimal.ZERO;
}
if (temp.compareTo(new BigDecimal(100)) > 0) {
return new BigDecimal(100);
}
return temp.setScale(1, RoundingMode.HALF_UP);
}
/**
* 寿0-100
*/
public static Integer formatFilterLife(Integer life) {
if (life == null) {
return 0;
}
if (life < 0) {
return 0;
}
if (life > 100) {
return 100;
}
return life;
}
/**
* Integer
*/
public static Integer add(Integer num1, Integer num2) {
if (num1 == null) num1 = 0;
if (num2 == null) num2 = 0;
return num1 + num2;
}
/**
* BigDecimal
*/
public static BigDecimal add(BigDecimal num1, BigDecimal num2) {
if (num1 == null) num1 = BigDecimal.ZERO;
if (num2 == null) num2 = BigDecimal.ZERO;
return num1.add(num2);
}
/**
* BigDecimal
*/
public static BigDecimal subtract(BigDecimal num1, BigDecimal num2) {
if (num1 == null) num1 = BigDecimal.ZERO;
if (num2 == null) num2 = BigDecimal.ZERO;
return num1.subtract(num2);
}
/**
* BigDecimal0
*/
public static int compare(BigDecimal num1, BigDecimal num2) {
if (num1 == null) num1 = BigDecimal.ZERO;
if (num2 == null) num2 = BigDecimal.ZERO;
return num1.compareTo(num2);
}
/**
* BigDecimal
*/
public static BigDecimal max(BigDecimal num1, BigDecimal num2) {
return compare(num1, num2) >= 0 ? num1 : num2;
}
/**
* BigDecimal
*/
public static BigDecimal min(BigDecimal num1, BigDecimal num2) {
return compare(num1, num2) <= 0 ? num1 : num2;
}
// ========== 设备集合处理方法 ==========
/**
*
*/
public static List<Device> filterDeviceByType(List<Device> deviceList, DeviceType type) {
List<Device> result = new ArrayList<>();
if (deviceList == null || deviceList.isEmpty() || type == null) {
return result;
}
for (Device device : deviceList) {
if (type.equals(device.getDeviceType())) {
result.add(device);
}
}
return result;
}
/**
* ID
*/
public static Set<String> convertDeviceListToIdSet(List<Device> deviceList) {
Set<String> idSet = new HashSet<>();
if (deviceList == null || deviceList.isEmpty()) {
return idSet;
}
for (Device device : deviceList) {
if (validateDeviceId(device.getDeviceId())) {
idSet.add(device.getDeviceId());
}
}
return idSet;
}
/**
* ID
*/
public static List<Device> sortDeviceById(List<Device> deviceList) {
List<Device> result = new ArrayList<>();
if (deviceList == null || deviceList.isEmpty()) {
return result;
}
result.addAll(deviceList);
result.sort((d1, d2) -> {
if (!validateDeviceId(d1.getDeviceId())) {
return 1;
}
if (!validateDeviceId(d2.getDeviceId())) {
return -1;
}
return d1.getDeviceId().compareTo(d2.getDeviceId());
});
return result;
}
/**
* ID
*/
public static Map<String, String> getDeviceNameMap(List<Device> deviceList) {
Map<String, String> nameMap = new HashMap<>();
if (deviceList == null || deviceList.isEmpty()) {
return nameMap;
}
for (Device device : deviceList) {
if (validateDeviceId(device.getDeviceId())) {
nameMap.put(device.getDeviceId(), device.getDeviceName());
}
}
return nameMap;
}
/**
*
*/
public static <T> boolean isEmpty(List<T> list) {
return list == null || list.isEmpty();
}
/**
*
*/
public static <T> boolean isNotEmpty(List<T> list) {
return !isEmpty(list);
}
/**
* Map
*/
public static <K, V> boolean isEmpty(Map<K, V> map) {
return map == null || map.isEmpty();
}
/**
*
*/
public static <T> T getFirst(List<T> list) {
if (isEmpty(list)) {
return null;
}
return list.get(0);
}
/**
* equals
*/
public static <T> List<T> distinct(List<T> list) {
if (isEmpty(list)) {
return new ArrayList<>();
}
return new ArrayList<>(new LinkedHashSet<>(list));
}
// ========== 日期时间处理方法 ==========
/**
* yyyy-MM-dd HH:mm:ss
*/
public static String formatDeviceDataTime(LocalDateTime time) {
if (time == null) {
return "";
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return time.format(formatter);
}
/**
* yyyy-MM-dd
*/
public static String formatInstallDate(Date date) {
if (date == null) {
return "";
}
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return localDateTime.format(formatter);
}
/**
*
*/
public static long getCurrentTimeSeconds() {
return System.currentTimeMillis() / 1000;
}
/**
*
*/
public static long calculateMinuteInterval(LocalDateTime startTime, LocalDateTime endTime) {
if (startTime == null || endTime == null) {
return 0;
}
return java.time.Duration.between(startTime, endTime).toMinutes();
}
/**
* LocalDateTime
*/
public static boolean isToday(LocalDateTime time) {
if (time == null) {
return false;
}
LocalDate today = LocalDate.now();
LocalDate targetDate = time.toLocalDate();
return today.equals(targetDate);
}
/**
* 00:00:00
*/
public static LocalDateTime getTodayStartTime() {
return LocalDate.now().atStartOfDay();
}
/**
* 23:59:59
*/
public static LocalDateTime getTodayEndTime() {
return LocalDate.now().atTime(23, 59, 59);
}
// ========== 设备数据随机生成(模拟测试用) ==========
/**
* TDS50-80
*/
public static BigDecimal generateMockTdsValue() {
Random random = new Random();
double value = 50 + random.nextDouble() * 30;
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP);
}
/**
* 0.1-0.5
*/
public static BigDecimal generateMockWaterPress() {
Random random = new Random();
double value = 0.1 + random.nextDouble() * 0.4;
return new BigDecimal(value).setScale(2, RoundingMode.HALF_UP);
}
/**
* 30-100
*/
public static BigDecimal generateMockWaterLevel() {
Random random = new Random();
double value = 30 + random.nextDouble() * 70;
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP);
}
/**
* 18-25
*/
public static BigDecimal generateMockTemperature() {
Random random = new Random();
double value = 18 + random.nextDouble() * 7;
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP);
}
/**
* 寿0-100
*/
public static Integer generateMockFilterLife() {
Random random = new Random();
return random.nextInt(101);
}
/**
* ID
*/
public static String generateMockDeviceId(DeviceType type) {
Random random = new Random();
String prefix = DeviceType.water_maker.equals(type) ? "WM" : "WS";
int num = random.nextInt(900) + 100; // 100-999
return prefix + num;
}
/**
*
*/
public static String generateMockDeviceName(DeviceType type) {
String[] makerNames = {"教学楼1号制水机", "图书馆制水机", "食堂制水机", "宿舍1号楼制水机", "办公楼制水机"};
String[] supplyNames = {"教学楼1号供水机", "图书馆供水机", "食堂供水机", "宿舍1号楼供水机", "办公楼供水机"};
Random random = new Random();
if (DeviceType.water_maker.equals(type)) {
return makerNames[random.nextInt(makerNames.length)];
} else {
return supplyNames[random.nextInt(supplyNames.length)];
}
}
// ========== 其他通用方法 ==========
/**
*
*/
public static void safeSleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
log.error("线程休眠被中断", e);
Thread.currentThread().interrupt();
}
}
/**
*
*/
public static <T> T defaultIfNull(T value, T defaultValue) {
return value == null ? defaultValue : value;
}
/**
* 32UUID线
*/
public static String generateUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
*
*/
public static String convertAlertLevelToCn(Integer level) {
if (level == null) {
return "普通";
}
return switch (level) {
case 1 -> "紧急";
case 2 -> "重要";
case 3 -> "普通";
default -> "普通";
};
}
}
Loading…
Cancel
Save