add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.dobbinsoft.fw.support.model;
 
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.Data;
import lombok.NoArgsConstructor;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2018-08-15
 * Time: 下午8:12
 */
@Data
@NoArgsConstructor
public class Page<T> implements Serializable, IPage<T> {
 
    private List<T> items;
 
    private int pageNo;
 
    private int pageSize;
 
    private long count;
 
    private List<OrderItem> orderItems;
 
    public Page(List<T> items, int pageNo, int pageSize, long count) {
        this.items = items;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.count = count;
    }
 
    public long getTotalPageNo() {
        return count / pageSize + (count % pageSize == 0 ? 0 : 1);
    }
 
    public List<T> getItems() {
        return items;
    }
 
    public boolean hasNext() {
        return getPageNo() < getTotalPageNo();
    }
 
    public boolean hasPrevious() {
        return getPageNo() > 1;
    }
 
    public String getMsg() {
        return "第" + pageNo + "页,共" + count + "条";
    }
 
    public int getCode() {
        return 0;
    }
 
    public long getCount() {
        return this.count;
    }
 
    /** 实现MP的IPage接口,以另一种形式展示 **/
 
    @Override
    @JSONField(serialize = false)
    public List<OrderItem> orders() {
        return orderItems;
    }
 
    @Override
    @JSONField(serialize = false)
    public List<T> getRecords() {
        return this.items;
    }
 
    @Override
    @JSONField(serialize = false)
    public IPage<T> setRecords(List<T> records) {
        this.setItems(records);
        return this;
    }
 
    public long getTotal() {
        return getCount();
    }
 
    @Override
    public IPage<T> setTotal(long total) {
        this.setCount(total);
        return this;
    }
 
    @Override
    @JSONField(serialize = false)
    public long getSize() {
        return this.getPageSize();
    }
 
    @Override
    @JSONField(serialize = false)
    public IPage<T> setSize(long size) {
        this.setPageSize((int) size);
        return this;
    }
 
    @Override
    @JSONField(serialize = false)
    public long getCurrent() {
        return this.getPageNo();
    }
 
    @Override
    @JSONField(serialize = false)
    public IPage<T> setCurrent(long current) {
        this.setPageNo((int) current);
        return this;
    }
 
    /**
     * 将分页中的 T 数据类型转换为 R 数据类型
     * @param transMethod
     * @param <R>
     * @return
     */
    public <R> Page<R> trans(Function<T, R> transMethod) {
        this.setItems((List) this.items.stream().map(transMethod).collect(Collectors.toList()));
        return (Page<R>) this;
    }
 
    /**
     * 正向排序
     * @param columns
     * @return
     */
    public Page<T> ases(String... columns) {
        this.orderItems = OrderItem.ascs(columns);
        return this;
    }
 
    /**
     * 正向排序
     * @param columns
     * @return
     */
    public Page<T> descs(String... columns) {
        this.orderItems = OrderItem.descs(columns);
        return this;
    }
 
    public Page<T> sort(boolean isAsc, String...columns) {
        if (isAsc) {
            return ases(columns);
        } else {
            return descs(columns);
        }
    }
 
    public static <T> Page<T> div(int pageNo, int pageSize, Class<T> clazz) {
        Page<T> page = new Page<T>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        return page;
    }
 
    public <R> Page<R> replace(List<R> items) {
        this.setItems((List) items);
        return (Page<R>) this;
    }
 
    private static List<?> emptyArray = new ArrayList<>();
 
    public static Page emptyPage = new Page(emptyArray, 1, 15, 0);
 
}