SqlSugarPagedList.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 分页泛型集合
  4. /// </summary>
  5. /// <typeparam name="TEntity"></typeparam>
  6. public class SqlSugarPagedList<TEntity>
  7. where TEntity : new()
  8. {
  9. /// <summary>
  10. /// 页码
  11. /// </summary>
  12. public int Page { get; set; }
  13. /// <summary>
  14. /// 页容量
  15. /// </summary>
  16. public int PageSize { get; set; }
  17. /// <summary>
  18. /// 总条数
  19. /// </summary>
  20. public int Total { get; set; }
  21. /// <summary>
  22. /// 总页数
  23. /// </summary>
  24. public int TotalPages { get; set; }
  25. /// <summary>
  26. /// 当前页集合
  27. /// </summary>
  28. public IEnumerable<TEntity> Items { get; set; }
  29. /// <summary>
  30. /// 是否有上一页
  31. /// </summary>
  32. public bool HasPrevPage { get; set; }
  33. /// <summary>
  34. /// 是否有下一页
  35. /// </summary>
  36. public bool HasNextPage { get; set; }
  37. }
  38. /// <summary>
  39. /// 分页拓展类
  40. /// </summary>
  41. public static class SqlSugarPagedExtensions
  42. {
  43. /// <summary>
  44. /// 分页拓展
  45. /// </summary>
  46. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  47. /// <param name="pageIndex">当前页码,从1开始</param>
  48. /// <param name="pageSize">页码容量</param>
  49. /// <param name="expression">查询结果 Select 表达式</param>
  50. /// <returns></returns>
  51. public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  52. Expression<Func<TEntity, TResult>> expression)
  53. where TEntity : new()
  54. where TResult : new()
  55. {
  56. var total = 0;
  57. var items = query.ToPageList(pageIndex, pageSize, ref total, expression);
  58. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  59. }
  60. /// <summary>
  61. /// 分页拓展
  62. /// </summary>
  63. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  64. /// <param name="pageIndex">当前页码,从1开始</param>
  65. /// <param name="pageSize">页码容量</param>
  66. /// <returns></returns>
  67. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  68. where TEntity : new()
  69. {
  70. var total = 0;
  71. var items = query.ToPageList(pageIndex, pageSize, ref total);
  72. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  73. }
  74. /// <summary>
  75. /// 分页拓展
  76. /// </summary>
  77. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  78. /// <param name="pageIndex">当前页码,从1开始</param>
  79. /// <param name="pageSize">页码容量</param>
  80. /// <param name="expression">查询结果 Select 表达式</param>
  81. /// <returns></returns>
  82. public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  83. Expression<Func<TEntity, TResult>> expression)
  84. where TEntity : new()
  85. where TResult : new()
  86. {
  87. RefAsync<int> total = 0;
  88. var items = await query.ToPageListAsync(pageIndex, pageSize, total, expression);
  89. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  90. }
  91. /// <summary>
  92. /// 分页拓展
  93. /// </summary>
  94. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  95. /// <param name="pageIndex">当前页码,从1开始</param>
  96. /// <param name="pageSize">页码容量</param>
  97. /// <returns></returns>
  98. public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  99. where TEntity : new()
  100. {
  101. RefAsync<int> total = 0;
  102. var items = await query.ToPageListAsync(pageIndex, pageSize, total);
  103. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  104. }
  105. /// <summary>
  106. /// 分页拓展
  107. /// </summary>
  108. /// <param name="list">集合对象</param>
  109. /// <param name="pageIndex">当前页码,从1开始</param>
  110. /// <param name="pageSize">页码容量</param>
  111. /// <returns></returns>
  112. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this IEnumerable<TEntity> list, int pageIndex, int pageSize)
  113. where TEntity : new()
  114. {
  115. var total = list.Count();
  116. var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  117. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  118. }
  119. /// <summary>
  120. /// 创建 <see cref="SqlSugarPagedList{TEntity}"/> 对象
  121. /// </summary>
  122. /// <typeparam name="TEntity"></typeparam>
  123. /// <param name="items">分页内容的对象集合</param>
  124. /// <param name="total">总条数</param>
  125. /// <param name="pageIndex">当前页码,从1开始</param>
  126. /// <param name="pageSize">页码容量</param>
  127. /// <returns></returns>
  128. private static SqlSugarPagedList<TEntity> CreateSqlSugarPagedList<TEntity>(IEnumerable<TEntity> items, int total, int pageIndex, int pageSize) where TEntity : new()
  129. {
  130. var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
  131. return new SqlSugarPagedList<TEntity>
  132. {
  133. Page = pageIndex,
  134. PageSize = pageSize,
  135. Items = items,
  136. Total = total,
  137. TotalPages = totalPages,
  138. HasNextPage = pageIndex < totalPages,
  139. HasPrevPage = pageIndex - 1 > 0
  140. };
  141. }
  142. }