|
|
|
|
@ -742,13 +742,26 @@ public class TDengineService {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. 表名
|
|
|
|
|
// 1. 数据库名
|
|
|
|
|
String dbName = "besure_server";
|
|
|
|
|
|
|
|
|
|
// 2. 表名
|
|
|
|
|
String tableName = "d_" + deviceId;
|
|
|
|
|
|
|
|
|
|
// 2. 构建列SQL,TDengine必须有 ts
|
|
|
|
|
// 3. 确保数据库存在
|
|
|
|
|
try {
|
|
|
|
|
String createDbSql = "CREATE DATABASE IF NOT EXISTS " + dbName;
|
|
|
|
|
jdbcTemplate.execute(createDbSql);
|
|
|
|
|
log.info("TDengine 数据库确保存在: {}", dbName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("TDengine 数据库创建失败: {}", dbName, e);
|
|
|
|
|
throw exception(TABLE_CREATION_FAILED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 构建列SQL,TDengine必须有 ts
|
|
|
|
|
StringBuilder columnsSql = new StringBuilder("ts TIMESTAMP");
|
|
|
|
|
|
|
|
|
|
// 3. 遍历 contactModelList,如果为空则不执行循环,表只含 ts
|
|
|
|
|
// 5. 遍历 contactModelList 构建列
|
|
|
|
|
if (contactModelList != null && !contactModelList.isEmpty()) {
|
|
|
|
|
for (DeviceContactModelDO contact : contactModelList) {
|
|
|
|
|
String attributeCode = contact.getAttributeCode();
|
|
|
|
|
@ -769,17 +782,16 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 构建完整 SQL
|
|
|
|
|
String createSql = "CREATE TABLE IF NOT EXISTS besure_server." + tableName + " ("
|
|
|
|
|
+ columnsSql.toString()
|
|
|
|
|
+ ")";
|
|
|
|
|
// 6. 构建完整 SQL
|
|
|
|
|
String createTableSql = "CREATE TABLE IF NOT EXISTS " + dbName + "." + tableName + " ("
|
|
|
|
|
+ columnsSql.toString() + ")";
|
|
|
|
|
|
|
|
|
|
// 5. 使用 JdbcTemplate 执行 SQL
|
|
|
|
|
// 7. 执行创建表
|
|
|
|
|
try {
|
|
|
|
|
jdbcTemplate.execute(createSql);
|
|
|
|
|
log.info("TDengine 表创建成功: {}", tableName);
|
|
|
|
|
jdbcTemplate.execute(createTableSql);
|
|
|
|
|
log.info("TDengine 表创建成功: {}.{}", dbName, tableName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("TDengine 表创建失败: {}", tableName, e);
|
|
|
|
|
log.error("TDengine 表创建失败: {}.{}", dbName, tableName, e);
|
|
|
|
|
throw exception(TABLE_CREATION_FAILED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|