PageUtils.java
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.canrd.webmagic.common.utils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.canrd.webmagic.domain.vo.BasePageVO;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.*;
/**
* @Author: dengbin
* @Date: 2020/9/11
* 分页工具类
*/
public class PageUtils implements Serializable {
private static final long serialVersionUID = 4359709211352400087L;
private static final String PAGE_INDEX = "pageNo";
private static final String PAGE_SIZE = "pageSize";
private static final String TOTAL_ROWS = "total";
private static final String TOTAL_PAGES = "pages";
private static final String RECORDS = "records";
private static final int DEFAULT_PAGE_INDEX = 1;
private static final int DEFAULT_PAGE_SIZE = 10;
private static final int DEFAULT_MAP_CAPACITY = 5;
/**
* 统一分页返回对象
*
* @param list
* @return
*/
public static Map<String, Object> getUnifiedPageReturn(IPage list) {
if (list == null) {
return getUnifiedEmptyPage(DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE);
}
Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY);
resultMap.put(PAGE_INDEX, list.getCurrent());
resultMap.put(PAGE_SIZE, list.getSize());
resultMap.put(RECORDS, list.getRecords());
resultMap.put(TOTAL_PAGES, list.getPages());
resultMap.put(TOTAL_ROWS, list.getTotal());
return resultMap;
}
/**
* 返回空分页信息给前端
* 返回空或其他字段不满足前端要求,会报错
*
* @return
*/
public static Map<String, Object> getUnifiedEmptyPage(int pageIndex, int pageSize) {
Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY);
resultMap.put(PAGE_INDEX, pageIndex);
resultMap.put(PAGE_SIZE, pageSize);
resultMap.put(RECORDS, new ArrayList<>());
resultMap.put(TOTAL_PAGES, 0);
resultMap.put(TOTAL_ROWS, 0);
return resultMap;
}
public static Map<String, Object> getUnifiedEmptyPage(BasePageVO pageVO) {
return getUnifiedEmptyPage(pageVO.getCurrent(), pageVO.getSize());
}
/**
* 将旧的分页转换成新的分页对象
*
* @param oldPage 旧的分页对象,例如:xxDo的分页对象
* @param records 新的分页对象,例如:xxVo的分页对象
* @param <T> 新的分页对象的类型
* @return 统一的返回的分页对象
*/
public static <T> Map<String, Object> transferPage(IPage<?> oldPage, List<T> records) {
Page<T> newPage = new Page<>();
newPage.setCurrent(oldPage.getCurrent());
newPage.setSize(oldPage.getSize());
newPage.setRecords(records);
newPage.setPages(oldPage.getPages());
newPage.setTotal(oldPage.getTotal());
return getUnifiedPageReturn(newPage);
}
/**
* 统一分页返回对象
*
* @param records 分页数据
* @param pageVO 分页参数
* @return
*/
public static Map<String, Object> getPageReturn(List records, BasePageVO pageVO) {
if (records == null) {
return getUnifiedEmptyPage(DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE);
}
if (CollectionUtils.isEmpty(records)) {
return getUnifiedEmptyPage(Math.toIntExact(pageVO.getCurrent()), Math.toIntExact(pageVO.getSize()));
}
Map<String, Object> resultMap = new HashMap<>(DEFAULT_MAP_CAPACITY);
resultMap.put(PAGE_INDEX, pageVO.getCurrent());
resultMap.put(PAGE_SIZE, pageVO.getSize());
resultMap.put(RECORDS, records);
resultMap.put(TOTAL_PAGES, (pageVO.getTotal() + pageVO.getSize() - 1) / pageVO.getSize());
resultMap.put(TOTAL_ROWS, pageVO.getTotal());
return resultMap;
}
public static <T> Page<T> getRAMPageReturn(Integer current, Integer size, List<T> records) {
if (Objects.isNull(current)) {
current = 1;
}
if (Objects.isNull(size)) {
size = 10;
}
int startIndex = (current - 1) * size;
int endIndex = Math.min(current * size, records.size());
if (startIndex > endIndex) {
startIndex = endIndex;
}
Page<T> page = new Page<>();
page.setCurrent(current);
page.setSize(size);
page.setRecords(records.subList(startIndex, endIndex));
int totalPages = size == 0 ? 0 : records.size() % size == 0 ? (records.size() / size) : (records.size() / size + 1);
page.setPages(totalPages);
page.setTotal(records.size());
return page;
}
}