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; /// /// 禁用用例集命令处理器 /// public class DisableTestCaseSetCommandHandler : IRequestHandler> { private readonly ITestCaseSetRepository _testCaseSetRepository; private readonly ILogger _logger; private readonly IUnitOfWork _unitOfWork; private readonly ICurrentUserService _currentUserService; /// /// 初始化命令处理器 /// public DisableTestCaseSetCommandHandler( ITestCaseSetRepository testCaseSetRepository, ILogger logger, IUnitOfWork unitOfWork, ICurrentUserService currentUserService) { _testCaseSetRepository = testCaseSetRepository; _logger = logger; _unitOfWork = unitOfWork; _currentUserService = currentUserService; } /// /// 处理禁用用例集命令 /// public async Task> 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.CreateFailure($"用例集ID {request.TestCaseSetId} 不存在"); } // 检查用例集是否已被软删除 if (existingTestCaseSet.IsDeleted) { _logger.LogWarning("用例集已被删除: {TestCaseSetId}", request.TestCaseSetId); return OperationResult.CreateFailure($"用例集ID {request.TestCaseSetId} 已被删除"); } // 检查用例集是否已经禁用 if (existingTestCaseSet.IsDisabled) { _logger.LogWarning("用例集已经禁用: {TestCaseSetId}", request.TestCaseSetId); return OperationResult.CreateFailure($"用例集ID {request.TestCaseSetId} 已经禁用"); } // 获取当前用户ID var currentUserId = _currentUserService.GetCurrentUserId(); if (string.IsNullOrEmpty(currentUserId)) { _logger.LogError("无法获取当前用户ID,用户可能未认证"); return OperationResult.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.CreateSuccess(true); } catch (Exception ex) { _logger.LogError(ex, "禁用用例集时发生错误,用例集ID: {TestCaseSetId}", request.TestCaseSetId); return OperationResult.CreateFailure($"禁用用例集时发生错误: {ex.Message}"); } } }