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

This commit is contained in:
zuohuaijun 2024-12-12 13:03:33 +08:00
parent 512f0d6c7a
commit 6a5093d997
4 changed files with 27 additions and 30 deletions

View File

@ -46,7 +46,7 @@
<PackageReference Include="SSH.NET" Version="2024.2.0" /> <PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.0" /> <PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1139" /> <PackageReference Include="TencentCloudSDK.Sms" Version="3.0.1140" />
<PackageReference Include="UAParser" Version="3.1.47" /> <PackageReference Include="UAParser" Version="3.1.47" />
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" /> <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
</ItemGroup> </ItemGroup>

View File

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

View File

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

View File

@ -74,7 +74,7 @@
"vue-router": "^4.5.0", "vue-router": "^4.5.0",
"vue-signature-pad": "^3.0.2", "vue-signature-pad": "^3.0.2",
"vue3-tree-org": "^4.2.2", "vue3-tree-org": "^4.2.2",
"vxe-pc-ui": "^4.3.27", "vxe-pc-ui": "^4.3.28",
"vxe-table": "^4.8.10", "vxe-table": "^4.8.10",
"vxe-table-plugin-element": "^4.0.4", "vxe-table-plugin-element": "^4.0.4",
"vxe-table-plugin-export-xlsx": "^4.0.7", "vxe-table-plugin-export-xlsx": "^4.0.7",
@ -93,7 +93,7 @@
"@vitejs/plugin-vue": "^5.2.1", "@vitejs/plugin-vue": "^5.2.1",
"@vitejs/plugin-vue-jsx": "^4.1.1", "@vitejs/plugin-vue-jsx": "^4.1.1",
"@vue/compiler-sfc": "^3.5.13", "@vue/compiler-sfc": "^3.5.13",
"code-inspector-plugin": "^0.18.2", "code-inspector-plugin": "^0.18.3",
"eslint": "^9.16.0", "eslint": "^9.16.0",
"eslint-plugin-vue": "^9.32.0", "eslint-plugin-vue": "^9.32.0",
"globals": "^15.13.0", "globals": "^15.13.0",