提交 | 用户 | 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 |
|
2e6720
|
14 |
public static <T> PageInfo<T> pageInfo(Integer currentPage, Integer pageSize, List list) { |
812cb5
|
15 |
|
F |
16 |
int fromIndex = 0; |
|
17 |
int toIndex = 0; |
|
18 |
//取出总记录数 |
|
19 |
int total = list.size(); |
|
20 |
//当前页码 |
2e6720
|
21 |
if (currentPage == null || currentPage <= 0) { |
F |
22 |
currentPage = 1; |
|
23 |
} |
812cb5
|
24 |
//当前页条目数 |
2e6720
|
25 |
if (pageSize == null || pageSize <= 0 || pageSize > 500) { |
F |
26 |
pageSize = 20; |
|
27 |
} |
812cb5
|
28 |
|
F |
29 |
if (total / pageSize == 0 && total % pageSize > 0) { |
|
30 |
fromIndex = 0; |
|
31 |
toIndex = total; |
|
32 |
} else { |
|
33 |
if (total / pageSize >= 1 && total % pageSize >= 0) { |
|
34 |
fromIndex = pageSize * (currentPage - 1); |
|
35 |
if (pageSize * currentPage >= total) { |
|
36 |
toIndex = total; |
|
37 |
} else { |
|
38 |
toIndex = pageSize * currentPage; |
|
39 |
} |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
43 |
List pageList = list.subList(fromIndex, toIndex); |
|
44 |
PageInfo pageInfo = new PageInfo<>(pageList); |
|
45 |
return initPageInfoObj(currentPage, total, pageSize, pageInfo); |
|
46 |
} |
|
47 |
|
|
48 |
private static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) { |
|
49 |
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage); |
|
50 |
pageInfo.setTotal(total); |
|
51 |
pageInfo.setPageNum(currentPage); |
|
52 |
pageInfo.setPages((total + pageSize - 1) / pageSize); |
|
53 |
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage); |
|
54 |
pageInfo.setIsFirstPage(currentPage == 1); |
|
55 |
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize); |
|
56 |
pageInfo.setHasPreviousPage(currentPage != 1); |
|
57 |
pageInfo.setHasNextPage(currentPage != (total + pageSize - 1) / pageSize); |
|
58 |
return calcNavigatepageNums(pageInfo); |
|
59 |
} |
|
60 |
|
|
61 |
private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) { |
|
62 |
//当总页数小于或等于导航页码数时 |
|
63 |
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) { |
|
64 |
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]); |
|
65 |
for (int i = 0; i < pageInfo.getPages(); i++) { |
|
66 |
pageInfo.getNavigatepageNums()[i] = i + 1; |
|
67 |
} |
|
68 |
} else { //当总页数大于导航页码数时 |
|
69 |
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]); |
|
70 |
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2; |
|
71 |
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2; |
|
72 |
|
|
73 |
if (startNum < 1) { |
|
74 |
startNum = 1; |
|
75 |
//(最前navigatePages页 |
|
76 |
for (int i = 0; i < pageInfo.getNavigatePages(); i++) { |
|
77 |
pageInfo.getNavigatepageNums()[i] = startNum++; |
|
78 |
} |
|
79 |
} else if (endNum > pageInfo.getPages()) { |
|
80 |
endNum = pageInfo.getPages(); |
|
81 |
//最后navigatePages页 |
|
82 |
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) { |
|
83 |
pageInfo.getNavigatepageNums()[i] = endNum--; |
|
84 |
} |
|
85 |
} else { |
|
86 |
//所有中间页 |
|
87 |
for (int i = 0; i < pageInfo.getNavigatePages(); i++) { |
|
88 |
pageInfo.getNavigatepageNums()[i] = startNum++; |
|
89 |
} |
|
90 |
} |
|
91 |
} |
|
92 |
return pageInfo; |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
} |
|
97 |
|