package com.ruoyi.utils.tools;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.io.*;
|
import java.lang.reflect.Field;
|
import java.text.DecimalFormat;
|
import java.time.LocalTime;
|
import java.util.*;
|
|
/**
|
* @Description: 工具方法
|
* @ClassName: ArdTool
|
* @Author: 刘苏义
|
* @Date: 2023年06月02日14:37
|
* @Version: 1.0
|
**/
|
@Slf4j
|
public class ArdTool {
|
|
/**
|
* @描述 实体对象转Map
|
* @参数 [entity]
|
* @返回值 java.util.Map<java.lang.String, java.lang.Object>
|
* @创建人 刘苏义
|
* @创建时间 2023/6/2 14:35
|
* @修改人和其它信息
|
*/
|
public static Map<String, Object> convertEntityToMap(Object entity) {
|
Map<String, Object> map = new HashMap<>();
|
Field[] fields = entity.getClass().getDeclaredFields();
|
for (Field field : fields) {
|
field.setAccessible(true);
|
try {
|
Object value = field.get(entity);
|
map.put(field.getName(), value);
|
} catch (IllegalAccessException e) {
|
e.printStackTrace();
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 求Map<K,V>中最小 Value 对应的Key值
|
*
|
* @param map
|
* @return
|
*/
|
public static String getKeyByMinValue(Map<String, Double> map) {
|
if (map == null || map.size() == 0) return null;
|
List<Map.Entry<String, Double>> list = new ArrayList(map.entrySet());
|
Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
|
String min = list.get(0).getKey();
|
// String max = list.get(list.size() - 1).getKey();
|
return min;
|
}
|
|
/**
|
* @描述 通过日夜切换时间判断当前使用通道号
|
* @参数 []
|
* @返回值 java.lang.Integer
|
* @创建人 刘苏义
|
* @创建时间 2023/6/17 13:50
|
* @修改人和其它信息
|
*/
|
public static Integer getChannelBydayNightTime(String dayNightTime) {
|
try {
|
// 获取当前时间
|
LocalTime currentTime = LocalTime.now();
|
String[] dayNight = dayNightTime.split("-");
|
// 解析时间字符串
|
LocalTime startTime = LocalTime.parse(dayNight[0]);
|
LocalTime endTime = LocalTime.parse(dayNight[1]);
|
|
// 检查当前时间是否在给定的时间范围内
|
boolean isWithinRange = !currentTime.isBefore(startTime) && !currentTime.isAfter(endTime);
|
// 输出结果
|
if (isWithinRange) {
|
return 1;
|
} else {
|
return 2;
|
}
|
} catch (Exception ex) {
|
log.error("光电切换获取通道异常:" + ex.getMessage());
|
return 1;
|
}
|
}
|
|
/**
|
* 文件大小智能转换
|
* 会将文件大小转换为最大满足单位
|
*
|
* @param size(文件大小,单位为B)
|
* @return 文件大小
|
*/
|
public static String formatFileSize(Long size) {
|
String sizeName = null;
|
if (1024 * 1024 > size && size >= 1024) {
|
sizeName = String.format("%.2f", size.doubleValue() / 1024) + "KB";
|
} else if (1024 * 1024 * 1024 > size && size >= 1024 * 1024) {
|
sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024)) + "MB";
|
} else if (size >= 1024 * 1024 * 1024) {
|
sizeName = String.format("%.2f", size.doubleValue() / (1024 * 1024 * 1024)) + "GB";
|
} else {
|
sizeName = size.toString() + "B";
|
}
|
return sizeName;
|
}
|
|
/**
|
* 文件大小智能转换
|
* 会将文件大小转换为最大满足单位
|
*
|
* @param size(文件大小,单位为B)
|
* @return 文件大小
|
*/
|
public static String readableFileSize(long size) {
|
if (size <= 0) {
|
return "0";
|
}
|
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
|
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
|
return new DecimalFormat("#,###.##").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
|
}
|
|
/**
|
* byte数组转字符串
|
* 刘苏义
|
* 2023/10/18 8:42:59
|
*
|
* @param byteArray
|
*/
|
public static String byteArrayToString(byte[] byteArray) {
|
StringBuilder stringBuilder = new StringBuilder();
|
for (byte b : byteArray) {
|
stringBuilder.append(String.valueOf(b));
|
}
|
return stringBuilder.toString();
|
}
|
|
/**
|
* 将数据写入txt文件
|
* 刘苏义
|
* 2024/3/8 12:58:39
|
*/
|
public static void writeStringToFile(String data, String fileName) {
|
|
try {
|
File file = new File(fileName);
|
File parentDir = file.getParentFile();
|
if (!parentDir.exists()) {
|
parentDir.mkdirs(); // 创建文件所在的目录,包括父目录
|
}
|
boolean fileExists = file.exists();
|
boolean dataExists = false;
|
if(fileExists) {
|
// 读取文件内容
|
BufferedReader reader = new BufferedReader(new FileReader(fileName));
|
String line;
|
while ((line = reader.readLine()) != null) {
|
if (line.equals(data)) {
|
dataExists = true;
|
break;
|
}
|
}
|
reader.close();
|
}
|
|
// 如果文件不存在或数据不存在,则追加到文件
|
if (!fileExists || !dataExists) {
|
FileWriter writer = new FileWriter(fileName, true);
|
writer.write(data + System.lineSeparator());
|
writer.close();
|
if (!fileExists) {
|
log.debug("File created and data has been appended.");
|
} else {
|
log.debug("Data has been appended to the file.");
|
}
|
} else {
|
log.debug("Data already exists in the file. Not appending.");
|
}
|
} catch (IOException e) {
|
log.error("An error occurred.");
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* 通过反射对象填充
|
* 刘苏义
|
* 2024/4/2 11:50:57
|
*/
|
public static void fillNullFields(Object source, Object target) {
|
if (source == null || target == null) {
|
return;
|
}
|
Field[] fields = source.getClass().getDeclaredFields();
|
for (Field field : fields) {
|
field.setAccessible(true);
|
try {
|
Object sourceValue = field.get(source);
|
Object targetValue = field.get(target);
|
if (targetValue == null && sourceValue != null) {
|
field.set(target, sourceValue);
|
}
|
} catch (IllegalAccessException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
}
|