😎优化过滤表达式,解决sql查询因大量or或嵌套语句导致的异常问题
This commit is contained in:
parent
512f0d6c7a
commit
6a5093d997
@ -46,7 +46,7 @@
|
||||
<PackageReference Include="SSH.NET" Version="2024.2.0" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.5.0" />
|
||||
<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="Yitter.IdGenerator" Version="1.0.14" />
|
||||
</ItemGroup>
|
||||
|
||||
@ -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>
|
||||
|
||||
@ -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 property = type.GetProperty(fieldName);
|
||||
Expression memberExp = Expression.Property(parameter, property!);
|
||||
|
||||
// 如果属性是可为空的类型,则转换为其基础类型
|
||||
var propertyType = property.PropertyType;
|
||||
if (Nullable.GetUnderlyingType(propertyType) != null)
|
||||
{
|
||||
temp = Expression.Convert(temp, Nullable.GetUnderlyingType(propertyType));
|
||||
}
|
||||
// 如果属性是可为空的类型,则转换为其基础类型
|
||||
var baseType = Nullable.GetUnderlyingType(property.PropertyType);
|
||||
if (baseType != null) memberExp = Expression.Convert(memberExp, baseType);
|
||||
|
||||
Expression left = Expression.Equal(temp, Expression.Constant(owner));
|
||||
right = Expression.OrElse(left, right);
|
||||
});
|
||||
});
|
||||
var finalExpression = Expression.Lambda(right, new ParameterExpression[] { parameter });
|
||||
return finalExpression;
|
||||
// 调用ownersCollection.Contains方法,检查是否包含属性值
|
||||
right = Expression.OrElse(Expression.Call(
|
||||
typeof(Enumerable),
|
||||
nameof(Enumerable.Contains),
|
||||
new[] { memberExp.Type },
|
||||
ownersCollection,
|
||||
memberExp
|
||||
), right);
|
||||
}
|
||||
return Expression.Lambda(right, parameter);
|
||||
}
|
||||
}
|
||||
@ -74,7 +74,7 @@
|
||||
"vue-router": "^4.5.0",
|
||||
"vue-signature-pad": "^3.0.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-plugin-element": "^4.0.4",
|
||||
"vxe-table-plugin-export-xlsx": "^4.0.7",
|
||||
@ -93,7 +93,7 @@
|
||||
"@vitejs/plugin-vue": "^5.2.1",
|
||||
"@vitejs/plugin-vue-jsx": "^4.1.1",
|
||||
"@vue/compiler-sfc": "^3.5.13",
|
||||
"code-inspector-plugin": "^0.18.2",
|
||||
"code-inspector-plugin": "^0.18.3",
|
||||
"eslint": "^9.16.0",
|
||||
"eslint-plugin-vue": "^9.32.0",
|
||||
"globals": "^15.13.0",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user