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.

103 lines
4.4 KiB

using MediatR;
using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using CellularManagement.Domain.Entities.Device;
using CellularManagement.Domain.Repositories.Device;
using CellularManagement.Domain.Repositories.Base;
using CellularManagement.Domain.Services;
namespace CellularManagement.Application.Features.TestCaseSets.Commands.DisableTestCaseSet;
/// <summary>
/// 禁用用例集命令处理器
/// </summary>
public class DisableTestCaseSetCommandHandler : IRequestHandler<DisableTestCaseSetCommand, OperationResult<bool>>
{
private readonly ITestCaseSetRepository _testCaseSetRepository;
private readonly ILogger<DisableTestCaseSetCommandHandler> _logger;
private readonly IUnitOfWork _unitOfWork;
private readonly ICurrentUserService _currentUserService;
/// <summary>
/// 初始化命令处理器
/// </summary>
public DisableTestCaseSetCommandHandler(
ITestCaseSetRepository testCaseSetRepository,
ILogger<DisableTestCaseSetCommandHandler> logger,
IUnitOfWork unitOfWork,
ICurrentUserService currentUserService)
{
_testCaseSetRepository = testCaseSetRepository;
_logger = logger;
_unitOfWork = unitOfWork;
_currentUserService = currentUserService;
}
/// <summary>
/// 处理禁用用例集命令
/// </summary>
public async Task<OperationResult<bool>> Handle(DisableTestCaseSetCommand request, CancellationToken cancellationToken)
{
try
{
_logger.LogInformation("开始禁用用例集,用例集ID: {TestCaseSetId}", request.TestCaseSetId);
// 检查用例集是否存在
var existingTestCaseSet = await _testCaseSetRepository.GetTestCaseSetByIdAsync(request.TestCaseSetId, cancellationToken);
if (existingTestCaseSet == null)
{
_logger.LogWarning("用例集不存在: {TestCaseSetId}", request.TestCaseSetId);
return OperationResult<bool>.CreateFailure($"用例集ID {request.TestCaseSetId} 不存在");
}
// 检查用例集是否已被软删除
if (existingTestCaseSet.IsDeleted)
{
_logger.LogWarning("用例集已被删除: {TestCaseSetId}", request.TestCaseSetId);
return OperationResult<bool>.CreateFailure($"用例集ID {request.TestCaseSetId} 已被删除");
}
// 检查用例集是否已经禁用
if (existingTestCaseSet.IsDisabled)
{
_logger.LogWarning("用例集已经禁用: {TestCaseSetId}", request.TestCaseSetId);
return OperationResult<bool>.CreateFailure($"用例集ID {request.TestCaseSetId} 已经禁用");
}
// 获取当前用户ID
var currentUserId = _currentUserService.GetCurrentUserId();
if (string.IsNullOrEmpty(currentUserId))
{
_logger.LogError("无法获取当前用户ID,用户可能未认证");
return OperationResult<bool>.CreateFailure("用户未认证,无法禁用用例集");
}
// 禁用用例集
existingTestCaseSet.Disable();
existingTestCaseSet.Update(
code: existingTestCaseSet.Code,
name: existingTestCaseSet.Name,
version: existingTestCaseSet.Version,
updatedBy: currentUserId,
description: existingTestCaseSet.Description,
versionUpdateInfo: existingTestCaseSet.VersionUpdateInfo,
isDisabled: true,
remarks: existingTestCaseSet.Remarks);
// 保存更改
_testCaseSetRepository.UpdateTestCaseSet(existingTestCaseSet);
// 保存更改到数据库
await _unitOfWork.SaveChangesAsync(cancellationToken);
_logger.LogInformation("用例集禁用成功,用例集ID: {TestCaseSetId}, 用例集编码: {Code}, 用例集名称: {Name}, 版本: {Version}",
existingTestCaseSet.Id, existingTestCaseSet.Code, existingTestCaseSet.Name, existingTestCaseSet.Version);
return OperationResult<bool>.CreateSuccess(true);
}
catch (Exception ex)
{
_logger.LogError(ex, "禁用用例集时发生错误,用例集ID: {TestCaseSetId}", request.TestCaseSetId);
return OperationResult<bool>.CreateFailure($"禁用用例集时发生错误: {ex.Message}");
}
}
}