using MediatR; using TakeoutSaaS.Application.Identity; using TakeoutSaaS.Application.Identity.Commands; using TakeoutSaaS.Application.Identity.Contracts; using TakeoutSaaS.Domain.Identity.Repositories; using TakeoutSaaS.Shared.Abstractions.Constants; using TakeoutSaaS.Shared.Abstractions.Exceptions; namespace TakeoutSaaS.Application.Identity.Handlers; /// /// 更新菜单处理器。 /// public sealed class UpdateMenuCommandHandler(IMenuRepository menuRepository) : IRequestHandler { /// public async Task Handle(UpdateMenuCommand request, CancellationToken cancellationToken) { // 1. 菜单固定时禁止修改 if (!MenuPolicy.CanMaintainMenus) { throw new BusinessException(ErrorCodes.Forbidden, "菜单已固定,禁止修改"); } // 2. 校验存在 var entity = await menuRepository.FindByIdAsync(request.Id, cancellationToken) ?? throw new BusinessException(ErrorCodes.NotFound, "菜单不存在"); // 3. 更新字段 entity.ParentId = request.ParentId; entity.Name = request.Name.Trim(); entity.Path = request.Path.Trim(); entity.Component = request.Component.Trim(); entity.Title = request.Title.Trim(); entity.Icon = request.Icon?.Trim(); entity.IsIframe = request.IsIframe; entity.Link = string.IsNullOrWhiteSpace(request.Link) ? null : request.Link.Trim(); entity.KeepAlive = request.KeepAlive; entity.SortOrder = request.SortOrder; entity.RequiredPermissions = MenuMapper.JoinCodes(request.RequiredPermissions); entity.MetaPermissions = MenuMapper.JoinCodes(request.MetaPermissions); entity.MetaRoles = MenuMapper.JoinCodes(request.MetaRoles); entity.AuthListJson = request.AuthList.Count == 0 ? null : System.Text.Json.JsonSerializer.Serialize(request.AuthList); // 4. 持久化 await menuRepository.UpdateAsync(entity, cancellationToken); await menuRepository.SaveChangesAsync(cancellationToken); // 5. 返回 DTO return MenuMapper.ToDto(entity); } }