提交 | 用户 | age
|
812cb5
|
1 |
package com.hz.his.util; |
F |
2 |
|
|
3 |
import com.github.pagehelper.PageInfo; |
|
4 |
|
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
/** |
|
8 |
* @author fwq |
|
9 |
* PageHelperTool |
|
10 |
* 分页工具类:对查询结果处理后再进行分页 |
|
11 |
*/ |
|
12 |
public class PageHelperUtil { |
|
13 |
|
|
14 |
public static <T> PageInfo<T> pageInfo(int currentPage, int pageSize, List list) { |
|
15 |
|
|
16 |
int fromIndex = 0; |
|
17 |
int toIndex = 0; |
|
18 |
//取出总记录数 |
|
19 |
int total = list.size(); |
|
20 |
//当前页码 |
|
21 |
currentPage = (currentPage <= 0) ? 1 : currentPage; |
|
22 |
//当前页条目数 |
|
23 |
pageSize = (pageSize <= 0) ? 10 : (Math.min(pageSize, total)); |
|
24 |
|
|
25 |
if (total / pageSize == 0 && total % pageSize > 0) { |
|
26 |
fromIndex = 0; |
|
27 |
toIndex = total; |
|
28 |
} else { |
|
29 |
if (total / pageSize >= 1 && total % pageSize >= 0) { |
|
30 |
fromIndex = pageSize * (currentPage - 1); |
|
31 |
if (pageSize * currentPage >= total) { |
|
32 |
toIndex = total; |
|
33 |
} else { |
|
34 |
toIndex = pageSize * currentPage; |
|
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
List pageList = list.subList(fromIndex, toIndex); |
|
40 |
PageInfo pageInfo = new PageInfo<>(pageList); |
|
41 |
return initPageInfoObj(currentPage, total, pageSize, pageInfo); |
|
42 |
} |
|
43 |
|
|
44 |
private static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) { |
|
45 |
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage); |
|
46 |
pageInfo.setTotal(total); |
|
47 |
pageInfo.setPageNum(currentPage); |
|
48 |
pageInfo.setPages((total + pageSize - 1) / pageSize); |
|
49 |
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage); |
|
50 |
pageInfo.setIsFirstPage(currentPage == 1); |
|
51 |
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize); |
|
52 |
pageInfo.setHasPreviousPage(currentPage != 1); |
|
53 |
pageInfo.setHasNextPage(currentPage != (total + pageSize - 1) / pageSize); |
|
54 |
return calcNavigatepageNums(pageInfo); |
|
55 |
} |
|
56 |
|
|
57 |
private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) { |
|
58 |
//当总页数小于或等于导航页码数时 |
|
59 |
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) { |
|
60 |
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]); |
|
61 |
for (int i = 0; i < pageInfo.getPages(); i++) { |
|
62 |
pageInfo.getNavigatepageNums()[i] = i + 1; |
|
63 |
} |
|
64 |
} else { //当总页数大于导航页码数时 |
|
65 |
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]); |
|
66 |
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2; |
|
67 |
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2; |
|
68 |
|
|
69 |
if (startNum < 1) { |
|
70 |
startNum = 1; |
|
71 |
//(最前navigatePages页 |
|
72 |
for (int i = 0; i < pageInfo.getNavigatePages(); i++) { |
|
73 |
pageInfo.getNavigatepageNums()[i] = startNum++; |
|
74 |
} |
|
75 |
} else if (endNum > pageInfo.getPages()) { |
|
76 |
endNum = pageInfo.getPages(); |
|
77 |
//最后navigatePages页 |
|
78 |
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) { |
|
79 |
pageInfo.getNavigatepageNums()[i] = endNum--; |
|
80 |
} |
|
81 |
} else { |
|
82 |
//所有中间页 |
|
83 |
for (int i = 0; i < pageInfo.getNavigatePages(); i++) { |
|
84 |
pageInfo.getNavigatepageNums()[i] = startNum++; |
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
return pageInfo; |
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
} |
|
93 |
|