|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using CellularManagement.Domain.Common;
|
|
|
|
|
using CellularManagement.Domain.Repositories.Device;
|
|
|
|
|
using CellularManagement.Domain.Repositories.Terminal;
|
|
|
|
|
|
|
|
|
|
namespace CellularManagement.Application.Features.TerminalDevices.Queries.GetTerminalDeviceById;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据ID获取终端设备查询处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class GetTerminalDeviceByIdQueryHandler : IRequestHandler<GetTerminalDeviceByIdQuery, OperationResult<GetTerminalDeviceByIdResponse>>
|
|
|
|
|
{
|
|
|
|
|
private readonly ITerminalDeviceRepository _deviceRepository;
|
|
|
|
|
private readonly ILogger<GetTerminalDeviceByIdQueryHandler> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化查询处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GetTerminalDeviceByIdQueryHandler(
|
|
|
|
|
ITerminalDeviceRepository deviceRepository,
|
|
|
|
|
ILogger<GetTerminalDeviceByIdQueryHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_deviceRepository = deviceRepository;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理根据ID获取终端设备查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<OperationResult<GetTerminalDeviceByIdResponse>> Handle(GetTerminalDeviceByIdQuery 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<GetTerminalDeviceByIdResponse>.CreateFailure("终端设备不存在");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建响应
|
|
|
|
|
var response = new GetTerminalDeviceByIdResponse
|
|
|
|
|
{
|
|
|
|
|
Id = device.Id,
|
|
|
|
|
Name = device.Name,
|
|
|
|
|
DeviceCode = device.DeviceCode,
|
|
|
|
|
Description = device.Description,
|
|
|
|
|
AgentPort = device.AgentPort,
|
|
|
|
|
IpAddress = device.IpAddress,
|
|
|
|
|
IsEnabled = device.IsEnabled,
|
|
|
|
|
DeviceType = device.DeviceType.ToString(),
|
|
|
|
|
CreatedAt = device.CreatedAt,
|
|
|
|
|
UpdatedAt = device.UpdatedAt,
|
|
|
|
|
CreatedBy = device.CreatedBy,
|
|
|
|
|
UpdatedBy = device.UpdatedBy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("获取终端设备详情成功,设备ID: {DeviceId}, 设备名称: {DeviceName}",
|
|
|
|
|
device.Id, device.Name);
|
|
|
|
|
|
|
|
|
|
return OperationResult<GetTerminalDeviceByIdResponse>.CreateSuccess(response);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "获取终端设备详情时发生错误,设备ID: {DeviceId}", request.DeviceId);
|
|
|
|
|
return OperationResult<GetTerminalDeviceByIdResponse>.CreateFailure("获取终端设备详情时发生错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|