Skip to content

Commit

Permalink
Merge pull request #159 from jhzou1/master
Browse files Browse the repository at this point in the history
表达式查询增加字段自动过滤
  • Loading branch information
liukuo362573 authored Sep 1, 2023
2 parents 8f298c9 + 361d979 commit 8881dd9
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 29 deletions.
40 changes: 27 additions & 13 deletions YiSha.Business/YiSha.Service/OrganizationManage/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,40 @@ public async Task ChangeUser(UserEntity entity)
#region 私有方法
private Expression<Func<UserEntity, bool>> ListFilter(UserListParam param)
{
var expression = LinqExtensions.True<UserEntity>();

//var expression = LinqExtensions.True<UserEntity>();

if (param.UserStatus == -1)
{
param.UserStatus = null;
}

//****根据查询字段自动过滤条件****
var expression = LinqExtensions.GetExpressionItems<UserEntity,UserListParam>(param);

if (param != null)
{
if (!string.IsNullOrEmpty(param.UserName))
{
expression = expression.And(t => t.UserName.Contains(param.UserName));
}
//if (!string.IsNullOrEmpty(param.UserName))
//{
// expression = expression.And(t => t.UserName.Contains(param.UserName));
//}


if (!string.IsNullOrEmpty(param.UserIds))
{
long[] userIdList = TextHelper.SplitToArray<long>(param.UserIds, ',');
expression = expression.And(t => userIdList.Contains(t.Id.Value));
}
if (!string.IsNullOrEmpty(param.Mobile))
{
expression = expression.And(t => t.Mobile.Contains(param.Mobile));
}
if (param.UserStatus > -1)
{
expression = expression.And(t => t.UserStatus == param.UserStatus);
}

//if (!string.IsNullOrEmpty(param.Mobile))
//{
// expression = expression.And(t => t.Mobile.Contains(param.Mobile));
//}
//if (param.UserStatus > -1)
//{
// expression = expression.And(t => t.UserStatus == param.UserStatus);
//}

if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
{
expression = expression.And(t => t.BaseModifyTime >= param.StartTime);
Expand Down
143 changes: 143 additions & 0 deletions YiSha.Util/YiSha.Util/Extension/Extension.Linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,148 @@ static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second,
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}

/// <summary>
/// #jhzou0616 根据数据类型获取实体表达式(目前仅支持string/日期范围 的条件筛选),字段属性名称必须匹配** 日期范围字段必须以 xxxStart/xxxEnd 结尾
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> GetExpressionItems<T, TT>(TT input)
{
var parameter = Expression.Parameter(typeof(T), "entity");
Expression expression = Expression.Constant(true);

var properties = typeof(TT).GetProperties();

var targetProperties = typeof(T).GetProperties();

foreach (var property in properties)
{
//字符类型筛选
if (property.PropertyType == typeof(string))
{


var value = property.GetValue(input);

if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
var propertyExpression = Expression.Property(parameter, property.Name);
var valueExpression = Expression.Constant(value);
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var containsExpression = Expression.Call(propertyExpression, containsMethod, valueExpression);
expression = Expression.AndAlso(expression, containsExpression);
}
}
//日期类型筛选
else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
{
var value = property.GetValue(input);

if (value != null)
{
// 开始日期筛选
if (property.Name.Contains("Start"))
{
var realPropertyName = property.Name.Substring(0, property.Name.Length - "Start".Length);
var propertyExpression = Expression.Property(parameter, realPropertyName);
var entityfield = targetProperties.FirstOrDefault(s => s.Name == realPropertyName);
var startDateTime = (DateTime)value;

if (entityfield.PropertyType == typeof(DateTime?))
{
var greaterThanOrEqualExpression = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(startDateTime, typeof(DateTime?)));
expression = Expression.AndAlso(expression, greaterThanOrEqualExpression);
}
else
{
var greaterThanOrEqualExpression = Expression.GreaterThanOrEqual(propertyExpression, Expression.Constant(startDateTime));
expression = Expression.AndAlso(expression, greaterThanOrEqualExpression);
}

}
// 结束日期筛选
else if (property.Name.Contains("End"))
{
var realPropertyName = property.Name.Substring(0, property.Name.Length - "End".Length);
var propertyExpression = Expression.Property(parameter, realPropertyName);
var entityfield = targetProperties.FirstOrDefault(s => s.Name == realPropertyName);
var endDateTime = ((DateTime)value).Date.AddDays(1).AddTicks(-1);

if (entityfield.PropertyType == typeof(DateTime?))
{
var lessThanOrEqualExpression = Expression.LessThanOrEqual(propertyExpression, Expression.Constant(endDateTime, typeof(DateTime?)));
expression = Expression.AndAlso(expression, lessThanOrEqualExpression);
}
else
{
var lessThanOrEqualExpression = Expression.LessThanOrEqual(propertyExpression, Expression.Constant(endDateTime));
expression = Expression.AndAlso(expression, lessThanOrEqualExpression);
}

}
// 其他日期属性
else
{
var propertyExpression = Expression.Property(parameter, property.Name);
var equalExpression = Expression.Equal(propertyExpression, Expression.Constant(value));
expression = Expression.AndAlso(expression, equalExpression);
}
}

}
//数值类型
else if (property.PropertyType == typeof(int?) || property.PropertyType == typeof(int))
{
var value = property.GetValue(input);
var propertyExpression = Expression.Property(parameter, property.Name);
var entityfield = targetProperties.FirstOrDefault(s => s.Name == property.Name);

if (value != null)
{
if (entityfield.PropertyType == typeof(int?))
{
var equalExpression = Expression.Equal(propertyExpression, Expression.Constant(value, typeof(int?)));
expression = Expression.AndAlso(expression, equalExpression);
}
else
{
var equalExpression = Expression.Equal(propertyExpression, Expression.Constant(value));
expression = Expression.AndAlso(expression, equalExpression);
}

}

}
//float
else if (property.PropertyType == typeof(float?) || property.PropertyType == typeof(float))
{
var value = property.GetValue(input);
var propertyExpression = Expression.Property(parameter, property.Name);
var entityfield = targetProperties.FirstOrDefault(s => s.Name == property.Name);

if (value != null)
{
if (entityfield.PropertyType == typeof(float?))
{
var equalExpression = Expression.Equal(propertyExpression, Expression.Constant(value, typeof(float?)));
expression = Expression.AndAlso(expression, equalExpression);
}
else
{
var equalExpression = Expression.Equal(propertyExpression, Expression.Constant(value));
expression = Expression.AndAlso(expression, equalExpression);
}

}

}

}

return Expression.Lambda<Func<T, bool>>(expression, parameter);
}

