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,73 @@
using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Application.Interfaces.Security;
using F9WebAiTest.Application.Interfaces.SnowflakeID;
using F9WebAiTest.Application.Interfaces.UOW;
using F9WebAiTest.Domain.Entities.SYS.Departments;
using F9WebAiTest.Domain.Entities.SYS.RBAC;
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Users;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Users.Commands;
/// <summary>命令定义</summary>
public record User_CreateCommand(string UserName, string PhoneNumber, string LoginName, long DepartmentId, int SortOrder) : IRequest<long>;
/// <summary>命令处理器</summary>
public class CreateUserCommandHandler(
IUnitOfWork_F9 unitOfWork,
IUserWriteRepository userRepository,
IDepartmentWriteRepository departmentWriteRepository,
IDepartmentUserRepository departmentUserRepository,
IPasswordHasher passwordHasher,
ISnowflakeIdGenerator snowflakeIdGenerator,
IPublisher eventBus) : IRequestHandler<User_CreateCommand, long>
{
public async Task<long> Handle(User_CreateCommand command, CancellationToken cancellationToken)
{
// 手写校验
if (string.IsNullOrWhiteSpace(command.UserName)) throw new F9OperationException("用户名不能为空");
if (string.IsNullOrWhiteSpace(command.LoginName)) throw new F9OperationException("登录名不能为空");
if (command.DepartmentId <= 0) throw new F9OperationException("部门不能为空");
User createdUser = null;
var ok = await unitOfWork.ExecuteWithUow(async () =>
{
// 1. 验证部门是否存在(应用层协调)
var department = await departmentWriteRepository.GetByIdAsync(command.DepartmentId, cancellationToken);
if (department == null) throw new F9NotFoundException("部门不存在");
// 2. 创建用户聚合根
var user = new User(
id: snowflakeIdGenerator.CreateId(),
command.UserName,
command.PhoneNumber,
command.LoginName,
command.DepartmentId,
command.SortOrder);
// 设置默认密码
var defaultPasswordMd5 = passwordHasher.GetMd5Hash(user.GetDefaultPasswordText());
var passwordHash = passwordHasher.HashPassword(defaultPasswordMd5);
user.SetPassword(passwordHash);
await userRepository.AddAsync(user);
// 3. 创建部门用户关联
var depUser = new DepartmentUser(snowflakeIdGenerator.CreateId(), command.DepartmentId, user.Id);
await departmentUserRepository.AssignAsync(depUser);
createdUser = user;
return true;
});
if (!ok) throw new F9OperationException("用户", "新增");
// 4. 事务提交后发布领域事件
foreach (var domainEvent in createdUser.DomainEvents)
await eventBus.Publish(domainEvent, cancellationToken);
createdUser.ClearDomainEvents();
return createdUser.Id;
}
}