package com.sensor.sellCabinet.util;

import lombok.Data;

import java.util.Collections;
import java.util.List;

@Data
public class Paging {

private Integer total;//总条数
private Integer totalPage;//总页数
private Integer size;//每页条数
private Integer current;//当前页码
private Integer queryIndex;//当前页从第几条开始查

public static Paging pagination(Integer total,Integer current,Integer pageSize){
if(current==null || current<=0){
current=1;
}
if(pageSize==null || pageSize<=0){
pageSize=10;
}
Paging page = new Paging();
page.setTotal(total);
Integer totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
page.setTotalPage(totalPage);
page.setCurrent(current);
page.setSize(pageSize);
page.setQueryIndex(pageSize * (current-1));
return page;
}

public <T> List<T> subListt(List<T> t){
int fromIndex = queryIndex;
int toIndex = 0;
if (fromIndex + getSize() >= total){
toIndex = total;
}else {
toIndex = fromIndex + getSize();
}
if (fromIndex > toIndex){
return Collections.EMPTY_LIST;
}else{
return t.subList(fromIndex,toIndex);
}
}
}


 

调用案例



Paging paging = Paging.pagination(normal.size(),current,size);
data.put("normal",paging.subListt(normal));