|
@@ -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();
|
|
|
}
|
|
|
}
|