using X1.Domain.Common;
using X1.Domain.Repositories.TestCase;
using Microsoft.Extensions.Logging;
using MediatR;
namespace X1.Application.Features.TestCaseFlow.Queries.GetTestCaseFlows;
///
/// 获取测试用例流程列表查询处理器
///
public class GetTestCaseFlowsQueryHandler : IRequestHandler>
{
private readonly ITestCaseFlowRepository _testCaseFlowRepository;
private readonly ILogger _logger;
///
/// 初始化查询处理器
///
public GetTestCaseFlowsQueryHandler(
ITestCaseFlowRepository testCaseFlowRepository,
ILogger logger)
{
_testCaseFlowRepository = testCaseFlowRepository;
_logger = logger;
}
///
/// 处理获取测试用例流程列表查询
///
public async Task> Handle(GetTestCaseFlowsQuery request, CancellationToken cancellationToken)
{
try
{
_logger.LogInformation("开始获取测试用例流程列表,搜索条件: {SearchTerm}, 类型: {Type}, 启用状态: {IsEnabled}, 页码: {PageNumber}, 每页大小: {PageSize}",
request.SearchTerm, request.Type, request.IsEnabled, request.PageNumber, request.PageSize);
// 解析流程类型
X1.Domain.Entities.TestCase.TestFlowType? flowType = null;
if (!string.IsNullOrEmpty(request.Type) && Enum.TryParse(request.Type, out var parsedType))
{
flowType = parsedType;
}
// 获取分页数据
var pagedResult = await _testCaseFlowRepository.GetPagedFlowsAsync(
name: request.SearchTerm,
type: flowType,
isEnabled: request.IsEnabled,
pageNumber: request.PageNumber,
pageSize: request.PageSize,
cancellationToken);
// 构建响应
var response = new GetTestCaseFlowsResponse
{
TestCaseFlows = pagedResult.Items.Select(flow => new TestCaseFlowDto
{
Id = flow.Id,
Name = flow.Name,
Description = flow.Description,
Type = flow.Type.ToString(),
IsEnabled = flow.IsEnabled,
ViewportX = flow.ViewportX,
ViewportY = flow.ViewportY,
ViewportZoom = flow.ViewportZoom,
CreatedAt = flow.CreatedAt,
CreatedBy = flow.CreatedBy,
UpdatedAt = flow.UpdatedAt,
UpdatedBy = flow.UpdatedBy
}).ToList(),
TotalCount = pagedResult.TotalCount,
PageNumber = request.PageNumber,
PageSize = request.PageSize,
TotalPages = (int)Math.Ceiling((double)pagedResult.TotalCount / request.PageSize)
};
_logger.LogInformation("成功获取测试用例流程列表,总数: {TotalCount}, 当前页: {PageNumber}, 每页大小: {PageSize}",
response.TotalCount, response.PageNumber, response.PageSize);
return OperationResult.CreateSuccess(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取测试用例流程列表时发生错误");
return OperationResult.CreateFailure("获取测试用例流程列表失败,请稍后重试");
}
}
}