Browse Source

违规启动设备统计封装处理更多设备类型的方法

lingpeng.li 5 months ago
parent
commit
b176f0ac72

+ 34 - 128
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/homePageData/service/impl/HomePageDataServiceImpl.java

@@ -439,94 +439,11 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
     public Map<String, Integer> illegalActivation() {
 
         Map<String, Integer> res = new HashMap<>();
-        LambdaQueryWrapper<LeanEventsHost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-        lambdaQueryWrapper.eq(LeanEventsHost::getDeviceInformationId, "tops");
-        lambdaQueryWrapper.eq(LeanEventsHost::getDeviceInformationType, "fpg_close");
-        LeanEventsHost one = leanEventsHostService.getOne(lambdaQueryWrapper);
-        if (null != one) {
-
-            if (oConvertUtils.isNotEmpty(one.getDeviceInformationList())) {
-
-                // 使用 String.split() 按逗号分割字符串
-                String[] array = one.getDeviceInformationList().split(",");
-
-                // 将数组转换为 List
-                List<String> list = Arrays.asList(array);
-
-                // 获取当天的日期和时间
-                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
-                String todayDateTime = LocalDateTime.now().format(formatter); // 获取当前的日期时间
-                String todayDate = todayDateTime.split(" ")[0]; // 获取当前日期部分(yyyy-MM-dd)
-
-
-                // 使用 deviceInformationId (s) 和 today's date 来构造 warnInfoLambdaQueryWrapper
-
-                LambdaQueryWrapper<LeanEventWarnInfo> warnInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
-                warnInfoLambdaQueryWrapper.in(LeanEventWarnInfo::getDeviceInformationId, list);
-                warnInfoLambdaQueryWrapper.likeRight(LeanEventWarnInfo::getCreateTime, todayDate);
-
-                // 查询结果
-                List<LeanEventWarnInfo> warnInfoList = leanEventWarnInfoService.list(warnInfoLambdaQueryWrapper);
-
-                // 使用 Map 去重,基于 deviceInformationId
-                Map<String, LeanEventWarnInfo> uniqueWarnInfoMap = warnInfoList.stream()
-                        .collect(Collectors.toMap(
-                                LeanEventWarnInfo::getDeviceInformationId,  // 用 deviceInformationId 作为 key
-                                warnInfo -> warnInfo,  // value 为 warnInfo 对象
-                                (existing, replacement) -> existing)); // 如果有重复的 key,保留第一个
-
-                // 获取去重后的结果列表
-                List<LeanEventWarnInfo> distinctWarnInfoList = uniqueWarnInfoMap.values().stream().collect(Collectors.toList());
-
-                res.put("tops", distinctWarnInfoList.size());
-            }
-
-            LambdaQueryWrapper<LeanEventsHost> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
-            lambdaQueryWrapper1.eq(LeanEventsHost::getDeviceInformationId, "peaks");
-            lambdaQueryWrapper1.eq(LeanEventsHost::getDeviceInformationType, "fpg_close");
-            LeanEventsHost one1 = leanEventsHostService.getOne(lambdaQueryWrapper);
-            if (null != one1) {
 
-                if (oConvertUtils.isNotEmpty(one1.getDeviceInformationList())) {
+        // 公共方法处理逻辑
+        processDeviceInformation("tops", "fpg_close", res);
+        processDeviceInformation("peaks", "fpg_close", res);
 
-                    // 使用 String.split() 按逗号分割字符串
-                    String[] array = one1.getDeviceInformationList().split(",");
-
-                    // 将数组转换为 List
-                    List<String> list = Arrays.asList(array);
-
-                    // 获取当天的日期和时间
-                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
-                    String todayDateTime = LocalDateTime.now().format(formatter); // 获取当前的日期时间
-                    String todayDate = todayDateTime.split(" ")[0]; // 获取当前日期部分(yyyy-MM-dd)
-
-
-                    // 使用 deviceInformationId (s) 和 today's date 来构造 warnInfoLambdaQueryWrapper
-
-                    LambdaQueryWrapper<LeanEventWarnInfo> warnInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
-                    warnInfoLambdaQueryWrapper.in(LeanEventWarnInfo::getDeviceInformationId, list);
-                    warnInfoLambdaQueryWrapper.likeRight(LeanEventWarnInfo::getCreateTime, todayDate);
-
-                    // 查询结果
-                    List<LeanEventWarnInfo> warnInfoList = leanEventWarnInfoService.list(warnInfoLambdaQueryWrapper);
-
-                    // 使用 Map 去重,基于 deviceInformationId
-                    Map<String, LeanEventWarnInfo> uniqueWarnInfoMap = warnInfoList.stream()
-                            .collect(Collectors.toMap(
-                                    LeanEventWarnInfo::getDeviceInformationId,  // 用 deviceInformationId 作为 key
-                                    warnInfo -> warnInfo,  // value 为 warnInfo 对象
-                                    (existing, replacement) -> existing)); // 如果有重复的 key,保留第一个
-
-                    // 获取去重后的结果列表
-                    List<LeanEventWarnInfo> distinctWarnInfoList = uniqueWarnInfoMap.values().stream().collect(Collectors.toList());
-
-                    res.put("peaks", distinctWarnInfoList.size());
-                }
-
-            }
-
-
-        }
         return res;
     }
 
