fix:数据查询优化排序问题

This commit is contained in:
2026-03-18 15:40:42 +08:00
parent c8503abc8a
commit 9922c82a74
2 changed files with 48 additions and 18 deletions

View File

@@ -73,24 +73,51 @@ public class FileSizeUtils {
* @param unit 文件大小单位 (B, KB, MB, GB, TB)
* @return 字节大小
*/
public static Long convertToBytes(Long size, String unit) {
public static Long convertToBytes(BigDecimal size, String unit) {
if (size == null || unit == null) {
return null;
}
BigDecimal result;
switch (unit.toUpperCase()) {
case "B":
return size;
case "KB":
return size * 1024L;
case "MB":
return size * 1024L * 1024L;
case "GB":
return size * 1024L * 1024L * 1024L;
case "TB":
return size * 1024L * 1024L * 1024L * 1024L;
default:
return size; // 默认认为是字节
case "B" -> result = size;
case "KB" -> result = size.multiply(new BigDecimal("1024"));
case "MB" -> result = size.multiply(new BigDecimal("1048576")); // 1024^2
case "GB" -> result = size.multiply(new BigDecimal("1073741824")); // 1024^3
case "TB" -> result = size.multiply(new BigDecimal("1099511627776")); // 1024^4
default -> result = size;
}
return result.longValue();
}
/**
* 将格式化后的文件大小字符串转换为字节数
* @param formatFileSize 格式化后的文件大小(如 "1.06 KB", "6.02 MB", "1.06KB"
* @return 字节数
*/
public static Long parseFileSizeToBytes(String formatFileSize) {
if (formatFileSize == null || formatFileSize.trim().isEmpty()) {
return 0L;
}
formatFileSize = formatFileSize.trim().toUpperCase();
// 使用正则表达式匹配数字和单位
// 匹配模式:数字(可能包含小数点)+ 可选的空白 + 单位
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("([\\d.]+)\\s*([A-Z]+)");
java.util.regex.Matcher matcher = pattern.matcher(formatFileSize);
if (!matcher.matches()) {
return 0L;
}
try {
BigDecimal value = new BigDecimal(matcher.group(1));
String unit = matcher.group(2);
return convertToBytes(value, unit);
} catch (NumberFormatException e) {
return 0L;
}
}