Эх сурвалжийг харах

fix: 优化过滤表达式,解决sql查询因大量or或嵌套语句导致的异常问题

喵你个旺呀 1 жил өмнө
parent
commit
31ad0c0a7b

+ 2 - 2
Admin.NET/Admin.NET.Core/Entity/EntityBase.cs

@@ -39,8 +39,8 @@ public abstract class EntityBase : EntityBaseId, IDeletedFilter
     /// <summary>
     /// 创建者Id
     /// </summary>
-    [SugarColumn(ColumnDescription = "创建者Id", IsOnlyIgnoreUpdate = true)]
     [OwnerUser]
+    [SugarColumn(ColumnDescription = "创建者Id", IsOnlyIgnoreUpdate = true)]
     public virtual long? CreateUserId { get; set; }
 
     ///// <summary>
@@ -92,8 +92,8 @@ public abstract class EntityBaseData : EntityBase, IOrgIdFilter
     /// <summary>
     /// 创建者部门Id
     /// </summary>
-    [SugarColumn(ColumnDescription = "创建者部门Id", IsOnlyIgnoreUpdate = true)]
     [OwnerOrg]
+    [SugarColumn(ColumnDescription = "创建者部门Id", IsOnlyIgnoreUpdate = true)]
     public virtual long? CreateOrgId { get; set; }
 
     /// <summary>

+ 24 - 27
Admin.NET/Admin.NET.Core/Extension/SqlSugarFilterExtension.cs

@@ -14,14 +14,11 @@ public static class SqlSugarFilterExtension
     /// <typeparam name="T"></typeparam>
     /// <param name="type"></param>
     /// <returns></returns>
-    private static List<string> GetPropertyNames<T>(this Type type)
-        where T : Attribute
+    private static List<string> GetPropertyNames<T>(this Type type) where T : Attribute
     {
-        var allProperties = type.GetProperties();
-
-        var properties = allProperties.Where(x => x.CustomAttributes.Any(a => a.AttributeType == typeof(T)));
-
-        return properties.Select(x => x.Name).ToList();
+        return type.GetProperties()
+            .Where(p => p.CustomAttributes.Any(x => x.AttributeType == typeof(T)))
+            .Select(x => x.Name).ToList();
     }
 
     /// <summary>
@@ -34,28 +31,28 @@ public static class SqlSugarFilterExtension
     public static LambdaExpression GetConditionExpression<T>(this Type type, List<long> owners) where T : Attribute
     {
         var fieldNames = type.GetPropertyNames<T>();
-
         ParameterExpression parameter = Expression.Parameter(type, "c");
+        
         Expression right = Expression.Constant(false);
-        fieldNames.ForEach(fieldName =>
+        ConstantExpression ownersCollection = Expression.Constant(owners);
+        foreach (var fieldName in fieldNames)
         {
-            owners.ForEach(owner =>
-            {
-                var property = type.GetProperty(fieldName);
-                Expression temp = Expression.Property(parameter, property);
-
-                // 如果属性是可为空的类型,则转换为其基础类型
-                var propertyType = property.PropertyType;
-                if (Nullable.GetUnderlyingType(propertyType) != null)
-                {
-                    temp = Expression.Convert(temp, Nullable.GetUnderlyingType(propertyType));
-                }
-
-                Expression left = Expression.Equal(temp, Expression.Constant(owner));
-                right = Expression.OrElse(left, right);
-            });
-        });
-        var finalExpression = Expression.Lambda(right, new ParameterExpression[] { parameter });
-        return finalExpression;
+            var property = type.GetProperty(fieldName);
+            Expression memberExp = Expression.Property(parameter, property!);
+
+            // 如果属性是可为空的类型,则转换为其基础类型
+            var baseType = Nullable.GetUnderlyingType(property.PropertyType);
+            if (baseType != null) memberExp = Expression.Convert(memberExp, baseType);
+
+            // 调用ownersCollection.Contains方法,检查是否包含属性值
+            right = Expression.OrElse(Expression.Call(
+                typeof(Enumerable),
+                nameof(Enumerable.Contains),
+                new[] { memberExp.Type },
+                ownersCollection,
+                memberExp
+            ), right);
+        }
+        return Expression.Lambda(right, parameter);
     }
 }