|
|
|
|
@ -79,6 +79,19 @@ public class TDengineService {
|
|
|
|
|
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")
|
|
|
|
|
// public void testConnection() {
|
|
|
|
|
// String testSQL = "SELECT SERVER_STATUS()";
|
|
|
|
|
@ -272,7 +285,6 @@ public class TDengineService {
|
|
|
|
|
String tableName = "d_" + id;
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
sqlBuilder.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName)).append(" WHERE 1=1");
|
|
|
|
|
|
|
|
|
|
if (StartTime != null) {
|
|
|
|
|
@ -2035,7 +2047,9 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 表名 - 使用固定表名或按设备分表
|
|
|
|
|
String tableName = tdengineTable("iot_device_operation_record");
|
|
|
|
|
String stableName = "iot_device_operation_record";
|
|
|
|
|
String tableName = tdengineTable(stableName);
|
|
|
|
|
String childTableName = tdengineSubTableName(stableName, record.getDeviceId());
|
|
|
|
|
// 或者按设备分表: tdengineTable("operation_record_" + record.getDeviceId());
|
|
|
|
|
|
|
|
|
|
// 构建 SQL
|
|
|
|
|
@ -2044,8 +2058,9 @@ public class TDengineService {
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 1. 必须字段: 时间戳 (TDengine 要求)
|
|
|
|
|
columnBuilder.append("ts");
|
|
|
|
|
valueBuilder.append("?");
|
|
|
|
|
addTbnameForStableInsert(childTableName, columnBuilder, valueBuilder, params);
|
|
|
|
|
columnBuilder.append(", ts");
|
|
|
|
|
valueBuilder.append(", ?");
|
|
|
|
|
// 使用当前时间作为 ts,或者使用 record 中的时间
|
|
|
|
|
// if (record.getCreateTime() != null) {
|
|
|
|
|
// 将 LocalDateTime 转换为 Timestamp
|
|
|
|
|
@ -2362,20 +2377,23 @@ public class TDengineService {
|
|
|
|
|
*/
|
|
|
|
|
@DS("tdengine")
|
|
|
|
|
public boolean insertDeviceWarningRecord(DeviceWarinningRecordDO deviceWarningRecordDO) {
|
|
|
|
|
if (deviceWarningRecordDO == null) {
|
|
|
|
|
if (deviceWarningRecordDO == null || deviceWarningRecordDO.getDeviceId() == null) {
|
|
|
|
|
log.warn("告警记录参数为空");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = tdengineTable("iot_device_warning_record");
|
|
|
|
|
String stableName = "iot_device_warning_record";
|
|
|
|
|
String tableName = tdengineTable(stableName);
|
|
|
|
|
String childTableName = tdengineSubTableName(stableName, deviceWarningRecordDO.getDeviceId());
|
|
|
|
|
|
|
|
|
|
StringBuilder columnBuilder = new StringBuilder();
|
|
|
|
|
StringBuilder valueBuilder = new StringBuilder();
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 1. 必须字段: 时间戳
|
|
|
|
|
columnBuilder.append("ts");
|
|
|
|
|
valueBuilder.append("?");
|
|
|
|
|
addTbnameForStableInsert(childTableName, columnBuilder, valueBuilder, params);
|
|
|
|
|
columnBuilder.append(", ts");
|
|
|
|
|
valueBuilder.append(", ?");
|
|
|
|
|
params.add(new Timestamp(System.currentTimeMillis()));
|
|
|
|
|
|
|
|
|
|
// 2. 设备信息
|
|
|
|
|
@ -2667,9 +2685,10 @@ public class TDengineService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
String sql = "INSERT INTO iot_device_capacity_record (ts, capacity_value, device_id) VALUES (NOW, "
|
|
|
|
|
+ capacityValue + ", " + deviceId + ")";
|
|
|
|
|
jdbcTemplate.execute(sql);
|
|
|
|
|
String stableName = "iot_device_capacity_record";
|
|
|
|
|
String sql = "INSERT INTO " + tdengineTable(stableName)
|
|
|
|
|
+ " (tbname, ts, capacity_value, device_id) VALUES (?, NOW, ?, ?)";
|
|
|
|
|
jdbcTemplate.update(sql, tdengineSubTableName(stableName, deviceId), capacityValue, deviceId);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("insertDeviceCapacityRecord failed, deviceId={}, capacityValue={}", deviceId, capacityValue, e);
|
|
|
|
|
|