From 382f59522fdb9f791c7c58a05e143aa47fe387ed Mon Sep 17 00:00:00 2001 From: MysticBoy Date: Tue, 15 Nov 2022 00:26:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E6=88=B7=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp.Extensions.EFCore/QueryCollection.cs | 2 +- IoTSharp/Controllers/TenantsController.cs | 16 +++ IoTSharp/Extensions/QueryExtensions.cs | 134 ++++++++++++++++-- 3 files changed, 142 insertions(+), 10 deletions(-) diff --git a/IoTSharp.Extensions.EFCore/QueryCollection.cs b/IoTSharp.Extensions.EFCore/QueryCollection.cs index 9fec3d158..b6ff1c6be 100644 --- a/IoTSharp.Extensions.EFCore/QueryCollection.cs +++ b/IoTSharp.Extensions.EFCore/QueryCollection.cs @@ -103,7 +103,7 @@ private static Expression> AndWith(this Expression>(func(arg, arg2), new ParameterExpression[1] { parameterExpression }); } - private static Expression> AsExpression(this QueryCollection queries, Condition? condition = Condition.OrElse) where T : class + public static Expression> AsExpression(this QueryCollection queries, Condition? condition = Condition.OrElse) where T : class { Type targetType = typeof(T); TypeInfo typeInfo = targetType.GetTypeInfo(); diff --git a/IoTSharp/Controllers/TenantsController.cs b/IoTSharp/Controllers/TenantsController.cs index e7bbb77b4..23c3486b2 100644 --- a/IoTSharp/Controllers/TenantsController.cs +++ b/IoTSharp/Controllers/TenantsController.cs @@ -17,6 +17,8 @@ using IoTSharp.Models; using Microsoft.EntityFrameworkCore.ChangeTracking; using IoTSharp.Contracts; +using IoTSharp.Extensions; +using System.Linq.Expressions; namespace IoTSharp.Controllers { @@ -66,6 +68,20 @@ public async Task>> GetTenant() } } + /// + /// 产品列表 + /// + /// + /// + [HttpGet] + public async Task>> 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>(ApiCode.Success, "OK", data); + } + /// /// 普通用户用于活的自身的租户信息 /// diff --git a/IoTSharp/Extensions/QueryExtensions.cs b/IoTSharp/Extensions/QueryExtensions.cs index aaa89d489..c47882482 100644 --- a/IoTSharp/Extensions/QueryExtensions.cs +++ b/IoTSharp/Extensions/QueryExtensions.cs @@ -15,30 +15,146 @@ namespace IoTSharp.Extensions public static class QueryExtensions { - public static async Task> Query(this QueryDto _dto, IQueryable src, Expression> _where, Expression> func , Expression> conver, CancellationToken cancellationToken = default(CancellationToken)) where T : class + /// + /// 分页查询 + /// + /// 原表类型 + /// 基础查询条件 + /// 查询源 + /// + /// + public static async Task> Query(this QueryDto _dto, IQueryable 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 { - var member = (MemberExpression)func.Body; - var queries = new QueryCollection + total = _total, + rows = rs + }; + return data; + } + /// + /// 分页查询 + /// + /// 原表类型 + /// 基础查询条件 + /// 查询源 + /// 额外的查询条件 + /// + /// + public static async Task> Query(this QueryDto _dto, IQueryable src, Expression> _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 + { + total = _total, + rows = rs + }; + + return data; + } + /// + /// 分页查询 + /// + /// 原表类型 + /// 查询名称的字段类型 + /// 基础查询条件 + /// 查询源 + /// 指定查询条件里面的Name查询对应的字段 + /// + /// + public static async Task> Query(this QueryDto _dto, IQueryable src, Expression> func, CancellationToken cancellationToken = default(CancellationToken)) where T : class + { + var _where = MemberToExpression(_dto, func); + return await _dto.Query(src, _where, cancellationToken); + } + + private static Expression> MemberToExpression(QueryDto _dto, Expression> 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(); + return _where; + } + /// + /// 分页查询 + /// + /// 原表类型 + /// 查询名称的字段类型 + /// 基础查询条件 + /// 查询源 + /// 指定查询条件里面的Name查询对应的字段 + /// 额外的查询条件 + /// + /// + public static async Task> Query(this QueryDto _dto, IQueryable src, Expression> _where, Expression> 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 rs = new List(); - rs = await query.Select(conver).ToListAsync(cancellationToken); - var data = new PagedData + var rs = await query.ToListAsync(cancellationToken); + var data = new PagedData { total = _total, rows = rs }; return data; } + /// + /// 分页查询 + /// + /// 原表类型 + /// 查询名称的字段类型 + /// 查询到的数据转换为指定的类型 + /// 基础查询条件 + /// 查询源 + /// 指定查询条件里面的Name查询对应的字段 + /// 额外的查询条件 + /// 查询到的数据转换为指定的表达式 + /// + /// + public static async Task> Query(this QueryDto _dto, IQueryable src, Expression> _where, Expression> func , Expression> 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 + { + total = _total, + }; + List rs = new List(); + rs = await query.Select(conver).ToListAsync(cancellationToken); + return data; + } + + private static Expression> WithNameQuery(this Expression> _where, QueryDto _dto, Expression> 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; + } } }