|
|
@@ -394,13 +394,33 @@ public class IotOpeationFillController {
|
|
|
* 处理累计逻辑(优化后:批量查询历史数据)
|
|
|
*/
|
|
|
private void processSummationLogic(List<IotOpeationFillSaveReqVO> allFillData) {
|
|
|
- // 1. 提前过滤+非空校验(流式操作简化)
|
|
|
+
|
|
|
+ // 前置空校验:避免无效流式处理
|
|
|
+ if (allFillData == null || allFillData.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 合并过滤逻辑:一次性过滤出累计数据,按sumId分组(避免两次流式过滤)
|
|
|
+ Map<Long, List<IotOpeationFillSaveReqVO>> sumDataGroup = allFillData.stream()
|
|
|
+ .filter(fill -> 1 == fill.getIsSum())
|
|
|
+ .collect(Collectors.groupingBy(IotOpeationFillSaveReqVO::getSumId));
|
|
|
+
|
|
|
+ // 拆分出两类累计数据(空集合安全处理)
|
|
|
+ List<IotOpeationFillSaveReqVO> sumDataList = sumDataGroup.getOrDefault(1L, Collections.emptyList());
|
|
|
+ List<IotOpeationFillSaveReqVO> sumDataList1 = sumDataGroup.getOrDefault(0L, Collections.emptyList());
|
|
|
+
|
|
|
+
|
|
|
+ /*// 1. 提前过滤+非空校验(流式操作简化)RH,RY的累计属性
|
|
|
List<IotOpeationFillSaveReqVO> sumDataList = allFillData.stream()
|
|
|
.filter(fill -> 1 == fill.getIsSum())
|
|
|
+ .filter(fill -> 1 == fill.getSumId())
|
|
|
.collect(Collectors.toList());
|
|
|
- if (CollectionUtils.isEmpty(sumDataList)) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ //瑞都累计属性
|
|
|
+ List<IotOpeationFillSaveReqVO> sumDataList1 = allFillData.stream()
|
|
|
+ .filter(fill -> 1 == fill.getIsSum())
|
|
|
+ .filter(fill -> 0 == fill.getSumId())
|
|
|
+ .collect(Collectors.toList());*/
|
|
|
+
|
|
|
|
|
|
// 2. 构建unSumData的索引(设备ID + 模型ID),避免嵌套循环
|
|
|
// 键:deviceId_modelId,值:该维度下的fillContent数值
|
|
|
@@ -419,15 +439,30 @@ public class IotOpeationFillController {
|
|
|
));
|
|
|
|
|
|
// 3. 批量构建查询条件(简化流式操作)
|
|
|
- List<IotDeviceRunLogDO> queryLogs = sumDataList.stream()
|
|
|
- .map(fill -> {
|
|
|
- IotDeviceRunLogDO queryLog = new IotDeviceRunLogDO();
|
|
|
- queryLog.setDeviceId(fill.getDeviceId());
|
|
|
- queryLog.setPointName(fill.getPointName());
|
|
|
- queryLog.setCreateTime(LocalDateTime.of(fill.getCreateTime(), LocalTime.MIDNIGHT));
|
|
|
- return queryLog;
|
|
|
- })
|
|
|
- .collect(Collectors.toList());
|
|
|
+ List<IotDeviceRunLogDO> queryLogs = new ArrayList<>();
|
|
|
+ if(!sumDataList.isEmpty()){
|
|
|
+ queryLogs = sumDataList.stream()
|
|
|
+ .map(fill -> {
|
|
|
+ IotDeviceRunLogDO queryLog = new IotDeviceRunLogDO();
|
|
|
+ queryLog.setDeviceId(fill.getDeviceId());
|
|
|
+ queryLog.setPointName(fill.getPointName());
|
|
|
+ queryLog.setCreateTime(LocalDateTime.of(fill.getCreateTime(), LocalTime.MIDNIGHT));
|
|
|
+ return queryLog;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ if(!sumDataList1.isEmpty()){
|
|
|
+ queryLogs = sumDataList1.stream()
|
|
|
+ .map(fill -> {
|
|
|
+ IotDeviceRunLogDO queryLog = new IotDeviceRunLogDO();
|
|
|
+ queryLog.setDeviceId(fill.getDeviceId());
|
|
|
+ queryLog.setPointName(fill.getPointName());
|
|
|
+ queryLog.setCreateTime(LocalDateTime.of(fill.getCreateTime(), LocalTime.MIDNIGHT));
|
|
|
+ return queryLog;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
// 4. 批量查询历史最大数据(原逻辑保留,1次数据库交互)
|
|
|
List<IotDeviceRunLogDO> maxFillDataList = iotOpeationFillService.batchQueryMaxReportData(queryLogs);
|
|
|
@@ -453,9 +488,12 @@ public class IotOpeationFillController {
|
|
|
String historyKey = fill.getDeviceId() + "_" + fill.getPointName();
|
|
|
BigDecimal maxHistory = maxDataMap.getOrDefault(historyKey, BigDecimal.ZERO);
|
|
|
|
|
|
- // 累计计算(简化逻辑,消除冗余判空)
|
|
|
+ fill.setTotalRunTime(new BigDecimal(fill.getFillContent()));
|
|
|
+
|
|
|
+ //RH,RY累计等于历史最大值+当日填写值
|
|
|
BigDecimal total = maxHistory.add(currentValue);
|
|
|
fill.setTotalRunTime(total);
|
|
|
+
|
|
|
} catch (NumberFormatException e) {
|
|
|
// 补充日志,便于问题排查
|
|
|
log.warn("累计数据处理失败:defaultValue转换异常,fillId={}, defaultValue={}",
|
|
|
@@ -465,67 +503,27 @@ public class IotOpeationFillController {
|
|
|
log.error("累计数据处理异常:fillId={}", fill.getId(), e);
|
|
|
}
|
|
|
});
|
|
|
- }
|
|
|
- /* private void processSummationLogic(List<IotOpeationFillSaveReqVO> allFillData) {
|
|
|
- // 筛选需要累计的数据(不变)
|
|
|
- List<IotOpeationFillSaveReqVO> sumDataList = allFillData.stream()
|
|
|
- .filter(fill -> 1 == fill.getIsSum())
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- List<IotOpeationFillSaveReqVO> unSumDataList = allFillData.stream()
|
|
|
- .filter(fill -> 0 == fill.getIsSum()&&fill.getSumId() == null)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(sumDataList)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 1. 批量构建查询条件(避免循环查询)
|
|
|
- List<IotDeviceRunLogDO> queryLogs = sumDataList.stream()
|
|
|
- .map(fill -> {
|
|
|
- IotDeviceRunLogDO queryLog = new IotDeviceRunLogDO();
|
|
|
- queryLog.setDeviceId(fill.getDeviceId());
|
|
|
- queryLog.setPointName(fill.getPointName());
|
|
|
- queryLog.setCreateTime(LocalDateTime.of(fill.getCreateTime(), LocalTime.MIDNIGHT));
|
|
|
- return queryLog;
|
|
|
- })
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- // 2. 批量查询历史最大数据(1次数据库交互)
|
|
|
- List<IotDeviceRunLogDO> maxFillDataList = iotOpeationFillService.batchQueryMaxReportData(queryLogs);
|
|
|
-
|
|
|
-
|
|
|
- Map<String, BigDecimal> maxDataMap = maxFillDataList.stream()
|
|
|
- .filter(data -> data.getTotalRunTime() != null)
|
|
|
- .collect(Collectors.toMap(
|
|
|
- data -> data.getDeviceId() + "_" + data.getPointName(), // 设备+测点唯一标识
|
|
|
- IotDeviceRunLogDO::getTotalRunTime,
|
|
|
- (v1, v2) -> v1 // 解决key重复问题,保留第一个值(根据业务调整)
|
|
|
- ));
|
|
|
-
|
|
|
- // 3. 并行处理累计计算(CPU密集型,并行提速)
|
|
|
- sumDataList.forEach(fill -> {
|
|
|
+ sumDataList1.forEach(fill->{
|
|
|
try {
|
|
|
- BigDecimal total = BigDecimal.ZERO;
|
|
|
- for (IotOpeationFillSaveReqVO targetFill : unSumDataList) {
|
|
|
- if(targetFill.getModelId()==Long.parseLong(fill.getDefaultValue())&& targetFill.getDeviceId().equals(fill.getDeviceId())){
|
|
|
- BigDecimal currentValue = new BigDecimal(targetFill.getFillContent());
|
|
|
- // 累加:从缓存获取历史最大值
|
|
|
- String key = fill.getDeviceId() + "_" + fill.getPointName();
|
|
|
- BigDecimal maxHistory = maxDataMap.getOrDefault(key, BigDecimal.ZERO);
|
|
|
- if(maxHistory!=null) {
|
|
|
- total = maxHistory.add(currentValue);
|
|
|
- }else{
|
|
|
- total = fill.getTotalRunTime().add(new BigDecimal(fill.getFillContent()));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- fill.setTotalRunTime(total);
|
|
|
+ // 提前解析defaultValue(模型ID),避免循环内重复解析
|
|
|
+ Long modelId = Long.parseLong(fill.getDefaultValue());
|
|
|
+ // 构建索引键:deviceId_modelId
|
|
|
+ String unSumKey = fill.getDeviceId() + "_" + modelId;
|
|
|
+ // 从索引获取目标数值(O(1)查询)
|
|
|
+ BigDecimal currentValue = unSumDataIndex.getOrDefault(unSumKey, BigDecimal.ZERO);
|
|
|
+ fill.setTotalRunTime(currentValue);
|
|
|
+
|
|
|
} catch (NumberFormatException e) {
|
|
|
- //log.warn("DefaultValue转换失败: {}", fill.getDefaultValue());
|
|
|
+ // 补充日志,便于问题排查
|
|
|
+ log.warn("累计数据处理失败:defaultValue转换异常,fillId={}, defaultValue={}",
|
|
|
+ fill.getId(), fill.getDefaultValue(), e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 兜底异常处理,避免单条数据失败导致整体中断
|
|
|
+ log.error("累计数据处理异常:fillId={}", fill.getId(), e);
|
|
|
}
|
|
|
});
|
|
|
- }*/
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|