Browse Source

提交 sql

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

Loading…
Cancel
Save