|
|
|
|
@ -18,6 +18,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.codec.DecoderException;
|
|
|
|
|
import org.apache.commons.codec.binary.Hex;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.dao.EmptyResultDataAccessException;
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
import org.springframework.jdbc.core.RowMapper;
|
|
|
|
|
@ -44,9 +45,40 @@ import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.*;
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class TDengineService {
|
|
|
|
|
private static final String DEFAULT_TDENGINE_DATABASE = "besure_server";
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
|
|
|
|
@Value("${spring.datasource.dynamic.datasource.tdengine.url:}")
|
|
|
|
|
private String tdengineUrl;
|
|
|
|
|
|
|
|
|
|
private String getTdengineDbName() {
|
|
|
|
|
if (StrUtil.isBlank(tdengineUrl)) {
|
|
|
|
|
return DEFAULT_TDENGINE_DATABASE;
|
|
|
|
|
}
|
|
|
|
|
int protocolIndex = tdengineUrl.indexOf("://");
|
|
|
|
|
int databaseStart = tdengineUrl.indexOf('/', protocolIndex >= 0 ? protocolIndex + 3 : 0);
|
|
|
|
|
if (databaseStart < 0 || databaseStart + 1 >= tdengineUrl.length()) {
|
|
|
|
|
return DEFAULT_TDENGINE_DATABASE;
|
|
|
|
|
}
|
|
|
|
|
int queryStart = tdengineUrl.indexOf('?', databaseStart + 1);
|
|
|
|
|
int semicolonStart = tdengineUrl.indexOf(';', databaseStart + 1);
|
|
|
|
|
int databaseEnd = tdengineUrl.length();
|
|
|
|
|
if (queryStart >= 0) {
|
|
|
|
|
databaseEnd = Math.min(databaseEnd, queryStart);
|
|
|
|
|
}
|
|
|
|
|
if (semicolonStart >= 0) {
|
|
|
|
|
databaseEnd = Math.min(databaseEnd, semicolonStart);
|
|
|
|
|
}
|
|
|
|
|
String database = tdengineUrl.substring(databaseStart + 1, databaseEnd);
|
|
|
|
|
return StrUtil.isBlank(database) ? DEFAULT_TDENGINE_DATABASE : database;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String tdengineTable(String tableName) {
|
|
|
|
|
return getTdengineDbName() + "." + tableName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @DS("tdengine")
|
|
|
|
|
// public void testConnection() {
|
|
|
|
|
// String testSQL = "SELECT SERVER_STATUS()";
|
|
|
|
|
@ -59,14 +91,14 @@ public class TDengineService {
|
|
|
|
|
public void initDatabaseAndTable(Long id) {
|
|
|
|
|
// 1. 创建数据库(使用正确的TDengine语法)
|
|
|
|
|
// 注意:KEEP 必须大于或等于 3 倍的 DURATION[6](@ref),建议调整
|
|
|
|
|
String createDbSQL = "CREATE DATABASE IF NOT EXISTS besure KEEP 365 DURATION 30";
|
|
|
|
|
String createDbSQL = "CREATE DATABASE IF NOT EXISTS " + getTdengineDbName() + " KEEP 365 DURATION 30";
|
|
|
|
|
jdbcTemplate.execute(createDbSQL);
|
|
|
|
|
|
|
|
|
|
// 2. 使用数据库
|
|
|
|
|
jdbcTemplate.execute("USE besure");
|
|
|
|
|
jdbcTemplate.execute("USE " + getTdengineDbName());
|
|
|
|
|
|
|
|
|
|
// 3. 创建超级表
|
|
|
|
|
String createSuperTableSQL = "CREATE STABLE IF NOT EXISTS device_data (" +
|
|
|
|
|
String createSuperTableSQL = "CREATE STABLE IF NOT EXISTS " + tdengineTable("device_data") + " (" +
|
|
|
|
|
"ts TIMESTAMP, " +
|
|
|
|
|
"query_data BLOB" +
|
|
|
|
|
") TAGS (device_id BIGINT)";
|
|
|
|
|
@ -75,8 +107,8 @@ public class TDengineService {
|
|
|
|
|
// 4. 创建子表
|
|
|
|
|
String tableName = "d_" + id;
|
|
|
|
|
String createTableSql = String.format(
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS %s USING device_data TAGS(%d)",
|
|
|
|
|
tableName, id);
|
|
|
|
|
"CREATE TABLE IF NOT EXISTS %s USING %s TAGS(%d)",
|
|
|
|
|
tdengineTable(tableName), tdengineTable("device_data"), id);
|
|
|
|
|
jdbcTemplate.execute(createTableSql);
|
|
|
|
|
|
|
|
|
|
log.info("TDengine表创建成功 {}", tableName);
|
|
|
|
|
@ -95,7 +127,7 @@ public class TDengineService {
|
|
|
|
|
public Map<String, Object> getLatestDeviceData(Long id) {
|
|
|
|
|
String tableName = "d_" + id;
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT ts, query_data FROM besure." + tableName +
|
|
|
|
|
String sql = "SELECT ts, query_data FROM " + tdengineTable(tableName) +
|
|
|
|
|
" ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@ -149,10 +181,10 @@ public class TDengineService {
|
|
|
|
|
public boolean insertDeviceData(Long id, String jsonString, Timestamp timestamp) {
|
|
|
|
|
try {
|
|
|
|
|
// 确保使用正确的数据库
|
|
|
|
|
jdbcTemplate.execute("USE besure");
|
|
|
|
|
jdbcTemplate.execute("USE " + getTdengineDbName());
|
|
|
|
|
|
|
|
|
|
String tableName = "d_" + id;
|
|
|
|
|
String sql = "INSERT INTO besure." + tableName + " (ts, query_data) VALUES (?, ?)";
|
|
|
|
|
String sql = "INSERT INTO " + tdengineTable(tableName) + " (ts, query_data) VALUES (?, ?)";
|
|
|
|
|
|
|
|
|
|
return jdbcTemplate.update(sql, ps -> {
|
|
|
|
|
ps.setTimestamp(1, timestamp);
|
|
|
|
|
@ -174,7 +206,7 @@ public class TDengineService {
|
|
|
|
|
@DS("tdengine")
|
|
|
|
|
public List<Map<String, Object>> getNewestDeviceDataOrderByTimeDesc(Long id) {
|
|
|
|
|
String tableName = "d_" + id;
|
|
|
|
|
String sql = "SELECT ts, query_data FROM besure." + tableName + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
String sql = "SELECT ts, query_data FROM " + tdengineTable(tableName) + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return Collections.singletonList(jdbcTemplate.queryForObject(sql, new RowMapper<Map<String, Object>>() {
|
|
|
|
|
@ -241,7 +273,7 @@ public class TDengineService {
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
sqlBuilder.append("SELECT ts, query_data FROM besure.").append(tableName).append(" WHERE 1=1");
|
|
|
|
|
sqlBuilder.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName)).append(" WHERE 1=1");
|
|
|
|
|
|
|
|
|
|
if (StartTime != null) {
|
|
|
|
|
// 直接将时间字符串拼接到SQL中
|
|
|
|
|
@ -294,7 +326,7 @@ public class TDengineService {
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
sqlBuilder.append("SELECT ts, query_data FROM besure.").append(tableName).append(" WHERE 1=1");
|
|
|
|
|
sqlBuilder.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName)).append(" WHERE 1=1");
|
|
|
|
|
|
|
|
|
|
if (StartTime != null) {
|
|
|
|
|
// 直接将时间字符串拼接到SQL中
|
|
|
|
|
@ -351,7 +383,7 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "d_" + deviceId;
|
|
|
|
|
StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM besure." + tableName + " WHERE 1=1");
|
|
|
|
|
StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM " + tdengineTable(tableName) + " WHERE 1=1");
|
|
|
|
|
|
|
|
|
|
if (startTime != null) {
|
|
|
|
|
countSql.append(" AND ts >= '").append(startTime).append("'");
|
|
|
|
|
@ -398,7 +430,7 @@ public class TDengineService {
|
|
|
|
|
// 构建 SQL
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
sql.append("SELECT ts, query_data ")
|
|
|
|
|
.append("FROM besure.").append(tableName)
|
|
|
|
|
.append("FROM ").append(tdengineTable(tableName))
|
|
|
|
|
.append(" WHERE 1=1 ");
|
|
|
|
|
|
|
|
|
|
// 起始时间条件
|
|
|
|
|
@ -503,7 +535,7 @@ public class TDengineService {
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
sql.append("SELECT ts, query_data ")
|
|
|
|
|
.append("FROM besure.").append(tableName)
|
|
|
|
|
.append("FROM ").append(tdengineTable(tableName))
|
|
|
|
|
.append(" WHERE ts >= '").append(startTime).append("'")
|
|
|
|
|
.append(" AND ts <= '").append(endTime).append("'")
|
|
|
|
|
.append(" ORDER BY ts ASC ");
|
|
|
|
|
@ -536,7 +568,7 @@ public class TDengineService {
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
sql.append("SELECT ts, query_data ")
|
|
|
|
|
.append("FROM besure.").append(tableName)
|
|
|
|
|
.append("FROM ").append(tdengineTable(tableName))
|
|
|
|
|
.append(" WHERE ts >= '").append(startTime).append("'")
|
|
|
|
|
.append(" AND ts <= '").append(endTime).append("'")
|
|
|
|
|
.append(" ORDER BY ts DESC ");
|
|
|
|
|
@ -569,7 +601,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
String tableName = "d_" + deviceId;
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
sql.append("SELECT ts, query_data FROM besure.").append(tableName)
|
|
|
|
|
sql.append("SELECT ts, query_data FROM ").append(tdengineTable(tableName))
|
|
|
|
|
.append(" WHERE ts >= '").append(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("'")
|
|
|
|
|
.append(" AND ts <= '").append(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).append("'")
|
|
|
|
|
.append(" ORDER BY ts ASC"); // 升序,方便取每小时最后一条
|
|
|
|
|
@ -603,7 +635,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
String sql =
|
|
|
|
|
"SELECT ts, query_data FROM besure.d_" + deviceId +
|
|
|
|
|
"SELECT ts, query_data FROM " + tdengineTable("d_" + deviceId) +
|
|
|
|
|
" WHERE ts >= '" + startTime.format(fmt) + "'" +
|
|
|
|
|
" AND ts <= '" + endTime.format(fmt) + "'" +
|
|
|
|
|
" ORDER BY ts ASC";
|
|
|
|
|
@ -639,7 +671,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
|
|
|
|
|
String tableName = "besure.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
String sql =
|
|
|
|
|
"SELECT ts, query_data FROM " + tableName +
|
|
|
|
|
@ -704,7 +736,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
@DS("tdengine")
|
|
|
|
|
public LocalDateTime selectLatestTs(Long deviceId) {
|
|
|
|
|
String sql = "SELECT ts FROM besure.d_" + deviceId + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
String sql = "SELECT ts FROM " + tdengineTable("d_" + deviceId) + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
final LocalDateTime[] latestTs = {null};
|
|
|
|
|
|
|
|
|
|
@ -723,7 +755,7 @@ public class TDengineService {
|
|
|
|
|
Map<Long, LocalDateTime> result = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
String tableName = "besure.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
String sql = "SELECT ts FROM " + tableName + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@ -750,7 +782,7 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. 数据库名
|
|
|
|
|
String dbName = "besure_server";
|
|
|
|
|
String dbName = getTdengineDbName();
|
|
|
|
|
String tableName = "d_" + deviceId;
|
|
|
|
|
|
|
|
|
|
// 2. 确保数据库存在
|
|
|
|
|
@ -834,7 +866,7 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
// Java类型 -> TDengine类型
|
|
|
|
|
String tdType = JavaToTdengineTypeEnum.getTdTypeByJavaType(dataType);
|
|
|
|
|
@ -870,7 +902,7 @@ public class TDengineService {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
Set<String> existingColumns = getTDColumnNames(tableName);
|
|
|
|
|
int failureCount = 0;
|
|
|
|
|
|
|
|
|
|
@ -931,7 +963,7 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
// 列名构建
|
|
|
|
|
StringBuilder columnBuilder = new StringBuilder("ts");
|
|
|
|
|
@ -1104,7 +1136,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
// ALTER TABLE RENAME COLUMN SQL
|
|
|
|
|
String alterSql = "ALTER TABLE " + tableName
|
|
|
|
|
@ -1147,7 +1179,7 @@ public class TDengineService {
|
|
|
|
|
log.info("=== 开始计算序号 ===");
|
|
|
|
|
log.info("参数: deviceId={}, originalName={}, date={}", deviceId, originalName, date);
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
log.info("表名: {}", tableName);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@ -1222,7 +1254,7 @@ public class TDengineService {
|
|
|
|
|
*/
|
|
|
|
|
@DS("tdengine")
|
|
|
|
|
private boolean tableExists(Long deviceId) {
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 方法1:直接尝试查询
|
|
|
|
|
@ -1257,7 +1289,7 @@ public class TDengineService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 方法1:直接尝试查询该列
|
|
|
|
|
@ -1405,7 +1437,7 @@ public class TDengineService {
|
|
|
|
|
Map<Long, LocalDateTime> result = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
String sql = "SELECT ts FROM " + tableName + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@ -1434,7 +1466,7 @@ public class TDengineService {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT * FROM " + tableName + " ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
@ -1484,7 +1516,7 @@ public class TDengineService {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
// 计算分页偏移量
|
|
|
|
|
int offset = (page - 1) * pageSize;
|
|
|
|
|
@ -1542,7 +1574,7 @@ public class TDengineService {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
sqlBuilder.append("SELECT COUNT(*) AS total FROM ").append(tableName).append(" WHERE 1=1 ");
|
|
|
|
|
@ -1593,7 +1625,7 @@ public class TDengineService {
|
|
|
|
|
// 默认限制条数(防止全表扫描)
|
|
|
|
|
// int safeLimit = limit == null ? 100 : limit;
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
sqlBuilder.append("SELECT * FROM ").append(tableName).append(" WHERE 1=1 ");
|
|
|
|
|
@ -1645,7 +1677,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
@ -1705,7 +1737,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT LAST(ts) AS ts FROM besure_server.d_" + deviceId;
|
|
|
|
|
String sql = "SELECT LAST(ts) AS ts FROM " + tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
@ -1745,7 +1777,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
@ -1812,7 +1844,7 @@ public class TDengineService {
|
|
|
|
|
String endStr = endTime.format(fmt);
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
// 动态查询所有列(*),按时间倒序
|
|
|
|
|
String sql = "SELECT * FROM " + tableName +
|
|
|
|
|
@ -1876,7 +1908,7 @@ public class TDengineService {
|
|
|
|
|
LocalDate endDate = startDate.plusDays(days);
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
String sql = String.format(
|
|
|
|
|
"SELECT *, ts as timestamp, DATE_FORMAT(ts, '%%Y-%%m-%%d') as day " +
|
|
|
|
|
@ -1919,7 +1951,7 @@ public class TDengineService {
|
|
|
|
|
|
|
|
|
|
for (Long deviceId : deviceIds) {
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT * FROM " + tableName +
|
|
|
|
|
" ORDER BY ts DESC LIMIT 1";
|
|
|
|
|
@ -2003,8 +2035,8 @@ public class TDengineService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 表名 - 使用固定表名或按设备分表
|
|
|
|
|
String tableName = "besure_server.iot_device_operation_record";
|
|
|
|
|
// 或者按设备分表: "besure_server.operation_record_" + record.getDeviceId();
|
|
|
|
|
String tableName = tdengineTable("iot_device_operation_record");
|
|
|
|
|
// 或者按设备分表: tdengineTable("operation_record_" + record.getDeviceId());
|
|
|
|
|
|
|
|
|
|
// 构建 SQL
|
|
|
|
|
StringBuilder columnBuilder = new StringBuilder();
|
|
|
|
|
@ -2119,7 +2151,7 @@ public class TDengineService {
|
|
|
|
|
"LAST(deleted) as deleted, " +
|
|
|
|
|
"LAST(tenant_id) as tenant_id, " +
|
|
|
|
|
"LAST(rule_id) as rule_id " +
|
|
|
|
|
"FROM besure_server.iot_device_operation_record " +
|
|
|
|
|
"FROM " + tdengineTable("iot_device_operation_record") + " " +
|
|
|
|
|
"WHERE deleted = 0 "
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
@ -2220,7 +2252,7 @@ public class TDengineService {
|
|
|
|
|
@DS("tdengine")
|
|
|
|
|
private String getSingleDeviceStatus(Long deviceId) {
|
|
|
|
|
String sql = "SELECT rule " +
|
|
|
|
|
"FROM besure_server.iot_device_operation_record " +
|
|
|
|
|
"FROM " + tdengineTable("iot_device_operation_record") + " " +
|
|
|
|
|
"WHERE device_id = ? " +
|
|
|
|
|
" AND deleted = 0 " +
|
|
|
|
|
" AND rule IN ('1', '2', '3', '4') " +
|
|
|
|
|
@ -2253,7 +2285,7 @@ public class TDengineService {
|
|
|
|
|
.append(" SUM(CASE WHEN rule = '1' THEN 1 ELSE 0 END) * 60 AS totalRunningTime, ")
|
|
|
|
|
.append(" SUM(CASE WHEN rule = '2' THEN 1 ELSE 0 END) * 60 AS totalStandbyTime, ")
|
|
|
|
|
.append(" SUM(CASE WHEN rule = '3' THEN 1 ELSE 0 END) * 60 AS totalFaultTime ")
|
|
|
|
|
.append("FROM besure_server.iot_device_operation_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_operation_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ")
|
|
|
|
|
.append(" AND device_id IN (");
|
|
|
|
|
|
|
|
|
|
@ -2335,7 +2367,7 @@ public class TDengineService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tableName = "besure_server.iot_device_warning_record";
|
|
|
|
|
String tableName = tdengineTable("iot_device_warning_record");
|
|
|
|
|
|
|
|
|
|
StringBuilder columnBuilder = new StringBuilder();
|
|
|
|
|
StringBuilder valueBuilder = new StringBuilder();
|
|
|
|
|
@ -2404,8 +2436,8 @@ public class TDengineService {
|
|
|
|
|
public PageResult<DeviceWarinningRecordDO> selectRunngingRecordPage(DeviceWarinningRecordPageReqVO reqVO) {
|
|
|
|
|
|
|
|
|
|
// 构建查询 SQL
|
|
|
|
|
StringBuilder sql = new StringBuilder("SELECT * FROM besure_server.iot_device_warning_record ");
|
|
|
|
|
StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM besure_server.iot_device_warning_record ");
|
|
|
|
|
StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tdengineTable("iot_device_warning_record")).append(" ");
|
|
|
|
|
StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM ").append(tdengineTable("iot_device_warning_record")).append(" ");
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
List<Object> countParams = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@ -2563,7 +2595,7 @@ public class TDengineService {
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
sql.append("SELECT * FROM besure_server.iot_device_warning_record ")
|
|
|
|
|
sql.append("SELECT * FROM ").append(tdengineTable("iot_device_warning_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ");
|
|
|
|
|
|
|
|
|
|
if (deviceId != null) {
|
|
|
|
|
@ -2649,7 +2681,7 @@ public class TDengineService {
|
|
|
|
|
if (deviceId == null || startTime == null || endTime == null || attributeCodes.isEmpty()) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
String tableName = "besure_server.d_" + deviceId;
|
|
|
|
|
String tableName = tdengineTable("d_" + deviceId);
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
sqlBuilder.append("SELECT ");
|
|
|
|
|
attributeCodes.stream().forEach(item -> {
|
|
|
|
|
@ -2695,7 +2727,7 @@ public class TDengineService {
|
|
|
|
|
try {
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
|
|
sql.append("SELECT _wstart AS day_key, device_id, LAST(capacity_value) AS capacityvalue ")
|
|
|
|
|
.append("FROM besure_server.iot_device_capacity_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_capacity_record")).append(" ")
|
|
|
|
|
.append("WHERE ts >= ? AND ts < ? AND device_id IN (")
|
|
|
|
|
.append(deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")))
|
|
|
|
|
.append(") PARTITION BY device_id INTERVAL(1d)");
|
|
|
|
|
@ -2738,7 +2770,7 @@ public class TDengineService {
|
|
|
|
|
.append("SUM(CASE WHEN rule = '1' THEN 1 ELSE 0 END) * 60 AS totalRunningTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '2' THEN 1 ELSE 0 END) * 60 AS totalStandbyTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '3' THEN 1 ELSE 0 END) * 60 AS totalFaultTime ")
|
|
|
|
|
.append("FROM besure_server.iot_device_operation_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_operation_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ");
|
|
|
|
|
|
|
|
|
|
if (startTime != null && !startTime.trim().isEmpty()) {
|
|
|
|
|
@ -2788,7 +2820,7 @@ public class TDengineService {
|
|
|
|
|
.append("SUM(CASE WHEN rule = '1' THEN 1 ELSE 0 END) * 60 AS totalRunningTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '2' THEN 1 ELSE 0 END) * 60 AS totalStandbyTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '3' THEN 1 ELSE 0 END) * 60 AS totalFaultTime ")
|
|
|
|
|
.append("FROM besure_server.iot_device_operation_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_operation_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ")
|
|
|
|
|
.append("AND device_id = ? ");
|
|
|
|
|
params.add(deviceId);
|
|
|
|
|
@ -2837,7 +2869,7 @@ public class TDengineService {
|
|
|
|
|
.append("SUM(CASE WHEN rule = '1' THEN 1 ELSE 0 END) * 60 AS totalRunningTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '2' THEN 1 ELSE 0 END) * 60 AS totalStandbyTime, ")
|
|
|
|
|
.append("SUM(CASE WHEN rule = '3' THEN 1 ELSE 0 END) * 60 AS totalFaultTime ")
|
|
|
|
|
.append("FROM besure_server.iot_device_operation_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_operation_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ");
|
|
|
|
|
|
|
|
|
|
if (startTime != null && !startTime.trim().isEmpty()) {
|
|
|
|
|
@ -2880,7 +2912,7 @@ public class TDengineService {
|
|
|
|
|
List<Object> params = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
sql.append("SELECT device_id, rule, create_time ")
|
|
|
|
|
.append("FROM besure_server.iot_device_operation_record ")
|
|
|
|
|
.append("FROM ").append(tdengineTable("iot_device_operation_record")).append(" ")
|
|
|
|
|
.append("WHERE deleted = 0 ");
|
|
|
|
|
|
|
|
|
|
sql.append("AND device_id IN (");
|
|
|
|
|
|