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 } }