P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package foundation.dao;
 
import java.util.HashSet;
import java.util.Set;
 
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.persist.NamedSQL;
import foundation.server.config.Configer;
import foundation.server.config.DBaseType;
import foundation.variant.provider.DataEvent;
import foundation.variant.provider.IVariantsProvider;
import foundation.variant.provider.VariantProviderType;
 
public class Page implements IVariantsProvider, IJSONProvider {  
    
    public static String ProviderName = "page";
    public static Set<String> VariantNames;
    public static int Default_Page_Size;
    private DBaseType dbaseType;
    private int pageSize;  
    private int recordCount;  
    private int pageNo;
    private boolean empty;
  
    static {
        VariantNames = new HashSet<String>();
        VariantNames.add("pagesize");
        VariantNames.add("recordcount");
        VariantNames.add("pageno");
        VariantNames.add("beginno");   
        VariantNames.add(NamedSQL.Param_Page_limit);
        VariantNames.add(NamedSQL.Param_Page_RowNo);
        
        Default_Page_Size = Configer.getInt("PageDefaultSize", 20);
    }
    
    public Page() {
        this(null);
    } 
    
    public Page(DBaseType dbaseType) { 
        if (dbaseType == null) {
            dbaseType = DBaseType.getMain();
        }
        
        this.pageSize = Default_Page_Size;
        this.pageNo = 1;
        this.recordCount = -1;
        this.empty = false;
        this.dbaseType = dbaseType;
    }
 
    public int getBeginRecordNo() { 
        int recordNo = pageSize * (pageNo - 1) + 1; 
        
        //?
        if (recordCount >= 0) {
            recordNo = Math.min(recordCount, recordNo);
        }
        
        return recordNo;  
    }  
    
    public int getBeginRecordNo_1() { 
        int recordNo = getBeginRecordNo();
        return Math.max(recordNo - 1, 0);  
    } 
    
    public int getEndRecordNo() {  
        int recordNo = pageSize * pageNo;
        
        if (recordCount < 0) {
            return recordNo;
        }
        
        return Math.min(recordNo, recordCount);  
    }  
    
    public int getPageSize() {  
        return pageSize;  
    }  
  
    public int getPageNo() {  
        return pageNo;
    }  
  
    public int getRecordCount() {  
        return recordCount;  
    }  
  
    public int getPageCount() {  
        return (int)Math.ceil(recordCount * 1.0d / pageSize);
    }  
  
    public void setRecordCount(int count) {
        this.recordCount = count;
    }
  
    public void setPageSize(Integer value) { 
        if (value == null) {
            return;
        }
        
        if (value <= 0) {
            return;
        }
        
           pageSize = value;  
    }  
    
    public void setPageNo(Integer value) {
        if (value == null) {
            return;
        }
        
        if (value <= 0) {
            return;
        }
        
        this.pageNo = value;
    }
 
    public void set(String name, String value) {
        name = name.toLowerCase();
        
        if ("pageno".equals(name)) {
            setPageNo(Integer.parseInt(value));
        }
        else if ("pagesize".equals(name)) {
            setPageSize(Integer.parseInt(value));            
        }
    }
 
     public String toString() {
        StringBuilder result = new StringBuilder();
        
        result.append("size=").append(pageSize).append(",");
        result.append("recordCount=").append(recordCount).append(",");
        result.append("pageNo=").append(pageNo);
        
        return result.toString();
    }
 
    public boolean next() {
        //1. 如果没有总条数,直接页数增加
        if (recordCount < 0) {
            pageNo++;
            return true;
        }
        
        //2. 如果有总条数,根据总条数计算页数增加
        int pageCount = getPageCount();
        
        if (pageNo < pageCount) {
            pageNo++;
            return true;
        }
        
        return false;
    }
 
    public void setEmpty(boolean empty) {
        this.empty = empty;
    }
 
    public boolean isEmpty() {
        return empty;
    }
 
    public String toSQLString() {
        //1. MYSQL
        if (dbaseType.isMySQL()) {
            return " limit " + getBeginRecordNo_1() + ", " + pageSize;
        }
        
        //2. SQLServer
        if (dbaseType.isSQLServer()) {
            return " offset " + getBeginRecordNo_1() + " rows fetch next " + pageSize + " rows only";
        }
        
//        //3. Oracle
//        if (dbaseType.isOracle()) {
//            return " and rownum between " + getBeginRecordNo() + " and " + getEndRecordNo();
//        }
        
        return "";
    }
    
    @Override
    public String getProviderName() {
        return ProviderName;
    }
 
    @Override
    public VariantProviderType getProviderType() {
        return VariantProviderType.Transiant;
    }
    
    @Override
    public Set<String> getVariantNames() {
        return VariantNames;
    }
 
    @Override
    public boolean containsVariant(String name) {
        if (name == null) {
            return false;
        }
        
        name = name.toLowerCase();
        return VariantNames.contains(name);
    }
 
    @Override
    public Object getVariantValue(DataEvent dataEvent, String name) {
        if ("pageSize".equalsIgnoreCase(name)) {
            return pageSize;
        }
        else if ("recordCount".equalsIgnoreCase(name)) {
            return recordCount;
        }
        else if ("pageNo".equalsIgnoreCase(name)) {
            return pageNo;
        }
        else if ("beginNo".equalsIgnoreCase(name)) {
            int beginNo = getBeginRecordNo_1();
            return beginNo;
        }
        else if ("endNo".equalsIgnoreCase(name)) {
            int endNo = getEndRecordNo();
            return endNo;
        }
        else if (NamedSQL.Param_Page_limit.equalsIgnoreCase(name)) {
            return toSQLString();
        }
        else if (NamedSQL.Param_Page_RowNo.equalsIgnoreCase(name)) {
            return toSQLString();
        }
        
        return null;
    }
 
    
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        writeJSONBody(writer);
        writer.endObject();        
    }
 
    public void writeJSONBody(IJSONWriter writer) {
        writer.write("recordcount", getRecordCount());
        writer.write("pagecount", getPageCount());
        writer.write("pagesize", getPageSize());
        writer.write("pageno", getPageNo());
    }
 
}