Files
2026-10-08 10:43:59 +08:00

32 lines
1.2 KiB
C#

using F9WebAiTest.Application.Common.Exceptions;
using F9WebAiTest.Domain.Interfaces.SYS.Departments;
using MediatR;
namespace F9WebAiTest.Application.CQRS.SYS.Departments.Commands;
/// <summary>命令定义</summary>
public record Department_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest<bool>;
/// <summary>命令处理器</summary>
public class ActiveOrDeactivateDepartmentCommandHandler(
IDepartmentWriteRepository departmentWriteRepository,
IPublisher eventBus) : IRequestHandler<Department_ActiveOrDeactivateCommand, bool>
{
public async Task<bool> Handle(Department_ActiveOrDeactivateCommand command, CancellationToken cancellationToken)
{
var dep = await departmentWriteRepository.GetByIdAsync(command.Id, CancellationToken.None);
if (dep == null) throw new F9NotFoundException("部门", command.Id);
if (command.Active) dep.Activate();
else dep.Deactivate();
var result = await departmentWriteRepository.ActiveOrDeactive(command.Id, dep.IsActive);
if (result == false) throw new F9OperationException("启用/禁用", "操作");
foreach (var de in dep.DomainEvents) await eventBus.Publish(de, cancellationToken);
dep.ClearDomainEvents();
return result;
}
}