using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using MediatR; using X1.Application.Features.Permissions.Commands.CreatePermission; using X1.Application.Features.Permissions.Commands.UpdatePermission; using X1.Application.Features.Permissions.Commands.DeletePermission; using X1.Application.Features.Permissions.Commands.BatchCreatePermissions; using X1.Application.Features.Permissions.Queries.GetPermission; using X1.Application.Features.Permissions.Queries.GetPermissionTree; using X1.Application.Features.Permissions.Queries; using X1.Application.Common; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; using X1.Presentation.Abstractions; using X1.Domain.Common; namespace X1.Presentation.Controllers; /// /// 权限管理控制器 /// 提供权限管理相关的 API 接口,包括创建、更新、删除和查询权限功能 /// [Authorize(Roles = RoleConstants.Admin)] // 只有管理员可以访问 [Route("api/permissions")] [ApiController] public class PermissionsController : ApiController { private readonly ILogger _logger; /// /// 初始化权限控制器 /// /// MediatR 中介者,用于处理命令和查询 /// 日志记录器 public PermissionsController( IMediator mediator, ILogger logger) : base(mediator) { _logger = logger; } /// /// 创建新权限 /// /// 创建权限命令 /// 创建结果 [HttpPost] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] public async Task> CreatePermission([FromBody] CreatePermissionCommand command) { try { var result = await mediator.Send(command); if (result.IsSuccess) { _logger.LogInformation("权限 {PermissionName} 创建成功", command.Name); } else { _logger.LogWarning("权限 {PermissionName} 创建失败: {Error}", command.Name, result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "创建权限 {PermissionName} 时发生异常", command.Name); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 更新权限信息 /// /// 更新权限命令 /// 更新结果 [HttpPut] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status404NotFound)] public async Task> UpdatePermission([FromBody] UpdatePermissionCommand command) { try { var result = await mediator.Send(command); if (result.IsSuccess) { _logger.LogInformation("权限 {PermissionId} 更新成功", command.Id); } else { _logger.LogWarning("权限 {PermissionId} 更新失败: {Error}", command.Id, result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "更新权限 {PermissionId} 时发生异常", command.Id); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 删除权限 /// /// 权限ID /// 删除结果 [HttpDelete("{permissionId}")] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status404NotFound)] public async Task> DeletePermission(string permissionId) { try { var command = new DeletePermissionCommand(permissionId); var result = await mediator.Send(command); if (result.IsSuccess) { _logger.LogInformation("权限 {PermissionId} 删除成功", permissionId); } else { _logger.LogWarning("权限 {PermissionId} 删除失败: {Error}", permissionId, result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "删除权限 {PermissionId} 时发生异常", permissionId); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 批量创建权限 /// /// 批量创建权限命令 /// 批量创建结果 [HttpPost("batch-create")] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] public async Task> BatchCreatePermissions([FromBody] BatchCreatePermissionsCommand command) { try { var result = await mediator.Send(command); if (result.IsSuccess) { _logger.LogInformation("批量创建权限成功,成功 {SuccessCount} 个,失败 {FailureCount} 个", result.Data?.SuccessCount, result.Data?.FailureCount); } else { _logger.LogWarning("批量创建权限失败: {Error}", result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "批量创建权限时发生异常"); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 获取权限信息 /// /// 权限ID /// 权限信息 [HttpGet("{permissionId}")] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status404NotFound)] public async Task> GetPermission(string permissionId) { try { var query = new GetPermissionQuery(permissionId); var result = await mediator.Send(query); if (result.IsSuccess) { _logger.LogInformation("获取权限 {PermissionId} 信息成功", permissionId); } else { _logger.LogWarning("获取权限 {PermissionId} 信息失败: {Error}", permissionId, result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "获取权限 {PermissionId} 信息时发生异常", permissionId); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 获取所有权限(分页+筛选) /// /// 查询参数 /// 权限列表 [HttpGet] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] public async Task> GetAllPermissions([FromQuery] GetAllPermissionsQuery query) { try { var result = await mediator.Send(query); if (result.IsSuccess) { _logger.LogInformation("获取所有权限成功,共 {Count} 个权限", result.Data?.Permissions.Count()); } else { _logger.LogWarning("获取所有权限失败: {Error}", result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "获取所有权限时发生异常"); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } /// /// 获取权限树结构 /// /// 权限树结构 [HttpGet("tree")] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status200OK)] [ProducesResponseType(typeof(OperationResult), StatusCodes.Status400BadRequest)] public async Task> GetPermissionTree() { try { var query = new GetPermissionTreeQuery(); var result = await mediator.Send(query); if (result.IsSuccess) { _logger.LogInformation("获取权限树成功,共 {Count} 个资源类型", result.Data?.PermissionTrees.Count()); } else { _logger.LogWarning("获取权限树失败: {Error}", result.ErrorMessages?.FirstOrDefault()); } return result; } catch (Exception ex) { _logger.LogError(ex, "获取权限树时发生异常"); return OperationResult.CreateFailure("系统错误,请稍后重试"); } } }