package com.hfkj.common.utils; import com.github.pagehelper.Page; import com.github.pagehelper.PageInfo; import java.util.List; /** * 分页工具 */ public class PageUtil { /** * 初始化 * @param currentPage * @param total * @param pageSize * @param pageInfo * @param * @return */ public static PageInfo initPageInfoObj(int currentPage, int total, int pageSize, PageInfo pageInfo) { List list = pageInfo.getList(); int fromIndex = 0; int toIndex = 0; 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; } } } try { list = list.subList(fromIndex, toIndex); } catch (IndexOutOfBoundsException e) { fromIndex = 0; toIndex= pageSize; list = list.subList(fromIndex, toIndex); }catch(IllegalArgumentException e) { fromIndex = total-pageSize; toIndex =total; list = list.subList(fromIndex, toIndex); } pageInfo.setList(list); pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage); pageInfo.setTotal(total); pageInfo.setPageNum(currentPage); pageInfo.setPages((total + pageSize - 1) / pageSize); pageInfo.setNavigateLastPage((total + pageSize - 1) / pageSize); pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage); pageInfo.setIsFirstPage(currentPage == 1 ? true : false); pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize ? true : false); pageInfo.setHasPreviousPage(currentPage == 1 ? false : true); pageInfo.setHasNextPage(currentPage == (total + pageSize - 1) / pageSize ? false : true); return calcNavigatepageNums(pageInfo); } private static PageInfo calcNavigatepageNums(PageInfo 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; } public static PageInfo PageInfo2PageInfoVo(PageInfo

pageInfoPo) { // 创建Page对象,实际上是⼀个ArrayList类型的集合 Page page = new Page<>(pageInfoPo.getPageNum(), pageInfoPo.getPageSize()); page.setTotal(pageInfoPo.getTotal()); return new PageInfo<>(page); } }