@@ -629,14 +546,11 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
                             illegalResult.setPower(leanModelDeviceHistoryInfo.getPower());
                             illegalResult.setCreateTime(DateUtils.str2Date(leanModelDeviceHistoryInfo.getDatestr(), DateUtils.datetimeFormat.get()));
 
-
                             illegalResultList.add(illegalResult);
                         }
-
                     }
                 }
             }
-
         }
 
         return illegalResultList;
@@ -664,7 +578,6 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
                 String todayDateTime = LocalDateTime.now().format(formatter); // 获取当前的日期时间
                 String todayDate = todayDateTime.split(" ")[0]; // 获取当前日期部分(yyyy-MM-dd)
 
-
                 // 使用 deviceInformationId (s) 和 today's date 来构造 warnInfoLambdaQueryWrapper
 
                 LambdaQueryWrapper<LeanEventWarnInfo> warnInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -740,14 +653,11 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
                             illegalResult.setPower(leanModelDeviceHistoryInfo.getPower());
                             illegalResult.setCreateTime(DateUtils.str2Date(leanModelDeviceHistoryInfo.getDatestr(), DateUtils.datetimeFormat.get()));
 
-
                             illegalResultList.add(illegalResult);
                         }
-
                     }
                 }
             }
-
         }
 
         return illegalResultList;
@@ -777,15 +687,6 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
         return list;
     }
 
-
-//    private Map<String,String> getMap(Date date,String value){
-//        Map<String,String> res = new HashMap<>();
-//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
-//        res.put("time",simpleDateFormat.format(date));
-//        res.put("value",value);
-//        return res;
-//    }
-
     // 添加一个方法将 datestr 转换为 Date 类型
     private Date parseDate(String datestr) {
         // 获取当前日期
@@ -806,32 +707,6 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
         }
     }
 
-    private List<Map<String, String>> mergeListByTime(List<Map<String, String>> list) {
-        Map<String, Double> mergedMap = new HashMap<>();
-
-        // 累加相同时间点的值
-        for (Map<String, String> entry : list) {
-            String time = entry.get("time");
-            Double value = Double.parseDouble(entry.get("value"));
-            mergedMap.put(time, mergedMap.getOrDefault(time, 0.0) + value);
-        }
-
-        // 转换回 List<Map<String, String>> 格式
-        List<Map<String, String>> mergedList = new ArrayList<>();
-        for (Map.Entry<String, Double> entry : mergedMap.entrySet()) {
-            Map<String, String> map = new HashMap<>();
-            map.put("time", entry.getKey());
-            map.put("value", String.format("%.2f", entry.getValue())); // 保留两位小数
-            mergedList.add(map);
-        }
-
-        // 按时间从小到大排序
-        mergedList.sort(Comparator.comparing(o -> o.get("time")));
-
-        return mergedList;
-    }
-
-
     private Map<String, String> getMap(Date date, String value, String regionId) {
         Map<String, String> res = new HashMap<>();
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
@@ -923,4 +798,35 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
     }
 
 
+    // 提取公共逻辑到一个方法中
+    private void processDeviceInformation(String deviceInformationId, String deviceInformationType, Map<String, Integer> res) {
+        LambdaQueryWrapper<LeanEventsHost> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        lambdaQueryWrapper.eq(LeanEventsHost::getDeviceInformationId, deviceInformationId);
+        lambdaQueryWrapper.eq(LeanEventsHost::getDeviceInformationType, deviceInformationType);
+
+        LeanEventsHost one = leanEventsHostService.getOne(lambdaQueryWrapper);
+        if (one != null && oConvertUtils.isNotEmpty(one.getDeviceInformationList())) {
+            // 按逗号分割字符串,转换为 List
+            List<String> list = Arrays.asList(one.getDeviceInformationList().split(","));
+
+            // 获取当天的日期
+            String todayDate = LocalDate.now().toString(); // 获取当前日期部分(yyyy-MM-dd)
+
+            // 构造 warnInfoLambdaQueryWrapper 查询条件
+            LambdaQueryWrapper<LeanEventWarnInfo> warnInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            warnInfoLambdaQueryWrapper.in(LeanEventWarnInfo::getDeviceInformationId, list);
+            warnInfoLambdaQueryWrapper.likeRight(LeanEventWarnInfo::getCreateTime, todayDate);
+
+            // 查询并去重
+            List<LeanEventWarnInfo> warnInfoList = leanEventWarnInfoService.list(warnInfoLambdaQueryWrapper);
+            long distinctCount = warnInfoList.stream()
+                    .map(LeanEventWarnInfo::getDeviceInformationId)
+                    .distinct()
+                    .count();
+
+            // 存入结果
+            res.put(deviceInformationId, (int) distinctCount);
+        }
+    }
+
 }