|
|
|
|
@ -836,11 +836,7 @@ public class EnergyDeviceServiceImpl implements EnergyDeviceService {
|
|
|
|
|
try {
|
|
|
|
|
LocalDateTime start = LocalDateTime.parse(reqVO.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
LocalDateTime end = LocalDateTime.parse(reqVO.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
Map<Long, List<OperationRulesVO>> deviceRulesMap = buildDeviceRulesMap(devices);
|
|
|
|
|
Set<Long> sourceDeviceIds = collectRuleDeviceIds(deviceRulesMap);
|
|
|
|
|
Map<Long, DeviceContactModelDO> pointInfoMap = batchGetPointInfoFromLocalDB(new ArrayList<>(collectPointIds(deviceRulesMap)));
|
|
|
|
|
|
|
|
|
|
if (sourceDeviceIds.isEmpty()) {
|
|
|
|
|
if (buildDeviceRulesMap(devices).isEmpty()) {
|
|
|
|
|
chart.setXAxis(Collections.emptyList());
|
|
|
|
|
chart.setData(Collections.emptyList());
|
|
|
|
|
return chart;
|
|
|
|
|
@ -848,9 +844,9 @@ public class EnergyDeviceServiceImpl implements EnergyDeviceService {
|
|
|
|
|
|
|
|
|
|
boolean sameDay = start.toLocalDate().equals(end.toLocalDate()) && !start.equals(end);
|
|
|
|
|
if (sameDay) {
|
|
|
|
|
return buildHourTrendChart(chart, deviceRulesMap, sourceDeviceIds, pointInfoMap, start, end);
|
|
|
|
|
return buildHourTrendChart(chart, devices, start, end);
|
|
|
|
|
}
|
|
|
|
|
return buildDayTrendChart(chart, deviceRulesMap, sourceDeviceIds, pointInfoMap, start.toLocalDate(), end.toLocalDate());
|
|
|
|
|
return buildDayTrendChart(chart, devices, start, end);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.warn("构建能源总览趋势数据失败, req={}", reqVO, e);
|
|
|
|
|
chart.setXAxis(Collections.emptyList());
|
|
|
|
|
@ -860,23 +856,21 @@ public class EnergyDeviceServiceImpl implements EnergyDeviceService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EnergyOverviewRespVO.TrendChart buildHourTrendChart(EnergyOverviewRespVO.TrendChart chart,
|
|
|
|
|
Map<Long, List<OperationRulesVO>> deviceRulesMap,
|
|
|
|
|
Set<Long> sourceDeviceIds,
|
|
|
|
|
Map<Long, DeviceContactModelDO> pointInfoMap,
|
|
|
|
|
List<EnergyDeviceDO> devices,
|
|
|
|
|
LocalDateTime start,
|
|
|
|
|
LocalDateTime end) {
|
|
|
|
|
DateTimeFormatter hourKeyFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
|
|
|
|
|
DateTimeFormatter displayFormatter = DateTimeFormatter.ofPattern("HH:00");
|
|
|
|
|
List<Map<String, Object>> allRows = tDengineService.newQueryLastDataByHourBatch(sourceDeviceIds, start, end);
|
|
|
|
|
Map<String, Map<Long, TimePointCache>> hourCacheMap = buildTimeCacheByHour(allRows, pointInfoMap, deviceRulesMap);
|
|
|
|
|
|
|
|
|
|
List<String> xAxis = new ArrayList<>();
|
|
|
|
|
List<String> data = new ArrayList<>();
|
|
|
|
|
LocalDateTime cursor = start.withMinute(0).withSecond(0);
|
|
|
|
|
LocalDateTime limit = end.withMinute(0).withSecond(0);
|
|
|
|
|
LocalDateTime cursor = start.withMinute(0).withSecond(0).withNano(0);
|
|
|
|
|
LocalDateTime limit = end.withMinute(0).withSecond(0).withNano(0);
|
|
|
|
|
while (!cursor.isAfter(limit)) {
|
|
|
|
|
String hourKey = cursor.format(hourKeyFormatter);
|
|
|
|
|
double total = sumGroupedEnergy(deviceRulesMap, hourCacheMap.getOrDefault(hourKey, Collections.emptyMap()));
|
|
|
|
|
LocalDateTime intervalStart = cursor.isBefore(start) ? start : cursor;
|
|
|
|
|
LocalDateTime intervalEnd = cursor.plusHours(1);
|
|
|
|
|
if (intervalEnd.isAfter(end)) {
|
|
|
|
|
intervalEnd = end;
|
|
|
|
|
}
|
|
|
|
|
double total = intervalStart.isAfter(intervalEnd) ? 0D : queryTrendConsumption(devices, intervalStart, intervalEnd);
|
|
|
|
|
xAxis.add(cursor.format(displayFormatter));
|
|
|
|
|
data.add(formatDouble(total));
|
|
|
|
|
cursor = cursor.plusHours(1);
|
|
|
|
|
@ -887,22 +881,25 @@ public class EnergyDeviceServiceImpl implements EnergyDeviceService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EnergyOverviewRespVO.TrendChart buildDayTrendChart(EnergyOverviewRespVO.TrendChart chart,
|
|
|
|
|
Map<Long, List<OperationRulesVO>> deviceRulesMap,
|
|
|
|
|
Set<Long> sourceDeviceIds,
|
|
|
|
|
Map<Long, DeviceContactModelDO> pointInfoMap,
|
|
|
|
|
LocalDate startDate,
|
|
|
|
|
LocalDate endDate) {
|
|
|
|
|
int days = (int) (endDate.toEpochDay() - startDate.toEpochDay()) + 1;
|
|
|
|
|
List<Map<String, Object>> allRows = tDengineService.queryLastDataByDayBatch(sourceDeviceIds, startDate, days);
|
|
|
|
|
Map<String, Map<Long, TimePointCache>> dayCacheMap = buildTimeCacheByDay(allRows, pointInfoMap, deviceRulesMap);
|
|
|
|
|
|
|
|
|
|
List<EnergyDeviceDO> devices,
|
|
|
|
|
LocalDateTime start,
|
|
|
|
|
LocalDateTime end) {
|
|
|
|
|
LocalDate startDate = start.toLocalDate();
|
|
|
|
|
LocalDate endDate = end.toLocalDate();
|
|
|
|
|
List<String> xAxis = new ArrayList<>();
|
|
|
|
|
List<String> data = new ArrayList<>();
|
|
|
|
|
LocalDate cursor = startDate;
|
|
|
|
|
while (!cursor.isAfter(endDate)) {
|
|
|
|
|
String dayKey = cursor.toString();
|
|
|
|
|
double total = sumGroupedEnergy(deviceRulesMap, dayCacheMap.getOrDefault(dayKey, Collections.emptyMap()));
|
|
|
|
|
xAxis.add(dayKey);
|
|
|
|
|
LocalDateTime intervalStart = cursor.atStartOfDay();
|
|
|
|
|
if (intervalStart.isBefore(start)) {
|
|
|
|
|
intervalStart = start;
|
|
|
|
|
}
|
|
|
|
|
LocalDateTime intervalEnd = cursor.plusDays(1).atStartOfDay();
|
|
|
|
|
if (intervalEnd.isAfter(end)) {
|
|
|
|
|
intervalEnd = end;
|
|
|
|
|
}
|
|
|
|
|
double total = intervalStart.isAfter(intervalEnd) ? 0D : queryTrendConsumption(devices, intervalStart, intervalEnd);
|
|
|
|
|
xAxis.add(cursor.toString());
|
|
|
|
|
data.add(formatDouble(total));
|
|
|
|
|
cursor = cursor.plusDays(1);
|
|
|
|
|
}
|
|
|
|
|
@ -911,6 +908,19 @@ public class EnergyDeviceServiceImpl implements EnergyDeviceService {
|
|
|
|
|
return chart;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double queryTrendConsumption(List<EnergyDeviceDO> devices, LocalDateTime start, LocalDateTime end) {
|
|
|
|
|
if (devices == null || devices.isEmpty()) {
|
|
|
|
|
return 0D;
|
|
|
|
|
}
|
|
|
|
|
EnergyDeviceConsumptionReqVO req = new EnergyDeviceConsumptionReqVO();
|
|
|
|
|
req.setIds(devices.stream().map(item -> String.valueOf(item.getId())).collect(Collectors.joining(",")));
|
|
|
|
|
req.setStartTime(start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
req.setEndTime(end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
|
|
return queryDataRecords(req).stream()
|
|
|
|
|
.mapToDouble(item -> parseEnergyValue(item.getEnergyConsumption()))
|
|
|
|
|
.sum();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Map<String, Map<Long, TimePointCache>> buildTimeCacheByHour(List<Map<String, Object>> allRows,
|
|
|
|
|
Map<Long, DeviceContactModelDO> pointInfoMap,
|
|
|
|
|
Map<Long, List<OperationRulesVO>> deviceRulesMap) {
|
|
|
|
|
|