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,
IEnumerable? layerTypes = null,
IEnumerable? runtimeStatuses = null,
bool orderByDescending = true,
CancellationToken cancellationToken = default)
{
try
{
// 构建 SQL 查询
var sql = @"
SELECT pl.*
FROM ""tb_protocol_logs"" pl
INNER JOIN ""tb_cellular_device_runtimes"" cdr
ON pl.""DeviceCode"" = cdr.""DeviceCode""";
var parameters = new List