|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using X1.Domain.Entities;
|
|
|
|
|
using X1.Domain.Entities.Enums;
|
|
|
|
|
using X1.Domain.Repositories.Identity;
|
|
|
|
|
using X1.Domain.Common;
|
|
|
|
|
|
|
|
|
|
namespace X1.Application.Features.Permissions.Queries;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有权限查询处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class GetAllPermissionsQueryHandler : IRequestHandler<GetAllPermissionsQuery, OperationResult<GetAllPermissionsResponse>>
|
|
|
|
|
{
|
|
|
|
|
private readonly IPermissionRepository _permissionRepository;
|
|
|
|
|
private readonly ILogger<GetAllPermissionsQueryHandler> _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GetAllPermissionsQueryHandler(
|
|
|
|
|
IPermissionRepository permissionRepository,
|
|
|
|
|
ILogger<GetAllPermissionsQueryHandler> logger)
|
|
|
|
|
{
|
|
|
|
|
_permissionRepository = permissionRepository;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 处理获取所有权限请求(分页+筛选)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<OperationResult<GetAllPermissionsResponse>> Handle(
|
|
|
|
|
GetAllPermissionsQuery request,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await _permissionRepository.GetPagedAsync(
|
|
|
|
|
pageNumber: request.PageNumber,
|
|
|
|
|
pageSize: request.PageSize,
|
|
|
|
|
searchTerm: request.Keyword,
|
|
|
|
|
isEnabled: request.IsEnabled,
|
|
|
|
|
isSystem: request.IsSystem,
|
|
|
|
|
cancellationToken: cancellationToken);
|
|
|
|
|
|
|
|
|
|
var permissionDtos = result.Items.Select(permission => new PermissionDto(
|
|
|
|
|
permission.Id,
|
|
|
|
|
permission.Name,
|
|
|
|
|
permission.Code,
|
|
|
|
|
permission.Description,
|
|
|
|
|
PermissionType.Resource, // 默认类型
|
|
|
|
|
PermissionLevel.View, // 默认级别
|
|
|
|
|
null, // ResourceType - 从 Code 解析
|
|
|
|
|
null, // ActionType - 从 Code 解析
|
|
|
|
|
permission.IsEnabled,
|
|
|
|
|
permission.IsSystem,
|
|
|
|
|
0, // SortOrder - 不需要
|
|
|
|
|
DateTime.UtcNow, // CreatedAt - 不需要
|
|
|
|
|
DateTime.UtcNow // UpdatedAt - 不需要
|
|
|
|
|
)).ToList();
|
|
|
|
|
|
|
|
|
|
var response = new GetAllPermissionsResponse(
|
|
|
|
|
permissionDtos,
|
|
|
|
|
result.TotalCount,
|
|
|
|
|
request.PageNumber,
|
|
|
|
|
request.PageSize
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("成功获取权限列表,共 {Count} 个", result.TotalCount);
|
|
|
|
|
|
|
|
|
|
return OperationResult<GetAllPermissionsResponse>.CreateSuccess(response);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "获取权限列表失败");
|
|
|
|
|
return OperationResult<GetAllPermissionsResponse>.CreateFailure("获取权限列表失败,请稍后重试");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|