using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using CellularManagement.Domain.Entities.Logging; using CellularManagement.Domain.Entities.Device; using CellularManagement.Domain.Repositories.Logging; using CellularManagement.Domain.Repositories.Base; using CellularManagement.Infrastructure.Repositories.Base; using System.Linq; using X1.Domain.Entities.Logging; namespace CellularManagement.Infrastructure.Repositories.Logging; /// /// 协议日志仓储实现类 /// public class ProtocolLogRepository : BaseRepository, IProtocolLogRepository { private readonly ILogger _logger; /// /// 构造函数 /// /// 命令仓储 /// 查询仓储 /// 日志记录器 public ProtocolLogRepository( ICommandRepository commandRepository, IQueryRepository queryRepository, ILogger logger) : base(commandRepository, queryRepository, logger) { _logger = logger; } /// /// 根据设备代码获取协议日志 /// /// 设备代码 /// 取消令牌 /// 协议日志列表 public async Task> GetByDeviceCodeAsync(string deviceCode, CancellationToken cancellationToken = default) { try { var logs = await QueryRepository.FindAsync( p => p.DeviceCode == deviceCode, cancellationToken: cancellationToken); return logs.OrderByDescending(p => p.Timestamp); } catch (Exception ex) { _logger.LogError(ex, "获取设备代码 {DeviceCode} 的协议日志时发生错误", deviceCode); throw; } } /// /// 根据运行时代码获取协议日志 /// /// 运行时代码 /// 取消令牌 /// 协议日志列表 public async Task> GetByRuntimeCodeAsync(string runtimeCode, CancellationToken cancellationToken = default) { try { var logs = await QueryRepository.FindAsync( p => p.RuntimeCode == runtimeCode, cancellationToken: cancellationToken); return logs.OrderByDescending(p => p.Timestamp); } catch (Exception ex) { _logger.LogError(ex, "获取运行时代码 {RuntimeCode} 的协议日志时发生错误", runtimeCode); throw; } } /// /// 根据设备代码和运行时代码获取协议日志 /// /// 设备代码 /// 运行时代码 /// 取消令牌 /// 协议日志列表 public async Task> GetByDeviceAndRuntimeCodeAsync(string deviceCode, string runtimeCode, CancellationToken cancellationToken = default) { try { var logs = await QueryRepository.FindAsync( p => p.DeviceCode == deviceCode && p.RuntimeCode == runtimeCode, cancellationToken: cancellationToken); return logs.OrderByDescending(p => p.Timestamp); } catch (Exception ex) { _logger.LogError(ex, "获取设备代码 {DeviceCode} 和运行时代码 {RuntimeCode} 的协议日志时发生错误", deviceCode, runtimeCode); throw; } } /// /// 根据时间范围获取协议日志 /// /// 开始时间戳 /// 结束时间戳 /// 取消令牌 /// 协议日志列表 public async Task> GetByTimeRangeAsync(long startTimestamp, long endTimestamp, CancellationToken cancellationToken = default) { try { var logs = await QueryRepository.FindAsync( p => p.Timestamp >= startTimestamp && p.Timestamp <= endTimestamp, cancellationToken: cancellationToken); return logs.OrderByDescending(p => p.Timestamp); } catch (Exception ex) { _logger.LogError(ex, "获取时间范围 {StartTimestamp} 到 {EndTimestamp} 的协议日志时发生错误", startTimestamp, endTimestamp); throw; } } /// /// 根据协议层类型获取协议日志 /// /// 协议层类型 /// 取消令牌 /// 协议日志列表 public async Task> GetByLayerTypeAsync(ProtocolLayer layerType, CancellationToken cancellationToken = default) { try { var logs = await QueryRepository.FindAsync( p => p.LayerType == layerType, cancellationToken: cancellationToken); return logs.OrderByDescending(p => p.Timestamp); } catch (Exception ex) { _logger.LogError(ex, "获取协议层类型 {LayerType} 的协议日志时发生错误", layerType); throw; } } /// /// 根据设备代码和运行时状态获取协议日志(高性能查询) /// /// 设备代码 /// 运行时代码集合 /// 开始时间戳 /// 结束时间戳 /// 协议层类型 /// 是否按时间戳降序排序 /// 取消令牌 /// 协议日志列表 public async Task> GetByDeviceWithFiltersAsync( string deviceCode, IEnumerable? runtimeCodes = null, long? startTimestamp = null, long? endTimestamp = null, string? layerType = null, bool orderByDescending = true, CancellationToken cancellationToken = default) { try { // 构建 SQL 查询 var sql = @" SELECT pl.* FROM ""ProtocolLogs"" pl INNER JOIN ""CellularDeviceRuntimes"" cdr ON pl.""DeviceCode"" = cdr.""DeviceCode"" AND pl.""RuntimeCode"" = cdr.""RuntimeCode"" WHERE pl.""DeviceCode"" = @deviceCode"; var parameters = new List { deviceCode }; // 添加运行时编码过滤 if (runtimeCodes != null && runtimeCodes.Any()) { var runtimeCodeList = string.Join(",", runtimeCodes.Select((_, i) => $"@runtimeCode{i}")); sql += $" AND pl.\"RuntimeCode\" IN ({runtimeCodeList})"; parameters.AddRange(runtimeCodes); } // 添加时间范围过滤 if (startTimestamp.HasValue) { sql += " AND pl.\"Timestamp\" >= @startTimestamp"; parameters.Add(startTimestamp.Value); } if (endTimestamp.HasValue) { sql += " AND pl.\"Timestamp\" <= @endTimestamp"; parameters.Add(endTimestamp.Value); } // 添加协议层类型过滤 if (!string.IsNullOrEmpty(layerType)) { sql += " AND pl.\"LayerType\" = @layerType"; parameters.Add(layerType); } // 添加排序 sql += orderByDescending ? " ORDER BY pl.\"Timestamp\" DESC" : " ORDER BY pl.\"Timestamp\" ASC"; // 执行 SQL 查询 var logs = await QueryRepository.ExecuteSqlQueryAsync(sql, parameters.ToArray(), cancellationToken); return logs; } catch (Exception ex) { _logger.LogError(ex, "获取设备代码 {DeviceCode} 的协议日志时发生错误", deviceCode); throw; } } }