using F9WebAiTest.Application.Common.Exceptions; using F9WebAiTest.Domain.Interfaces.SYS.RBAC.Menus; using MediatR; namespace F9WebAiTest.Application.CQRS.SYS.RBAC.Menus.Commands; /// 命令定义 public record Menu_ActiveOrDeactivateCommand(long Id, bool Active) : IRequest; /// 命令处理器 public class ActiveOrDeactivateMenuCommandHandler( IMenuWriteRepository menuWriteRepository, IPublisher eventBus) : IRequestHandler { public async Task Handle(Menu_ActiveOrDeactivateCommand command, CancellationToken cancellationToken) { var menu = await menuWriteRepository.GetByIdAsync(command.Id); if (menu == null) throw new F9NotFoundException("菜单", command.Id); if (command.Active) menu.Activate(); else menu.Deactivate(); var result = await menuWriteRepository.ActiveOrDeactive(command.Id, menu.IsActive); if (result == false) throw new F9OperationException("启用/禁用", "操作"); foreach (var de in menu.DomainEvents) await eventBus.Publish(de, cancellationToken); menu.ClearDomainEvents(); return result; } }