Prechádzať zdrojové kódy

搞定日统计,界面数据统计

wukai 11 mesiacov pred
rodič
commit
7521eded41

+ 12 - 23
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TaskServiceImpl.java

@@ -47,38 +47,31 @@ public class TaskServiceImpl implements ITaskService {
     private ITwinCalcStopService stopService;
     @Resource
     private ITwinCalcAlarmsService alarmsService;
+    @Resource
+    private ITwinCalcHourService hourService;
 
     /**
      * 从数据库最后一个时间段统计至当前的上一个偶数时间点
      */
     @Override
     public void calc2Curr() {
-        TwinCalc2hr calc2hr = calc2hrService.lastPeriod();
-        LocalDate localDate = DateUtils.toLocalDate(calc2hr.getDataDate());
-        Long lastPeriod = calc2hr.getTimePeriod();
-        if (lastPeriod == 12L) {
-            localDate = localDate.plusDays(1);
-            lastPeriod = 1L;
-        }
-
-        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
-        start = start.plusHours(lastPeriod * 2);
-        LocalDateTime end = start.plusHours(2);
+        TwinCalcHour lastHour = hourService.lastHour();
+        LocalDate localDate = DateUtils.toLocalDate(lastHour.getDataDate());
+        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN).plusHours(lastHour.getHour() + 1);
+        LocalDateTime end = start.plusHours(1);
 
         LocalDateTime stop = Tools.currWholeTime();
-        //当前时间之前的偶数时间段
-        stop = stop.minusHours(stop.getHour() % 2);
         while (!end.isAfter(stop)) {
             start = start.minusSeconds(1);
             calc4device(start, end);
             log.info("补录数据===========start:{},end:{},stop:{}", start, end, stop);
-            if (end.getHour() == 0) {
+            if (end.getHour() == 7) {
                 //跨天,统计前一天的总数据
                 log.info("----------------{},{}", start, end);
                 dayService.calc4date(start.toLocalDate());
             }
             start = end;
-            end = end.plusHours(2);
+            end = end.plusHours(1);
         }
     }
 
@@ -111,10 +104,8 @@ public class TaskServiceImpl implements ITaskService {
      */
     @Override
     public void calcToday() {
-        Tools.timePeriod().forEach(map -> {
-            LocalDateTime start = (LocalDateTime) map.get("start");
-            LocalDateTime end = (LocalDateTime) map.get("end");
-            calc4device(start, end);
+        Tools.timePeriod().forEach(pair -> {
+            calc4device(pair.getKey(), pair.getValue());
         });
     }
 
@@ -152,10 +143,8 @@ public class TaskServiceImpl implements ITaskService {
      */
     @Override
     public void calc(LocalDate date) {
-        Tools.timePeriod(date).forEach(map -> {
-            LocalDateTime start = (LocalDateTime) map.get("start");
-            LocalDateTime end = (LocalDateTime) map.get("end");
-            calc4device(start, end);
+        Tools.timePeriod(date).forEach(pair -> {
+            calc4device(pair.getKey(), pair.getValue());
         });
     }
 

+ 38 - 29
ruoyi-admin/src/main/java/com/ruoyi/biz/tools/Tools.java

@@ -116,36 +116,29 @@ public class Tools {
      * @param date 字符串格式的 yyyy-mm-dd
      * @return list map
      */
-    public static List<Map<String, Object>> timePeriod(String date) {
+    public static List<Pair<LocalDateTime, LocalDateTime>> timePeriod(String date) {
         LocalDate localDate = LocalDate.parse(date);
         return timePeriod(localDate);
     }
 
 
     /**
-     * 获取指定日期的时间分段
+     * 获取指定日期的时间分段 早上7点至第二天7点(不含)
      * 每小时一条
      *
      * @param date LocalDate yyyy-mm-dd
      * @return LocalDateTime map.get("start") map.get("end")
      */
-    public static List<Map<String, Object>> timePeriod(LocalDate date) {
-        List<Map<String, Object>> result = new ArrayList<>();
-        //获取当天0点
+    public static List<Pair<LocalDateTime, LocalDateTime>> timePeriod(LocalDate date) {
+        List<Pair<LocalDateTime, LocalDateTime>> result = new ArrayList<>();
         LocalDateTime ldt = LocalDateTime.of(date, LocalTime.MIN);
-        LocalDateTime start = ldt;
-        LocalDateTime end = start.plusHours(1);
-        do {
-            //-1秒 把上一轮最后一条数据取出来
-            start = start.minusSeconds(1);
-
-            Map<String, Object> map = new HashMap<>(16);
-            map.put("start", start);
-            map.put("end", end);
-            result.add(map);
-            start = end;
-            end = start.plusHours(1);
-        } while (!ldt.plusDays(1).isBefore(end));
+        LocalDateTime start = ldt.plusHours(7);
+        for (int i = 0; i < 24; i++) {
+            //需要减少1秒,取出上一轮最后一条数据
+            Pair<LocalDateTime, LocalDateTime> pair = new Pair<>(start.minusSeconds(1), start.plusHours(1));
+            result.add(pair);
+            start = start.plusHours(1);
+        }
         return result;
     }
 
@@ -154,23 +147,20 @@ public class Tools {
      *
      * @return LocalDateTime map.get("start") map.get("end")
      */
-    public static List<Map<String, Object>> timePeriod() {
-        List<Map<String, Object>> result = new ArrayList<>();
+    public static List<Pair<LocalDateTime, LocalDateTime>> timePeriod() {
+        List<Pair<LocalDateTime, LocalDateTime>> result = new ArrayList<>();
         //获取当天0点
         LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
         LocalDateTime start = ldt.withHour(0);
-        LocalDateTime end = start.plusHours(2);
-        LocalDateTime stop = ldt.minusHours(ldt.getHour() % 2);
+        LocalDateTime end = start.plusHours(1);
+        LocalDateTime stop = ldt.minusHours(1);
         do {
             //-1秒 把上一轮最后一条数据取出来
             start = start.minusSeconds(1);
-
-            Map<String, Object> map = new HashMap<>(16);
-            map.put("start", start);
-            map.put("end", end);
-            result.add(map);
+            Pair<LocalDateTime, LocalDateTime> pair = new Pair<>(start, end);
+            result.add(pair);
             start = end;
-            end = start.plusHours(2);
+            end = start.plusHours(1);
         } while (!end.isAfter(stop));
         return result;
     }
@@ -195,7 +185,26 @@ public class Tools {
     public static Pair<Date, Date> calcDay(LocalDate localDate) {
         LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
         LocalDateTime start = ldt.plusHours(7);
-        LocalDateTime end = start.plusHours(23);
+        LocalDateTime end = start.plusHours(23).plusMinutes(59).plusSeconds(59);
+        Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
+        Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
+        return new Pair<>(sTime, eTime);
+    }
+
+    /**
+     * 获取统计时间 当天7天至结束时间(不含)
+     *
+     * @return 开始结束时间对
+     */
+    public static Pair<Date, Date> calcToday() {
+        LocalDateTime end = LocalDateTime.now();
+        LocalDate localDate = end.toLocalDate();
+        if (end.getHour() < 7) {
+            //如果小于7点,则表示生产时间是前一天
+            localDate = localDate.minusDays(1);
+        }
+        //生产时间从7点开始
+        LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN).plusHours(7);
         Date sTime = Date.from(start.atZone(ZoneId.systemDefault()).toInstant());
         Date eTime = Date.from(end.atZone(ZoneId.systemDefault()).toInstant());
         return new Pair<>(sTime, eTime);

+ 6 - 10
ruoyi-admin/src/test/java/com/jjt/DataHour.java

@@ -9,6 +9,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
 
 import javax.annotation.Resource;
 import java.time.Instant;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 
@@ -30,17 +31,12 @@ public class DataHour {
 
     @Test
     public void test() {
-//        for (int i = 7; i < 24; i++) {
-//            taskService.calc("2024-07-01", i);
+//        LocalDate localDate = LocalDate.parse("2024-06-27");
+//        for (int i = 0; i < 6; i++) {
+//            taskService.calc(localDate.plusDays(i));
 //        }
-//        for (int i = 0; i < 24; i++) {
-//            taskService.calc("2024-07-02", i);
-//        }
-//        for (int i = 0; i < 7; i++) {
-//            taskService.calc("2024-07-03", i);
-//        }
-
-        taskService.calc("2024-07-01", 1);
+        LocalDate localDate = LocalDate.parse("2024-07-03");
+        taskService.calc(localDate);
     }
 
     @Test