PageResponse.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.ym.mec.education.base;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.ym.mec.education.enums.ReturnCodeEnum;
  4. import org.apache.commons.lang3.StringUtils;
  5. import java.io.Serializable;
  6. import java.util.Collections;
  7. import java.util.List;
  8. /**
  9. * @version V1.0
  10. * @Description: TODO
  11. * @date Date : 2019年09月25日 17:53
  12. */
  13. public class PageResponse<T> extends Response implements Serializable {
  14. private static final long serialVersionUID = 1L;
  15. private List<T> records;
  16. private Integer total;
  17. private Integer size;
  18. private Integer pages;
  19. private Integer current;
  20. private boolean searchCount;
  21. private boolean optimizeCount;
  22. private String orderByField;
  23. private boolean isAsc;
  24. public PageResponse() {
  25. this.records = Collections.emptyList();
  26. this.size = 10;
  27. this.current = 1;
  28. this.searchCount = true;
  29. this.optimizeCount = false;
  30. this.isAsc = true;
  31. }
  32. public static PageResponse success(IPage page) {
  33. PageResponse pageResponse = new PageResponse(Math.toIntExact(page.getCurrent()), Math.toIntExact(page.getSize()));
  34. pageResponse.setReturnCode(ReturnCodeEnum.CODE_200.getCode());
  35. pageResponse.setMessage(ReturnCodeEnum.CODE_200.getValue());
  36. pageResponse.setRecords(page.getRecords());
  37. pageResponse.setTotal(Math.toIntExact(page.getTotal()));
  38. return pageResponse;
  39. }
  40. public PageResponse(Integer current, Integer size) {
  41. this(current, size, true);
  42. }
  43. public PageResponse(Integer current, Integer size, String orderByField) {
  44. this.records = Collections.emptyList();
  45. this.setOrderByField(orderByField);
  46. }
  47. public PageResponse(Integer current, Integer size, boolean searchCount) {
  48. this.records = Collections.emptyList();
  49. this.size = 10;
  50. this.current = 1;
  51. this.searchCount = true;
  52. this.optimizeCount = false;
  53. this.isAsc = true;
  54. if (current > 1) {
  55. this.current = current;
  56. }
  57. this.size = size;
  58. this.searchCount = searchCount;
  59. }
  60. public List<T> getRecords() {
  61. return this.records;
  62. }
  63. public void setRecords(List<T> records) {
  64. this.records = records;
  65. }
  66. protected static Integer offsetCurrent(Integer current, Integer size) {
  67. return current > 0 ? (current - 1) * size : 0;
  68. }
  69. public Integer getOffsetCurrent() {
  70. return offsetCurrent(this.current, this.size);
  71. }
  72. public boolean hasPrevious() {
  73. return this.current > 1;
  74. }
  75. public boolean hasNext() {
  76. return this.current < this.pages;
  77. }
  78. public Integer getTotal() {
  79. return this.total;
  80. }
  81. public void setTotal(Integer total) {
  82. this.total = total;
  83. }
  84. public Integer getSize() {
  85. return this.size;
  86. }
  87. public void setSize(Integer size) {
  88. this.size = size;
  89. }
  90. public Integer getPages() {
  91. if (this.size != null && 0 != this.size && this.total != null) {
  92. this.pages = this.total / this.size;
  93. if (this.total % this.size != 0) {
  94. this.pages = this.pages + 1;
  95. }
  96. return this.pages;
  97. } else {
  98. return null;
  99. }
  100. }
  101. public void setCurrent(Integer current) {
  102. this.current = current;
  103. }
  104. public Integer getCurrent() {
  105. return this.current;
  106. }
  107. public boolean isSearchCount() {
  108. return this.searchCount;
  109. }
  110. public void setSearchCount(boolean searchCount) {
  111. this.searchCount = searchCount;
  112. }
  113. public boolean isOptimizeCount() {
  114. return this.optimizeCount;
  115. }
  116. public void setOptimizeCount(boolean optimizeCount) {
  117. this.optimizeCount = optimizeCount;
  118. }
  119. public String getOrderByField() {
  120. return this.orderByField;
  121. }
  122. public void setOrderByField(String orderByField) {
  123. if (StringUtils.isNotEmpty(orderByField)) {
  124. this.orderByField = orderByField;
  125. }
  126. }
  127. public boolean isAsc() {
  128. return this.isAsc;
  129. }
  130. public void setAsc(boolean isAsc) {
  131. this.isAsc = isAsc;
  132. }
  133. @Override
  134. public String toString() {
  135. StringBuffer pg = new StringBuffer();
  136. pg.append(" Page:{ [").append(super.toString()).append("], ");
  137. if (this.records != null) {
  138. pg.append("records-size:").append(this.records.size());
  139. } else {
  140. pg.append("records is null");
  141. }
  142. return pg.append(" }").toString();
  143. }
  144. }