first commit

This commit is contained in:
yanpeng200212
2026-10-08 10:43:59 +08:00
commit 8430ba0452
288 changed files with 29595 additions and 0 deletions
@@ -0,0 +1,80 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Application.Interfaces.UOW;
using F9WebAiTest.Domain.Common;
using F9WebAiTest.Domain.Entities.SYS.RBAC;
using F9WebAiTest.Domain.Enums.SYS.RBAC;
using NotificationTypeEntity = F9WebAiTest.Domain.Entities.Notice.NotificationType;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Permissions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record RolePermission_AssignCommand(long RoleId, Permission_Type_Enum PermissionType, Resource_Type_Enum ResourceType, string ResourceIdentity, long ResourceId) : IRequest<long>;
/// <summary>命令处理器</summary>
public class AssignRolePermissionCommandHandler(
IRoleWriteRepository roleRepository,
IUnitOfWork_F9 unitOfWork,
IRolePermissionWriteRepository rolePermissionRep,
IPermissionWriteRepository permissionWriteRepository,
IRepository<NotificationTypeEntity> notificationTypeRepository,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler<RolePermission_AssignCommand, long>
{
public async Task<long> Handle(RolePermission_AssignCommand command, CancellationToken cancellationToken)
{
if (command.RoleId <= 0) throw new F9OperationException("角色ID不能为空");
if (command.ResourceId <= 0) throw new F9OperationException("资源ID不能为空");
if (string.IsNullOrWhiteSpace(command.ResourceIdentity)) throw new F9OperationException("资源标识不能为空");
Role role = null;
long rolePermissionId = 0;
var ok = await unitOfWork.ExecuteWithUow(async () =>
{
// 查或创建权限
var dbPermission = await permissionWriteRepository.GetByIdAsync(command.PermissionType, command.ResourceType, command.ResourceId);
if (dbPermission == null)
{
dbPermission = new Permission(
snowflakeIdGenerator.CreateId(),
command.PermissionType,
command.ResourceType,
command.ResourceIdentity,
command.ResourceId);
// 消息订阅权限:Code 对齐消息类型的 RequiredPermission(业务权限码),保证订阅校验一致
if (command.ResourceType == Resource_Type_Enum.消息订阅)
{
var type = await notificationTypeRepository.GetByIdAsync(command.ResourceId);
if (type != null && !string.IsNullOrEmpty(type.RequiredPermission))
dbPermission.SetCode(type.RequiredPermission);
}
await permissionWriteRepository.AddRangeAsync(new[] { dbPermission });
}
// 绑定角色权限
var rolePermission = new RolePermission(snowflakeIdGenerator.CreateId(), command.RoleId, dbPermission.Id);
var result = await rolePermissionRep.AssignAsync(rolePermission);
role = await roleRepository.GetByIdAsync(command.RoleId);
role.AddPermission(dbPermission.Id, command.ResourceType);
rolePermissionId = rolePermission.Id;
return result;
});
if (!ok) throw new F9OperationException("权限", "绑定");
// 发布领域事件
foreach (var domainEvent in role.DomainEvents)
await eventBus.Publish(domainEvent, cancellationToken);
role.ClearDomainEvents();
return rolePermissionId;
}
}
@@ -0,0 +1,37 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Enums.SYS.RBAC;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record RolePermission_RemoveCommand(long Id, Resource_Type_Enum ResourceType) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class RemoveRolePermissionCommandHandler(
IRoleWriteRepository roleRepository,
IRolePermissionWriteRepository rolePermissionWriteRepository,
IPublisher eventBus) : IRequestHandler<RolePermission_RemoveCommand, bool>
{
public async Task<bool> Handle(RolePermission_RemoveCommand command, CancellationToken cancellationToken)
{
if (command.Id <= 0) throw new F9OperationException("角色权限ID不能为空");
var rolePermission = await rolePermissionWriteRepository.GetByIdAsync(command.Id);
if (rolePermission == null) throw new F9NotFoundException("角色权限", command.Id);
var role = await roleRepository.GetByIdAsync(rolePermission.RoleId);
role.RemovePermission(rolePermission.PermissionId, command.ResourceType);
var result = await rolePermissionWriteRepository.RemoveAsync(command.Id);
if (result == false) throw new F9OperationException("权限", "解绑");
// 发布领域事件
foreach (var domainEvent in role.DomainEvents)
await eventBus.Publish(domainEvent, cancellationToken);
role.ClearDomainEvents();
return true;
}
}
@@ -0,0 +1,26 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record Role_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class ActiveOrDeactivateRoleCommandHandler(
IRoleWriteRepository roleWriteRepository) : IRequestHandler<Role_ActiveOrDeactivateCommand, bool>
{
public async Task<bool> Handle(Role_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
{
var role = await roleWriteRepository.GetByIdAsync(command.Id);
if (role == null) throw new F9NotFoundException("角色", command.Id);
if (command.Active) role.Activate();
else role.Deactivate();
var result = await roleWriteRepository.ActiveOrDeactive(command.Id, role.IsActive);
if (result == false) throw new F9OperationException("启用/禁用", "操作");
return result;
}
}
@@ -0,0 +1,27 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Domain.Entities.SYS.RBAC;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record Role_CreateCommand(string Name, string Code, int SortOrder, string Description) : IRequest<long>;
/// <summary>命令处理器</summary>
public class CreateRoleCommandHandler(
IRoleWriteRepository roleRepository,
ISnowflakeIdGenerator snowflakeIdGenerator) : IRequestHandler<Role_CreateCommand, long>
{
public async Task<long> Handle(Role_CreateCommand command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空");
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空");
var newEntity = new Role(snowflakeIdGenerator.CreateId(), command.Name, command.Code, command.SortOrder, command.Description);
var result = await roleRepository.AddAsync(newEntity);
if (result == false) throw new F9OperationException("角色", "新增");
return newEntity.Id;
}
}
@@ -0,0 +1,22 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record Role_DelCommand(long Id) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class DelRoleCommandHandler(
IRoleWriteRepository roleWriteRepository) : IRequestHandler<Role_DelCommand, bool>
{
public async Task<bool> Handle(Role_DelCommand command, CancellationToken cancellationToken)
{
if (command.Id <= 0) throw new F9OperationException("角色ID不能为空");
var result = await roleWriteRepository.SoftDeleteAsync(command.Id);
if (result == false) throw new F9OperationException("角色", "删除");
return result;
}
}
@@ -0,0 +1,25 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Entities.SYS.RBAC;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Roles;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Roles.Commands;
/// <summary>命令定义</summary>
public record Role_UpdateCommand(long Id, string Name, string Code, int SortOrder, string Description) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class UpdateRoleCommandHandler(
IRoleWriteRepository roleRepository) : IRequestHandler<Role_UpdateCommand, bool>
{
public async Task<bool> Handle(Role_UpdateCommand command, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("角色名称不能为空");
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("角色编码不能为空");
var newEntity = new Role(command.Id, command.Name, command.Code, command.SortOrder, command.Description);
var result = await roleRepository.UpdateAsync(newEntity);
if (result == false) throw new F9OperationException("角色", "修改");
return result;
}
}