| | |
| | | { |
| | | var parent = await rep.FirstOrDefaultAsync(it => it.Id == request.ParentId); |
| | | if (parent == null) throw Oops.Oh(EnumErrorCodeType.s404, "上级菜单"); |
| | | if (request.Id.HasValue) |
| | | { |
| | | var entity = await rep.FirstOrDefaultAsync(it => it.Id == request.Id); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "该菜单"); |
| | | if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "上级Id"); |
| | | request.Adapt(entity); |
| | | if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s405, "菜单编号"); |
| | | await rep.UpdateAsync(entity); |
| | | return entity.Id; |
| | | } |
| | | else |
| | | { |
| | | var entity = new Menu(); |
| | | entity.Path = $"{parent.Path}{parent.Code}/"; |
| | | entity.Type = EnumMenuType.Field; |
| | | entity.VisitLevel = parent.VisitLevel; |
| | | request.Adapt(entity); |
| | | if (await CheckExist(entity)) throw Oops.Oh(EnumErrorCodeType.s406, "菜单编号"); |
| | | await rep.InsertAsync(entity); |
| | | return entity.Id; |
| | | } |
| | | return await request.SaveData<Menu, SaveMenuFieldCommand>( |
| | | null, |
| | | it => |
| | | it.ParentId == request.ParentId |
| | | && it.Type == EnumMenuType.Field |
| | | && it.Group == request.Group |
| | | && it.Code == request.Code |
| | | && it.Id != request.Id, |
| | | (entity) => |
| | | { |
| | | if (request.Id.HasValue) |
| | | { |
| | | if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s410, "上级Id"); |
| | | } |
| | | else |
| | | { |
| | | entity.Path = $"{parent.Path}{parent.Code}/"; |
| | | entity.Type = EnumMenuType.Field; |
| | | entity.VisitLevel = parent.VisitLevel; |
| | | } |
| | | request.Adapt(entity); |
| | | }, |
| | | cancellationToken); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 校验菜单是否重复 |
| | | /// </summary> |
| | | /// <param name="entity"></param> |
| | | /// <returns></returns> |
| | | private async Task<bool> CheckExist(Menu entity) |
| | | { |
| | | return await rep.AsQueryable().AsNoTracking() |
| | | .AnyAsync(it => |
| | | it.ParentId == entity.ParentId |
| | | && it.Type == entity.Type |
| | | && it.Group == entity.Group |
| | | && it.Code == entity.Code |
| | | && it.Id != entity.Id); |
| | | } |
| | | |
| | | } |
| | | } |