能源概览查询修改

main
liutao 1 week ago
parent 41db99aab2
commit d341ead2c7

@ -79,19 +79,6 @@ public class TDengineService {
return getTdengineDbName() + "." + tableName; return getTdengineDbName() + "." + tableName;
} }
private String tdengineSubTableName(String stableName, Long deviceId) {
return stableName + "_" + deviceId;
}
private void addTbnameForStableInsert(String childTableName,
StringBuilder columnBuilder,
StringBuilder valueBuilder,
List<Object> params) {
columnBuilder.append("tbname");
valueBuilder.append("?");
params.add(childTableName);
}
// @DS("tdengine") // @DS("tdengine")
// public void testConnection() { // public void testConnection() {
// String testSQL = "SELECT SERVER_STATUS()"; // String testSQL = "SELECT SERVER_STATUS()";
@ -285,6 +272,7 @@ public class TDengineService {
String tableName = "d_" + id; String tableName = "d_" + id;
StringBuilder sqlBuilder = new StringBuilder(); StringBuilder sqlBuilder = new StringBuilder();
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
sqlBuilder.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName)).append(" WHERE 1=1"); sqlBuilder.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName)).append(" WHERE 1=1");
if (StartTime != null) { if (StartTime != null) {
@ -1416,7 +1404,7 @@ public class TDengineService {
// 2. 必须以字母开头 // 2. 必须以字母开头
if (!Character.isLetter(tableName.charAt(0))) { if (!Character.isLetter(tableName.charAt(0))) {
throw exception(DEVICE_MODEL_MUST_LETTER, throw exception(DEVICE_MODEL_MUST_LETTER,
tableName); tableName);
} }
// 3. 只能包含字母、数字、下划线 // 3. 只能包含字母、数字、下划线
@ -1523,7 +1511,7 @@ public class TDengineService {
*/ */
@DS("tdengine") @DS("tdengine")
public List<Map<String, Object>> newSelectBatchPage(Long deviceId, String startTime, String endTime, public List<Map<String, Object>> newSelectBatchPage(Long deviceId, String startTime, String endTime,
Integer page, Integer pageSize) { Integer page, Integer pageSize) {
if (deviceId == null || page == null || pageSize == null) { if (deviceId == null || page == null || pageSize == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -2047,9 +2035,7 @@ public class TDengineService {
} }
// 表名 - 使用固定表名或按设备分表 // 表名 - 使用固定表名或按设备分表
String stableName = "iot_device_operation_record"; String tableName = tdengineTable("iot_device_operation_record");
String tableName = tdengineTable(stableName);
String childTableName = tdengineSubTableName(stableName, record.getDeviceId());
// 或者按设备分表: tdengineTable("operation_record_" + record.getDeviceId()); // 或者按设备分表: tdengineTable("operation_record_" + record.getDeviceId());
// 构建 SQL // 构建 SQL
@ -2058,12 +2044,11 @@ public class TDengineService {
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
// 1. 必须字段: 时间戳 (TDengine 要求) // 1. 必须字段: 时间戳 (TDengine 要求)
addTbnameForStableInsert(childTableName, columnBuilder, valueBuilder, params); columnBuilder.append("ts");
columnBuilder.append(", ts"); valueBuilder.append("?");
valueBuilder.append(", ?");
// 使用当前时间作为 ts或者使用 record 中的时间 // 使用当前时间作为 ts或者使用 record 中的时间
// if (record.getCreateTime() != null) { // if (record.getCreateTime() != null) {
// 将 LocalDateTime 转换为 Timestamp // 将 LocalDateTime 转换为 Timestamp
// Timestamp tsTimestamp = Timestamp.valueOf(record.getCreateTime()); // Timestamp tsTimestamp = Timestamp.valueOf(record.getCreateTime());
params.add(new Timestamp(System.currentTimeMillis())); params.add(new Timestamp(System.currentTimeMillis()));
// } else { // } else {
@ -2377,23 +2362,20 @@ public class TDengineService {
*/ */
@DS("tdengine") @DS("tdengine")
public boolean insertDeviceWarningRecord(DeviceWarinningRecordDO deviceWarningRecordDO) { public boolean insertDeviceWarningRecord(DeviceWarinningRecordDO deviceWarningRecordDO) {
if (deviceWarningRecordDO == null || deviceWarningRecordDO.getDeviceId() == null) { if (deviceWarningRecordDO == null) {
log.warn("告警记录参数为空"); log.warn("告警记录参数为空");
return false; return false;
} }
String stableName = "iot_device_warning_record"; String tableName = tdengineTable("iot_device_warning_record");
String tableName = tdengineTable(stableName);
String childTableName = tdengineSubTableName(stableName, deviceWarningRecordDO.getDeviceId());
StringBuilder columnBuilder = new StringBuilder(); StringBuilder columnBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder();
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
// 1. 必须字段: 时间戳 // 1. 必须字段: 时间戳
addTbnameForStableInsert(childTableName, columnBuilder, valueBuilder, params); columnBuilder.append("ts");
columnBuilder.append(", ts"); valueBuilder.append("?");
valueBuilder.append(", ?");
params.add(new Timestamp(System.currentTimeMillis())); params.add(new Timestamp(System.currentTimeMillis()));
// 2. 设备信息 // 2. 设备信息
@ -2685,10 +2667,9 @@ public class TDengineService {
return false; return false;
} }
try { try {
String stableName = "iot_device_capacity_record"; String sql = "INSERT INTO iot_device_capacity_record (ts, capacity_value, device_id) VALUES (NOW, "
String sql = "INSERT INTO " + tdengineTable(stableName) + capacityValue + ", " + deviceId + ")";
+ " (tbname, ts, capacity_value, device_id) VALUES (?, NOW, ?, ?)"; jdbcTemplate.execute(sql);
jdbcTemplate.update(sql, tdengineSubTableName(stableName, deviceId), capacityValue, deviceId);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error("insertDeviceCapacityRecord failed, deviceId={}, capacityValue={}", deviceId, capacityValue, e); log.error("insertDeviceCapacityRecord failed, deviceId={}, capacityValue={}", deviceId, capacityValue, e);

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

Loading…
Cancel
Save