Skip to content

Commit

Permalink
租户查询
Browse files Browse the repository at this point in the history
  • Loading branch information
maikebing committed Nov 14, 2022
1 parent 9cd78a8 commit 382f595
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 10 deletions.
2 changes: 1 addition & 1 deletion IoTSharp.Extensions.EFCore/QueryCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static Expression<Func<T, bool>> AndWith<T>(this Expression<Func<T, bool
Expression arg2 = new ReplaceExpressionVisitor(expr2.Parameters[0], parameterExpression).Visit(expr2.Body);
return Expression.Lambda<Func<T, bool>>(func(arg, arg2), new ParameterExpression[1] { parameterExpression });
}
private static Expression<Func<T, bool>> AsExpression<T>(this QueryCollection queries, Condition? condition = Condition.OrElse) where T : class
public static Expression<Func<T, bool>> AsExpression<T>(this QueryCollection queries, Condition? condition = Condition.OrElse) where T : class
{
Type targetType = typeof(T);
TypeInfo typeInfo = targetType.GetTypeInfo();
Expand Down
16 changes: 16 additions & 0 deletions IoTSharp/Controllers/TenantsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using IoTSharp.Models;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using IoTSharp.Contracts;
using IoTSharp.Extensions;
using System.Linq.Expressions;

namespace IoTSharp.Controllers
{
Expand Down Expand Up @@ -66,6 +68,20 @@ public async Task<ActionResult<List<Tenant>>> GetTenant()
}
}

/// <summary>
/// 产品列表
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult<PagedData<Tenant>>> Query([FromQuery] QueryDto m)
{
var profile = this.GetUserProfile();
var querym = _context.Tenant.Where(c=>c.Deleted==false);
var data = await m.Query(querym, c => c.Name);
return new ApiResult<PagedData<Tenant>>(ApiCode.Success, "OK", data);
}

/// <summary>
/// 普通用户用于活的自身的租户信息
/// </summary>
Expand Down
134 changes: 125 additions & 9 deletions IoTSharp/Extensions/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,146 @@ namespace IoTSharp.Extensions

public static class QueryExtensions
{
public static async Task<PagedData<R>> Query<T, R,P>(this QueryDto _dto, IQueryable<T> src, Expression<Func<T, bool>> _where, Expression<Func<T,P>> func , Expression<Func<T, R>> conver, CancellationToken cancellationToken = default(CancellationToken)) where T : class
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">原表类型</typeparam>
/// <param name="_dto">基础查询条件</param>
/// <param name="src">查询源</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<PagedData<T>> Query<T>(this QueryDto _dto, IQueryable<T> src ,CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
if (!string.IsNullOrEmpty( _dto.Name))
var _total = await src.CountAsync( cancellationToken);
var query = src.Skip((_dto.Offset) * _dto.Limit)
.Take(_dto.Limit);
var rs = await query.ToListAsync(cancellationToken);
var data = new PagedData<T>
{
var member = (MemberExpression)func.Body;
var queries = new QueryCollection
total = _total,
rows = rs
};
return data;
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">原表类型</typeparam>
/// <param name="_dto">基础查询条件</param>
/// <param name="src">查询源</param>
/// <param name="_where">额外的查询条件</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<PagedData<T>> Query<T>(this QueryDto _dto, IQueryable<T> src, Expression<Func<T, bool>> _where, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
var _total = await src.CountAsync(_where, cancellationToken);
var query = src.Where(_where).Skip((_dto.Offset) * _dto.Limit)
.Take(_dto.Limit);
var rs = await query.ToListAsync(cancellationToken);
var data = new PagedData<T>
{
total = _total,
rows = rs
};

return data;
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">原表类型</typeparam>
/// <typeparam name="P">查询名称的字段类型</typeparam>
/// <param name="_dto">基础查询条件</param>
/// <param name="src">查询源</param>
/// <param name="func">指定查询条件里面的Name查询对应的字段</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<PagedData<T>> Query<T, P>(this QueryDto _dto, IQueryable<T> src, Expression<Func<T, P>> func, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
var _where = MemberToExpression(_dto, func);
return await _dto.Query(src, _where, cancellationToken);
}

private static Expression<Func<T, bool>> MemberToExpression<T, P>(QueryDto _dto, Expression<Func<T, P>> func) where T : class
{
var member = (MemberExpression)func.Body;
var queries = new QueryCollection
{
new Query { Name = member.Member.Name, Operator =Operators.StartWith, Value = _dto.Name },
new Query { Name = member.Member.Name, Operator =Operators.Contains, Value = _dto.Name },
new Query { Name = member.Member.Name, Operator = Operators.EndWidth, Value = _dto.Name }
};
_where = _where.AndWith(queries);
}
var _where = queries.AsExpression<T>();
return _where;
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">原表类型</typeparam>
/// <typeparam name="P">查询名称的字段类型</typeparam>
/// <param name="_dto">基础查询条件</param>
/// <param name="src">查询源</param>
/// <param name="func">指定查询条件里面的Name查询对应的字段</param>
/// <param name="_where">额外的查询条件</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<PagedData<T>> Query<T, P>(this QueryDto _dto, IQueryable<T> src, Expression<Func<T, bool>> _where, Expression<Func<T, P>> func, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
_where = _where.WithNameQuery(_dto, func);
var _total = await src.CountAsync(_where, cancellationToken);
var query = src.Where(_where).Skip((_dto.Offset) * _dto.Limit)
.Take(_dto.Limit);
List<R> rs = new List<R>();
rs = await query.Select(conver).ToListAsync(cancellationToken);
var data = new PagedData<R>
var rs = await query.ToListAsync(cancellationToken);
var data = new PagedData<T>
{
total = _total,
rows = rs
};
return data;
}
/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">原表类型</typeparam>
/// <typeparam name="P">查询名称的字段类型</typeparam>
/// <typeparam name="R">查询到的数据转换为指定的类型</typeparam>
/// <param name="_dto">基础查询条件</param>
/// <param name="src">查询源</param>
/// <param name="func">指定查询条件里面的Name查询对应的字段</param>
/// <param name="_where">额外的查询条件</param>
/// <param name="conver">查询到的数据转换为指定的表达式</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<PagedData<R>> Query<T, R,P>(this QueryDto _dto, IQueryable<T> src, Expression<Func<T, bool>> _where, Expression<Func<T,P>> func , Expression<Func<T, R>> conver, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
_where = _where.WithNameQuery( _dto, func);
var _total = await src.CountAsync(_where, cancellationToken);
var query = src.Where(_where).Skip((_dto.Offset) * _dto.Limit)
.Take(_dto.Limit);
var data = new PagedData<R>
{
total = _total,
};
List<R> rs = new List<R>();
rs = await query.Select(conver).ToListAsync(cancellationToken);
return data;
}

private static Expression<Func<T, bool>> WithNameQuery<T, P>(this Expression<Func<T, bool>> _where, QueryDto _dto, Expression<Func<T, P>> func) where T : class
{
if (!string.IsNullOrEmpty(_dto.Name))
{
var member = (MemberExpression)func.Body;
var queries = new QueryCollection
{
new Query { Name = member.Member.Name, Operator =Operators.StartWith, Value = _dto.Name },
new Query { Name = member.Member.Name, Operator =Operators.Contains, Value = _dto.Name },
new Query { Name = member.Member.Name, Operator = Operators.EndWidth, Value = _dto.Name }
};
_where = _where.AndWith(queries);
}

return _where;
}
}
}

0 comments on commit 382f595

Please sign in to comment.