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.

111 lines
4.7 KiB

using MediatR;
using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using CellularManagement.Domain.Entities.Device;
using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Repositories.Device;
using CellularManagement.Domain.Repositories.Base;
using CellularManagement.Domain.Services;
namespace CellularManagement.Application.Features.Devices.Commands.CreateDevice;
/// <summary>
/// 创建设备命令处理器
/// </summary>
public class CreateDeviceCommandHandler : IRequestHandler<CreateDeviceCommand, OperationResult<CreateDeviceResponse>>
{
private readonly ICellularDeviceRepository _deviceRepository;
private readonly IProtocolVersionRepository _protocolVersionRepository;
private readonly ILogger<CreateDeviceCommandHandler> _logger;
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// 初始化命令处理器
/// </summary>
public CreateDeviceCommandHandler(
ICellularDeviceRepository deviceRepository,
IProtocolVersionRepository protocolVersionRepository,
ILogger<CreateDeviceCommandHandler> logger,
IUnitOfWork unitOfWork,
ICurrentUserService currentUserService)
{
_deviceRepository = deviceRepository;
_protocolVersionRepository = protocolVersionRepository;
_logger = logger;
_unitOfWork = unitOfWork;
_currentUserService = currentUserService;
}
/// <summary>
/// 处理创建设备命令
/// </summary>
public async Task<OperationResult<CreateDeviceResponse>> Handle(CreateDeviceCommand request, CancellationToken cancellationToken)
{
try
{
_logger.LogInformation("开始创建设备,设备名称: {DeviceName}, 序列号: {SerialNumber}",
request.DeviceName, request.SerialNumber);
// 检查序列号是否已存在
if (await _deviceRepository.SerialNumberExistsAsync(request.SerialNumber, cancellationToken))
{
_logger.LogWarning("设备序列号已存在: {SerialNumber}", request.SerialNumber);
return OperationResult<CreateDeviceResponse>.CreateFailure($"设备序列号 {request.SerialNumber} 已存在");
}
// 获取当前用户ID
var currentUserId = _currentUserService.GetCurrentUserId();
if (string.IsNullOrEmpty(currentUserId))
{
_logger.LogError("无法获取当前用户ID,用户可能未认证");
return OperationResult<CreateDeviceResponse>.CreateFailure("用户未认证,无法创建设备");
}
// 创建设备实体
var device = CellularDevice.Create(
name: request.DeviceName,
serialNumber: request.SerialNumber,
description: request.Description,
protocolVersionId: request.ProtocolVersionId,
agentPort: request.AgentPort,
ipAddress: request.IpAddress,
createdBy: currentUserId,
isEnabled: request.IsEnabled,
isRunning: request.IsRunning);
// 保存设备
await _deviceRepository.AddDeviceAsync(device, cancellationToken);
// 保存更改到数据库
await _unitOfWork.SaveChangesAsync(cancellationToken);
// 获取协议版本信息
var protocolVersion = await _protocolVersionRepository.GetProtocolVersionByIdAsync(device.ProtocolVersionId, cancellationToken);
// 构建响应
var response = new CreateDeviceResponse
{
DeviceId = device.Id,
DeviceName = device.Name,
SerialNumber = device.SerialNumber,
Description = device.Description,
ProtocolVersion = protocolVersion?.Version ?? "未知",
AgentPort = device.AgentPort,
IsEnabled = device.IsEnabled,
IsRunning = device.IsRunning,
CreatedAt = device.CreatedAt
};
_logger.LogInformation("设备创建成功,设备ID: {DeviceId}, 设备名称: {DeviceName}, 序列号: {SerialNumber}",
device.Id, device.Name, device.SerialNumber);
return OperationResult<CreateDeviceResponse>.CreateSuccess(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "创建设备时发生错误,设备名称: {DeviceName}, 序列号: {SerialNumber}",
request.DeviceName, request.SerialNumber);
return OperationResult<CreateDeviceResponse>.CreateFailure($"创建设备时发生错误: {ex.Message}");
}
}
}