Jelajahi Sumber

数据裁剪时间修改;

Gaokun Wang 9 jam lalu
induk
melakukan
f26cbe021a

+ 29 - 7
als-modules/agile-assurance/src/main/java/org/eco/als/service/impl/FormulaService.java

@@ -25,6 +25,8 @@ import org.springframework.stereotype.Service;
 
 import java.time.Duration;
 import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -36,6 +38,8 @@ import java.util.Map;
 @Service
 @Slf4j
 public class FormulaService implements IFormulaService {
+
+    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
     @Resource
     private ISysOssService ossService;
     private ExpressRunner runner;
@@ -45,6 +49,7 @@ public class FormulaService implements IFormulaService {
         // 虚警
         int result = 1;
         String timeFlag = null;
+        String errorTimeFlag = null;
         JSONObject resultJson = new JSONObject();
         try {
 //            CsvUtils.getCsvHeaders("");
@@ -69,9 +74,11 @@ public class FormulaService implements IFormulaService {
             }
 
             JSONArray arr = CsvUtils.fileCsvToJsonRegex(path);
+            JSONObject obj = (JSONObject) arr.getFirst();
+            String startTime = obj.getStr("时间");
             for (Object object : arr) {
                 timeFlag = ((JSONObject) object).getStr("时间");
-                if (durationTime(expressBo.getCutTime(),timeFlag)< 1) {
+                if (durationTime(expressBo.getCutTime(),timeFlag, startTime)) {
                     continue;
                 }
                 DefaultContext<String, Object> context = new DefaultContext<>();
@@ -80,6 +87,7 @@ public class FormulaService implements IFormulaService {
                 Boolean isMatch = (Boolean) runner.execute(expressBo.getExpression(), context, null, true, false);
                 log.info("isMatch==========================:{}", isMatch);
                 if (isMatch) {
+                    errorTimeFlag = ((JSONObject) object).getStr("时间");
                     // 实警
                     result = 0;
                     break;
@@ -88,7 +96,7 @@ public class FormulaService implements IFormulaService {
         } catch (Exception e) {
             throw new BusinessException(e.getMessage());
         }
-        resultJson.set("timeFlag", timeFlag);
+        resultJson.set("timeFlag", errorTimeFlag);
         resultJson.set("result", result);
         return resultJson;
     }
@@ -107,11 +115,25 @@ public class FormulaService implements IFormulaService {
         return variables;
     }
 
-    private int durationTime(int cutTime, String timeStr) {
-        LocalTime time = LocalTime.parse(timeStr);
-        Duration duration = Duration.between(LocalTime.MIN, time);
-        Duration minutesDuration = Duration.ofMinutes(cutTime);
-        return duration.compareTo(minutesDuration);
+    private static boolean durationTime(int thresholdMinutes, String time1, String time2) {
+        LocalTime t1 = LocalTime.parse(time1, TIME_FORMATTER);
+        LocalTime t2 = LocalTime.parse(time2, TIME_FORMATTER);
+        Duration duration = Duration.between(t1, t2).abs();
+        long diffMinutes = duration.toMinutes();
+        return diffMinutes <= thresholdMinutes;
+    }
+
+    public static void main(String[] args) {
+        List<String> times = new ArrayList<>();
+        times.add("14:01:00.020");
+        times.add("14:05:00.020");
+        times.add("14:06:30.100");
+        times.add("14:11:19.300");
+        times.add("14:31:19.300");
+        times.forEach(time -> {
+            System.out.println(durationTime(10, time, "14:05:00.020"));
+        });
+
     }
 
     /**