You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.7 KiB
92 lines
3.7 KiB
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using CellularManagement.Domain.Common;
|
|
using CellularManagement.Domain.Entities;
|
|
using CellularManagement.Domain.Repositories;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
|
|
namespace CellularManagement.Application.Features.Devices.Queries.GetDevices;
|
|
|
|
/// <summary>
|
|
/// 获取设备列表查询处理器
|
|
/// </summary>
|
|
public class GetDevicesQueryHandler : IRequestHandler<GetDevicesQuery, OperationResult<GetDevicesResponse>>
|
|
{
|
|
private readonly IDeviceRepository _deviceRepository;
|
|
private readonly ILogger<GetDevicesQueryHandler> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化查询处理器
|
|
/// </summary>
|
|
public GetDevicesQueryHandler(
|
|
IDeviceRepository deviceRepository,
|
|
ILogger<GetDevicesQueryHandler> logger)
|
|
{
|
|
_deviceRepository = deviceRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理获取设备列表查询
|
|
/// </summary>
|
|
public async Task<OperationResult<GetDevicesResponse>> Handle(GetDevicesQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
// 验证请求参数
|
|
var validationContext = new ValidationContext(request);
|
|
var validationResults = new List<ValidationResult>();
|
|
if (!Validator.TryValidateObject(request, validationContext, validationResults, true))
|
|
{
|
|
var errorMessages = validationResults.Select(r => r.ErrorMessage).ToList();
|
|
_logger.LogWarning("Invalid request parameters: {Errors}", string.Join(", ", errorMessages));
|
|
return OperationResult<GetDevicesResponse>.CreateFailure(errorMessages);
|
|
}
|
|
|
|
_logger.LogInformation("Getting devices with page: {PageNumber}, size: {PageSize}, search: {SearchTerm}, isDisabled: {IsDisabled}",
|
|
request.PageNumber, request.PageSize, request.SearchTerm, request.IsDisabled);
|
|
|
|
// 获取分页数据
|
|
var (totalCount, items) = await _deviceRepository.GetPagedAsync(
|
|
request.PageNumber,
|
|
request.PageSize,
|
|
request.SearchTerm,
|
|
request.IsDisabled,
|
|
cancellationToken);
|
|
|
|
// 构建响应
|
|
var response = new GetDevicesResponse
|
|
{
|
|
TotalCount = totalCount,
|
|
PageNumber = request.PageNumber,
|
|
PageSize = request.PageSize,
|
|
TotalPages = (int)Math.Ceiling(totalCount / (double)request.PageSize),
|
|
HasPreviousPage = request.PageNumber > 1,
|
|
HasNextPage = request.PageNumber < (int)Math.Ceiling(totalCount / (double)request.PageSize),
|
|
Items = items.Select(d => new DeviceDto
|
|
{
|
|
DeviceId = d.DeviceID,
|
|
DeviceName = d.DeviceName,
|
|
ProtocolVersion = d.ProtocolVersion,
|
|
SupportBands = d.SupportBands,
|
|
HardwareVersion = d.HardwareVersion,
|
|
SerialNumber = d.SerialNumber,
|
|
Comment = d.Comment,
|
|
IPAddress = d.IPAddress,
|
|
IsDisabled = d.IsDisabled,
|
|
CreatedAt = d.CreatedAt,
|
|
UpdatedAt = d.UpdatedAt
|
|
}).ToList()
|
|
};
|
|
|
|
_logger.LogInformation("Successfully retrieved {Count} devices", items.Count());
|
|
return OperationResult<GetDevicesResponse>.CreateSuccess(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error getting devices");
|
|
return OperationResult<GetDevicesResponse>.CreateFailure($"获取设备列表时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
}
|