From 0b49fee10340ea40a6762da657239675d3db22f6 Mon Sep 17 00:00:00 2001
From: sunpengfei <i@angelzzz.com>
Date: 星期四, 07 八月 2025 09:41:31 +0800
Subject: [PATCH] feat:权限和请求日志

---
 FlexJobApi.Core/Utils/DbUtils/DbUtils.cs |   70 +++++++++++++++++++----------------
 1 files changed, 38 insertions(+), 32 deletions(-)

diff --git a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs
index e37e10c..03c260d 100644
--- a/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs
+++ b/FlexJobApi.Core/Utils/DbUtils/DbUtils.cs
@@ -8,6 +8,7 @@
 using Microsoft.EntityFrameworkCore.ChangeTracking;
 using Microsoft.EntityFrameworkCore.Diagnostics;
 using Microsoft.EntityFrameworkCore.Metadata.Builders;
+using Microsoft.EntityFrameworkCore.Migrations;
 using Microsoft.Extensions.Configuration;
 using Newtonsoft.Json;
 using System;
@@ -98,7 +99,7 @@
         /// <param name="query"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        public static async Task<List<SelectQueryResultOption<TValue, TData>>> GetSelect<TEntity, TValue, TData>(
+        public static async Task<List<SelectOption<TValue, TData>>> GetSelect<TEntity, TValue, TData>(
             this SelectQuery<TValue, TData> request,
             Func<TData, TValue> getValue,
             Func<TData, string> getLabel,
@@ -113,10 +114,10 @@
             var models = await q
                 .ProjectToType<TData>()
                 .ToListAsync(cancellationToken);
-            var options = new List<SelectQueryResultOption<TValue, TData>>();
+            var options = new List<SelectOption<TValue, TData>>();
             foreach (var model in models)
             {
-                var option = new SelectQueryResultOption<TValue, TData>();
+                var option = new SelectOption<TValue, TData>();
                 option.Data = model;
                 option.Value = getValue(model);
                 option.Label = getLabel(model);
@@ -348,35 +349,41 @@
         /// <returns></returns>
         public static async Task BuildEntity(ModelBuilder modelBuilder, Type dbContextLocator = null)
         {
-            var xmlDoc = await XmlDocUtils.GetXmlDocAsync();
-            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
+            if (App.GetService<IMigrator>() != null)
             {
-                Console.WriteLine($"姝e湪鐢熸垚琛細{entityType.Name}");
-                // 鑾峰彇瀹炰綋绫荤殑XML娉ㄩ噴锛屽苟璁剧疆涓鸿〃娉ㄩ噴
-                var entityBuilder = modelBuilder.Entity(entityType.ClrType);
-                string typeComment = (await entityType.ClrType.GetXmlDocMemberAsync(xmlDoc))?.Summary;
-                if (!string.IsNullOrEmpty(typeComment))
+                var xmlDoc = await XmlDocUtils.GetXmlDocAsync();
+                foreach (var entityType in modelBuilder.Model.GetEntityTypes())
                 {
-                    Console.WriteLine($"姝e湪鐢熸垚琛ㄦ敞閲婏細{entityType.Name}-{typeComment}");
-                    entityBuilder.ToTable(it => it.HasComment(typeComment));
-                }
-                // 鑾峰彇瀹炰綋灞炴�х殑XML娉ㄩ噴锛屽苟璁剧疆涓哄垪娉ㄩ噴
-                var properties = entityType.ClrType.GetProperties()
-                    .Where(p =>
-                        p.CanRead
-                        && p.CanWrite
-                        && !typeof(System.Collections.ICollection).IsAssignableFrom(p.PropertyType)
-                        && (p.PropertyType.IsClass ? p.PropertyType.FullName.Contains("System.") : true));
-                foreach (var property in properties)
-                {
-                    string propComment = (await property.GetXmlDocMemberAsync(xmlDoc))?.Summary;
-                    if (!string.IsNullOrEmpty(propComment))
+                    Console.WriteLine($"姝e湪鐢熸垚琛細{entityType.Name}");
+                    // 鑾峰彇瀹炰綋绫荤殑XML娉ㄩ噴锛屽苟璁剧疆涓鸿〃娉ㄩ噴
+                    var entityBuilder = modelBuilder.Entity(entityType.ClrType);
+                    string typeComment = (await entityType.ClrType.GetXmlDocMemberAsync(xmlDoc))?.Summary;
+                    if (!string.IsNullOrEmpty(typeComment))
                     {
-                        Console.WriteLine($"姝e湪鐢熸垚灞炴�ф敞閲婏細{property.Name}-{propComment}");
-                        entityBuilder.Property(property.Name).HasComment(propComment);
+                        Console.WriteLine($"姝e湪鐢熸垚琛ㄦ敞閲婏細{entityType.Name}-{typeComment}");
+                        entityBuilder.ToTable(it => it.HasComment(typeComment));
+                    }
+                    // 鑾峰彇瀹炰綋灞炴�х殑XML娉ㄩ噴锛屽苟璁剧疆涓哄垪娉ㄩ噴
+                    var properties = entityType.ClrType.GetProperties()
+                        .Where(p =>
+                            p.CanRead
+                            && p.CanWrite
+                            && !typeof(System.Collections.ICollection).IsAssignableFrom(p.PropertyType)
+                            && (p.PropertyType.IsClass ? p.PropertyType.FullName.Contains("System.") : true));
+                    foreach (var property in properties)
+                    {
+                        string propComment = (await property.GetXmlDocMemberAsync(xmlDoc))?.Summary;
+                        if (!string.IsNullOrEmpty(propComment))
+                        {
+                            Console.WriteLine($"姝e湪鐢熸垚灞炴�ф敞閲婏細{property.Name}-{propComment}");
+                            entityBuilder.Property(property.Name).HasComment(propComment);
+                        }
                     }
                 }
+            }
 
+            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
+            {
                 if (typeof(CommonEntity).IsAssignableFrom(entityType.ClrType))
                 {
                     // 鏋勫缓绛涢�夋潯浠讹細IsDeleted == false
@@ -390,8 +397,6 @@
                     modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
                 }
             }
-
-            Console.WriteLine("鏁版嵁搴撻摼鎺ュ湴鍧�锛�" + App.Configuration.GetConnectionString("FlexJobApi"));
         }
 
         /// <summary>
@@ -420,6 +425,7 @@
 
             // 閫氳繃璇锋眰涓幏鍙栧綋鍓嶆搷浣滀汉
             var logier = JwtUtils.GetCurrentLogier();
+            var traceId = App.GetTraceId();
 
             // 鑾峰彇鎵�鏈夊凡鏇存敼鐨勫疄浣�
             foreach (var entity in entities)
@@ -453,9 +459,9 @@
 
                     // 璧嬪�艰窡韪狪d
                     prop = entity.Property(nameof(CommonEntity.TraceId));
-                    if (prop != null)
+                    if (prop != null && prop.CurrentValue == null && traceId.IsNotNull())
                     {
-                        prop.CurrentValue = App.GetTraceId();
+                        prop.CurrentValue = traceId;
                     }
                 }
                 else
@@ -476,9 +482,9 @@
 
                     // 璧嬪�艰窡韪狪d
                     prop = entity.Property(nameof(CommonEntity.TraceId));
-                    if (prop != null)
+                    if (prop != null && traceId.IsNotNull())
                     {
-                        prop.CurrentValue = App.GetTraceId();
+                        prop.CurrentValue = traceId;
                     }
 
                     // 杞垹闄�

--
Gitblit v1.9.1