Ver código fonte

fix: 时间计算问题引发的比较问题

jackzhou 5 meses atrás
pai
commit
7aa669d2fd

+ 11 - 9
snowy-plugin/snowy-plugin-coldchain/src/main/java/vip/xiaonuo/coldchain/modular/app/param/AggregationWindow.java

@@ -1,5 +1,8 @@
 package vip.xiaonuo.coldchain.modular.app.param;
 
+import cn.hutool.core.util.StrUtil;
+import vip.xiaonuo.coldchain.core.util.DateFormatter;
+
 import java.time.Duration;
 import java.time.Instant;
 import java.time.LocalDate;
@@ -13,13 +16,7 @@ import java.time.ZoneId;
  * @date 2024/12/10 15:31:56
  */
 public enum AggregationWindow {
-    MINUTE("1m", "分钟"),
-    HOUR("1h", "小时"),
-    HALF_DAY("12h", "半天"),
-    DAY("1d", "天"),
-    WEEK("1w", "自然周"),
-    MONTH("1mo", "月度"),
-    YEAR("1y", "年度");
+    MINUTE("1m", "分钟"), HOUR("1h", "小时"), HALF_DAY("12h", "半天"), DAY("1d", "天"), WEEK("1w", "自然周"), MONTH("1mo", "月度"), YEAR("1y", "年度"),DEFAULT("1d", "天");
 
     private final String code;
     private final String description;
@@ -47,14 +44,19 @@ public enum AggregationWindow {
                 return aggregationWindow;
             }
         }
-        throw new IllegalArgumentException("Unknown aggregation window: " + window);
+        return AggregationWindow.DEFAULT;
     }
 
     // 根据时间范围计算推荐的聚合窗口
     public static AggregationWindow determineAggregationWindow(String startTime, String stopTime) {
+        if (StrUtil.isBlank(startTime) || StrUtil.isBlank(stopTime)) {
+            return AggregationWindow.DEFAULT;
+        }
+        // 替换日期格式,确保格式一致
+        startTime = DateFormatter.replaceDateFormat(startTime);
+        stopTime = DateFormatter.replaceDateFormat(stopTime);
         LocalDate localDate = LocalDate.parse(startTime);  // Parse as LocalDate
         Instant start = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();  // Convert to Instant
-
         LocalDate localDate2 = LocalDate.parse(stopTime);  // Parse as LocalDate
         Instant stop = localDate2.atStartOfDay(ZoneId.systemDefault()).toInstant();  // Convert to Instant
         Duration duration = Duration.between(start, stop);

+ 8 - 7
snowy-plugin/snowy-plugin-coldchain/src/main/java/vip/xiaonuo/coldchain/modular/app/service/AppDeviceService.java

@@ -150,15 +150,16 @@ public class AppDeviceService {
     public SensorEchartDataResult queryDataByDeviceIdAndRoads(AppDeviceQueryParams appDeviceQueryParams) {
         Assert.notNull(appDeviceQueryParams, "appDeviceQueryParams cannot be null");
         // 获取用户选择的聚合维度,优先使用用户选择的聚合维度,如果没有选择则根据时间范围计算
+        AggregationWindow window = null;
         String aggregationWindow = appDeviceQueryParams.getAggregationWindow();
         if (StrUtil.isBlank(aggregationWindow)) {
-            aggregationWindow = AggregationWindow.determineAggregationWindow(
+            window = AggregationWindow.determineAggregationWindow(
                     appDeviceQueryParams.getStartTime(),
-                    appDeviceQueryParams.getEndTime()).getCode();
+                    appDeviceQueryParams.getEndTime());
+        } else {
+            // 如果用户选择了聚合维度,但不在枚举中,使用默认的聚合窗口
+            window = AggregationWindow.fromString(aggregationWindow);
         }
-        // 根据聚合维度获取相应的聚合窗口
-        AggregationWindow window = AggregationWindow.fromString(aggregationWindow);
-
         // 查找监控目标区域
         MonitorTargetRegion monitorTarget = monitorTargetRegionService.findOneByDeviceCodeAndSensorNo(
                 appDeviceQueryParams.getSensorCode(),
@@ -185,6 +186,7 @@ public class AppDeviceService {
         SensorEchartDataResult result = new SensorEchartDataResult();
 
         // 遍历传感器类型映射
+        AggregationWindow finalWindow = window;
         sensorMapping.forEach((key, dataType) -> {
             // 如果传感器类型包含对应的字符
             if (sensorType.toLowerCase().contains(key)) {
@@ -194,7 +196,7 @@ public class AppDeviceService {
                         appDeviceQueryParams.getSensorRoute(),
                         appDeviceQueryParams.getStartTime(),
                         appDeviceQueryParams.getEndTime(),
-                        dataType,window
+                        dataType, finalWindow
                 );
 
                 // 处理数据并设置到对应的字段
@@ -217,7 +219,6 @@ public class AppDeviceService {
     }
 
 
-
     public Boolean updateDeviceAlarm(AppDeviceAlarmParam appDeviceAlarmParam) {
         Assert.notNull(appDeviceAlarmParam, "appDeviceAlarmParam cannot be null");
         MonitorTargetRegion monitorTargetRegion = monitorTargetRegionService.getById(appDeviceAlarmParam.getMonitorTargetRegionId());