wukai 1 рік тому
батько
коміт
bbfea019fc

+ 43 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/domain/TwinCalc2hr.java

@@ -21,6 +21,25 @@ import java.util.Date;
 public class TwinCalc2hr extends BaseEntity {
     private static final long serialVersionUID = 1L;
 
+    public TwinCalc2hr() {
+        this.alarm = 0L;
+        this.stop1A = 0L;
+        this.stop1B = 0L;
+        this.stop2A = 0L;
+        this.stop2B = 0L;
+        this.stop3A = 0L;
+        this.stop3B = 0L;
+
+        this.openTimeA = BigDecimal.ZERO;
+        this.openTimeB = BigDecimal.ZERO;
+        this.closeTimeA = BigDecimal.ZERO;
+        this.closeTimeB = BigDecimal.ZERO;
+        this.lengthA = BigDecimal.ZERO;
+        this.lengthB = BigDecimal.ZERO;
+        this.weightA = BigDecimal.ZERO;
+        this.weightB = BigDecimal.ZERO;
+    }
+
     /**
      * ID
      */
@@ -190,6 +209,30 @@ public class TwinCalc2hr extends BaseEntity {
     @ApiModelProperty("更新时间")
     private Date updatedTime;
 
+    /**
+     * 计算累加数据
+     *
+     * @param calc2hr obj
+     */
+    public void calc(TwinCalc2hr calc2hr) {
+        this.alarm += calc2hr.getAlarm();
+        this.stop1A += calc2hr.getStop1A();
+        this.stop1B += calc2hr.getStop1B();
+        this.stop2A += calc2hr.getStop2A();
+        this.stop2B += calc2hr.getStop2B();
+        this.stop3A += calc2hr.getStop3A();
+        this.stop3B += calc2hr.getStop3B();
+
+        this.openTimeA = this.openTimeA.add(calc2hr.getOpenTimeA());
+        this.openTimeB = this.openTimeB.add(calc2hr.getOpenTimeB());
+        this.closeTimeA = this.closeTimeA.add(calc2hr.getCloseTimeA());
+        this.closeTimeB = this.closeTimeB.add(calc2hr.getCloseTimeB());
+        this.lengthA = this.lengthA.add(calc2hr.getLengthA());
+        this.lengthB = this.lengthB.add(calc2hr.getLengthB());
+        this.weightA = this.weightA.add(calc2hr.getWeightA());
+        this.weightB = this.weightB.add(calc2hr.getWeightB());
+    }
+
     public void setId(Long id) {
         this.id = id;
     }

+ 13 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/mapper/TwinCalc2hrMapper.java

@@ -58,4 +58,17 @@ public interface TwinCalc2hrMapper {
      * @return 结果
      */
     public int deleteTwinCalc2hrByIds(String[] ids);
+    /**
+     * 获取上一个时段
+     *
+     * @return 时段 1-12
+     */
+    Long lastPeriod();
+
+    /**
+     * 计算当天的统计数据
+     *
+     * @return 统计数据
+     */
+    TwinCalc2hr calcToday();
 }

+ 8 - 3
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITaskService.java

@@ -1,7 +1,6 @@
 package com.ruoyi.biz.service;
 
 import java.time.LocalDate;
-import java.util.Map;
 
 /**
  * 计算任务服务
@@ -10,9 +9,10 @@ import java.util.Map;
  */
 public interface ITaskService {
     /**
-     * 统计上一个偶数时间至当前的数据
+     * 统计上一个时段数据
      */
-    void calcCurr();
+    void calcLastPeriod();
+
 
     /**
      * 统计昨日数据
@@ -20,6 +20,11 @@ public interface ITaskService {
     void calcYesterday();
 
     /**
+     * 统计当日数据,直到当前时间的上一个偶数时间
+     */
+    void calcToday();
+
+    /**
      * 统计指定日期数据
      *
      * @param date 指定日期

+ 14 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITwinCalc2hrService.java

@@ -2,9 +2,7 @@ package com.ruoyi.biz.service;
 
 import com.ruoyi.biz.domain.TwinCalc2hr;
 
-import java.time.LocalDate;
 import java.util.List;
-import java.util.Map;
 
 /**
  * 2小时统计数据Service接口
@@ -60,4 +58,18 @@ public interface ITwinCalc2hrService {
      * @return 结果
      */
     public int deleteTwinCalc2hrById(Long id);
+
+    /**
+     * 获取上一个时段
+     *
+     * @return 时段 1-12
+     */
+    Long lastPeriod();
+
+    /**
+     * 计算当天的统计数据
+     *
+     * @return 统计数据
+     */
+    TwinCalc2hr calcToday();
 }

+ 26 - 8
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/AsyncServiceImpl.java

@@ -8,7 +8,6 @@ import com.ruoyi.biz.domain.TwinRecordAlarms;
 import com.ruoyi.biz.service.IIotService;
 import javafx.util.Pair;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
@@ -30,16 +29,35 @@ import java.util.concurrent.Future;
 public class AsyncServiceImpl {
     @Resource
     private IIotService iotService;
+
+    @Async("threadPoolTaskExecutor")
+    public Future<Map<String, Object>> currData(TwinDevice twinDevice) {
+        String table = twinDevice.getDevicePath();
+        String sql = "select last * from " + table;
+        JSONObject jsonObject = iotService.query(sql);
+        JSONObject data = jsonObject.getJSONObject("data");
+        JSONArray values = data.getJSONArray("values");
+
+        Map<String, Object> dataMap = new HashMap<>(16);
+
+        for (int i = 0; i < values.size(); i++) {
+            JSONArray d = values.getJSONArray(i);
+            String key = d.getStr(0).replace(table + ".", "");
+            dataMap.put(key, d.getFloat(1));
+        }
+        return new AsyncResult<>(dataMap);
+    }
+
     @Async("threadPoolTaskExecutor")
-    public Future<Map<String, List<Object[]>>> process(TwinDevice twinDevice, Date date, long startTime, long endTime, String period) {
-        Map<String, List<Object[]>> result = new HashMap<>();
-        List<Object[]> calc2hrList = new ArrayList<>();
-        List<Object[]> recordAlarmsList = new ArrayList<>();
+    public Future<Map<String, List<?>>> process(TwinDevice twinDevice, Date date, long startTime, long endTime, int period) {
+        Map<String, List<?>> result = new HashMap<>(16);
+        List<TwinCalc2hr> calc2hrList = new ArrayList<>();
+        List<TwinRecordAlarms> recordAlarmsList = new ArrayList<>();
         String table = twinDevice.getDevicePath();
         TwinCalc2hr calc2hr = new TwinCalc2hr();
         calc2hr.setDeviceId(twinDevice.getId());
         calc2hr.setDataDate(date);
-        calc2hr.setTimePeriod(period);
+        calc2hr.setTimePeriod(period + "");
         Map<String, Object> map = calc(table, startTime, endTime);
         //0.已织造米数 1.A班组总开机时间 2.A班组总停机时间 3.A班总产量 4.B班组总开机时间 5.B班组总停机时间 6.B班总产量 7.A班总重量 8.B班总重量
         float[] total = (float[]) map.get("total");
@@ -68,7 +86,7 @@ public class AsyncServiceImpl {
         calc2hr.setAlarm((long) alarmsTimes);
         calc2hr.setRemark(Arrays.toString(alarms));
 
-        calc2hrList.add(calc2hr.toArray());
+        calc2hrList.add(calc2hr);
         List<Pair<String, Long>> alarmRecord = (List<Pair<String, Long>>) map.get("alarmRecord");
         String strStop = "stop";
         String strAlarm = "alarm";
@@ -84,7 +102,7 @@ public class AsyncServiceImpl {
                 recordAlarms.setAlarmCode(Long.parseLong(key.replace(strAlarm, "")));
             }
             recordAlarms.setDataTime(new Date(pair.getValue()));
-            recordAlarmsList.add(recordAlarms.toArray());
+            recordAlarmsList.add(recordAlarms);
         });
 
         result.put("calc", calc2hrList);

+ 53 - 61
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TaskServiceImpl.java

@@ -2,8 +2,9 @@ package com.ruoyi.biz.service.impl;
 
 import com.ruoyi.biz.domain.TwinCalc2hr;
 import com.ruoyi.biz.domain.TwinDevice;
-import com.ruoyi.biz.service.IIotService;
+import com.ruoyi.biz.domain.TwinRecordAlarms;
 import com.ruoyi.biz.service.ITaskService;
+import com.ruoyi.biz.service.ITwinCalc2hrService;
 import com.ruoyi.biz.service.ITwinDeviceService;
 import com.ruoyi.common.utils.Tools;
 import lombok.extern.slf4j.Slf4j;
@@ -11,11 +12,14 @@ import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.ZoneOffset;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
@@ -31,7 +35,7 @@ public class TaskServiceImpl implements ITaskService {
     @Resource
     private ITwinDeviceService deviceService;
     @Resource
-    private IIotService iotService;
+    private ITwinCalc2hrService calc2hrService;
     @Resource
     private IotTokenServiceImpl iotTokenService;
     @Resource
@@ -40,51 +44,18 @@ public class TaskServiceImpl implements ITaskService {
     private AsyncServiceImpl asyncService;
 
     /**
-     * 统计上一个偶数时间至当前的数据
+     * 统计上一个时段数据
      */
     @Override
-    public void calcCurr() {
-        LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
+    public void calcLastPeriod() {
+        LocalDateTime ldt = Tools.currWholeTime();
         //当前时间之前的偶数时间段
-        ldt = ldt.minusHours(ldt.getHour() % 2);
+        ldt = ldt.minusHours(ldt.getHour() % 2).minusHours(2);
         //开始时间需要向前取一秒
         LocalDateTime start = ldt.minusSeconds(1);
-        //获取时段范围 时间 1=0-2点 2=2-4
-        LocalDateTime end = LocalDateTime.now();
-        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
-        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
-        deviceService.selectTwinDeviceList(new TwinDevice()).forEach(twinDevice -> {
-            String table = twinDevice.getDevicePath();
-            TwinCalc2hr calc2hr = new TwinCalc2hr();
-            calc2hr.setDeviceId(twinDevice.getId());
-            Map<String, Object> map = asyncService.calc(table, startTime, endTime);
-            //0.已织造米数 1.A班组总开机时间 2.A班组总停机时间 3.A班总产量 4.B班组总开机时间 5.B班组总停机时间 6.B班总产量 7.A班总重量 8.B班总重量
-            float[] total = (float[]) map.get("total");
-            calc2hr.setLengthA(BigDecimal.valueOf(total[3]));
-            calc2hr.setLengthB(BigDecimal.valueOf(total[6]));
-            calc2hr.setWeightA(BigDecimal.valueOf(total[7]));
-            calc2hr.setWeightB(BigDecimal.valueOf(total[8]));
-            calc2hr.setOpenTimeA(BigDecimal.valueOf(total[1]));
-            calc2hr.setOpenTimeB(BigDecimal.valueOf(total[4]));
-            calc2hr.setCloseTimeA(BigDecimal.valueOf(total[2]));
-            calc2hr.setCloseTimeB(BigDecimal.valueOf(total[5]));
+        LocalDateTime end = ldt.plusHours(2);
+        calc4device(start, end);
 
-            Map<String, Long> stop = (Map<String, Long>) map.get("stop");
-            calc2hr.setStop1A(stop.get("stop1_A"));
-            calc2hr.setStop1B(stop.get("stop1_B"));
-            calc2hr.setStop2A(stop.get("stop2_A"));
-            calc2hr.setStop2B(stop.get("stop2_B"));
-            calc2hr.setStop3A(stop.get("stop3_A"));
-            calc2hr.setStop3B(stop.get("stop3_B"));
-
-            int[] alarms = (int[]) map.get("alarms");
-            int alarmsTimes = 0;
-            for (int i = 0; i < alarms.length; i++) {
-                alarmsTimes += alarms[i];
-            }
-            calc2hr.setAlarm((long) alarmsTimes);
-            calc2hr.setRemark(Arrays.toString(alarms));
-        });
     }
 
     /**
@@ -97,6 +68,18 @@ 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);
+        });
+    }
+
+    /**
      * 统计指定日期数据
      *
      * @param date 指定日期 yyyy-mm-dd
@@ -115,49 +98,58 @@ public class TaskServiceImpl implements ITaskService {
     @Override
     public void calc(LocalDate date) {
         Tools.timePeriod(date).forEach(map -> {
-            long start = (long) map.get("start");
-            long end = (long) map.get("end");
-            String period = (String) map.get("period");
-            Date d = Date.from(date.atStartOfDay(ZoneOffset.of("+8")).toInstant());
-            calc4device(d, start, end, period);
+            LocalDateTime start = (LocalDateTime) map.get("start");
+            LocalDateTime end = (LocalDateTime) map.get("end");
+            calc4device(start, end);
         });
     }
 
     /**
      * 按设备循环计算
      *
-     * @param date      日期
-     * @param startTime 开始时间戳
-     * @param endTime   结束时间戳
-     * @param period    时段
+     * @param start 开始时间戳
+     * @param end   结束时间戳
      */
-    private void calc4device(Date date, long startTime, long endTime, String period) {
+    private void calc4device(LocalDateTime start, LocalDateTime end) {
+        //开始时间都减了一秒的,这里要加回来,用于计算时段等数据
+        LocalDateTime ldt = start.plusSeconds(1);
+        Date date = Date.from(ldt.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
+        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        //获取时段范围 时间 1=0-2点 2=2-4
+
+        int period = ldt.getHour() / 2 + 1;
         List<Object[]> calc2hrList = new ArrayList<>();
         List<Object[]> recordAlarmsList = new ArrayList<>();
         Date s = new Date();
         //为了避免多线程同时获取token导致重复执行,先执行一次获取token
         iotTokenService.getToken();
         List<TwinDevice> list = deviceService.selectTwinDeviceList(new TwinDevice());
-        List<Future<Map<String, List<Object[]>>>> futureList = new ArrayList<>();
+        List<Future<Map<String, List<?>>>> futureList = new ArrayList<>();
         for (int i = 0; i < list.size(); i++) {
             TwinDevice twinDevice = list.get(i);
             futureList.add(asyncService.process(twinDevice, date, startTime, endTime, period));
         }
         try {
-            for (Future<Map<String, List<Object[]>>> future : futureList) {
-                Map<String, List<Object[]>> map = future.get();
-                calc2hrList.addAll(map.get("calc"));
-                recordAlarmsList.addAll(map.get("record"));
+            for (Future<Map<String, List<?>>> future : futureList) {
+                Map<String, List<?>> map = future.get();
+                List<TwinCalc2hr> calc2hrs = (List<TwinCalc2hr>) map.get("calc");
+                List<TwinRecordAlarms> recordAlarms = (List<TwinRecordAlarms>) map.get("record");
+                for (TwinCalc2hr calc2hr : calc2hrs) {
+                    calc2hrList.add(calc2hr.toArray());
+                }
+                for (TwinRecordAlarms alarms : recordAlarms) {
+                    recordAlarmsList.add(alarms.toArray());
+                }
             }
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
         } catch (ExecutionException e) {
             throw new RuntimeException(e);
         }
+
         Date d = new Date();
-        System.err.println("总共消耗:");
-        System.err.println(d.getTime() - s.getTime());
-        System.err.println("ms");
+        log.info("总共消耗:{}ms", d.getTime() - s.getTime());
         String sql = "INSERT INTO TWIN_CALC_2HR (DATA_DATE, TIME_PERIOD, DEVICE_ID, LENGTH_A, OPEN_TIME_A, CLOSE_TIME_A, LENGTH_B, CLOSE_TIME_B, OPEN_TIME_B, WEIGHT, WEIGHT_A, WEIGHT_B, ALARM, STOP1_A, STOP2_A, STOP3_A, STOP1_B, STOP2_B, STOP3_B,REMARK)" +
                 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
         jdbcTemplate.batchUpdate(sql, calc2hrList);

+ 76 - 11
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinCalc2hrServiceImpl.java

@@ -1,26 +1,24 @@
 package com.ruoyi.biz.service.impl;
 
-import cn.hutool.json.JSONArray;
-import cn.hutool.json.JSONObject;
 import com.ruoyi.biz.domain.TwinCalc2hr;
 import com.ruoyi.biz.domain.TwinDevice;
 import com.ruoyi.biz.mapper.TwinCalc2hrMapper;
-import com.ruoyi.biz.service.IIotService;
 import com.ruoyi.biz.service.ITwinCalc2hrService;
 import com.ruoyi.biz.service.ITwinDeviceService;
 import com.ruoyi.common.core.text.Convert;
-import com.ruoyi.common.utils.Tools;
-import javafx.util.Pair;
-import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.ZoneOffset;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 
 /**
  * 2小时统计数据Service业务层处理
@@ -35,9 +33,9 @@ public class TwinCalc2hrServiceImpl implements ITwinCalc2hrService {
     @Resource
     private ITwinDeviceService deviceService;
     @Resource
-    private IIotService iotService;
+    private IotTokenServiceImpl iotTokenService;
     @Resource
-    private JdbcTemplate jdbcTemplate;
+    private AsyncServiceImpl asyncService;
 
     /**
      * 查询2小时统计数据
@@ -104,4 +102,71 @@ public class TwinCalc2hrServiceImpl implements ITwinCalc2hrService {
     public int deleteTwinCalc2hrById(Long id) {
         return twinCalc2hrMapper.deleteTwinCalc2hrById(id);
     }
+
+    /**
+     * 获取上一个时段
+     *
+     * @return 时段 1-12
+     */
+    @Override
+    public Long lastPeriod() {
+        return twinCalc2hrMapper.lastPeriod();
+    }
+
+    /**
+     * 计算当天的统计数据
+     *
+     * @return 统计数据
+     */
+    @Override
+    public TwinCalc2hr calcToday() {
+        TwinCalc2hr calc2hr = twinCalc2hrMapper.calcToday();
+        TwinCalc2hr curr2hr = calcCurr();
+        calc2hr.calc(curr2hr);
+        return calc2hr;
+    }
+
+    /**
+     * 统计上一个偶数时间至当前的数据
+     */
+    private TwinCalc2hr calcCurr() {
+        TwinCalc2hr result = new TwinCalc2hr();
+        //获取上一个时段
+        Long lastPeriod = lastPeriod();
+        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
+        if (lastPeriod != 12L) {
+            //如果为12,则代表是前一天的,那就从今天0点开始
+            start = start.plusHours(2 * lastPeriod);
+
+        }
+        start = start.minusSeconds(1);
+        //获取时段范围 时间 1=0-2点 2=2-4
+        LocalDateTime end = LocalDateTime.now();
+
+        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+
+        //为了避免多线程同时获取token导致重复执行,先执行一次获取token
+        iotTokenService.getToken();
+        List<TwinDevice> list = deviceService.selectTwinDeviceList(new TwinDevice());
+        List<Future<Map<String, List<?>>>> futureList = new ArrayList<>();
+        for (int i = 0; i < list.size(); i++) {
+            TwinDevice twinDevice = list.get(i);
+            futureList.add(asyncService.process(twinDevice, new Date(), startTime, endTime, 0));
+        }
+        try {
+            for (Future<Map<String, List<?>>> future : futureList) {
+                Map<String, List<?>> map = future.get();
+                List<TwinCalc2hr> calc2hrs = (List<TwinCalc2hr>) map.get("calc");
+                for (TwinCalc2hr calc2hr : calc2hrs) {
+                    result.calc(calc2hr);
+                }
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
+        return result;
+    }
 }

+ 113 - 61
ruoyi-admin/src/main/resources/mapper/biz/TwinCalc2hrMapper.xml

@@ -1,76 +1,126 @@
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
-PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
-"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ruoyi.biz.mapper.TwinCalc2hrMapper">
-    
+
     <resultMap type="TwinCalc2hr" id="TwinCalc2hrResult">
-        <result property="id"    column="ID"    />
-        <result property="dataDate"    column="DATA_DATE"    />
-        <result property="timePeriod"    column="TIME_PERIOD"    />
-        <result property="deviceId"    column="DEVICE_ID"    />
-        <result property="lengthA"    column="LENGTH_A"    />
-        <result property="openTimeA"    column="OPEN_TIME_A"    />
-        <result property="closeTimeA"    column="CLOSE_TIME_A"    />
-        <result property="lengthB"    column="LENGTH_B"    />
-        <result property="closeTimeB"    column="CLOSE_TIME_B"    />
-        <result property="openTimeB"    column="OPEN_TIME_B"    />
-        <result property="weight"    column="WEIGHT"    />
-        <result property="weightA"    column="WEIGHT_A"    />
-        <result property="weightB"    column="WEIGHT_B"    />
-        <result property="alarm"    column="ALARM"    />
-        <result property="stop1A"    column="STOP1_A"    />
-        <result property="stop2A"    column="STOP2_A"    />
-        <result property="stop3A"    column="STOP3_A"    />
-        <result property="stop1B"    column="STOP1_B"    />
-        <result property="stop2B"    column="STOP2_B"    />
-        <result property="stop3B"    column="STOP3_B"    />
-        <result property="createdBy"    column="CREATED_BY"    />
-        <result property="createdTime"    column="CREATED_TIME"    />
-        <result property="updatedBy"    column="UPDATED_BY"    />
-        <result property="updatedTime"    column="UPDATED_TIME"    />
-        <result property="remark"    column="REMARK"    />
+        <result property="id" column="ID"/>
+        <result property="dataDate" column="DATA_DATE"/>
+        <result property="timePeriod" column="TIME_PERIOD"/>
+        <result property="deviceId" column="DEVICE_ID"/>
+        <result property="lengthA" column="LENGTH_A"/>
+        <result property="openTimeA" column="OPEN_TIME_A"/>
+        <result property="closeTimeA" column="CLOSE_TIME_A"/>
+        <result property="lengthB" column="LENGTH_B"/>
+        <result property="closeTimeB" column="CLOSE_TIME_B"/>
+        <result property="openTimeB" column="OPEN_TIME_B"/>
+        <result property="weight" column="WEIGHT"/>
+        <result property="weightA" column="WEIGHT_A"/>
+        <result property="weightB" column="WEIGHT_B"/>
+        <result property="alarm" column="ALARM"/>
+        <result property="stop1A" column="STOP1_A"/>
+        <result property="stop2A" column="STOP2_A"/>
+        <result property="stop3A" column="STOP3_A"/>
+        <result property="stop1B" column="STOP1_B"/>
+        <result property="stop2B" column="STOP2_B"/>
+        <result property="stop3B" column="STOP3_B"/>
+        <result property="createdBy" column="CREATED_BY"/>
+        <result property="createdTime" column="CREATED_TIME"/>
+        <result property="updatedBy" column="UPDATED_BY"/>
+        <result property="updatedTime" column="UPDATED_TIME"/>
+        <result property="remark" column="REMARK"/>
     </resultMap>
 
     <sql id="selectTwinCalc2hrVo">
-        select ID, DATA_DATE, TIME_PERIOD, DEVICE_ID, LENGTH_A, OPEN_TIME_A, CLOSE_TIME_A, LENGTH_B, CLOSE_TIME_B, OPEN_TIME_B, WEIGHT, WEIGHT_A, WEIGHT_B, ALARM, STOP1_A, STOP2_A, STOP3_A, STOP1_B, STOP2_B, STOP3_B, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME, REMARK from twin_calc_2hr
+        select ID,
+               DATA_DATE,
+               TIME_PERIOD,
+               DEVICE_ID,
+               LENGTH_A,
+               OPEN_TIME_A,
+               CLOSE_TIME_A,
+               LENGTH_B,
+               CLOSE_TIME_B,
+               OPEN_TIME_B,
+               WEIGHT,
+               WEIGHT_A,
+               WEIGHT_B,
+               ALARM,
+               STOP1_A,
+               STOP2_A,
+               STOP3_A,
+               STOP1_B,
+               STOP2_B,
+               STOP3_B,
+               CREATED_BY,
+               CREATED_TIME,
+               UPDATED_BY,
+               UPDATED_TIME,
+               REMARK
+        from twin_calc_2hr
     </sql>
 
     <select id="selectTwinCalc2hrList" parameterType="TwinCalc2hr" resultMap="TwinCalc2hrResult">
         <include refid="selectTwinCalc2hrVo"/>
-        <where>  
-            <if test="dataDate != null "> and DATA_DATE = #{dataDate}</if>
-            <if test="timePeriod != null  and timePeriod != ''"> and TIME_PERIOD = #{timePeriod}</if>
-            <if test="deviceId != null "> and DEVICE_ID = #{deviceId}</if>
-            <if test="lengthA != null "> and LENGTH_A = #{lengthA}</if>
-            <if test="openTimeA != null "> and OPEN_TIME_A = #{openTimeA}</if>
-            <if test="closeTimeA != null "> and CLOSE_TIME_A = #{closeTimeA}</if>
-            <if test="lengthB != null "> and LENGTH_B = #{lengthB}</if>
-            <if test="closeTimeB != null "> and CLOSE_TIME_B = #{closeTimeB}</if>
-            <if test="openTimeB != null "> and OPEN_TIME_B = #{openTimeB}</if>
-            <if test="weight != null "> and WEIGHT = #{weight}</if>
-            <if test="weightA != null "> and WEIGHT_A = #{weightA}</if>
-            <if test="weightB != null "> and WEIGHT_B = #{weightB}</if>
-            <if test="alarm != null "> and ALARM = #{alarm}</if>
-            <if test="stop1A != null "> and STOP1_A = #{stop1A}</if>
-            <if test="stop2A != null "> and STOP2_A = #{stop2A}</if>
-            <if test="stop3A != null "> and STOP3_A = #{stop3A}</if>
-            <if test="stop1B != null "> and STOP1_B = #{stop1B}</if>
-            <if test="stop2B != null "> and STOP2_B = #{stop2B}</if>
-            <if test="stop3B != null "> and STOP3_B = #{stop3B}</if>
-            <if test="createdBy != null  and createdBy != ''"> and CREATED_BY = #{createdBy}</if>
-            <if test="createdTime != null "> and CREATED_TIME = #{createdTime}</if>
-            <if test="updatedBy != null  and updatedBy != ''"> and UPDATED_BY = #{updatedBy}</if>
-            <if test="updatedTime != null "> and UPDATED_TIME = #{updatedTime}</if>
-            <if test="remark != null  and remark != ''"> and REMARK = #{remark}</if>
+        <where>
+            <if test="dataDate != null ">and DATA_DATE = #{dataDate}</if>
+            <if test="timePeriod != null  and timePeriod != ''">and TIME_PERIOD = #{timePeriod}</if>
+            <if test="deviceId != null ">and DEVICE_ID = #{deviceId}</if>
+            <if test="lengthA != null ">and LENGTH_A = #{lengthA}</if>
+            <if test="openTimeA != null ">and OPEN_TIME_A = #{openTimeA}</if>
+            <if test="closeTimeA != null ">and CLOSE_TIME_A = #{closeTimeA}</if>
+            <if test="lengthB != null ">and LENGTH_B = #{lengthB}</if>
+            <if test="closeTimeB != null ">and CLOSE_TIME_B = #{closeTimeB}</if>
+            <if test="openTimeB != null ">and OPEN_TIME_B = #{openTimeB}</if>
+            <if test="weight != null ">and WEIGHT = #{weight}</if>
+            <if test="weightA != null ">and WEIGHT_A = #{weightA}</if>
+            <if test="weightB != null ">and WEIGHT_B = #{weightB}</if>
+            <if test="alarm != null ">and ALARM = #{alarm}</if>
+            <if test="stop1A != null ">and STOP1_A = #{stop1A}</if>
+            <if test="stop2A != null ">and STOP2_A = #{stop2A}</if>
+            <if test="stop3A != null ">and STOP3_A = #{stop3A}</if>
+            <if test="stop1B != null ">and STOP1_B = #{stop1B}</if>
+            <if test="stop2B != null ">and STOP2_B = #{stop2B}</if>
+            <if test="stop3B != null ">and STOP3_B = #{stop3B}</if>
+            <if test="createdBy != null  and createdBy != ''">and CREATED_BY = #{createdBy}</if>
+            <if test="createdTime != null ">and CREATED_TIME = #{createdTime}</if>
+            <if test="updatedBy != null  and updatedBy != ''">and UPDATED_BY = #{updatedBy}</if>
+            <if test="updatedTime != null ">and UPDATED_TIME = #{updatedTime}</if>
+            <if test="remark != null  and remark != ''">and REMARK = #{remark}</if>
         </where>
     </select>
-    
+
     <select id="selectTwinCalc2hrById" parameterType="Long" resultMap="TwinCalc2hrResult">
         <include refid="selectTwinCalc2hrVo"/>
         where ID = #{id}
     </select>
-        
+    <select id="lastPeriod" resultType="java.lang.Long">
+        SELECT (TIME_PERIOD + 0)
+        FROM TWIN_CALC_2HR A
+        ORDER BY DATA_DATE DESC, (TIME_PERIOD + 0) DESC LIMIT 1
+    </select>
+    <select id="calcToday" resultMap="TwinCalc2hrResult">
+        SELECT DATA_DATE,
+               SUM(LENGTH_A)     LENGTH_A,
+               SUM(OPEN_TIME_A)  OPEN_TIME_A,
+               SUM(CLOSE_TIME_A) CLOSE_TIME_A,
+               SUM(LENGTH_B)     LENGTH_B,
+               SUM(CLOSE_TIME_B) CLOSE_TIME_B,
+               SUM(OPEN_TIME_B)  OPEN_TIME_B,
+               SUM(WEIGHT_A)     WEIGHT_A,
+               SUM(WEIGHT_B)     WEIGHT_B,
+               SUM(ALARM)        ALARM,
+               SUM(STOP1_A)      STOP1_A,
+               SUM(STOP2_A)      STOP2_A,
+               SUM(STOP3_A)      STOP3_A,
+               SUM(STOP1_B)      STOP1_B,
+               SUM(STOP2_B)      STOP2_B,
+               SUM(STOP3_B)      STOP3_B
+        FROM TWIN_CALC_2HR
+        WHERE DATA_DATE = curdate() GROUP BY DATA_DATE
+    </select>
+
     <insert id="insertTwinCalc2hr" parameterType="TwinCalc2hr" useGeneratedKeys="true" keyProperty="id">
         insert into twin_calc_2hr
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -98,7 +148,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="updatedBy != null">UPDATED_BY,</if>
             <if test="updatedTime != null">UPDATED_TIME,</if>
             <if test="remark != null">REMARK,</if>
-         </trim>
+        </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="dataDate != null">#{dataDate},</if>
             <if test="timePeriod != null">#{timePeriod},</if>
@@ -124,7 +174,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="updatedBy != null">#{updatedBy},</if>
             <if test="updatedTime != null">#{updatedTime},</if>
             <if test="remark != null">#{remark},</if>
-         </trim>
+        </trim>
     </insert>
 
     <update id="updateTwinCalc2hr" parameterType="TwinCalc2hr">
@@ -159,14 +209,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     </update>
 
     <delete id="deleteTwinCalc2hrById" parameterType="Long">
-        delete from twin_calc_2hr where ID = #{id}
+        delete
+        from twin_calc_2hr
+        where ID = #{id}
     </delete>
 
     <delete id="deleteTwinCalc2hrByIds" parameterType="String">
-        delete from twin_calc_2hr where ID in 
+        delete from twin_calc_2hr where ID in
         <foreach item="id" collection="array" open="(" separator="," close=")">
             #{id}
         </foreach>
     </delete>
 
-</mapper>
+</mapper>

+ 24 - 23
ruoyi-admin/src/test/java/com/jjt/DataPF.java

@@ -3,23 +3,19 @@ package com.jjt;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import com.ruoyi.RuoYiApplication;
+import com.ruoyi.biz.domain.TwinCalc2hr;
 import com.ruoyi.biz.domain.TwinDevice;
-import com.ruoyi.biz.domain.TwinRecordAlarms;
 import com.ruoyi.biz.service.IIotService;
 import com.ruoyi.biz.service.ITaskService;
+import com.ruoyi.biz.service.ITwinCalc2hrService;
 import com.ruoyi.biz.service.ITwinDeviceService;
 import com.ruoyi.biz.service.impl.AsyncServiceImpl;
-import javafx.util.Pair;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.scheduling.annotation.EnableAsync;
 
 import javax.annotation.Resource;
-import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.List;
-import java.util.Map;
 
 /**
  * DataProcess$
@@ -38,22 +34,26 @@ public class DataPF {
     @Resource
     private ITwinDeviceService deviceService;
     @Resource
-    private ITaskService calc2hrService;
+    private ITaskService taskService;
     @Resource
     private AsyncServiceImpl asyncService;
+    @Resource
+    private ITwinCalc2hrService calc2hrService;
 
     public static void main(String[] args) {
-        LocalDateTime ldt = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
-        //当前时间之前的偶数时间段
-        ldt = ldt.minusHours(ldt.getHour() % 2);
-        //开始时间需要向前取一秒
-        LocalDateTime start = ldt.minusSeconds(1);
-        //获取时段范围 时间 1=0-2点 2=2-4
-        int period = start.getHour() / 2 + 1;
-        LocalDateTime end = ldt.plusHours(2);
-        System.err.println(start + "" + end);
-        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
-        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        TwinCalc2hr calc = new TwinCalc2hr();
+        System.err.println(calc);
+    }
+
+    @Test
+    void today() {
+        TwinCalc2hr calc = calc2hrService.calcToday();
+        System.err.println(calc);
+    }
+
+    @Test
+    void curr() {
+        taskService.calcLastPeriod();
     }
 
 
@@ -64,11 +64,12 @@ public class DataPF {
 //        calc2hrService.calc("2024-05-13");
 //        calc2hrService.calc("2024-05-14");
 //        calc2hrService.calc("2024-05-15");
-        calc2hrService.calc("2024-05-16");
-        calc2hrService.calc("2024-05-17");
-        calc2hrService.calc("2024-05-18");
-        calc2hrService.calc("2024-05-19");
-        calc2hrService.calc("2024-05-20");
+//        calc2hrService.calc("2024-05-16");
+//        calc2hrService.calc("2024-05-17");
+//        calc2hrService.calc("2024-05-18");
+//        calc2hrService.calc("2024-05-19");
+//        calc2hrService.calc("2024-05-20");
+        taskService.calcLastPeriod();
 //        String table = "root.tl.suxi.knittings97_plc1";
 //        long startTime = 1715709599000l;
 //        long endTime = 1715716800000l;

+ 45 - 23
ruoyi-common/src/main/java/com/ruoyi/common/utils/Tools.java

@@ -1,13 +1,13 @@
 package com.ruoyi.common.utils;
 
-import com.ruoyi.common.json.JSONObject;
-import com.sun.deploy.net.HttpResponse;
-
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
 import java.time.ZoneOffset;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Tool$
@@ -17,15 +17,19 @@ import java.util.*;
  */
 public class Tools {
     public static void main(String[] args) {
-        LocalDate localDate = LocalDate.now().minusDays(1);
-        System.err.println(localDate);
-        Tools.timePeriod(localDate).forEach(map -> {
-            long start = (long) map.get("start");
-            Date d = new Date(start);
-            System.err.println(map + "" + d);
-        });
+        int p = 12;
+        LocalDateTime ldt = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
+        System.err.println(ldt.plusHours(2 * p));
     }
 
+    /**
+     * 当前的整点时间,0分0秒0毫秒
+     *
+     * @return LocalDateTime
+     */
+    public static LocalDateTime currWholeTime() {
+        return LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
+    }
 
     /**
      * 按照给定日期,按2小时分段,组成where条件
@@ -71,12 +75,9 @@ public class Tools {
 
     /**
      * 获取指定日期的时间分段
-     * map.put("period", period + "");
-     * map.put("start", startTime);
-     * map.put("end", endTime);
-     * * @param date LocalDate yyyy-mm-dd
      *
-     * @return list map
+     * @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<>();
@@ -85,17 +86,12 @@ public class Tools {
         LocalDateTime start = ldt;
         LocalDateTime end = start.plusHours(2);
         do {
-            //获取时段范围 时间 1=0-2点 2=2-4
-            int period = start.getHour() / 2 + 1;
             //-1秒 把上一轮最后一条数据取出来
             start = start.minusSeconds(1);
-            Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
-            Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
 
             Map<String, Object> map = new HashMap<>(16);
-            map.put("period", period + "");
-            map.put("start", startTime);
-            map.put("end", endTime);
+            map.put("start", start);
+            map.put("end", end);
             result.add(map);
             start = end;
             end = start.plusHours(2);
@@ -103,4 +99,30 @@ public class Tools {
         return result;
     }
 
+    /**
+     * 获取当日 直到当前时间的上一个偶数时间
+     *
+     * @return LocalDateTime map.get("start") map.get("end")
+     */
+    public static List<Map<String, Object>> timePeriod() {
+        List<Map<String, Object>> 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);
+        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(2);
+        } while (!end.isAfter(stop));
+        return result;
+    }
+
 }