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.

111 lines
4.7 KiB

using MediatR;
using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using CellularManagement.Domain.Entities.Device;
using CellularManagement.Domain.Repositories;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using CellularManagement.Application.Features.TestCaseSets.Queries.GetTestCaseSetById;
using CellularManagement.Domain.Repositories.Device;
namespace CellularManagement.Application.Features.TestCaseSets.Queries.GetTestCaseSets;
/// <summary>
/// 获取用例集列表查询处理器
/// </summary>
public class GetTestCaseSetsQueryHandler : IRequestHandler<GetTestCaseSetsQuery, OperationResult<GetTestCaseSetsResponse>>
{
private readonly ITestCaseSetRepository _testCaseSetRepository;
private readonly ILogger<GetTestCaseSetsQueryHandler> _logger;
/// <summary>
/// 初始化查询处理器
/// </summary>
public GetTestCaseSetsQueryHandler(
ITestCaseSetRepository testCaseSetRepository,
ILogger<GetTestCaseSetsQueryHandler> logger)
{
_testCaseSetRepository = testCaseSetRepository;
_logger = logger;
}
/// <summary>
/// 处理获取用例集列表查询
/// </summary>
public async Task<OperationResult<GetTestCaseSetsResponse>> Handle(GetTestCaseSetsQuery request, CancellationToken cancellationToken)
{
try
{
// 验证请求参数
var validationContext = new ValidationContext(request);
var validationResults = new List<ValidationResult>();
if (!Validator.TryValidateObject(request, validationContext, validationResults, true))
{
var errorMessages = validationResults.Select(r => r.ErrorMessage).ToList();
_logger.LogWarning("请求参数无效: {Errors}", string.Join(", ", errorMessages));
return OperationResult<GetTestCaseSetsResponse>.CreateFailure(errorMessages);
}
_logger.LogInformation("开始获取用例集列表,页码: {PageNumber}, 每页数量: {PageSize}, 搜索关键词: {SearchTerm}, 是否启用: {IsEnabled}, 版本: {Version}",
request.PageNumber, request.PageSize, request.SearchTerm, request.IsEnabled, request.Version);
// 获取用例集数据
var testCaseSets = await _testCaseSetRepository.SearchTestCaseSetsAsync(
request.SearchTerm,
cancellationToken);
// 如果指定了启用状态过滤
if (request.IsEnabled.HasValue)
{
testCaseSets = testCaseSets.Where(tcs => !tcs.IsDisabled == request.IsEnabled.Value).ToList();
}
// 如果指定了版本过滤
if (!string.IsNullOrEmpty(request.Version))
{
testCaseSets = testCaseSets.Where(tcs => tcs.Version == request.Version).ToList();
}
// 计算分页
int totalCount = testCaseSets.Count();
var items = testCaseSets
.Skip((request.PageNumber - 1) * request.PageSize)
.Take(request.PageSize)
.ToList();
// 构建响应
var response = new GetTestCaseSetsResponse
{
TotalCount = totalCount,
PageNumber = request.PageNumber,
PageSize = request.PageSize,
TotalPages = (int)Math.Ceiling(totalCount / (double)request.PageSize),
HasPreviousPage = request.PageNumber > 1,
HasNextPage = request.PageNumber < (int)Math.Ceiling(totalCount / (double)request.PageSize),
Items = items.Select(tcs => new GetTestCaseSetByIdResponse
{
TestCaseSetId = tcs.Id,
Code = tcs.Code,
Name = tcs.Name,
Description = tcs.Description,
Version = tcs.Version,
VersionUpdateInfo = tcs.VersionUpdateInfo,
IsDisabled = tcs.IsDisabled,
Remarks = tcs.Remarks,
CreatedAt = tcs.CreatedAt,
UpdatedAt = tcs.UpdatedAt,
CreatedBy = tcs.CreatedBy,
UpdatedBy = tcs.UpdatedBy
}).ToList()
};
_logger.LogInformation("成功获取用例集列表,共 {Count} 条记录", items.Count);
return OperationResult<GetTestCaseSetsResponse>.CreateSuccess(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "获取用例集列表时发生错误");
return OperationResult<GetTestCaseSetsResponse>.CreateFailure($"获取用例集列表时发生错误: {ex.Message}");
}
}
}