/// <summary>
/// ParameterRebinder
/// </summary>
Expand Down Expand Up @@ -105,6 +247,7 @@ protected override Expression VisitParameter(ParameterExpression p)
}
return base.VisitParameter(p);
}

}
}
}
2 changes: 2 additions & 0 deletions YiSha.Web/YiSha.Admin.Web/YiSha.Admin.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@
<ProjectReference Include="..\..\YiSha.Util\YiSha.Util\YiSha.Util.csproj" />
<ProjectReference Include="..\YiSha.Web.Code\YiSha.Web.Code.csproj" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
</Project>
32 changes: 16 additions & 16 deletions YiSha.Web/YiSha.Admin.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
}
},
"AllowedHosts": "*",
"SystemConfig": {
"Demo": false, // 是否是演示模式
"LoginMultiple": true, // 是否允许一个账户在多处登录
"LoginProvider": "Cookie", // 登录信息保存方式 Cookie Session WebApi
"SnowFlakeWorkerId": 1, // SnowFlake 节点序号
"ApiSite": "http://localhost:5001", // Api地址,例如可以上传文件到Api
"VirtualDirectory": "/admin", // 虚拟目录
"DBProvider": "MySql",
"DBConnectionString": "server=localhost;database=YiShaAdmin;user=root;password=123456;port=3306;pooling=true;max pool size=20;persist security info=True;charset=utf8mb4;",
"DBCommandTimeout": 180, // 数据库超时时间,单位秒
"DBBackup": "", // 数据库备份路径
"CacheProvider": "Memory", // 缓存使用方式 Memory Redis
"RedisConnectionString": "127.0.0.1:6379"
}
"SystemConfig": {
"Demo": false, // 是否是演示模式
"LoginMultiple": true, // 是否允许一个账户在多处登录
"LoginProvider": "Cookie", // 登录信息保存方式 Cookie Session WebApi
"SnowFlakeWorkerId": 1, // SnowFlake 节点序号
"ApiSite": "http://localhost:5001", // Api地址,例如可以上传文件到Api
"VirtualDirectory": "/admin", // 虚拟目录

"DBProvider": "SqlServer",
"DBConnectionString": "Server=localhost;User Id=sa;Password=hmAdmin@;Database=YsData;",
"DBCommandTimeout": 180, // 数据库超时时间,单位秒
"DBBackup": "", // 数据库备份路径

"CacheProvider": "Memory", // 缓存使用方式 Memory Redis
"RedisConnectionString": "127.0.0.1:6379"
}
}

0 comments on commit 8881dd9

Please sign in to comment.