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.
95 lines
4.1 KiB
95 lines
4.1 KiB
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using CellularManagement.Domain.Common;
|
|
using CellularManagement.Domain.Repositories.NetworkProfile;
|
|
using CellularManagement.Domain.Repositories.Base;
|
|
using CellularManagement.Domain.Services;
|
|
using CellularManagement.Domain.Entities.NetworkProfile;
|
|
|
|
namespace CellularManagement.Application.Features.IMSConfiguration.Commands.CreateIMS_Configuration;
|
|
|
|
/// <summary>
|
|
/// 创建IMS配置命令处理器
|
|
/// </summary>
|
|
public class CreateIMS_ConfigurationCommandHandler : IRequestHandler<CreateIMS_ConfigurationCommand, OperationResult<CreateIMS_ConfigurationResponse>>
|
|
{
|
|
private readonly IIMS_ConfigurationRepository _imsConfigurationRepository;
|
|
private readonly ILogger<CreateIMS_ConfigurationCommandHandler> _logger;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
/// <summary>
|
|
/// 初始化命令处理器
|
|
/// </summary>
|
|
public CreateIMS_ConfigurationCommandHandler(
|
|
IIMS_ConfigurationRepository imsConfigurationRepository,
|
|
ILogger<CreateIMS_ConfigurationCommandHandler> logger,
|
|
IUnitOfWork unitOfWork,
|
|
ICurrentUserService currentUserService)
|
|
{
|
|
_imsConfigurationRepository = imsConfigurationRepository;
|
|
_logger = logger;
|
|
_unitOfWork = unitOfWork;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理创建IMS配置命令
|
|
/// </summary>
|
|
public async Task<OperationResult<CreateIMS_ConfigurationResponse>> Handle(CreateIMS_ConfigurationCommand request, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("开始创建IMS配置,配置名称: {Name}", request.Name);
|
|
|
|
// 检查配置名称是否已存在
|
|
if (await _imsConfigurationRepository.NameExistsAsync(request.Name, cancellationToken))
|
|
{
|
|
_logger.LogWarning("IMS配置名称已存在: {Name}", request.Name);
|
|
return OperationResult<CreateIMS_ConfigurationResponse>.CreateFailure($"IMS配置名称 {request.Name} 已存在");
|
|
}
|
|
|
|
// 获取当前用户ID
|
|
var currentUserId = _currentUserService.GetCurrentUserId();
|
|
if (string.IsNullOrEmpty(currentUserId))
|
|
{
|
|
_logger.LogError("无法获取当前用户ID,用户可能未认证");
|
|
return OperationResult<CreateIMS_ConfigurationResponse>.CreateFailure("用户未认证,无法创建IMS配置");
|
|
}
|
|
|
|
// 创建IMS配置实体
|
|
var imsConfiguration = IMS_Configuration.Create(
|
|
name: request.Name,
|
|
configContent: request.ConfigContent,
|
|
createdBy: currentUserId,
|
|
description: request.Description,
|
|
isDisabled: request.IsDisabled);
|
|
|
|
// 保存IMS配置
|
|
await _imsConfigurationRepository.AddIMS_ConfigurationAsync(imsConfiguration, cancellationToken);
|
|
|
|
// 保存更改到数据库
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
|
|
// 构建响应
|
|
var response = new CreateIMS_ConfigurationResponse
|
|
{
|
|
IMS_ConfigurationId = imsConfiguration.Id,
|
|
Name = imsConfiguration.Name,
|
|
ConfigContent = imsConfiguration.ConfigContent,
|
|
Description = imsConfiguration.Description,
|
|
IsDisabled = imsConfiguration.IsDisabled,
|
|
CreatedAt = imsConfiguration.CreatedAt
|
|
};
|
|
|
|
_logger.LogInformation("IMS配置创建成功,配置ID: {IMS_ConfigurationId}, 配置名称: {Name}",
|
|
imsConfiguration.Id, imsConfiguration.Name);
|
|
return OperationResult<CreateIMS_ConfigurationResponse>.CreateSuccess(response);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "创建IMS配置时发生错误,配置名称: {Name}", request.Name);
|
|
return OperationResult<CreateIMS_ConfigurationResponse>.CreateFailure($"创建IMS配置时发生错误: {ex.Message}");
|
|
}
|
|
}
|
|
}
|