From c0a4b045720e83f8e52ffc110b773aeece590b55 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期四, 07 八月 2025 18:26:29 +0800 Subject: [PATCH] Merge branch 'master' of http://120.26.58.240:8888/r/ApiFlexJob --- FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 96 insertions(+), 6 deletions(-) diff --git a/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs b/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs index 57f7d0c..3228f39 100644 --- a/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs +++ b/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs @@ -1,5 +1,8 @@ -锘縰sing FlexJobApi.Core; +锘縰sing Consul; +using EFCore.BulkExtensions; +using FlexJobApi.Core; using Furion.DatabaseAccessor; +using Furion.DatabaseAccessor.Extensions; using Furion.FriendlyException; using Mapster; using MediatR; @@ -21,7 +24,8 @@ IRepository<DictionaryCategory> repDictionaryCategory ) : IRequestHandler<SaveDictionaryDataCommand, Guid>, - IRequestHandler<SetDictionaryDataIsDisabledCommand, int> + IRequestHandler<SetDictionaryDataIsDisabledCommand, int>, + IRequestHandler<SyncHumanResourcesAreaDictionaryDataCommand, int> { private readonly IRepository<DictionaryData> rep = rep; @@ -46,6 +50,10 @@ if (category == null) throw Oops.Oh(EnumErrorCodeType.s404, "鏁版嵁瀛楀吀绫诲埆"); request.CategoryId = category.Id; if (!request.Code.StartsWith($"{category.Code}-")) throw Oops.Oh(EnumErrorCodeType.s400, "缂栧彿寮�澶撮渶瑕佸寘鍚被鍒紪鍙�-"); + var parent = request.ParentId.HasValue + ? await rep.AsQueryable().AsNoTracking() + .FirstOrDefaultAsync(it => it.Id == request.ParentId) + : null; return await request.SaveData<DictionaryData, SaveDictionaryDataCommand>( null, it => @@ -58,10 +66,15 @@ if (request.Id.HasValue) { if (entity.Code != request.Code) throw Oops.Oh(EnumErrorCodeType.s400, "缂栧彿涓嶅彲淇敼"); - DbUtils.UpdateTreeDataChildrenPath<DictionaryData>( - $"{entity.Path}/{entity.Code}/", - $"{entity.Path}/{request.Code}/", - cancellationToken).Wait(); + if (entity.ParentId != request.ParentId) throw Oops.Oh(EnumErrorCodeType.s400, "涓婄骇Id涓嶅彲淇敼"); + //DbUtils.UpdateTreeDataChildrenPath<DictionaryData>( + // $"{entity.Path}/{entity.Code}/", + // $"{entity.Path}/{request.Code}/", + // cancellationToken).Wait(); + } + else + { + entity.Deep = request.ParentId == null ? 1 : parent.Deep + 1; } request.Adapt(entity); }, @@ -78,5 +91,82 @@ { return request.SetIsDisabled<DictionaryData>(cancellationToken: cancellationToken); } + + /// <summary> + /// 鍚屾浜哄姏璧勬簮鍦板尯瀛楀吀鏁版嵁 + /// </summary> + /// <param name="request"></param> + /// <param name="cancellationToken"></param> + /// <returns></returns> + public async Task<int> Handle(SyncHumanResourcesAreaDictionaryDataCommand request, CancellationToken cancellationToken) + { + var repHumanResourcesBaseArea = Db.GetRepository<BaseArea, HumanResourcesDbContextLocator>(); + var areas = await repHumanResourcesBaseArea.AsQueryable().AsNoTracking() + .OrderBy(it => it.AreaCode) + .ToListAsync(); + var entities = new List<DictionaryData>(); + var categoryId = new Guid("B21FE000-BB7F-4498-08E9-08DDD572EF73"); + await "DELETE FROM DictionaryData WHERE CategoryId = @CategoryId".SqlNonQueryAsync(new + { + CategoryId = categoryId + }); + var parentAreas = areas.Where(it => it.ParentId == 0).ToList(); + LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, areas, parentAreas); + await rep.Context.BulkInsertAsync(entities); + return entities.Count; + } + + /// <summary> + /// 閫掑綊鍚屾浜哄姏璧勬簮鍦板尯瀛楀吀鏁版嵁 + /// </summary> + /// <param name="categoryId"></param> + /// <param name="entities"></param> + /// <param name="all"></param> + /// <param name="areas"></param> + /// <param name="deep"></param> + private void LoopSyncHumanResourcesAreaDictionaryData(Guid categoryId, List<DictionaryData> entities, List<BaseArea> all, List<BaseArea> areas, int deep = 1) + { + if (areas.IsNotNull()) + { + foreach (var area in areas) + { + var entity = new DictionaryData(); + entity.CategoryId = categoryId; + entity.Id = area.Id; + if (area.ParentId != 0) + { + var parent = entities.FirstOrDefault(it => it.Field1 == area.ParentId.ToString()); + if (parent == null) + { + var parentArea = areas.FirstOrDefault(it => it.AreaCode == area.ParentId); + if (parentArea != null) + { + throw Oops.Oh(EnumErrorCodeType.s404, "涓婄骇鍦板尯"); + } + continue; + } + entity.ParentId = parent.Id; + entity.Path = $"{parent.Path}{parent.Code}/"; + } + else + { + entity.Path = "/"; + } + entity.Code = $"70-{area.AreaCode}"; + entity.Content = area.AreaName; + entity.Field1 = area.AreaCode.ToString(); + entity.Field2 = area.QuickQuery; + entity.Field3 = area.SimpleSpelling; + entity.Field4 = area.Layer.ToString(); + entity.Field5 = area.Description; + entity.Deep = deep; + entity.Sort = area.Sort ?? 0; + entities.Add(entity); + + var children = all.Where(it => it.ParentId == area.AreaCode).ToList(); + LoopSyncHumanResourcesAreaDictionaryData(categoryId, entities, all, children, deep + 1); + } + } + } } } -- Gitblit v1.9.1