Browse Source

提交 sql

feature/x1-web-request
root 1 day ago
parent
commit
c849e86c87
  1. 59
      src/X1.Infrastructure/Repositories/Logging/ProtocolLogRepository.cs

59
src/X1.Infrastructure/Repositories/Logging/ProtocolLogRepository.cs

@ -176,7 +176,7 @@ public class ProtocolLogRepository : BaseRepository<ProtocolLog>, IProtocolLogRe
{ {
try try
{ {
// 构建 SQL 查询 - 排除 MessageDetailJson 字段以提升性能 // 构建 SQL 查询 - 先过滤运行时状态,再进行JOIN
var sql = @" var sql = @"
SELECT SELECT
pl.""Id"", pl.""Id"",
@ -194,41 +194,56 @@ public class ProtocolLogRepository : BaseRepository<ProtocolLog>, IProtocolLogRe
pl.""DeviceCode"", pl.""DeviceCode"",
pl.""RuntimeCode"" pl.""RuntimeCode""
FROM ""tb_protocol_logs"" pl FROM ""tb_protocol_logs"" pl
INNER JOIN ""tb_cellular_device_runtimes"" cdr INNER JOIN (
ON pl.""DeviceCode"" = cdr.""DeviceCode"""; SELECT DISTINCT cdr.""RuntimeCode""
FROM ""tb_cellular_device_runtimes"" cdr
INNER JOIN ""tb_cellular_device_runtime_details"" cdr_detail
ON cdr.""RuntimeCode"" = cdr_detail.""RuntimeCode""
WHERE 1=1";
var parameters = new List<object>(); var parameters = new List<object>();
var paramIndex = 0; var paramIndex = 0;
// 添加设备代码过滤(如果提供) // 添加设备代码过滤到子查询
if (!string.IsNullOrEmpty(deviceCode)) if (!string.IsNullOrEmpty(deviceCode))
{ {
sql += $" WHERE pl.\"DeviceCode\" = {{{paramIndex}}}"; sql += $" AND cdr.\"DeviceCode\" = {{{paramIndex}}}";
parameters.Add(deviceCode); parameters.Add(deviceCode);
paramIndex++; paramIndex++;
} }
else
{
sql += " WHERE 1=1"; // 如果没有设备代码,使用占位符
}
// 添加运行时状态过滤 // 添加运行时状态过滤到子查询
if (runtimeStatuses != null && runtimeStatuses.Any()) if (runtimeStatuses != null && runtimeStatuses.Any())
{ {
// 支持多个运行时状态过滤
var statusList = string.Join(" OR ", runtimeStatuses.Select(status => $"cdr.\"RuntimeStatus\" = {status}")); var statusList = string.Join(" OR ", runtimeStatuses.Select(status => $"cdr.\"RuntimeStatus\" = {status}"));
sql += $" AND ({statusList})"; sql += $" AND ({statusList})";
} }
// 添加运行时编码过滤 // 添加运行时编码过滤到子查询
if (runtimeCodes != null && runtimeCodes.Any()) if (runtimeCodes != null && runtimeCodes.Any())
{ {
var runtimeCodeList = string.Join(",", runtimeCodes.Select((_, i) => $"{{{paramIndex + i}}}")); var runtimeCodeList = string.Join(",", runtimeCodes.Select((_, i) => $"{{{paramIndex + i}}}"));
sql += $" AND pl.\"RuntimeCode\" IN ({runtimeCodeList})"; sql += $" AND cdr.\"RuntimeCode\" IN ({runtimeCodeList})";
parameters.AddRange(runtimeCodes); parameters.AddRange(runtimeCodes);
paramIndex += runtimeCodes.Count(); paramIndex += runtimeCodes.Count();
} }
// 关闭子查询
sql += @"
) filtered_runtimes ON pl.""RuntimeCode"" = filtered_runtimes.""RuntimeCode""";
// 添加主查询的过滤条件
if (!string.IsNullOrEmpty(deviceCode))
{
sql += $" WHERE pl.\"DeviceCode\" = {{{paramIndex}}}";
parameters.Add(deviceCode);
paramIndex++;
}
else
{
sql += " WHERE 1=1";
}
// 添加时间范围过滤 // 添加时间范围过滤
if (startTimestamp.HasValue) if (startTimestamp.HasValue)
{ {
@ -255,15 +270,15 @@ public class ProtocolLogRepository : BaseRepository<ProtocolLog>, IProtocolLogRe
} }
} }
// 添加排序 - 只按Timestamp排序 // 添加排序
//if (orderByDescending) if (orderByDescending)
//{ {
// sql += " ORDER BY pl.\"Timestamp\" DESC"; sql += " ORDER BY pl.\"Timestamp\" DESC";
//} }
//else else
//{ {
// sql += " ORDER BY pl.\"Timestamp\" ASC"; sql += " ORDER BY pl.\"Timestamp\" ASC";
//} }
// 执行 SQL 查询,直接映射到ProtocolLogListDto // 执行 SQL 查询,直接映射到ProtocolLogListDto
var logs = await QueryRepository.ExecuteSqlQueryAsync<ProtocolLogListDto>(sql, parameters.ToArray(), cancellationToken); var logs = await QueryRepository.ExecuteSqlQueryAsync<ProtocolLogListDto>(sql, parameters.ToArray(), cancellationToken);

Loading…
Cancel
Save