فهرست منبع

抓取生产消耗数据时对分组进行排序,并且计算消耗量时增加同一天的条件

lingpeng.li 4 ماه پیش
والد
کامیت
b7ff398f1d
1فایلهای تغییر یافته به همراه19 افزوده شده و 4 حذف شده
  1. 19 4
      jeecg-module-gather/src/main/java/org/jeecg/modules/watch/ProductPostgreSQLWatch.java

+ 19 - 4
jeecg-module-gather/src/main/java/org/jeecg/modules/watch/ProductPostgreSQLWatch.java

@@ -21,6 +21,8 @@ import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.Timestamp;
+import java.time.LocalDate;
+import java.time.ZoneId;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -72,10 +74,10 @@ public class ProductPostgreSQLWatch {
                 // 使用 split 方法分割字符串
                 String[] strArray = device.getDeviceVariable().split(",");
 
-                 // 将数组转换为 List
+                // 将数组转换为 List
                 List<String> list = Arrays.asList(strArray);
 
-                 // 将 List 转换为正确的 SQL IN 子句格式
+                // 将 List 转换为正确的 SQL IN 子句格式
                 String deviceSnInClause = list.stream()
                         .map(String::trim) // 去掉可能的空格
                         .map(sn -> "'" + sn + "'") // 每个值加上单引号
@@ -106,20 +108,33 @@ public class ProductPostgreSQLWatch {
                 Map<Timestamp, List<Map<String, Object>>> groupedData = resultList.stream()
                         .collect(Collectors.groupingBy(row -> (Timestamp) row.get("upload_time")));
 
+                // 按 upload_time 升序排序
+                List<Map.Entry<Timestamp, List<Map<String, Object>>>> sortedEntries = groupedData.entrySet()
+                        .stream()
+                        .sorted(Map.Entry.comparingByKey()) // 按 Timestamp 升序排列
+                        .collect(Collectors.toList());
+
                 // 对分组后的数据进行处理
-                for (Map.Entry<Timestamp, List<Map<String, Object>>> entry : groupedData.entrySet()) {
+                for (Map.Entry<Timestamp, List<Map<String, Object>>> entry : sortedEntries) {
                     ProductionConsume productionConsume = new ProductionConsume();
                     productionConsume.setRegionTitle(device.getDeviceRegion());
                     productionConsume.setDeviceTitle(device.getDeviceTitle());
 
                     Timestamp uploadTime = entry.getKey();
+                    // 获取 uploadTime 的日期部分
+                    LocalDate uploadDate = uploadTime.toInstant()
+                            .atZone(ZoneId.systemDefault())
+                            .toLocalDate();
+
                     List<Map<String, Object>> group = entry.getValue();
                     productionConsume.setCreateTime(uploadTime);
 
-                    // 查询最新记录
+                    // 查询最新记录,要求 createTime 和 uploadTime 都是同一天的数据
                     LambdaQueryWrapper<ProductionConsume> consumeQueryWrapper = new LambdaQueryWrapper<ProductionConsume>()
                             .eq(ProductionConsume::getRegionTitle, device.getDeviceRegion())
                             .eq(ProductionConsume::getDeviceTitle, device.getDeviceTitle())
+                            .ge(ProductionConsume::getCreateTime, uploadDate.atStartOfDay()) // createTime >= uploadDate 00:00:00
+                            .lt(ProductionConsume::getCreateTime, uploadDate.plusDays(1).atStartOfDay()) // createTime < uploadDate + 1 00:00:00
                             .orderByDesc(ProductionConsume::getCreateTime)
                             .last("limit 1");