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.
69 lines
2.5 KiB
69 lines
2.5 KiB
using CellularManagement.Domain.Common;
|
|
using CellularManagement.Domain.Entities.Device;
|
|
using CellularManagement.Domain.Repositories;
|
|
using CellularManagement.Domain.Repositories.Device;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CellularManagement.Application.Features.Devices.Queries.GetDeviceById;
|
|
|
|
/// <summary>
|
|
/// 根据ID获取设备查询处理器
|
|
/// </summary>
|
|
public class GetDeviceByIdQueryHandler : IRequestHandler<GetDeviceByIdQuery, OperationResult<GetDeviceByIdResponse>>
|
|
{
|
|
private readonly ICellularDeviceRepository _deviceRepository;
|
|
private readonly ILogger<GetDeviceByIdQueryHandler> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化查询处理器
|
|
/// </summary>
|
|
public GetDeviceByIdQueryHandler(
|
|
ICellularDeviceRepository deviceRepository,
|
|
ILogger<GetDeviceByIdQueryHandler> logger)
|
|
{
|
|
_deviceRepository = deviceRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理查询请求
|
|
/// </summary>
|
|
public async Task<OperationResult<GetDeviceByIdResponse>> Handle(GetDeviceByIdQuery request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("开始查询设备,设备ID: {DeviceId}", request.DeviceId);
|
|
|
|
var device = await _deviceRepository.GetDeviceByIdAsync(request.DeviceId, cancellationToken);
|
|
|
|
if (device == null)
|
|
{
|
|
_logger.LogWarning("未找到设备,设备ID: {DeviceId}", request.DeviceId);
|
|
return OperationResult<GetDeviceByIdResponse>.CreateFailure($"未找到ID为 {request.DeviceId} 的设备");
|
|
}
|
|
|
|
var response = new GetDeviceByIdResponse
|
|
{
|
|
DeviceId = device.Id,
|
|
DeviceName = device.Name,
|
|
DeviceCode = device.DeviceCode,
|
|
Description = device.Description,
|
|
AgentPort = device.AgentPort,
|
|
IsEnabled = device.IsEnabled,
|
|
IsRunning = device.IsRunning,
|
|
CreatedAt = device.CreatedAt
|
|
};
|
|
|
|
_logger.LogInformation("成功查询到设备,设备ID: {DeviceId}", request.DeviceId);
|
|
return OperationResult<GetDeviceByIdResponse>.CreateSuccess(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "查询设备时发生错误,设备ID: {DeviceId}", request.DeviceId);
|
|
return OperationResult<GetDeviceByIdResponse>.CreateFailure($"查询设备时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
|
|
}
|