From 350aa66d4818f85f042cb2d399691209dc97b97e Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期三, 06 八月 2025 14:46:54 +0800 Subject: [PATCH] feat:字典开发 --- FlexJobApi.User.Application/Menus/Commands/SaveMenuCommandHandler.cs | 148 +++++++++++++------------------------------------ 1 files changed, 39 insertions(+), 109 deletions(-) diff --git a/FlexJobApi.User.Application/Menus/Commands/SaveMenuCommandHandler.cs b/FlexJobApi.User.Application/Menus/Commands/SaveMenuCommandHandler.cs index 7b1a417..925775d 100644 --- a/FlexJobApi.User.Application/Menus/Commands/SaveMenuCommandHandler.cs +++ b/FlexJobApi.User.Application/Menus/Commands/SaveMenuCommandHandler.cs @@ -1,4 +1,5 @@ -锘縰sing FlexJobApi.Core; +锘縰sing Consul; +using FlexJobApi.Core; using Furion.DatabaseAccessor; using Furion.DataValidation; using Furion.FriendlyException; @@ -29,35 +30,29 @@ /// <inheritdoc/> public async Task<Guid> Handle(SaveMenuCommand request, CancellationToken cancellationToken) { - if (request.Id.HasValue) - { - var entity = await rep.AsQueryable() - .Include(it => it.Childrens) - .FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken); - if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "鑿滃崟"); - if (entity.UserType != request.UserType) throw Oops.Oh(EnumErrorCodeType.s410, "鐢ㄦ埛绫诲瀷"); - if (entity.ClientType != request.ClientType) throw Oops.Oh(EnumErrorCodeType.s410, "瀹㈡埛绔被鍨�"); - - var path = await GetPathAsync(request.ParentId, request.Code, cancellationToken); - if (await CheckExist(entity, request.ParentId, request.Code)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿"); - await UpdateChildrensPath($"{entity.Path}/{entity.Code}", $"{path}/{request.Code}", cancellationToken); - request.Adapt(entity); - SaveChildrens(entity, request); - await rep.UpdateAsync(entity); - - return entity.Id; - } - else - { - var entity = new Menu(); - request.Adapt(entity); - entity.Path = await GetPathAsync(entity.ParentId, entity.Code, cancellationToken); - if (await CheckExist(entity, request.ParentId, request.Code)) throw Oops.Oh(EnumErrorCodeType.s405, "鑿滃崟缂栧彿"); - SaveChildrens(entity, request); - await rep.InsertAsync(entity); - - return entity.Id; - } + return await request.SaveData<Menu, SaveMenuCommand>( + it => + it.UserType == request.UserType + && it.ClientType == request.ClientType + && it.ParentId == request.ParentId + && it.Code == request.Code + && it.Id != request.Id, + (entity) => + { + entity.Path = DbUtils.GetTreeDataPath<DictionaryData>(request.ParentId, cancellationToken).Result; + if (request.Id.HasValue) + { + if (entity.UserType != request.UserType) throw Oops.Oh(EnumErrorCodeType.s410, "鐢ㄦ埛绫诲瀷"); + if (entity.ClientType != request.ClientType) throw Oops.Oh(EnumErrorCodeType.s410, "瀹㈡埛绔被鍨�"); + DbUtils.UpdateTreeDataChildrenPath<DictionaryData>( + $"{entity.Path}/{entity.Code}/", + $"{entity.Path}/{request.Code}/", + cancellationToken).Wait(); + } + request.Adapt(entity); + SaveChildrens(entity, request); + }, + cancellationToken); } /// <summary> @@ -71,11 +66,13 @@ // 鑾峰彇瀛愰泦Id var childrenIds = GetRequestChildrenIds(request); // 鍒犻櫎瀛愮骇 - entity.Childrens = entity.Childrens.Where(it => - it.Type == EnumMenuType.Menu - || it.Type == EnumMenuType.Page - || it.Type == EnumMenuType.Modal - || childrenIds.Contains(it.Id)).ToList(); + entity.Children = entity.Children + .Where(it => + it.Type == EnumMenuType.Menu + || it.Type == EnumMenuType.Page + || it.Type == EnumMenuType.Modal + || childrenIds.Contains(it.Id)) + .ToList(); // 閬嶅巻鍒嗙粍 @@ -87,7 +84,7 @@ // 閬嶅巻鎸夐挳 娣诲姞鎴栨洿鏂� foreach (var button in buttonLocation.Buttons) { - var buttonEntity = entity.Childrens.FirstOrDefault(it => it.Id == button.Id); + var buttonEntity = entity.Children.FirstOrDefault(it => it.Id == button.Id); if (buttonEntity == null) { if (button.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"褰撳墠鍒嗙粍{group.Group}-浣嶇疆{buttonLocation.Location}-鎸夐挳{button.Code}"); @@ -96,8 +93,9 @@ UserType = entity.UserType, ClientType = entity.ClientType, Type = EnumMenuType.Button, + VisitLevel = entity.VisitLevel, }; - entity.Childrens.Add(buttonEntity); + entity.Children.Add(buttonEntity); } else if (buttonEntity.Type != EnumMenuType.Button) throw Oops.Oh(EnumErrorCodeType.s400, $"褰撳墠鍒嗙粍{group.Group}-瀛楁{button.Code}骞堕潪涓�涓寜閽�"); buttonEntity.Path = $"{entity.Path}{entity.Code}/"; @@ -110,7 +108,7 @@ // 閬嶅巻瀛楁 娣诲姞鎴栨洿鏂� foreach (var field in group.Fields) { - var fieldEntity = entity.Childrens.FirstOrDefault(it => it.Id == field.Id); + var fieldEntity = entity.Children.FirstOrDefault(it => it.Id == field.Id); if (fieldEntity == null) { if (field.Id.HasValue) throw Oops.Oh(EnumErrorCodeType.s404, $"褰撳墠鍒嗙粍{group.Group}-瀛楁{field.Code}"); @@ -119,8 +117,9 @@ UserType = entity.UserType, ClientType = entity.ClientType, Type = EnumMenuType.Field, + VisitLevel = entity.VisitLevel, }; - entity.Childrens.Add(fieldEntity); + entity.Children.Add(fieldEntity); } else if (fieldEntity.Type != EnumMenuType.Field) throw Oops.Oh(EnumErrorCodeType.s400, $"褰撳墠鍒嗙粍{group.Group}-浣嶇疆{fieldEntity.Location}-鎸夐挳{fieldEntity.Code}骞堕潪涓�涓瓧娈�"); fieldEntity.Path = $"{entity.Path}{entity.Code}/"; @@ -137,7 +136,7 @@ /// <param name="entity"></param> private void CheckRepeatChildrens(Menu entity) { - var repeats = entity.Childrens + var repeats = entity.Children .GroupBy(it => { return it.Type == EnumMenuType.Button @@ -168,75 +167,6 @@ g.Fields.Where(f => f.Id.HasValue).Select(f => f.Id!.Value))) .SelectMany(it => it) .ToList(); - } - - /// <summary> - /// 鏍¢獙鑿滃崟鏄惁閲嶅 - /// </summary> - /// <param name="entity"></param> - /// <param name="parentId"></param> - /// <param name="code"></param> - /// <returns></returns> - private async Task<bool> CheckExist(Menu entity, Guid? parentId, string code) - { - return await rep.AsQueryable().AsNoTracking() - .AnyAsync(it => - it.UserType == entity.UserType - && it.ClientType == entity.ClientType - && it.ParentId == parentId - && it.Code == code - && it.Id != entity.Id); - } - - /// <summary> - /// 鑾峰彇鍦板潃 - /// </summary> - /// <param name="parentId"></param> - /// <param name="code"></param> - /// <param name="cancellationToken"></param> - /// <returns></returns> - private async Task<string> GetPathAsync(Guid? parentId, string code, CancellationToken cancellationToken) - { - if (parentId.HasValue) - { - var parent = await rep.AsQueryable().AsNoTracking() - .Where(it => it.Id == parentId) - .Select(it => new - { - it.Path, - it.Code - }) - .FirstOrDefaultAsync(cancellationToken); - if (parent == null) throw Oops.Oh(EnumErrorCodeType.s404, "涓婄骇鑿滃崟"); - return $"{parent.Path}{parent.Code}/"; - } - else - { - return "/"; - } - } - - /// <summary> - /// 鏇存柊涓嬬骇鑿滃崟璺緞 - /// </summary> - /// <param name="oldPath"></param> - /// <param name="newPath"></param> - /// <param name="cancellationToken"></param> - /// <returns></returns> - private async Task UpdateChildrensPath(string oldPath, string newPath, CancellationToken cancellationToken) - { - var models = await rep.AsQueryable() - .Where(it => it.Path.StartsWith(oldPath)) - .Where(it => it.Type == EnumMenuType.Menu || it.Type == EnumMenuType.Page || it.Type == EnumMenuType.Modal) - .ToListAsync(cancellationToken); - if (models.IsNotNull()) - { - foreach (var model in models) - { - model.Path = model.Path.Replace(oldPath, newPath); - } - await rep.UpdateAsync(models); - } } } } -- Gitblit v1.9.1