0918优化-综合查询最大最小加日期
This commit is contained in:
@ -14,10 +14,12 @@ public class DevicePointDataList
|
|||||||
private String parentDeviceId;
|
private String parentDeviceId;
|
||||||
// 该设备点位数据list
|
// 该设备点位数据list
|
||||||
private List<GeneralQueryDataVo> pointValueList;
|
private List<GeneralQueryDataVo> pointValueList;
|
||||||
// 最大值
|
// 最大值&时间
|
||||||
private BigDecimal maxValue;
|
private BigDecimal maxValue;
|
||||||
// 最小值
|
private String maxDate;
|
||||||
|
// 最小值&时间
|
||||||
private BigDecimal minValue;
|
private BigDecimal minValue;
|
||||||
|
private String minDate;
|
||||||
// 平均值(保留4位小数)
|
// 平均值(保留4位小数)
|
||||||
private BigDecimal avgValue;
|
private BigDecimal avgValue;
|
||||||
// 差值(max - min)
|
// 差值(max - min)
|
||||||
@ -25,7 +27,8 @@ public class DevicePointDataList
|
|||||||
|
|
||||||
public DevicePointDataList(String deviceId, List<GeneralQueryDataVo> pointValueList,String parentDeviceId,
|
public DevicePointDataList(String deviceId, List<GeneralQueryDataVo> pointValueList,String parentDeviceId,
|
||||||
BigDecimal maxValue, BigDecimal minValue,
|
BigDecimal maxValue, BigDecimal minValue,
|
||||||
BigDecimal avgValue, BigDecimal diffValue) {
|
BigDecimal avgValue, BigDecimal diffValue,
|
||||||
|
String maxDate, String minDate) {
|
||||||
this.deviceId = deviceId;
|
this.deviceId = deviceId;
|
||||||
this.pointValueList = pointValueList;
|
this.pointValueList = pointValueList;
|
||||||
this.parentDeviceId = parentDeviceId;
|
this.parentDeviceId = parentDeviceId;
|
||||||
@ -33,12 +36,30 @@ public class DevicePointDataList
|
|||||||
this.minValue = minValue;
|
this.minValue = minValue;
|
||||||
this.avgValue = avgValue;
|
this.avgValue = avgValue;
|
||||||
this.diffValue = diffValue;
|
this.diffValue = diffValue;
|
||||||
|
this.maxDate = maxDate;
|
||||||
|
this.minDate = minDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DevicePointDataList() {
|
public DevicePointDataList() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMaxDate() {
|
||||||
|
return maxDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxDate(String maxDate) {
|
||||||
|
this.maxDate = maxDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMinDate() {
|
||||||
|
return minDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinDate(String minDate) {
|
||||||
|
this.minDate = minDate;
|
||||||
|
}
|
||||||
|
|
||||||
public String getDeviceId() {
|
public String getDeviceId() {
|
||||||
return deviceId;
|
return deviceId;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -222,7 +222,7 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService
|
|||||||
|
|
||||||
// 4. 构建DeviceItem
|
// 4. 构建DeviceItem
|
||||||
return new DevicePointDataList(deviceId, pointValueList,parentDeviceId,
|
return new DevicePointDataList(deviceId, pointValueList,parentDeviceId,
|
||||||
stats.max, stats.min,stats.avg,stats.diff);
|
stats.max, stats.min,stats.avg,stats.diff,stats.maxDate,stats.minDate);
|
||||||
})// 关键排序步骤:先按deviceId升序,再按parentDeviceId升序
|
})// 关键排序步骤:先按deviceId升序,再按parentDeviceId升序
|
||||||
.sorted(
|
.sorted(
|
||||||
Comparator.comparing(DevicePointDataList::getDeviceId) // 第一排序键:deviceId
|
Comparator.comparing(DevicePointDataList::getDeviceId) // 第一排序键:deviceId
|
||||||
@ -258,7 +258,7 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService
|
|||||||
Stats stats = clacStats(deviceDataList);
|
Stats stats = clacStats(deviceDataList);
|
||||||
|
|
||||||
return new DevicePointDataList(deviceId, deviceDataList,null,
|
return new DevicePointDataList(deviceId, deviceDataList,null,
|
||||||
stats.max, stats.min,stats.avg,stats.diff);
|
stats.max, stats.min,stats.avg,stats.diff,stats.maxDate,stats.minDate);
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
@ -269,25 +269,30 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Stats clacStats(List<GeneralQueryDataVo> deviceDataList) {
|
private Stats clacStats(List<GeneralQueryDataVo> deviceDataList) {
|
||||||
// 提取有效数值(过滤null)
|
// 转换并过滤数据,封装数值和对应的时间
|
||||||
List<BigDecimal> values = deviceDataList.stream()
|
List<ValueTimePair> validPairs = deviceDataList.stream()
|
||||||
.map(vo -> convertToBigDecimal(vo.getPointValue()))
|
.map(vo -> {
|
||||||
.filter(value -> value != null) // 过滤null值
|
BigDecimal value = convertToBigDecimal(vo.getPointValue());
|
||||||
|
return value != null ?
|
||||||
|
new ValueTimePair(value, vo.getValueDate()) :
|
||||||
|
null;
|
||||||
|
})
|
||||||
|
.filter(pair -> pair != null) // 过滤无效数据
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
if (values.isEmpty()) {
|
|
||||||
return new Stats(null, null, null, null);
|
if (validPairs.isEmpty()) {
|
||||||
|
return new Stats(null, null, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算最大最小值
|
// 计算最大最小值
|
||||||
BigDecimal maxValue = values.stream().max(BigDecimal::compareTo).get();
|
Optional<ValueTimePair> maxPair = validPairs.stream().max((p1, p2) -> p1.value.compareTo(p2.value));
|
||||||
BigDecimal minValue = values.stream().min(BigDecimal::compareTo).get();
|
Optional<ValueTimePair> minPair = validPairs.stream().min((p1, p2) -> p1.value.compareTo(p2.value));
|
||||||
// 计算平均值,四舍五入保留4为小数
|
// 计算平均值,四舍五入保留4为小数
|
||||||
BigDecimal sum = values.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal sum = validPairs.stream() .map(pair -> pair.value).reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||||
BigDecimal avgValue = sum.divide(BigDecimal.valueOf(values.size()), 4, BigDecimal.ROUND_HALF_UP);
|
BigDecimal avgValue = sum.divide(BigDecimal.valueOf(validPairs.size()), 4, BigDecimal.ROUND_HALF_UP);
|
||||||
// 增量数据,计算差值
|
// 增量数据,计算差值
|
||||||
BigDecimal diff = maxValue.subtract(minValue);
|
BigDecimal diff = maxPair.get().value.subtract(minPair.get().value);
|
||||||
|
|
||||||
return new Stats(maxValue,minValue,avgValue,diff);
|
return new Stats(maxPair.get().value,minPair.get().value,avgValue,diff,maxPair.get().time,minPair.get().time);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BigDecimal convertToBigDecimal(Object pointValue) {
|
private BigDecimal convertToBigDecimal(Object pointValue) {
|
||||||
@ -492,15 +497,34 @@ public class GeneralQueryServiceImpl implements IGeneralQueryService
|
|||||||
*/
|
*/
|
||||||
private static class Stats {
|
private static class Stats {
|
||||||
private final BigDecimal max;
|
private final BigDecimal max;
|
||||||
|
private final String maxDate;
|
||||||
private final BigDecimal min;
|
private final BigDecimal min;
|
||||||
|
private final String minDate;
|
||||||
private final BigDecimal avg;
|
private final BigDecimal avg;
|
||||||
private final BigDecimal diff;
|
private final BigDecimal diff;
|
||||||
|
|
||||||
public Stats(BigDecimal max, BigDecimal min, BigDecimal avg, BigDecimal diff) {
|
public Stats(BigDecimal max, BigDecimal min, BigDecimal avg, BigDecimal diff,
|
||||||
|
String maxDate, String minDate) {
|
||||||
this.max = max;
|
this.max = max;
|
||||||
|
this.maxDate = maxDate;
|
||||||
this.min = min;
|
this.min = min;
|
||||||
|
this.minDate = minDate;
|
||||||
this.avg = avg;
|
this.avg = avg;
|
||||||
this.diff = diff;
|
this.diff = diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 辅助类:关联数值和对应的时间
|
||||||
|
*/
|
||||||
|
private static class ValueTimePair {
|
||||||
|
final BigDecimal value;
|
||||||
|
final String time;
|
||||||
|
|
||||||
|
ValueTimePair(BigDecimal value, String time) {
|
||||||
|
this.value = value;
|
||||||
|
this.time = time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user