Browse Source

优化实时设备运行统计,提取为不同的方法,方便拓展

lingpeng.li 5 months ago
parent
commit
0003aed8c0

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

@@ -343,73 +343,27 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
 
     @Override
     public List<Map<String, String>> realDeviceRun() {
-        List<DeviceRegion> list = deviceRegionService.list(); // 获取所有设备区域
-        List<Map<String, String>> res = new ArrayList<>(); // 存储结果
+        // 获取所有设备区域
+        List<DeviceRegion> deviceRegions = deviceRegionService.list();
+        List<Map<String, String>> result = new ArrayList<>();
 
-        // 获取当前日期并计算当月的起始日期和结束日期
+        // 计算当月的起始日期和结束日期
         LocalDate now = LocalDate.now();
-        LocalDate startOfMonth = now.withDayOfMonth(1); // 当前月的第一天
-        LocalDate endOfMonth = now.withDayOfMonth(now.lengthOfMonth()); // 当前月的最后一天
-
-        // 使用 DateTimeFormatter 将 LocalDate 格式化为字符串(假设格式为 yyyy-MM-dd)
-        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-        String startDateString = startOfMonth.format(formatter);
-        String endDateString = endOfMonth.format(formatter);
-
-        // 遍历所有设备区域
-        for (DeviceRegion deviceRegion : list) {
-            Query querylr = new Query();
-
-            // 添加筛选条件:deviceRegionId 和 当月日期范围
-            querylr.addCriteria(Criteria.where("deviceRegionId").in(deviceRegion.getId())
-                    .and("dates").gte(startDateString).lte(endDateString));
-
-            // 执行查询,获取该区域的设备统计数据
-            List<DeviceStatiscsModelMongodb> deviceStatiscsModelMongodbList = mongoTemplate.find(
-                    querylr, DeviceStatiscsModelMongodb.class, "total_day_powerproportioncurrent");
-
-            // 初始化总功率以及各时段功率的累加值
-            BigDecimal totalPower = BigDecimal.ZERO;
-            BigDecimal topsPower = BigDecimal.ZERO;
-            BigDecimal peaksPower = BigDecimal.ZERO;
-            BigDecimal flatPower = BigDecimal.ZERO;
-            BigDecimal valleysPower = BigDecimal.ZERO;
-
-            // 遍历当前区域的所有设备统计数据,进行功率累加
-            for (DeviceStatiscsModelMongodb deviceStats : deviceStatiscsModelMongodbList) {
-                topsPower = topsPower.add(deviceStats.getTopsPower() != null ? deviceStats.getTopsPower() : BigDecimal.ZERO);
-                peaksPower = peaksPower.add(deviceStats.getPeaksPower() != null ? deviceStats.getPeaksPower() : BigDecimal.ZERO);
-                flatPower = flatPower.add(deviceStats.getFlatPower() != null ? deviceStats.getFlatPower() : BigDecimal.ZERO);
-                valleysPower = valleysPower.add(deviceStats.getValleysPower() != null ? deviceStats.getValleysPower() : BigDecimal.ZERO);
-            }
-
-            // 计算当月总功率
-            totalPower = topsPower.add(peaksPower).add(flatPower).add(valleysPower);
-
-            // 创建一个Map,保存该区域的统计信息
-            Map<String, String> regionStats = new HashMap<>();
-            regionStats.put("deviceRegionId", deviceRegion.getId());
-            regionStats.put("deviceTitle", deviceRegion.getRegionTitle());
-            regionStats.put("totalPower", totalPower.toString()); // 总功率
-
-            // 计算并保存各时段的占比
-            if (totalPower.compareTo(BigDecimal.ZERO) > 0) {
-                regionStats.put("topsPowerProportion", topsPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString());
-                regionStats.put("peaksPowerProportion", peaksPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString());
-                regionStats.put("flatPowerProportion", flatPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString());
-                regionStats.put("valleysPowerProportion", valleysPower.divide(totalPower, 4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).stripTrailingZeros().toString());
-            } else {
-                // 如果总功率为0,则占比设为0
-                regionStats.put("topsPowerProportion", "0.00");
-                regionStats.put("peaksPowerProportion", "0.00");
-                regionStats.put("flatPowerProportion", "0.00");
-                regionStats.put("valleysPowerProportion", "0.00");
-            }
-
-            // 将每个设备区域的统计信息添加到结果列表中
-            res.add(regionStats);
+        String startDateString = now.withDayOfMonth(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+        String endDateString = now.withDayOfMonth(now.lengthOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+
+        // 遍历设备区域
+        for (DeviceRegion deviceRegion : deviceRegions) {
+            // 查询统计数据
+            List<DeviceStatiscsModelMongodb> deviceStatistics = getDeviceStatisticsForRegion(
+                    deviceRegion.getId(), startDateString, endDateString);
+
+            // 计算功率统计和占比
+            Map<String, String> regionStats = calculateRegionStats(deviceRegion, deviceStatistics);
+            result.add(regionStats);
         }
-        return res;
+
+        return result;
     }
 
     @Override
@@ -695,7 +649,7 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
         String currentDate = dateFormat.format(new Date());
 
         // 拼接日期和时间
-        String dateTimeStr = currentDate + " " + datestr + ":00";  // 例如 "2024-12-12 01:00:00"
+        String dateTimeStr = currentDate + " " + datestr + ":00";
 
         // 使用 "yyyy-MM-dd HH:mm:ss" 格式解析完整的日期时间字符串
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -829,4 +783,68 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
         }
     }
 
+    /**
+     * 获取指定区域的统计数据
+     */
+    private List<DeviceStatiscsModelMongodb> getDeviceStatisticsForRegion(String regionId, String startDate, String endDate) {
+        Query query = new Query();
+        query.addCriteria(Criteria.where("deviceRegionId").is(regionId)
+                .and("dates").gte(startDate).lte(endDate));
+        return mongoTemplate.find(query, DeviceStatiscsModelMongodb.class, "total_day_powerproportioncurrent");
+    }
+
+
+    /**
+     * 计算区域的功率统计和占比
+     */
+    private Map<String, String> calculateRegionStats(DeviceRegion deviceRegion, List<DeviceStatiscsModelMongodb> statistics) {
+        BigDecimal totalPower = BigDecimal.ZERO;
+        BigDecimal topsPower = BigDecimal.ZERO;
+        BigDecimal peaksPower = BigDecimal.ZERO;
+        BigDecimal flatPower = BigDecimal.ZERO;
+        BigDecimal valleysPower = BigDecimal.ZERO;
+
+        // 累加功率
+        for (DeviceStatiscsModelMongodb stat : statistics) {
+            topsPower = topsPower.add(stat.getTopsPower() != null ? stat.getTopsPower() : BigDecimal.ZERO);
+            peaksPower = peaksPower.add(stat.getPeaksPower() != null ? stat.getPeaksPower() : BigDecimal.ZERO);
+            flatPower = flatPower.add(stat.getFlatPower() != null ? stat.getFlatPower() : BigDecimal.ZERO);
+            valleysPower = valleysPower.add(stat.getValleysPower() != null ? stat.getValleysPower() : BigDecimal.ZERO);
+        }
+
+        // 计算总功率
+        totalPower = topsPower.add(peaksPower).add(flatPower).add(valleysPower);
+
+        // 构造统计结果
+        Map<String, String> regionStats = new HashMap<>();
+        regionStats.put("deviceRegionId", deviceRegion.getId());
+        regionStats.put("deviceTitle", deviceRegion.getRegionTitle());
+        regionStats.put("totalPower", totalPower.stripTrailingZeros().toString());
+
+        // 计算占比
+        if (totalPower.compareTo(BigDecimal.ZERO) > 0) {
+            regionStats.put("topsPowerProportion", calculatePercentage(topsPower, totalPower));
+            regionStats.put("peaksPowerProportion", calculatePercentage(peaksPower, totalPower));
+            regionStats.put("flatPowerProportion", calculatePercentage(flatPower, totalPower));
+            regionStats.put("valleysPowerProportion", calculatePercentage(valleysPower, totalPower));
+        } else {
+            regionStats.put("topsPowerProportion", "0.00");
+            regionStats.put("peaksPowerProportion", "0.00");
+            regionStats.put("flatPowerProportion", "0.00");
+            regionStats.put("valleysPowerProportion", "0.00");
+        }
+
+        return regionStats;
+    }
+
+    /**
+     * 计算百分比并格式化为字符串
+     */
+    private String calculatePercentage(BigDecimal part, BigDecimal total) {
+        return part.divide(total, 4, RoundingMode.HALF_UP)
+                .multiply(BigDecimal.valueOf(100))
+                .stripTrailingZeros()
+                .toPlainString();
+    }
+
 }