唐耀东
2021-12-01 ac56d8fbfbe82096da31ed9f73bc29308284edfe
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
package com.ruoyi.common.core.page;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.Data;
import lombok.experimental.Accessors;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
/**
 * 分页 Page 增强对象
 *
 * @param <T> 数据库实体
 * @param <K> vo实体
 * @author Lion Li
 */
@Data
@Accessors(chain = true)
public class PagePlus<T,K> implements IPage<T> {
 
    /**
     * domain实体列表
     */
    private List<T> records = Collections.emptyList();
 
    /**
     * vo实体列表
     */
    private List<K> recordsVo = Collections.emptyList();
 
    /**
     * 总数
     */
    private long total = 0L;
 
    /**
     * 页长度
     */
    private long size = 10L;
 
    /**
     * 当前页
     */
    private long current = 1L;
 
    /**
     * 排序字段信息
     */
    private List<OrderItem> orders = new ArrayList<>();
 
    /**
     * 自动优化 COUNT SQL
     */
    private boolean optimizeCountSql = true;
 
    /**
     * 是否进行 count 查询
     */
    private boolean isSearchCount = true;
 
    /**
     * 是否命中count缓存
     */
    private boolean hitCount = false;
 
    /**
     * countId
     */
    private String countId;
 
    /**
     * 最大limit
     */
    private Long maxLimit;
 
    public PagePlus() {
    }
 
    public PagePlus(long current, long size) {
        this(current, size, 0L);
    }
 
    public PagePlus(long current, long size, long total) {
        this(current, size, total, true);
    }
 
    public PagePlus(long current, long size, boolean isSearchCount) {
        this(current, size, 0L, isSearchCount);
    }
 
    public PagePlus(long current, long size, long total, boolean isSearchCount) {
        if (current > 1L) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.isSearchCount = isSearchCount;
    }
 
    @Override
    public String countId() {
        return this.getCountId();
    }
 
    @Override
    public Long maxLimit() {
        return this.getMaxLimit();
    }
 
    public PagePlus<T, K> addOrder(OrderItem... items) {
        this.orders.addAll(Arrays.asList(items));
        return this;
    }
 
    public PagePlus<T, K> addOrder(List<OrderItem> items) {
        this.orders.addAll(items);
        return this;
    }
 
    @Override
    public List<OrderItem> orders() {
        return this.getOrders();
    }
 
    @Override
    public boolean optimizeCountSql() {
        return this.optimizeCountSql;
    }
 
    @Override
    public long getPages() {
        // 解决 github issues/3208
        return IPage.super.getPages();
    }
 
    public static <T,K> PagePlus<T,K> of(long current, long size) {
        return of(current, size, 0);
    }
 
    public static <T,K> PagePlus<T,K> of(long current, long size, long total) {
        return of(current, size, total, true);
    }
 
    public static <T,K> PagePlus<T,K> of(long current, long size, boolean searchCount) {
        return of(current, size, 0, searchCount);
    }
 
    public static <T,K> PagePlus<T,K> of(long current, long size, long total, boolean searchCount) {
        return new PagePlus<>(current, size, total, searchCount);
    }
 
}