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.
86 lines
3.5 KiB
86 lines
3.5 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;
|
|
|
|
namespace CellularManagement.Application.Features.Devices.Commands.CreateDevice;
|
|
|
|
/// <summary>
|
|
/// 创建设备命令处理器
|
|
/// </summary>
|
|
public class CreateDeviceCommandHandler : IRequestHandler<CreateDeviceCommand, OperationResult<CreateDeviceResponse>>
|
|
{
|
|
private readonly ICellularDeviceRepository _deviceRepository;
|
|
private readonly ILogger<CreateDeviceCommandHandler> _logger;
|
|
|
|
/// <summary>
|
|
/// 初始化命令处理器
|
|
/// </summary>
|
|
public CreateDeviceCommandHandler(
|
|
ICellularDeviceRepository deviceRepository,
|
|
ILogger<CreateDeviceCommandHandler> logger)
|
|
{
|
|
_deviceRepository = deviceRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <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} 已存在");
|
|
}
|
|
|
|
// 创建设备实体
|
|
var device = CellularDevice.Create(
|
|
name: request.DeviceName,
|
|
serialNumber: request.SerialNumber,
|
|
description: request.Comment,
|
|
deviceTypeId: request.DeviceTypeId,
|
|
statusId: request.StatusId,
|
|
protocolVersionId: request.ProtocolVersionId,
|
|
firmwareVersion: request.HardwareVersion);
|
|
|
|
// 保存设备
|
|
await _deviceRepository.AddDeviceAsync(device, cancellationToken);
|
|
|
|
// 加载导航属性
|
|
//await _deviceRepository.LoadNavigationPropertiesAsync(device, cancellationToken);
|
|
|
|
// 构建响应
|
|
var response = new CreateDeviceResponse
|
|
{
|
|
DeviceId = device.Id,
|
|
DeviceName = device.Name,
|
|
SerialNumber = device.SerialNumber,
|
|
DeviceType = device.DeviceType?.Name ?? "未知",
|
|
Status = device.Status?.Name ?? "未知",
|
|
ProtocolVersion = device.ProtocolVersion?.Version ?? "未知",
|
|
FirmwareVersion = device.FirmwareVersion,
|
|
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}");
|
|
}
|
|
}
|
|
}
|