From f612f7e4e9334bdf11fc201e2e52525411dfc39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=AA=E5=B8=A6=E5=A4=A7=E4=BD=AC=E6=B0=94=E5=9C=BA?= <188633308@qq.com> Date: Thu, 27 May 2021 19:37:59 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=AF=B9dapper=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E8=BF=94=E5=9B=9E=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=BA=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/Ewide.Core/Extension/PageExtensions.cs | 61 ++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/Api/Ewide.Core/Extension/PageExtensions.cs b/Api/Ewide.Core/Extension/PageExtensions.cs index dd99564..ba9a606 100644 --- a/Api/Ewide.Core/Extension/PageExtensions.cs +++ b/Api/Ewide.Core/Extension/PageExtensions.cs @@ -51,26 +51,78 @@ namespace Ewide.Core.Extension return source.OrderBy(OrderBuilder(input)).Select(u => u.Adapt()).ToPagedListAsync(input.PageNo, input.PageSize); } - public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) + #region DAPPER + public async static Task QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { - return source.QueryAsync( + var count = await source.PageTotalCount( + sql, + param: param, + transaction: transaction, + commandTimeout: commandTimeout, + commandType: commandType + ); + + var data = await source.QueryAsync( PageSqlBuilder(sql, input), param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); + + var page = new PagedList + { + PageIndex = input.PageNo, + PageSize = input.PageSize, + Items = data, + TotalCount = count, + TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize) + }; + + return page; } - public static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) + public async static Task> QueryPageData(this IDapperRepository source, string sql, PageInputBase input, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) where T : new() { - return source.QueryAsync( + var count = await source.PageTotalCount( + sql, + param: param, + transaction: transaction, + commandTimeout: commandTimeout, + commandType: commandType + ); + + var data = await source.QueryAsync( PageSqlBuilder(sql, input), param: param, transaction: transaction, commandTimeout: commandTimeout, commandType: commandType ); + + var page = new PagedList + { + PageIndex = input.PageNo, + PageSize = input.PageSize, + Items = data, + TotalCount = count, + TotalPages = (int)Math.Ceiling((decimal)count / (decimal)input.PageSize) + }; + + return page; + } + + private async static Task PageTotalCount(this IDapperRepository source, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) + { + var countSql = String.Format("SELECT COUNT(0) FROM ({0}) T", sql); + var countAsync = await source.QueryAsync( + countSql, + param: param, + transaction: transaction, + commandTimeout: commandTimeout, + commandType: commandType); + var count = countAsync.SingleOrDefault(); + return count; } private static string PageSqlBuilder(string sql , PageInputBase input) @@ -83,5 +135,6 @@ namespace Ewide.Core.Extension sql += String.Join("", sqlStrList); return sql; } + #endregion } }