forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter.go
490 lines (417 loc) · 12 KB
/
parameter.go
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package parameter
import (
"net/url"
"strconv"
"strings"
"github.com/backyio/go-admin/plugins/admin/modules"
"github.com/backyio/go-admin/plugins/admin/modules/constant"
"github.com/backyio/go-admin/plugins/admin/modules/form"
)
type Parameters struct {
Page string
PageInt int
PageSize string
PageSizeInt int
SortField string
Columns []string
SortType string
Animation bool
URLPath string
Fields map[string][]string
OrConditions map[string]string
cacheFixedStr url.Values
}
const (
Page = "__page"
PageSize = "__pageSize"
Sort = "__sort"
SortType = "__sort_type"
Columns = "__columns"
Prefix = "__prefix"
Pjax = "_pjax"
sortTypeDesc = "desc"
sortTypeAsc = "asc"
IsAll = "__is_all"
PrimaryKey = "__pk"
True = "true"
False = "false"
FilterRangeParamStartSuffix = "_start__admin"
FilterRangeParamEndSuffix = "_end__admin"
FilterParamJoinInfix = "_admin_join_"
FilterParamOperatorSuffix = "__admin_operator__"
FilterParamCountInfix = "__admin_index__"
Separator = "__admin_separator__"
)
var operators = map[string]string{
"like": "like",
"gr": ">",
"gq": ">=",
"eq": "=",
"ne": "!=",
"le": "<",
"lq": "<=",
"free": "free",
}
var keys = []string{Page, PageSize, Sort, Columns, Prefix, Pjax, form.NoAnimationKey}
func BaseParam() Parameters {
return Parameters{Page: "1", PageSize: "10", PageInt: 1, PageSizeInt: 10, Fields: make(map[string][]string)}
}
func GetParam(u *url.URL, defaultPageSize int, p ...string) Parameters {
values := u.Query()
primaryKey := "id"
defaultSortType := "desc"
if len(p) > 0 {
primaryKey = p[0]
defaultSortType = p[1]
}
page := getDefault(values, Page, "1")
pageSize := getDefault(values, PageSize, strconv.Itoa(defaultPageSize))
sortField := getDefault(values, Sort, primaryKey)
sortType := getDefault(values, SortType, defaultSortType)
columns := getDefault(values, Columns, "")
animation := true
if values.Get(form.NoAnimationKey) == "true" {
animation = false
}
fields := make(map[string][]string)
for key, value := range values {
if !modules.InArray(keys, key) && len(value) > 0 && value[0] != "" {
if key == SortType {
if value[0] != sortTypeDesc && value[0] != sortTypeAsc {
fields[key] = []string{sortTypeDesc}
}
} else {
if strings.Contains(key, FilterParamOperatorSuffix) &&
values.Get(strings.ReplaceAll(key, FilterParamOperatorSuffix, "")) == "" {
continue
}
fields[strings.ReplaceAll(key, "[]", "")] = value
}
}
}
columnsArr := make([]string, 0)
if columns != "" {
columns, _ = url.QueryUnescape(columns)
columnsArr = strings.Split(columns, ",")
}
pageInt, _ := strconv.Atoi(page)
pageSizeInt, _ := strconv.Atoi(pageSize)
return Parameters{
Page: page,
PageSize: pageSize,
PageSizeInt: pageSizeInt,
PageInt: pageInt,
URLPath: u.Path,
SortField: sortField,
SortType: sortType,
Fields: fields,
OrConditions: map[string]string{},
Animation: animation,
Columns: columnsArr,
}
}
func GetParamFromURL(urlStr string, defaultPageSize int, defaultSortType, primaryKey string) Parameters {
u, err := url.Parse(urlStr)
if err != nil {
return BaseParam()
}
return GetParam(u, defaultPageSize, primaryKey, defaultSortType)
}
func (param Parameters) WithPKs(id ...string) Parameters {
param.Fields[PrimaryKey] = []string{strings.Join(id, ",")}
return param
}
func (param Parameters) PKs() []string {
pk := param.GetFieldValue(PrimaryKey)
if pk == "" {
return []string{}
}
return strings.Split(param.GetFieldValue(PrimaryKey), ",")
}
func (param Parameters) DeletePK() Parameters {
delete(param.Fields, PrimaryKey)
return param
}
func (param Parameters) PK() string {
pks := param.PKs()
if len(pks) > 0 {
return pks[0]
}
return ""
}
func (param Parameters) IsAll() bool {
return param.GetFieldValue(IsAll) == True
}
func (param *Parameters) WithURLPath(path string) Parameters {
param.URLPath = path
return *param
}
func (param *Parameters) isAllTrue() {
param.Fields[IsAll] = []string{True}
}
func (param *Parameters) isAllFalse() {
param.Fields[IsAll] = []string{False}
}
func (param Parameters) WithIsAll(isAll bool) Parameters {
if isAll {
param.isAllTrue()
} else {
param.isAllFalse()
}
return param
}
func (param Parameters) DeleteIsAll() Parameters {
delete(param.Fields, IsAll)
return param
}
func (param Parameters) GetFilterFieldValueStart(field string) string {
return param.GetFieldValue(field + FilterRangeParamStartSuffix)
}
func (param Parameters) GetFilterFieldValueEnd(field string) string {
return param.GetFieldValue(field + FilterRangeParamEndSuffix)
}
func (param Parameters) GetFieldValue(field string) string {
value, ok := param.Fields[field]
if ok && len(value) > 0 {
return value[0]
}
return ""
}
func (param Parameters) AddField(field, value string) Parameters {
param.Fields[field] = []string{value}
return param
}
func (param Parameters) DeleteField(fields ...string) Parameters {
for _, field := range fields {
delete(param.Fields, field)
}
return param
}
func (param Parameters) DeleteEditPk() Parameters {
delete(param.Fields, constant.EditPKKey)
return param
}
func (param Parameters) DeleteDetailPk() Parameters {
delete(param.Fields, constant.DetailPKKey)
return param
}
func (param Parameters) GetFieldValues(field string) []string {
return param.Fields[field]
}
func (param Parameters) GetFieldValuesStr(field string) string {
return strings.Join(param.Fields[field], Separator)
}
func (param Parameters) GetFieldOperator(field, suffix string) string {
op := param.GetFieldValue(field + FilterParamOperatorSuffix + suffix)
if op == "" {
return "eq"
}
return op
}
func (param Parameters) Join() string {
p := param.GetFixedParamStr()
p.Add(Page, param.Page)
return p.Encode()
}
func (param *Parameters) SetPage(page string) Parameters {
param.Page = page
param.PageInt, _ = strconv.Atoi(page)
return *param
}
func (param *Parameters) SetPageSize(pageSize string) Parameters {
param.PageSize = pageSize
param.PageSizeInt, _ = strconv.Atoi(pageSize)
return *param
}
func (param Parameters) GetRouteParamStr() string {
p := param.GetFixedParamStr()
p.Add(Page, param.Page)
return "?" + p.Encode()
}
func (param Parameters) URL(page string) string {
return param.URLPath + param.SetPage(page).GetRouteParamStr()
}
func (param Parameters) URLNoAnimation(page string) string {
return param.URLPath + param.SetPage(page).GetRouteParamStr() + "&" + form.NoAnimationKey + "=true"
}
func (param Parameters) GetRouteParamStrWithoutPageSize(page string) string {
p := make(url.Values)
p.Add(Sort, param.SortField)
p.Add(Page, page)
p.Add(SortType, param.SortType)
if len(param.Columns) > 0 {
p.Add(Columns, strings.Join(param.Columns, ","))
}
for key, value := range param.Fields {
p[key] = value
}
return "?" + p.Encode()
}
func (param Parameters) GetFixedParamStrFromCache() url.Values {
if param.cacheFixedStr != nil {
return param.cacheFixedStr
}
p := param.GetFixedParamStr()
param.cacheFixedStr = p
return p
}
func (param Parameters) GetLastPageRouteParamStr(cache ...bool) string {
var p url.Values
if len(cache) > 0 && cache[0] {
p = param.GetFixedParamStrFromCache()
} else {
p = param.GetFixedParamStr()
}
p.Add(Page, strconv.Itoa(param.PageInt-1))
return "?" + p.Encode()
}
func (param Parameters) GetNextPageRouteParamStr(cache ...bool) string {
var p url.Values
if len(cache) > 0 && cache[0] {
p = param.GetFixedParamStrFromCache()
} else {
p = param.GetFixedParamStr()
}
p.Add(Page, strconv.Itoa(param.PageInt+1))
return "?" + p.Encode()
}
func (param Parameters) GetFixedParamStr() url.Values {
p := make(url.Values)
p.Add(Sort, param.SortField)
p.Add(PageSize, param.PageSize)
p.Add(SortType, param.SortType)
if len(param.Columns) > 0 {
p.Add(Columns, strings.Join(param.Columns, ","))
}
for key, value := range param.Fields {
p[key] = value
}
return p
}
func (param Parameters) GetFixedParamStrWithoutColumnsAndPage() string {
p := make(url.Values)
p.Add(Sort, param.SortField)
p.Add(PageSize, param.PageSize)
if len(param.Columns) > 0 {
p.Add(Columns, strings.Join(param.Columns, ","))
}
p.Add(SortType, param.SortType)
return "?" + p.Encode()
}
func (param Parameters) GetFixedParamStrWithoutSort() string {
p := make(url.Values)
p.Add(PageSize, param.PageSize)
for key, value := range param.Fields {
p[key] = value
}
p.Add(form.NoAnimationKey, "true")
if len(param.Columns) > 0 {
p.Add(Columns, strings.Join(param.Columns, ","))
}
return "&" + p.Encode()
}
func (param Parameters) Statement(wheres, table, delimiter, delimiter2 string, whereArgs []interface{}, columns, existKeys []string,
filterProcess func(string, string, string) string) (string, []interface{}, []string) {
var multiKey = make(map[string]uint8)
for key, value := range param.Fields {
keyIndexSuffix := ""
keyArr := strings.Split(key, FilterParamCountInfix)
if len(keyArr) > 1 {
key = keyArr[0]
keyIndexSuffix = FilterParamCountInfix + keyArr[1]
}
if keyIndexSuffix != "" {
multiKey[key] = 0
} else if _, exist := multiKey[key]; !exist && modules.InArray(existKeys, key) {
continue
}
var op string
if strings.Contains(key, FilterRangeParamEndSuffix) {
key = strings.ReplaceAll(key, FilterRangeParamEndSuffix, "")
op = "<="
} else if strings.Contains(key, FilterRangeParamStartSuffix) {
key = strings.ReplaceAll(key, FilterRangeParamStartSuffix, "")
op = ">="
} else if len(value) > 1 {
op = "in"
} else if !strings.Contains(key, FilterParamOperatorSuffix) {
op = operators[param.GetFieldOperator(key, keyIndexSuffix)]
} else {
continue
}
if strings.Contains(key, FilterParamJoinInfix) {
keys := strings.Split(key, FilterParamJoinInfix)
val := filterProcess(key, value[0], keyIndexSuffix)
if op == "in" {
qmark := ""
for range value {
qmark += "?,"
}
wheres += keys[0] + "." + modules.FilterField(keys[1], delimiter, delimiter2) + " " + op + " (" + qmark[:len(qmark)-1] + ") and "
} else {
wheres += keys[0] + "." + modules.FilterField(keys[1], delimiter, delimiter2) + " " + op + " ? and "
}
if op == "like" && !strings.Contains(val, "%") {
whereArgs = append(whereArgs, "%"+val+"%")
} else {
for _, v := range value {
whereArgs = append(whereArgs, filterProcess(key, v, keyIndexSuffix))
}
}
} else {
if modules.InArray(columns, key) {
if op == "in" {
qmark := ""
for range value {
qmark += "?,"
}
wheres += modules.Delimiter(delimiter, delimiter2, table) + "." + modules.FilterField(key, delimiter, delimiter2) + " " + op + " (" + qmark[:len(qmark)-1] + ") and "
} else {
wheres += modules.Delimiter(delimiter, delimiter2, table) + "." + modules.FilterField(key, delimiter, delimiter2) + " " + op + " ? and "
}
if op == "like" && !strings.Contains(value[0], "%") {
whereArgs = append(whereArgs, "%"+filterProcess(key, value[0], keyIndexSuffix)+"%")
} else {
for _, v := range value {
whereArgs = append(whereArgs, filterProcess(key, v, keyIndexSuffix))
}
}
} else {
continue
}
}
existKeys = append(existKeys, key)
}
if len(wheres) > 3 {
wheres = wheres[:len(wheres)-4]
}
for key, value := range param.OrConditions {
columns = strings.Split(key, ",")
op := "="
if strings.Contains(value, "%") {
op = "like"
}
if len(wheres) > 0 {
wheres += " and "
}
wheres += "("
for _, column := range columns {
keys := strings.Split(column, FilterParamJoinInfix)
if len(keys) > 1 {
wheres += keys[0] + "." + modules.FilterField(keys[1], delimiter, delimiter2) + " " + op + " ? or "
} else {
wheres += modules.FilterField(column, delimiter, delimiter2) + " " + op + " ? or "
}
whereArgs = append(whereArgs, value)
}
wheres = strings.TrimSuffix(wheres, "or ") + ")"
}
return wheres, whereArgs, existKeys
}
func getDefault(values url.Values, key, def string) string {
value := values.Get(key)
if value == "" {
return def
}
return value
}