123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package com.kcim.util;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.fasterxml.jackson.annotation.JsonInclude;
- import java.io.Serializable;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 分页工具类
- *
- * @author Mark sunlightcs@gmail.com
- */
- public class PageUtils implements Serializable {
- private static final long serialVersionUID = 1L;
- /**
- * 总记录数
- */
- private int totalCount;
- /**
- * 每页记录数
- */
- private int pageSize;
- /**
- * 总页数
- */
- private int totalPage;
- /**
- * 当前页数
- */
- private int current;
- /**
- * 列表数据
- */
- private List<?> list;
- /**
- * 归集后的数据总额
- */
- @JsonInclude(JsonInclude.Include.NON_NULL)
- private BigDecimal totalAmount;
-
- /**
- * 分页
- * @param list 列表数据
- * @param totalCount 总记录数
- * @param pageSize 每页记录数
- * @param current 当前页数
- */
- public PageUtils(List<?> list, int totalCount, int pageSize, int current) {
- this.list = list;
- this.totalCount = totalCount;
- this.pageSize = pageSize;
- this.current = current ;
- this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
- }
- /**
- * 分页
- * @param list 列表数据
- * @param totalCount 总记录数
- * @param pageSize 每页记录数
- * @param current 当前页数
- * @param totalAmount 总金额
- */
- public PageUtils(List<?> list, int totalCount, int pageSize, int current,BigDecimal totalAmount) {
- this.list = list;
- this.totalCount = totalCount;
- this.pageSize = pageSize;
- this.current = current ;
- this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
- this.totalAmount = totalAmount;
- }
- /**
- * 分页
- */
- public PageUtils(IPage<?> page) {
- this.list = page.getRecords();
- this.totalCount = (int)page.getTotal();
- this.pageSize = (int)page.getSize();
- this.current = (int)page.getCurrent();
- this.totalPage = (int)page.getPages();
- }
- public int getTotalCount() {
- return totalCount;
- }
- public void setTotalCount(int totalCount) {
- this.totalCount = totalCount;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getCurrent() {
- return current;
- }
- public void setCurrent(int current) {
- this.current = current;
- }
- public List<?> getList() {
- return list;
- }
- public void setList(List<?> list) {
- this.list = list;
- }
- public static long getSerialVersionUID() {
- return serialVersionUID;
- }
- public BigDecimal getTotalAmount() {
- return totalAmount;
- }
- public void setTotalAmount(BigDecimal totalAmount) {
- this.totalAmount = totalAmount;
- }
- public static <T> List<T> getPageList(List<T> list,int current,int pageSize){
- if(list ==null){
- return new ArrayList<>();
- }
- if(list.size() == 0){
- return list;
- }
- if(current ==0){
- current = 1;
- }
- //记录总数
- int count = list.size();
- //页数
- int pageCount = 0;
- if(count % pageSize==0){
- pageCount = count / pageSize;
- }else {
- pageCount = count / pageSize + 1;
- }
- int fromIndex = 0;
- int toIndex = 0;
- if(current>pageCount){
- return new ArrayList<>();
- }
- if(current != pageCount){
- fromIndex = (current - 1)*pageSize;
- toIndex = fromIndex + pageSize;
- }else {
- fromIndex = (current - 1)*pageSize;
- toIndex = count;
- }
- return list.subList(fromIndex,toIndex);
- }
- }
|