From b4205c80b0fd6220632fa0ab7df4c75ba844afc4 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期四, 07 八月 2025 15:13:21 +0800
Subject: [PATCH] feat:字典开发

---
 FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs |   67 ++++++++++++++++++++++++++++++---
 1 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs b/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs
index 7df5b54..ac2f0cf 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;
@@ -82,12 +85,64 @@
 
         public async Task<int> Handle(SyncHumanResourcesAreaDictionaryDataCommand request, CancellationToken cancellationToken)
         {
-            var rep = Db.GetRepository<BaseArea, HumanResourcesDbContextLocator>();
-            var areas = await rep.AsQueryable().AsNoTracking()
-                .Take(10)
+            var repHumanResourcesBaseArea = Db.GetRepository<BaseArea, HumanResourcesDbContextLocator>();
+            var areas = await repHumanResourcesBaseArea.AsQueryable().AsNoTracking()
+                .OrderBy(it => it.AreaCode)
                 .ToListAsync();
-            Console.WriteLine();
-            return 1;
+            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();
+            LoopAdd(categoryId, entities, areas, parentAreas);
+            await rep.Context.BulkInsertAsync(entities);
+            return entities.Count;
+        }
+
+        public void LoopAdd(Guid categoryId, List<DictionaryData> entities, List<BaseArea> all, List<BaseArea> areas)
+        {
+            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.Sort = area.Sort ?? 0;
+                    entities.Add(entity);
+
+                    var children = all.Where(it => it.ParentId == area.AreaCode).ToList();
+                    LoopAdd(categoryId, entities, all, children);
+                }
+            }
         }
     }
 }

--
Gitblit v1.9.1