|
@@ -20,7 +20,6 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneOffset;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -121,32 +120,35 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
String config = configService.selectConfigByKey("data.legal.time");
|
|
|
int legalTime = Integer.parseInt(config) * 1000;
|
|
|
|
|
|
- Date date = Date.from(startTime.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
|
|
|
+ Date date = DateUtils.toDate(startTime.toLocalDate());
|
|
|
Date startDate = DateUtils.toDate(startTime);
|
|
|
Date endDate = DateUtils.toDate(endTime);
|
|
|
- List<TwinCalcStop> insertList = new ArrayList<>();
|
|
|
- List<TwinCalcStop> updateList = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
//查询上次未统计完整的数据
|
|
|
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<TwinCalcStop> insertList = new ArrayList<>();
|
|
|
+ List<TwinCalcStop> updateList = new ArrayList<>();
|
|
|
List<TwinCalcHour> calcStopTimeList = new ArrayList<>();
|
|
|
+
|
|
|
List<TwinRecordStop> recordList = new ArrayList<>();
|
|
|
- //需要删除的数据
|
|
|
- List<Long> delIds = new ArrayList<>();
|
|
|
for (TwinDevice device : deviceList) {
|
|
|
//先处理上次未统计完整的数据
|
|
|
List<TwinRecordStop> list = new ArrayList<>();
|
|
@@ -166,50 +168,18 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
list.addAll(newMap.get(device.getDeviceId()));
|
|
|
}
|
|
|
|
|
|
- Map<Integer, List<TwinRecordStop>> groupList = list.stream().collect(Collectors.groupingBy(TwinRecordStop::getStopType));
|
|
|
- for (Map.Entry<Integer, List<TwinRecordStop>> entry : groupList.entrySet()) {
|
|
|
- List<TwinRecordStop> temp = entry.getValue();
|
|
|
- if (temp.size() == 1) {
|
|
|
- //只有一条记录,则直接记录下来
|
|
|
- recordList.add(temp.get(0));
|
|
|
- }
|
|
|
- TwinRecordStop lastStop = null;
|
|
|
- for (int i = 0; i < temp.size(); i++) {
|
|
|
- TwinRecordStop currStop = temp.get(i);
|
|
|
- if (lastStop == null) {
|
|
|
- lastStop = currStop;
|
|
|
- } else {
|
|
|
- //1.第一轮,下一条开始时间-上一条结束时间小于 seconds 则两条记录合并
|
|
|
- if (currStop.getStartTime().getTime() - lastStop.getEndTime().getTime() < legalTime) {
|
|
|
- lastStop.setEndTime(currStop.getEndTime());
|
|
|
- lastStop.setCalcStatus(currStop.getCalcStatus());
|
|
|
- } else {
|
|
|
- //2.判断持续时间,如果大于seconds则记录
|
|
|
- if (lastStop.getEndTime().getTime() - lastStop.getStartTime().getTime() > legalTime) {
|
|
|
- recordList.add(lastStop);
|
|
|
- }
|
|
|
-
|
|
|
- if (StringUtils.isNotEmpty(lastStop.getRemark())) {
|
|
|
- //如果无效数据是未统计完成的记录,则需要删除统计里面的数据
|
|
|
- delIds.add(Long.parseLong(lastStop.getRemark()));
|
|
|
- }
|
|
|
-
|
|
|
- lastStop = currStop;
|
|
|
- }
|
|
|
- if (i + 1 == temp.size()) {
|
|
|
- //补一次最后一条记录,如果最后一条记录是统计未完成的,或者持续时间大于seconds的,则补录
|
|
|
- if ("1".equals(lastStop.getCalcStatus()) || lastStop.getEndTime().getTime() - lastStop.getStartTime().getTime() > legalTime) {
|
|
|
- recordList.add(lastStop);
|
|
|
- }
|
|
|
+ //1.先循环一轮,处理记录合并
|
|
|
+ List<TwinRecordStop> list1 = mergeList(list, legalTime);
|
|
|
+ List<TwinRecordStop> list2 = new ArrayList<>();
|
|
|
+ //2.再循环合并后的记录,删除无效数据
|
|
|
+ for (TwinRecordStop stop : list1) {
|
|
|
+ if ((stop.getEndTime().getTime() - stop.getStartTime().getTime()) > legalTime) {
|
|
|
+ list2.add(stop);
|
|
|
|
|
|
- //如果无效数据是未统计完成的记录,则需要删除统计里面的数据
|
|
|
- if ("1".equals(lastStop.getCalcStatus()) && StringUtils.isNotEmpty(lastStop.getRemark())) {
|
|
|
- delIds.add(Long.parseLong(lastStop.getRemark()));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
+ //3.还要循环一轮,进行记录合并
|
|
|
+ recordList.addAll(mergeList(list2, legalTime));
|
|
|
}
|
|
|
for (TwinRecordStop stop : recordList) {
|
|
|
//2.复制记录
|
|
@@ -222,6 +192,7 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
stop.setStartTime(startDate);
|
|
|
}
|
|
|
long stopTime = stop.getEndTime().getTime() - stop.getStartTime().getTime();
|
|
|
+
|
|
|
calcHour.setDeviceId(stop.getDeviceId());
|
|
|
calcHour.setCloseTime(stopTime);
|
|
|
calcStopTimeList.add(calcHour);
|
|
@@ -244,8 +215,9 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
Long closeTime = 0L;
|
|
|
if (resultMap.get(hour.getDeviceId()) != null) {
|
|
|
closeTime = resultMap.get(hour.getDeviceId());
|
|
|
+ closeTime = Math.min(closeTime / 1000, 3600);
|
|
|
}
|
|
|
- closeTime = closeTime / 1000;
|
|
|
+
|
|
|
Long openTime = 3600 - closeTime;
|
|
|
hour.setCloseTime(closeTime);
|
|
|
hour.setOpenTime(openTime);
|
|
@@ -265,14 +237,31 @@ public class TwinCalcStopServiceImpl implements ITwinCalcStopService {
|
|
|
sqlSession.commit();
|
|
|
}
|
|
|
|
|
|
- if (delIds.size() > 0) {
|
|
|
- deleteTwinCalcStopByIds(delIds.toArray(new Long[]{}));
|
|
|
- }
|
|
|
-
|
|
|
//统计完成删除记录数据,避免记录超长
|
|
|
stopService.deleteTwinRecordStop(date, startTime.getHour());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 合并数据记录
|
|
|
+ */
|
|
|
+ private List<TwinRecordStop> mergeList(List<TwinRecordStop> list, int legalTime) {
|
|
|
+ List<TwinRecordStop> result = new ArrayList<>();
|
|
|
+ TwinRecordStop lastStop = null;
|
|
|
+
|
|
|
+ for (TwinRecordStop stop : list) {
|
|
|
+ if (lastStop != null && stop.getStopType().equals(lastStop.getStopType()) && (stop.getStartTime().getTime() - lastStop.getEndTime().getTime()) < legalTime) {
|
|
|
+ //判断同一个类型,并且下一条记录的开始时间-上一条记录的结束时间 小于数据合法时间
|
|
|
+ lastStop.setEndTime(stop.getEndTime());
|
|
|
+ //需要设置统计未完成状态 为当前记录的
|
|
|
+ lastStop.setCalcStatus(stop.getCalcStatus());
|
|
|
+ } else {
|
|
|
+ result.add(stop);
|
|
|
+ lastStop = stop;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 按开始和结束时间查询数据
|