package com.hz.his.util;
|
|
import com.github.pagehelper.PageInfo;
|
|
import java.util.List;
|
|
/**
|
* @author fwq
|
* PageHelperTool
|
* 分页工具类:对查询结果处理后再进行分页
|
*/
|
public class PageHelperUtil {
|
|
public static <T> PageInfo<T> pageInfo(Integer currentPage, Integer pageSize, List list) {
|
|
int fromIndex = 0;
|
int toIndex = 0;
|
//取出总记录数
|
int total = list.size();
|
//当前页码
|
if (currentPage == null || currentPage <= 0) {
|
currentPage = 1;
|
}
|
//当前页条目数
|
if (pageSize == null || pageSize <= 0 || pageSize > 500) {
|
pageSize = 20;
|
}
|
|
if (total / pageSize == 0 && total % pageSize > 0) {
|
fromIndex = 0;
|
toIndex = total;
|
} else {
|
if (total / pageSize >= 1 && total % pageSize >= 0) {
|
fromIndex = pageSize * (currentPage - 1);
|
if (pageSize * currentPage >= total) {
|
toIndex = total;
|
} else {
|
toIndex = pageSize * currentPage;
|
}
|
}
|
}
|
|
List pageList = list.subList(fromIndex, toIndex);
|
PageInfo pageInfo = new PageInfo<>(pageList);
|
return initPageInfoObj(currentPage, total, pageSize, pageInfo);
|
}
|
|
private static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) {
|
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage);
|
pageInfo.setTotal(total);
|
pageInfo.setPageNum(currentPage);
|
pageInfo.setPages((total + pageSize - 1) / pageSize);
|
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage);
|
pageInfo.setIsFirstPage(currentPage == 1);
|
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize);
|
pageInfo.setHasPreviousPage(currentPage != 1);
|
pageInfo.setHasNextPage(currentPage != (total + pageSize - 1) / pageSize);
|
return calcNavigatepageNums(pageInfo);
|
}
|
|
private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) {
|
//当总页数小于或等于导航页码数时
|
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) {
|
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]);
|
for (int i = 0; i < pageInfo.getPages(); i++) {
|
pageInfo.getNavigatepageNums()[i] = i + 1;
|
}
|
} else { //当总页数大于导航页码数时
|
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]);
|
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2;
|
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2;
|
|
if (startNum < 1) {
|
startNum = 1;
|
//(最前navigatePages页
|
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
|
pageInfo.getNavigatepageNums()[i] = startNum++;
|
}
|
} else if (endNum > pageInfo.getPages()) {
|
endNum = pageInfo.getPages();
|
//最后navigatePages页
|
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) {
|
pageInfo.getNavigatepageNums()[i] = endNum--;
|
}
|
} else {
|
//所有中间页
|
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
|
pageInfo.getNavigatepageNums()[i] = startNum++;
|
}
|
}
|
}
|
return pageInfo;
|
}
|
|
|
}
|