36 lines
1.6 KiB
C#
36 lines
1.6 KiB
C#
using F9WebAiTest.Application.Common.Exceptions;
|
|
using F9WebAiTest.Application.Interfaces.SnowflakeID;
|
|
using F9WebAiTest.Domain.Entities.SYS.Departments;
|
|
using F9WebAiTest.Domain.Enums.SYS.Departments;
|
|
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
|
|
using MediatR;
|
|
|
|
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
|
|
|
|
/// <summary>命令定义</summary>
|
|
public record Department_CreateCommand(string Code, string Name, long ParentId, Department_ManageType_Enum ManageType, Department_DutyType_Enum DutyType, int SortOrder, string Description) : IRequest<long>;
|
|
|
|
/// <summary>命令处理器</summary>
|
|
public class CreateDepartmentCommandHandler(
|
|
IDepartmentWriteRepository departmentRepository,
|
|
ISnowflakeIdGenerator snowflakeIdGenerator,
|
|
IPublisher eventBus) : IRequestHandler<Department_CreateCommand, long>
|
|
{
|
|
public async Task<long> Handle(Department_CreateCommand command, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(command.Code)) throw new F9OperationException("部门编码不能为空");
|
|
if (string.IsNullOrWhiteSpace(command.Name)) throw new F9OperationException("部门名称不能为空");
|
|
|
|
var dep = new Department(snowflakeIdGenerator.CreateId(), command.Code, command.Name, command.ParentId,
|
|
command.ManageType, command.DutyType, command.SortOrder, command.Description);
|
|
|
|
var result = await departmentRepository.AddAsync(dep);
|
|
if (result == false) throw new F9OperationException("部门", "新增");
|
|
|
|
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
|
|
dep.ClearDomainEvents();
|
|
|
|
return dep.Id;
|
|
}
|
|
}
|