From 28db30e5a458ee012959269d50e16eadaa0c5d12 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期五, 05 九月 2025 15:10:09 +0800
Subject: [PATCH] feat:开发

---
 FlexJobApi.CommonServer.Application/Dictionaries/Queries/DictionaryDatasQueryHandler.cs |  141 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 98 insertions(+), 43 deletions(-)

diff --git a/FlexJobApi.CommonServer.Application/Dictionaries/Queries/DictionaryDatasQueryHandler.cs b/FlexJobApi.CommonServer.Application/Dictionaries/Queries/DictionaryDatasQueryHandler.cs
index 1d96d5b..002d01e 100644
--- a/FlexJobApi.CommonServer.Application/Dictionaries/Queries/DictionaryDatasQueryHandler.cs
+++ b/FlexJobApi.CommonServer.Application/Dictionaries/Queries/DictionaryDatasQueryHandler.cs
@@ -4,11 +4,13 @@
 using Mapster;
 using MediatR;
 using Microsoft.EntityFrameworkCore;
+using Newtonsoft.Json.Linq;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
 namespace FlexJobApi.CommonServer.Application
 {
@@ -18,7 +20,8 @@
     public class DictionaryDatasQueryHandler(
             IRepository<DictionaryData> rep
         ) : IRequestHandler<GetDictionaryDatasQuery, PagedListQueryResult<GetDictionaryDatasQueryResultItem>>,
-            IRequestHandler<GetDictionaryDataSelectQuery, List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>>
+            IRequestHandler<GetDictionaryDataSelectQuery, List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>>,
+            IRequestHandler<GetAreaSelectQuery, List<GetAreaSelectQueryResultOption>>
     {
         private readonly IRepository<DictionaryData> rep = rep;
 
@@ -71,50 +74,102 @@
         /// <param name="request"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        public Task<List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>> Handle(GetDictionaryDataSelectQuery request, CancellationToken cancellationToken)
+        public async Task<List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>> Handle(GetDictionaryDataSelectQuery request, CancellationToken cancellationToken)
         {
-            return request.GetSelect<DictionaryData, string, GetDictionaryDataSelectQueryResultOption>(
-                it => it.Code,
-                it => it.Content,
-                q =>
+            var sql = @"SELECT d.* FROM DictionaryData d
+INNER JOIN DictionaryCategory c ON d.CategoryId = c.Id
+WHERE d.IsDisabled = 0";
+            if (request.All != true)
+            {
+                if (request.ParentId == null)
                 {
-                    q = q
-                        .OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime)
-                        .Where(it => !it.IsDisabled);
-                    if (request.All != true)
-                    {
-                        q = q.Where(it => it.ParentId == request.ParentId);
-                    }
-                    if (request.MaxDeep.HasValue)
-                    {
-                        q = q.Where(it => it.Deep <= request.MaxDeep);
-                    }
-                    if (request.Keywords.IsNotNull())
-                    {
-                        q = q.Where(it =>
-                            it.Code.Contains(request.Keywords)
-                            || it.Content.Contains(request.Keywords)
-                            || it.Field1.Contains(request.Keywords)
-                            || it.Field2.Contains(request.Keywords)
-                            || it.Field3.Contains(request.Keywords)
-                            || it.Field4.Contains(request.Keywords)
-                            || it.Field5.Contains(request.Keywords));
-                    }
-                    if (request.CategoryId.HasValue)
-                    {
-                        q = q.Where(it => it.CategoryId == request.CategoryId);
-                    }
-                    else if (request.CategoryCode.IsNotNull())
-                    {
-                        q = q.Where(it => it.Category.Code == request.CategoryCode);
-                    }
-                    else
-                    {
-                        throw Oops.Oh(EnumErrorCodeType.s400, "璇峰~鍐欑被鍒獻d鎴栫紪鍙�");
-                    }
-                    return q;
-                },
-                cancellationToken);
+                    sql += " AND d.ParentId IS NULL";
+                }
+                else
+                {
+                    sql += " AND d.ParentId = @ParentId";
+                }
+            }
+            if (request.MaxDeep.HasValue)
+            {
+                sql += " AND d.Deep <= @MaxDeep";
+            }
+            if (request.Keywords.IsNotNull())
+            {
+                sql += @" AND (d.Code LIKE @Keywords
+OR d.Content LIKE @Keywords
+OR d.Field1 LIKE @Keywords
+OR d.Field2 LIKE @Keywords
+OR d.Field3 LIKE @Keywords
+OR d.Field4 LIKE @Keywords
+OR d.Field5 LIKE @Keywords)";
+            }
+            if (request.CategoryId.HasValue)
+            {
+                sql += " AND d.CategoryId = @CategoryId";
+            }
+            else if (request.CategoryCode.IsNotNull())
+            {
+                sql += " AND c.Code = @CategoryCode";
+            }
+            else
+            {
+                throw Oops.Oh(EnumErrorCodeType.s400, "璇峰~鍐欑被鍒獻d鎴栫紪鍙�");
+            }
+            sql += " ORDER BY d.Sort";
+            var models = await rep.SqlQueriesAsync<GetDictionaryDataSelectQueryResultOption>(sql, request, cancellationToken);
+            var options = new List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>>();
+            foreach (var model in models)
+            {
+                var option = new SelectOption<string, GetDictionaryDataSelectQueryResultOption>();
+                option.Data = model;
+                option.Value = model.Code;
+                option.Label = model.Content;
+                options.Add(option);
+            }
+            if (request.WithChildren == true)
+            {
+                var parents = options.Where(it => it.Data.ParentId == null).ToList();
+                LoopChildrens(parents, options);
+                return parents;
+            }
+            return options;
+        }
+
+        /// <summary>
+        /// 鏌ヨ鍦板尯閫夋嫨鍣�
+        /// </summary>
+        /// <param name="request"></param>
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        public async Task<List<GetAreaSelectQueryResultOption>> Handle(GetAreaSelectQuery request, CancellationToken cancellationToken)
+        {
+            var models = await Handle(new GetDictionaryDataSelectQuery
+            {
+                CategoryCode = "70",
+                All = true,
+                MaxDeep = request.MaxDeep,
+                WithChildren = true
+            }, cancellationToken);
+            var result = models.Adapt<List<GetAreaSelectQueryResultOption>>();
+            return result;
+        }
+
+        /// <summary>
+        /// 閫掑綊璧嬪�间笅绾�
+        /// </summary>
+        /// <param name="models"></param>
+        /// <param name="all"></param>
+        private void LoopChildrens(List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>> models, List<SelectOption<string, GetDictionaryDataSelectQueryResultOption>> all)
+        {
+            foreach (var item in models)
+            {
+                item.Data.Children = all.Where(it => it.Data.ParentId == item.Data.Id).ToList();
+                if (item.Data.Children.IsNotNull())
+                {
+                    LoopChildrens(item.Data.Children, all);
+                }
+            }
         }
     }
 }

--
Gitblit v1.9.1