Quellcode durchsuchen

优化日志记录与软删除过滤逻辑

调整日志记录逻辑,恢复并优化原生 SQL 执行信息的记录。重新启用并改进软删除过滤器,基于 `ISoftDelete` 接口动态生成过滤表达式,并通过缓存机制提升性能。清理冗余代码,恢复部分功能并优化实现。
xf vor 3 Tagen
Ursprung
Commit
17e7791484

+ 46 - 46
src/Hotline.Repository.SqlSugar/Extensions/SqlSugarStartupExtensions.cs

@@ -226,8 +226,8 @@ namespace Hotline.Repository.SqlSugar.Extensions
                 //Log.Warning($"sql参数:{JsonSerializer.Serialize(db.Ado.SqlStackTrace, new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.CjkUnifiedIdeographs) })}");
 
 
-                ////获取原生SQL推荐 5.1.4.63  性能OK
-                //Log.Information(UtilMethods.GetNativeSql(sql, pars));
+                //获取原生SQL推荐 5.1.4.63  性能OK
+                Log.Information(UtilMethods.GetNativeSql(sql, pars));
 
                 //Log.Information("Sql: {0}", sql);
                 //Log.Information("SqlParameters: {0}", string.Join(',', pars.Select(d => d.Value)));
@@ -331,52 +331,52 @@ namespace Hotline.Repository.SqlSugar.Extensions
                 }
             };
 
-            //SetDeletedEntityFilter(db);
+            SetDeletedEntityFilter(db);
         }
 
-        //private static void SetDeletedEntityFilter(SqlSugarClient db)
-        //{
-        //    var cacheKey = $"DbFilter:{db.CurrentConnectionConfig.ConfigId}:IsDeleted";
-        //    var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
-        //    if (tableFilterItemList == null)
-        //    {
-        //        // 获取基类实体数据表
-        //        var entityTypes = AppDomain.CurrentDomain.GetAssemblies()
-        //            .SelectMany(d => d.GetTypes())
-        //            .Where(d => !d.IsInterface
-        //                        && !d.IsAbstract
-        //                        && d.IsClass
-        //                        && d.GetInterfaces().Any(x => x == typeof(ISoftDelete)));
-        //        if (!entityTypes.Any()) return;
-
-        //        var tableFilterItems = new List<TableFilterItem<object>>();
-        //        foreach (var entityType in entityTypes)
-        //        {
-        //            if (entityType.GetProperty("IsDeleted") is null) continue;
-        //            //// 排除非当前数据库实体
-        //            //var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
-        //            //if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
-        //            //    (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
-        //            //    continue;
-
-        //            var lambda = DynamicExpressionParser.ParseLambda(new[] {
-        //                Expression.Parameter(entityType, "d") },
-        //            typeof(bool),
-        //            $"{nameof(SoftDeleteEntity.IsDeleted)} == @0", false);
-        //            var tableFilterItem = new TableFilterItem<object>(entityType, lambda);
-        //            tableFilterItems.Add(tableFilterItem);
-        //            db.QueryFilter.Add(tableFilterItem);
-        //        }
-        //        db.DataCache.Add(cacheKey, tableFilterItems);
-        //    }
-        //    else
-        //    {
-        //        tableFilterItemList.ForEach(u =>
-        //        {
-        //            db.QueryFilter.Add(u);
-        //        });
-        //    }
-        //}
+        private static void SetDeletedEntityFilter(SqlSugarClient db)
+        {
+            var cacheKey = $"DbFilter:{db.CurrentConnectionConfig.ConfigId}:IsDeleted";
+            var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
+            if (tableFilterItemList == null)
+            {
+                // 获取基类实体数据表
+                var entityTypes = AppDomain.CurrentDomain.GetAssemblies()
+                    .SelectMany(d => d.GetTypes())
+                    .Where(d => !d.IsInterface
+                                && !d.IsAbstract
+                                && d.IsClass
+                                && d.GetInterfaces().Any(x => x == typeof(ISoftDelete)));
+                if (!entityTypes.Any()) return;
+
+                var tableFilterItems = new List<TableFilterItem<object>>();
+                foreach (var entityType in entityTypes)
+                {
+                    if (entityType.GetProperty("IsDeleted") is null) continue;
+                    //// 排除非当前数据库实体
+                    //var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
+                    //if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
+                    //    (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
+                    //    continue;
+
+                    var lambda = DynamicExpressionParser.ParseLambda(new[] {
+                        Expression.Parameter(entityType, "d") },
+                    typeof(bool),
+                    $"{nameof(SoftDeleteEntity.IsDeleted)} == @0", false);
+                    var tableFilterItem = new TableFilterItem<object>(entityType, lambda);
+                    tableFilterItems.Add(tableFilterItem);
+                    db.QueryFilter.Add(tableFilterItem);
+                }
+                db.DataCache.Add(cacheKey, tableFilterItems);
+            }
+            else
+            {
+                tableFilterItemList.ForEach(u =>
+                {
+                    db.QueryFilter.Add(u);
+                });
+            }
+        }
 
         #endregion
     }