3
3
import com .alibaba .fastjson .JSON ;
4
4
import com .alibaba .fastjson .JSONArray ;
5
5
import com .alibaba .fastjson .JSONObject ;
6
+ import lombok .Getter ;
7
+ import lombok .Setter ;
6
8
import org .springframework .data .domain .Sort ;
7
9
import org .springframework .util .StringUtils ;
8
10
import org .springframework .web .context .request .RequestContextHolder ;
12
14
import java .lang .reflect .Field ;
13
15
import java .util .ArrayList ;
14
16
import java .util .Base64 ;
15
- import java .util .Enumeration ;
16
17
import java .util .List ;
17
- import java .util .stream .Collectors ;
18
18
19
19
/**
20
20
* HttpServletRequest 请求参数解析成 PageRequest对象
@@ -46,17 +46,21 @@ public void setPageSize(int pageSize) {
46
46
this .removeKeys .add ("pageSize" );
47
47
}
48
48
49
- private String decode (String value ) {
50
- return new String (Base64 .getDecoder ().decode (value ));
51
- }
52
-
53
-
54
49
public void addSort (Sort sort ) {
55
50
pageRequest .addSort (sort );
56
51
}
57
52
58
53
public void removeFilter (String key ) {
59
54
pageRequest .removeFilter (key );
55
+ this .removeKeys .add (key );
56
+ }
57
+
58
+ public String getParameter (String key ) {
59
+ return request .getParameter (key );
60
+ }
61
+
62
+ public String [] getParameterValues (String key ) {
63
+ return request .getParameterValues (key );
60
64
}
61
65
62
66
public PageRequest addFilter (String key , Relation relation , Object ... value ) {
@@ -75,6 +79,12 @@ public PageRequest orFilters(Filter... filters) {
75
79
return pageRequest .orFilters (filters );
76
80
}
77
81
82
+
83
+ private String decode (String value ) {
84
+ return new String (Base64 .getDecoder ().decode (value ));
85
+ }
86
+
87
+
78
88
static class ClassContent {
79
89
80
90
private final Class <?> clazz ;
@@ -85,14 +95,20 @@ public ClassContent(Class<?> clazz, PageRequest pageRequest) {
85
95
this .pageRequest = pageRequest ;
86
96
}
87
97
98
+ public void addFilter (String key , Relation relation , String value ) {
99
+ Class <?> keyClass = getKeyType (key );
100
+ Object v = parseObject (value , keyClass );
101
+ pageRequest .addFilter (key , relation , v );
102
+ }
103
+
88
104
public void addFilter (String key , String value ) {
89
105
Class <?> keyClass = getKeyType (key );
90
106
Object v = parseObject (value , keyClass );
91
107
pageRequest .addFilter (key , Relation .EQUAL , v );
92
108
}
93
109
94
110
private Object parseObject (String value , Class <?> keyClass ) {
95
- if (value .getClass ().equals (keyClass )) {
111
+ if (value .getClass ().equals (keyClass )) {
96
112
return value ;
97
113
}
98
114
return JSON .parseObject (value , keyClass );
@@ -124,12 +140,37 @@ private Class<?> getKeyType(String key) {
124
140
125
141
}
126
142
143
+ @ Setter
144
+ @ Getter
145
+ static class ParamOperation {
146
+ private String key ;
147
+ private String type ;
148
+
149
+ public Relation getOperation () {
150
+ return Relation .valueOf (type );
151
+ }
152
+ }
153
+
154
+ private List <ParamOperation > loadParamOperations () {
155
+ String params = request .getParameter ("params" );
156
+ if (StringUtils .hasLength (params )) {
157
+ params = decode (params );
158
+ if (JSON .isValid (params )) {
159
+ removeKeys .add ("params" );
160
+ return JSON .parseArray (params , ParamOperation .class );
161
+ }
162
+ }
163
+ return null ;
164
+ }
165
+
127
166
public PageRequest toPageRequest (Class <?> clazz ) {
128
167
pageRequest .setCurrent (current );
129
168
pageRequest .setPageSize (pageSize );
130
169
131
170
ClassContent content = new ClassContent (clazz , pageRequest );
132
171
172
+ List <ParamOperation > loadParams = loadParamOperations ();
173
+
133
174
String sort = request .getParameter ("sort" );
134
175
if (StringUtils .hasLength (sort )) {
135
176
sort = decode (sort );
@@ -157,24 +198,34 @@ public PageRequest toPageRequest(Class<?> clazz) {
157
198
for (String key : jsonObject .keySet ()) {
158
199
JSONArray value = jsonObject .getJSONArray (key );
159
200
if (value != null && !value .isEmpty ()) {
160
- List <String > values = value .stream ().map (Object ::toString ).collect ( Collectors . toList () );
201
+ List <String > values = value .stream ().map (Object ::toString ).toList ();
161
202
content .addFilter (key , values );
162
203
}
163
204
}
164
205
}
165
206
}
166
207
167
- Enumeration <String > enumeration = request .getParameterNames ();
168
- while (enumeration .hasMoreElements ()) {
169
- String key = enumeration .nextElement ();
208
+
209
+ request .getParameterNames ().asIterator ().forEachRemaining (key -> {
170
210
if (!removeKeys .contains (key )) {
171
211
String value = request .getParameter (key );
172
212
if (StringUtils .hasLength (value )) {
173
- content .addFilter (key , value );
213
+ if (loadParams != null ) {
214
+ ParamOperation operation = loadParams .stream ()
215
+ .filter (paramOperation -> paramOperation .getKey ().equals (key ))
216
+ .findFirst ()
217
+ .orElse (null );
218
+ if (operation != null ) {
219
+ content .addFilter (key , operation .getOperation (), value );
220
+ } else {
221
+ content .addFilter (key , value );
222
+ }
223
+ } else {
224
+ content .addFilter (key , value );
225
+ }
174
226
}
175
227
}
176
- }
177
-
228
+ });
178
229
179
230
return pageRequest ;
180
231
}
0 commit comments