SqlSugarPagedList.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core;
  10. /// <summary>
  11. /// 分页泛型集合
  12. /// </summary>
  13. /// <typeparam name="TEntity"></typeparam>
  14. public class SqlSugarPagedList<TEntity>
  15. {
  16. /// <summary>
  17. /// 页码
  18. /// </summary>
  19. public int Page { get; set; }
  20. /// <summary>
  21. /// 页容量
  22. /// </summary>
  23. public int PageSize { get; set; }
  24. /// <summary>
  25. /// 总条数
  26. /// </summary>
  27. public int Total { get; set; }
  28. /// <summary>
  29. /// 总页数
  30. /// </summary>
  31. public int TotalPages { get; set; }
  32. /// <summary>
  33. /// 当前页集合
  34. /// </summary>
  35. public IEnumerable<TEntity> Items { get; set; }
  36. /// <summary>
  37. /// 是否有上一页
  38. /// </summary>
  39. public bool HasPrevPage { get; set; }
  40. /// <summary>
  41. /// 是否有下一页
  42. /// </summary>
  43. public bool HasNextPage { get; set; }
  44. }
  45. /// <summary>
  46. /// 分页拓展类
  47. /// </summary>
  48. public static class SqlSugarPagedExtensions
  49. {
  50. /// <summary>
  51. /// 分页拓展
  52. /// </summary>
  53. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  54. /// <param name="pageIndex">当前页码,从1开始</param>
  55. /// <param name="pageSize">页码容量</param>
  56. /// <param name="expression">查询结果 Select 表达式</param>
  57. /// <returns></returns>
  58. public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  59. Expression<Func<TEntity, TResult>> expression)
  60. {
  61. var total = 0;
  62. var items = query.ToPageList(pageIndex, pageSize, ref total, expression);
  63. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  64. }
  65. /// <summary>
  66. /// 分页拓展
  67. /// </summary>
  68. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  69. /// <param name="pageIndex">当前页码,从1开始</param>
  70. /// <param name="pageSize">页码容量</param>
  71. /// <returns></returns>
  72. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  73. {
  74. var total = 0;
  75. var items = query.ToPageList(pageIndex, pageSize, ref total);
  76. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  77. }
  78. /// <summary>
  79. /// 分页拓展
  80. /// </summary>
  81. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  82. /// <param name="pageIndex">当前页码,从1开始</param>
  83. /// <param name="pageSize">页码容量</param>
  84. /// <param name="expression">查询结果 Select 表达式</param>
  85. /// <returns></returns>
  86. public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
  87. Expression<Func<TEntity, TResult>> expression)
  88. {
  89. RefAsync<int> total = 0;
  90. var items = await query.ToPageListAsync(pageIndex, pageSize, total, expression);
  91. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  92. }
  93. /// <summary>
  94. /// 分页拓展
  95. /// </summary>
  96. /// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
  97. /// <param name="pageIndex">当前页码,从1开始</param>
  98. /// <param name="pageSize">页码容量</param>
  99. /// <returns></returns>
  100. public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
  101. {
  102. RefAsync<int> total = 0;
  103. var items = await query.ToPageListAsync(pageIndex, pageSize, total);
  104. return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
  105. }
  106. /// <summary>
  107. /// 分页拓展
  108. /// </summary>
  109. /// <param name="list">集合对象</param>
  110. /// <param name="pageIndex">当前页码,从1开始</param>
  111. /// <param name="pageSize">页码容量</param>
  112. /// <returns></returns>
  113. public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this IEnumerable<TEntity> list, int pageIndex, int pageSize)
  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)
  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. }