点位清单-按数值排序
This commit is contained in:
@ -27,6 +27,16 @@ public class PointDataRequest {
|
|||||||
/** 点位数据-范围上下限 */
|
/** 点位数据-范围上下限 */
|
||||||
private BigDecimal lower;
|
private BigDecimal lower;
|
||||||
private BigDecimal upper;
|
private BigDecimal upper;
|
||||||
|
/** 排序字段 */
|
||||||
|
private String sortData;
|
||||||
|
|
||||||
|
public String getSortData() {
|
||||||
|
return sortData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortData(String sortData) {
|
||||||
|
this.sortData = sortData;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSiteId() {
|
public String getSiteId() {
|
||||||
return siteId;
|
return siteId;
|
||||||
|
|||||||
@ -187,20 +187,48 @@ public class EmsDeviceSettingServiceImpl implements IEmsDeviceSettingService
|
|||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结果排序
|
// 确保赋值后不会被修改
|
||||||
String sortMethod = request.getSortMethod();
|
final List<PointQueryResponse> resultResponse = response;
|
||||||
if (sortMethod==null || sortMethod.isEmpty() || "asc".equals(sortMethod)) {// 升序
|
|
||||||
response = response.stream()
|
|
||||||
.filter(p -> p.getPointValue() != null)
|
|
||||||
.sorted(Comparator.comparing(PointQueryResponse::getUpdateTime)).collect(Collectors.toList());
|
|
||||||
} else if ("desc".equals(sortMethod)) {//降序
|
|
||||||
response = response.stream()
|
|
||||||
.filter(p -> p.getPointValue() != null)
|
|
||||||
.sorted(Comparator.comparing(PointQueryResponse::getUpdateTime).reversed()).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 根据排序字段和排序方式进行排序
|
||||||
|
Comparator<PointQueryResponse> comparator;
|
||||||
|
String sortData = request.getSortData();
|
||||||
|
if ("pointValue".equals(sortData)) {// 按pointValue排序
|
||||||
|
comparator = Comparator.comparing(
|
||||||
|
PointQueryResponse::getPointValue,
|
||||||
|
EmsDeviceSettingServiceImpl::compareMultiType);
|
||||||
|
} else {// 默认按updateTime排序
|
||||||
|
comparator = Comparator.comparing(PointQueryResponse::getUpdateTime);
|
||||||
|
}
|
||||||
|
// 排序方式
|
||||||
|
String sortMethod = request.getSortMethod();
|
||||||
|
boolean isAsc = sortMethod == null || sortMethod.isEmpty() || "asc".equals(sortMethod);
|
||||||
|
Comparator<PointQueryResponse> finalComparator = Comparator.comparing(
|
||||||
|
PointQueryResponse::getPointValue, // 用于判断null的字段
|
||||||
|
(v1, v2) -> { // 核心逻辑:null值始终返回1(排在后面)
|
||||||
|
if (v1 == v2) return 0;
|
||||||
|
if (v1 == null) return 1;
|
||||||
|
if (v2 == null) return -1;
|
||||||
|
// 非null值使用基础比较器逻辑
|
||||||
|
return isAsc ?
|
||||||
|
comparator.compare(findByValue(resultResponse, v1), findByValue(resultResponse, v2)) :
|
||||||
|
comparator.reversed().compare(findByValue(resultResponse, v1), findByValue(resultResponse, v2));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return resultResponse.stream()
|
||||||
|
.sorted(finalComparator)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
// 辅助方法:根据值查找对应的对象(用于比较器中获取完整对象)
|
||||||
|
private PointQueryResponse findByValue(List<PointQueryResponse> list, Object value) {
|
||||||
|
return list.stream()
|
||||||
|
.filter(p -> {
|
||||||
|
Object val = p.getPointValue();
|
||||||
|
return val == value || (val != null && val.equals(value));
|
||||||
|
})
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
// Object转BigDecimal(支持多种类型)
|
// Object转BigDecimal(支持多种类型)
|
||||||
private BigDecimal parseToBigDecimal(Object dataValue) {
|
private BigDecimal parseToBigDecimal(Object dataValue) {
|
||||||
if (dataValue instanceof BigDecimal) {
|
if (dataValue instanceof BigDecimal) {
|
||||||
@ -276,5 +304,64 @@ public class EmsDeviceSettingServiceImpl implements IEmsDeviceSettingService
|
|||||||
// 3. 转换为Date
|
// 3. 转换为Date
|
||||||
return new Date(updateTime);
|
return new Date(updateTime);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 自定义多类型比较器,支持BigDecimal、Integer、String等类型比较
|
||||||
|
*/
|
||||||
|
private static int compareMultiType(Object o1, Object o2) {
|
||||||
|
// 统一转换为Comparable进行比较
|
||||||
|
try {
|
||||||
|
// 对于数字类型(BigDecimal、Integer等)
|
||||||
|
if (o1 instanceof Number && o2 instanceof Number) {
|
||||||
|
Number numObj1 = (Number) o1;
|
||||||
|
Number numObj2 = (Number) o2;
|
||||||
|
|
||||||
|
BigDecimal num1 = toBigDecimal(numObj1);
|
||||||
|
BigDecimal num2 = toBigDecimal(numObj2);
|
||||||
|
return num1.compareTo(num2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 对于字符串类型
|
||||||
|
if (o1 instanceof String && o2 instanceof String) {
|
||||||
|
String str1 = (String) o1;
|
||||||
|
String str2 = (String) o2;
|
||||||
|
|
||||||
|
if (isNumericString(str1) && isNumericString(str2)) {
|
||||||
|
return new BigDecimal(str1).compareTo(new BigDecimal(str2));
|
||||||
|
}
|
||||||
|
return str1.compareTo(str2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他类型默认按toString比较
|
||||||
|
return o1.toString().compareTo(o2.toString());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 比较失败时按toString兜底
|
||||||
|
return o1.toString().compareTo(o2.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将Number类型转换为BigDecimal
|
||||||
|
*/
|
||||||
|
private static BigDecimal toBigDecimal(Number number) {
|
||||||
|
if (number instanceof BigDecimal) {
|
||||||
|
return (BigDecimal) number;
|
||||||
|
}
|
||||||
|
return new BigDecimal(number.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断字符串是否为数字格式
|
||||||
|
*/
|
||||||
|
private static boolean isNumericString(String str) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
new BigDecimal(str);
|
||||||
|
return true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user