PageUtils.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.kcim.util;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.fasterxml.jackson.annotation.JsonInclude;
  4. import java.io.Serializable;
  5. import java.math.BigDecimal;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. /**
  9. * 分页工具类
  10. *
  11. * @author Mark sunlightcs@gmail.com
  12. */
  13. public class PageUtils implements Serializable {
  14. private static final long serialVersionUID = 1L;
  15. /**
  16. * 总记录数
  17. */
  18. private int totalCount;
  19. /**
  20. * 每页记录数
  21. */
  22. private int pageSize;
  23. /**
  24. * 总页数
  25. */
  26. private int totalPage;
  27. /**
  28. * 当前页数
  29. */
  30. private int current;
  31. /**
  32. * 列表数据
  33. */
  34. private List<?> list;
  35. /**
  36. * 归集后的数据总额
  37. */
  38. @JsonInclude(JsonInclude.Include.NON_NULL)
  39. private BigDecimal totalAmount;
  40. /**
  41. * 分页
  42. * @param list 列表数据
  43. * @param totalCount 总记录数
  44. * @param pageSize 每页记录数
  45. * @param current 当前页数
  46. */
  47. public PageUtils(List<?> list, int totalCount, int pageSize, int current) {
  48. this.list = list;
  49. this.totalCount = totalCount;
  50. this.pageSize = pageSize;
  51. this.current = current ;
  52. this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
  53. }
  54. /**
  55. * 分页
  56. * @param list 列表数据
  57. * @param totalCount 总记录数
  58. * @param pageSize 每页记录数
  59. * @param current 当前页数
  60. * @param totalAmount 总金额
  61. */
  62. public PageUtils(List<?> list, int totalCount, int pageSize, int current,BigDecimal totalAmount) {
  63. this.list = list;
  64. this.totalCount = totalCount;
  65. this.pageSize = pageSize;
  66. this.current = current ;
  67. this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
  68. this.totalAmount = totalAmount;
  69. }
  70. /**
  71. * 分页
  72. */
  73. public PageUtils(IPage<?> page) {
  74. this.list = page.getRecords();
  75. this.totalCount = (int)page.getTotal();
  76. this.pageSize = (int)page.getSize();
  77. this.current = (int)page.getCurrent();
  78. this.totalPage = (int)page.getPages();
  79. }
  80. public int getTotalCount() {
  81. return totalCount;
  82. }
  83. public void setTotalCount(int totalCount) {
  84. this.totalCount = totalCount;
  85. }
  86. public int getPageSize() {
  87. return pageSize;
  88. }
  89. public void setPageSize(int pageSize) {
  90. this.pageSize = pageSize;
  91. }
  92. public int getTotalPage() {
  93. return totalPage;
  94. }
  95. public void setTotalPage(int totalPage) {
  96. this.totalPage = totalPage;
  97. }
  98. public int getCurrent() {
  99. return current;
  100. }
  101. public void setCurrent(int current) {
  102. this.current = current;
  103. }
  104. public List<?> getList() {
  105. return list;
  106. }
  107. public void setList(List<?> list) {
  108. this.list = list;
  109. }
  110. public static long getSerialVersionUID() {
  111. return serialVersionUID;
  112. }
  113. public BigDecimal getTotalAmount() {
  114. return totalAmount;
  115. }
  116. public void setTotalAmount(BigDecimal totalAmount) {
  117. this.totalAmount = totalAmount;
  118. }
  119. public static <T> List<T> getPageList(List<T> list,int current,int pageSize){
  120. if(list ==null){
  121. return new ArrayList<>();
  122. }
  123. if(list.size() == 0){
  124. return list;
  125. }
  126. if(current ==0){
  127. current = 1;
  128. }
  129. //记录总数
  130. int count = list.size();
  131. //页数
  132. int pageCount = 0;
  133. if(count % pageSize==0){
  134. pageCount = count / pageSize;
  135. }else {
  136. pageCount = count / pageSize + 1;
  137. }
  138. int fromIndex = 0;
  139. int toIndex = 0;
  140. if(current>pageCount){
  141. return new ArrayList<>();
  142. }
  143. if(current != pageCount){
  144. fromIndex = (current - 1)*pageSize;
  145. toIndex = fromIndex + pageSize;
  146. }else {
  147. fromIndex = (current - 1)*pageSize;
  148. toIndex = count;
  149. }
  150. return list.subList(fromIndex,toIndex);
  151. }
  152. }