From 7fdb2b0ac2c16d78089b5b8c022f74a6edefd22f Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期四, 07 八月 2025 14:52:55 +0800
Subject: [PATCH] feat:去掉字典选择器登录校验

---
 FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs |   89 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 82 insertions(+), 7 deletions(-)

diff --git a/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs b/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs
index f9c7a0b..819710c 100644
--- a/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs
+++ b/FlexJobApi.Application/Dictionaries/Commands/DictionaryDataCommandHandler.cs
@@ -1,7 +1,11 @@
-锘縰sing FlexJobApi.Core;
+锘縰sing EFCore.BulkExtensions;
+using FlexJobApi.Core;
 using Furion.DatabaseAccessor;
+using Furion.DatabaseAccessor.Extensions;
+using Furion.FriendlyException;
 using Mapster;
 using MediatR;
+using Microsoft.EntityFrameworkCore;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -14,31 +18,49 @@
     /// <summary>
     /// 鏁版嵁瀛楀吀鍛戒护澶勭悊鍣�
     /// </summary>
-    public class DictionaryDataCommandHandler :
+    public class DictionaryDataCommandHandler(
+            IRepository<DictionaryData> rep,
+            IRepository<DictionaryCategory> repDictionaryCategory
+        ) :
         IRequestHandler<SaveDictionaryDataCommand, Guid>,
-        IRequestHandler<SetDictionaryDataIsDisabledCommand, int>
+        IRequestHandler<SetDictionaryDataIsDisabledCommand, int>,
+        IRequestHandler<SyncHumanResourcesAreaDictionaryDataCommand, int>
 
     {
+        private readonly IRepository<DictionaryData> rep = rep;
+        private readonly IRepository<DictionaryCategory> repDictionaryCategory = repDictionaryCategory;
+
         /// <summary>
         /// 淇濆瓨鏁版嵁瀛楀吀
         /// </summary>
         /// <param name="request"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        public Task<Guid> Handle(SaveDictionaryDataCommand request, CancellationToken cancellationToken)
+        public async Task<Guid> Handle(SaveDictionaryDataCommand request, CancellationToken cancellationToken)
         {
-            return request.SaveData<DictionaryData, SaveDictionaryDataCommand>(
+            var category = await repDictionaryCategory
+                .Where(it => it.Id == request.CategoryId || it.Code == request.CategoryCode)
+                .Select(it => new
+                {
+                    it.Id,
+                    it.Code
+                })
+                .FirstOrDefaultAsync();
+            if (category == null) throw Oops.Oh(EnumErrorCodeType.s404, "鏁版嵁瀛楀吀绫诲埆");
+            request.CategoryId = category.Id;
+            if (!request.Code.StartsWith($"{category.Code}-")) throw Oops.Oh(EnumErrorCodeType.s400, "缂栧彿寮�澶撮渶瑕佸寘鍚被鍒紪鍙�-");
+            return await request.SaveData<DictionaryData, SaveDictionaryDataCommand>(
+                null,
                 it =>
                     it.CategoryId == request.CategoryId
-                    && it.ParentId == request.ParentId
                     && it.Code == request.Code
-                    && it.Content == request.Content
                     && it.Id != request.Id,
                 (entity) =>
                 {
                     entity.Path = DbUtils.GetTreeDataPath<DictionaryData>(request.ParentId, cancellationToken).Result;
                     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}/",
@@ -59,5 +81,58 @@
         {
             return request.SetIsDisabled<DictionaryData>(cancellationToken: cancellationToken);
         }
+
+        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
+            });
+            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, "鏈壘鍒颁笂绾у湴鍖�");
+                        }
+                        else
+                        {
+                            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);
+            }
+            await rep.Context.BulkInsertAsync(entities);
+            return entities.Count;
+        }
     }
 }

--
Gitblit v1.9.1