Browse Source

处理停机时间和解决告警的小BUG

wukai 5 months ago
parent
commit
bee7b02f77

+ 16 - 4
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/DataController.java

@@ -55,8 +55,14 @@ public class DataController extends BaseController {
             jdbcTemplate.update(sql, date, sp);
             sql = "DELETE FROM TWIN_CALC_HOUR_SPEC WHERE DATA_DATE=? AND HOUR=?";
             jdbcTemplate.update(sql, date, sp);
-            sql = "DELETE FROM TWIN_RECORD_ALARMS WHERE START_TIME<=? AND END_TIME>=?";
-            jdbcTemplate.update(sql, new Date(startTime), new Date(endTime));
+            sql = "DELETE FROM TWIN_RECORD_ALARMS WHERE DATA_DATE=? AND HOUR=?";
+            jdbcTemplate.update(sql, date, sp);
+            sql = "DELETE FROM TWIN_CALC_ALARMS WHERE DATA_DATE=? AND HOUR=?";
+            jdbcTemplate.update(sql, date, sp);
+            sql = "DELETE FROM TWIN_RECORD_STOP WHERE DATA_DATE=? AND HOUR=?";
+            jdbcTemplate.update(sql, date, sp);
+            sql = "DELETE FROM TWIN_CALC_STOP WHERE DATA_DATE=? AND HOUR=?";
+            jdbcTemplate.update(sql, date, sp);
             sql = "DELETE FROM TWIN_PAN_HEAD_INFO WHERE RECORD_TIME>=? AND RECORD_TIME<=?";
             jdbcTemplate.update(sql, new Date(startTime), new Date(endTime));
             taskService.calc(date, sp);
@@ -79,8 +85,14 @@ public class DataController extends BaseController {
                 Long endTime = edt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
                 String sql = "DELETE FROM TWIN_CALC_HOUR WHERE DATA_DATE=? AND HOUR=?";
                 jdbcTemplate.update(sql, date, i);
-                sql = "DELETE FROM TWIN_RECORD_ALARMS WHERE START_TIME<=? AND END_TIME>=?";
-                jdbcTemplate.update(sql, new Date(startTime), new Date(endTime));
+                sql = "DELETE FROM TWIN_RECORD_ALARMS WHERE DATA_DATE=? AND HOUR=?";
+                jdbcTemplate.update(sql, date, i);
+                sql = "DELETE FROM TWIN_CALC_ALARMS WHERE DATA_DATE=? AND HOUR=?";
+                jdbcTemplate.update(sql, date, i);
+                sql = "DELETE FROM TWIN_RECORD_STOP WHERE DATA_DATE=? AND HOUR=?";
+                jdbcTemplate.update(sql, date, i);
+                sql = "DELETE FROM TWIN_CALC_STOP WHERE DATA_DATE=? AND HOUR=?";
+                jdbcTemplate.update(sql, date, i);
                 sql = "DELETE FROM TWIN_PAN_HEAD_INFO WHERE RECORD_TIME>=? AND RECORD_TIME<=?";
                 jdbcTemplate.update(sql, new Date(startTime), new Date(endTime));
 

+ 2 - 1
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/DemoController.java

@@ -50,7 +50,8 @@ public class DemoController extends BaseController {
 
     @GetMapping("/calc")
     public AjaxResult calc(String date) {
-        return toAjax(calcService.calc(DateUtils.parseDate(date)));
+//        return toAjax(calcService.calc(DateUtils.parseDate(date)));
+        return toAjax(twinEmpCalcService.calc(DateUtils.parseDate(date)));
     }
 
     @ApiOperation("在线设备")

+ 1 - 1
ruoyi-admin/src/main/java/com/ruoyi/biz/domain/TwinCalcAlarms.java

@@ -46,7 +46,7 @@ public class TwinCalcAlarms extends BaseEntity {
      */
     @Excel(name = "小时;0-23")
     @ApiModelProperty("小时;0-23")
-    private String hour;
+    private Integer hour;
 
     /**
      * 告警开始时间

+ 38 - 43
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinCalcAlarmsServiceImpl.java

@@ -116,31 +116,38 @@ public class TwinCalcAlarmsServiceImpl implements ITwinCalcAlarmsService {
 
         Date date = Date.from(startTime.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
         String oldStr = "old";
+
+        //先处理上次未统计完整的数据
+        TwinCalcAlarms calcSearch = new TwinCalcAlarms();
+        calcSearch.setCalcStatus("1");
+        List<TwinRecordAlarms> list = new ArrayList<>();
+        List<TwinCalcAlarms> oldList = selectTwinCalcAlarmsList(calcSearch);
+        Map<Long, List<TwinCalcAlarms>> oldMap = oldList.stream().collect(Collectors.groupingBy(TwinCalcAlarms::getDeviceId, LinkedHashMap::new, Collectors.toList()));
+        //再处理本次数据,2次结合
+        TwinRecordAlarms recordSearch = new TwinRecordAlarms();
+        recordSearch.setDataDate(date);
+        recordSearch.setHour(startTime.getHour());
+        List<TwinRecordAlarms> newList = alarmsService.selectTwinRecordAlarmsList(recordSearch);
+        Map<Long, List<TwinRecordAlarms>> newMap = newList.stream().collect(Collectors.groupingBy(TwinRecordAlarms::getDeviceId, LinkedHashMap::new, Collectors.toList()));
+
         List<TwinCalcAlarms> insertList = new ArrayList<>();
         List<TwinCalcAlarms> updateList = new ArrayList<>();
+        List<TwinRecordAlarms> recordList = new ArrayList<>();
         deviceList.forEach(device -> {
-            //先处理上次未统计完整的数据
-            TwinCalcAlarms calcSearch = new TwinCalcAlarms();
-            calcSearch.setDeviceId(device.getDeviceId());
-            calcSearch.setCalcStatus("1");
-            List<TwinRecordAlarms> list = new ArrayList<>();
-            List<TwinCalcAlarms> oldList = selectTwinCalcAlarmsList(calcSearch);
-            oldList.forEach(old -> {
-                TwinRecordAlarms alarms = new TwinRecordAlarms();
-                BeanUtils.copyProperties(old, alarms);
-                alarms.setRemark(oldStr);
-                list.add(alarms);
-            });
+            if (oldMap.get(device.getDeviceId()) != null) {
+                oldMap.get(device.getDeviceId()).forEach(old -> {
+                    TwinRecordAlarms alarms = new TwinRecordAlarms();
+                    BeanUtils.copyProperties(old, alarms);
+                    alarms.setRemark(oldStr);
+                    list.add(alarms);
+                });
+            }
             //再处理本次数据,2次结合
-            TwinRecordAlarms recordSearch = new TwinRecordAlarms();
-            recordSearch.setDeviceId(device.getDeviceId());
-            recordSearch.setDataDate(date);
-            recordSearch.setHour(startTime.getHour());
-            List<TwinRecordAlarms> newList = alarmsService.selectTwinRecordAlarmsList(recordSearch);
-            list.addAll(newList);
+            if (newMap.get(device.getDeviceId()) != null) {
+                list.addAll(newMap.get(device.getDeviceId()));
+            }
 
             Map<Integer, List<TwinRecordAlarms>> groupList = list.stream().collect(Collectors.groupingBy(TwinRecordAlarms::getAlarmType));
-            List<TwinRecordAlarms> recordList = new ArrayList<>();
             for (Map.Entry<Integer, List<TwinRecordAlarms>> entry : groupList.entrySet()) {
                 List<TwinRecordAlarms> temp = entry.getValue();
                 if (temp.size() == 1) {
@@ -174,32 +181,20 @@ public class TwinCalcAlarmsServiceImpl implements ITwinCalcAlarmsService {
                 }
             }
 
-//            recordList.forEach(stop -> {
-//                TwinCalcAlarms v = new TwinCalcAlarms();
-//                BeanUtils.copyProperties(stop, v);
-//                if (oldStr.equals(stop.getRemark())) {
-//                    updateTwinCalcAlarms(v);
-//                } else {
-//                    v.setId(null);
-//                    insertTwinCalcAlarms(v);
-//                }
-//            });
-
-
-            for (TwinRecordAlarms stop : recordList) {
-                TwinCalcAlarms v = new TwinCalcAlarms();
-                BeanUtils.copyProperties(stop, v);
-                if (oldStr.equals(stop.getRemark())) {
-                    updateList.add(v);
-//                    updateTwinCalcStop(v);
-                } else {
-                    v.setId(null);
-                    insertList.add(v);
-//                    insertTwinCalcStop(v);
-                }
-            }
 
         });
+        for (TwinRecordAlarms stop : recordList) {
+            TwinCalcAlarms v = new TwinCalcAlarms();
+            BeanUtils.copyProperties(stop, v);
+            if (oldStr.equals(stop.getRemark())) {
+                v.setCalcStatus("0");
+                v.setRemark("");
+                updateList.add(v);
+            } else {
+                v.setId(null);
+                insertList.add(v);
+            }
+        }
         try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
             TwinCalcAlarmsMapper mapper = sqlSession.getMapper(TwinCalcAlarmsMapper.class);
             insertList.forEach(mapper::insertTwinCalcAlarms);

+ 84 - 31
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TwinCalcStopServiceImpl.java

@@ -1,9 +1,12 @@
 package com.ruoyi.biz.service.impl;
 
+import com.ruoyi.biz.domain.TwinCalcHour;
 import com.ruoyi.biz.domain.TwinCalcStop;
 import com.ruoyi.biz.domain.TwinDevice;
 import com.ruoyi.biz.domain.TwinRecordStop;
+import com.ruoyi.biz.mapper.TwinCalcHourMapper;
 import com.ruoyi.biz.mapper.TwinCalcStopMapper;
+import com.ruoyi.biz.service.ITwinCalcHourService;
 import com.ruoyi.biz.service.ITwinCalcStopService;
 import com.ruoyi.biz.service.ITwinRecordStopService;
 import com.ruoyi.common.core.text.Convert;
@@ -38,6 +41,8 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
     private ITwinRecordStopService stopService;
     @Resource
     private SqlSessionFactory factory;
+    @Resource
+    private ITwinCalcHourService calcHourService;
 
     /**
      * 查询停机数据统计
@@ -115,34 +120,47 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
     public void process(LocalDateTime startTime, List<TwinDevice> deviceList) {
         String config = configService.selectConfigByKey("data.legal.time");
         int legalTime = Integer.parseInt(config) * 1000;
-
+        Date s = new Date();
         Date date = Date.from(startTime.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
         List<TwinCalcStop> insertList = new ArrayList<>();
         List<TwinCalcStop> updateList = new ArrayList<>();
         String oldStr = "old";
+        //查询上次未统计完整的数据
+        TwinCalcStop calcSearch = new TwinCalcStop();
+        calcSearch.setCalcStatus("1");
+        List<TwinCalcStop> oldList = selectTwinCalcStopList(calcSearch);
+        Map<Long, List<TwinCalcStop>> oldMap = oldList.stream().collect(Collectors.groupingBy(TwinCalcStop::getDeviceId, LinkedHashMap::new, Collectors.toList()));
+        //查询本次数据
+        TwinRecordStop recordSearch = new TwinRecordStop();
+        recordSearch.setDataDate(date);
+        recordSearch.setHour(startTime.getHour());
+        List<TwinRecordStop> newList = stopService.selectTwinRecordStopList(recordSearch);
+        Map<Long, List<TwinRecordStop>> newMap = newList.stream().collect(Collectors.groupingBy(TwinRecordStop::getDeviceId, LinkedHashMap::new, Collectors.toList()));
+        //查询统计数据
+        TwinCalcHour hourSearch = new TwinCalcHour();
+        hourSearch.setDataDate(date);
+        hourSearch.setHour(startTime.getHour());
+        List<TwinCalcHour> hourList = calcHourService.selectTwinCalcHourList(hourSearch);
+        List<TwinCalcHour> calcStopTimeList = new ArrayList<>();
+
+        List<TwinRecordStop> recordList = new ArrayList<>();
         deviceList.forEach(device -> {
             //先处理上次未统计完整的数据
-            TwinCalcStop calcSearch = new TwinCalcStop();
-            calcSearch.setDeviceId(device.getDeviceId());
-            calcSearch.setCalcStatus("1");
             List<TwinRecordStop> list = new ArrayList<>();
-            List<TwinCalcStop> oldList = selectTwinCalcStopList(calcSearch);
-            oldList.forEach(old -> {
-                TwinRecordStop stop = new TwinRecordStop();
-                BeanUtils.copyProperties(old, stop);
-                stop.setRemark(oldStr);
-                list.add(stop);
-            });
+            if (oldMap.get(device.getDeviceId()) != null) {
+                oldMap.get(device.getDeviceId()).forEach(old -> {
+                    TwinRecordStop stop = new TwinRecordStop();
+                    BeanUtils.copyProperties(old, stop);
+                    stop.setRemark(oldStr);
+                    list.add(stop);
+                });
+            }
             //再处理本次数据,2次结合
-            TwinRecordStop recordSearch = new TwinRecordStop();
-            recordSearch.setDeviceId(device.getDeviceId());
-            recordSearch.setDataDate(date);
-            recordSearch.setHour(startTime.getHour());
-            List<TwinRecordStop> newList = stopService.selectTwinRecordStopList(recordSearch);
-            list.addAll(newList);
+            if (newMap.get(device.getDeviceId()) != null) {
+                list.addAll(newMap.get(device.getDeviceId()));
+            }
 
             Map<Integer, List<TwinRecordStop>> groupList = list.stream().collect(Collectors.groupingBy(TwinRecordStop::getStopType));
-            List<TwinRecordStop> recordList = new ArrayList<>();
             for (Map.Entry<Integer, List<TwinRecordStop>> entry : groupList.entrySet()) {
                 List<TwinRecordStop> temp = entry.getValue();
                 if (temp.size() == 1) {
@@ -175,25 +193,60 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
                     }
                 }
             }
+        });
+        for (TwinRecordStop stop : recordList) {
+            TwinCalcHour calcHour = new TwinCalcHour();
+            //1.计算停机时长
+            long stopTime = stop.getEndTime().getTime();
+            if (stop.getStartTime().before(date)) {
+                stopTime -= date.getTime();
+            } else {
+                stopTime -= stop.getStartTime().getTime();
+            }
+            calcHour.setDeviceId(stop.getDeviceId());
+            calcHour.setCloseTime(stopTime);
+            calcStopTimeList.add(calcHour);
 
-            for (TwinRecordStop stop : recordList) {
-                TwinCalcStop v = new TwinCalcStop();
-                BeanUtils.copyProperties(stop, v);
-                if (oldStr.equals(stop.getRemark())) {
-                    updateList.add(v);
-//                    updateTwinCalcStop(v);
-                } else {
-                    v.setId(null);
-                    insertList.add(v);
-//                    insertTwinCalcStop(v);
-                }
+            //2.复制记录
+            TwinCalcStop v = new TwinCalcStop();
+            BeanUtils.copyProperties(stop, v);
+            if (oldStr.equals(stop.getRemark())) {
+                v.setCalcStatus("0");
+                v.setRemark("");
+                updateList.add(v);
+            } else {
+                v.setId(null);
+                insertList.add(v);
             }
+        }
+
+        //按ID分组,并统计停机时间
+        Map<Long, Long> resultMap = calcStopTimeList.stream().collect(Collectors.groupingBy(TwinCalcHour::getDeviceId, Collectors.reducing(0L, TwinCalcHour::getCloseTime, Long::sum)));
+
+        Date e = new Date();
+        System.err.println(e.getTime() - s.getTime());
+        hourList.forEach(hour -> {
+            Long closeTime = 0L;
+            if (resultMap.get(hour.getDeviceId()) != null) {
+                closeTime = resultMap.get(hour.getDeviceId());
+            }
+            closeTime = closeTime / 1000;
+            Long openTime = 3600 - closeTime;
+            hour.setCloseTime(closeTime);
+            hour.setOpenTime(openTime);
         });
 
         try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
             TwinCalcStopMapper mapper = sqlSession.getMapper(TwinCalcStopMapper.class);
-            insertList.forEach(mapper::insertTwinCalcStop);
-            updateList.forEach(mapper::updateTwinCalcStop);
+            if (insertList.size() > 0) {
+                insertList.forEach(mapper::insertTwinCalcStop);
+            }
+            if (updateList.size() > 0) {
+                updateList.forEach(mapper::updateTwinCalcStop);
+            }
+
+            TwinCalcHourMapper hourMapper = sqlSession.getMapper(TwinCalcHourMapper.class);
+            hourList.forEach(hourMapper::updateTwinCalcHour);
             sqlSession.commit();
         }
     }