-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
60 lines (51 loc) · 1.54 KB
/
list.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
package gormx
import "fmt"
// List lists records.
func List[T any](page, pageSize uint, where *Where, orderBy *OrderBy) (data []*T, total int64, err error) {
offset := int((page - 1) * pageSize)
limit := int(pageSize)
// whereClauses := []string{}
// whereValues := []interface{}{}
// for _, w := range *where {
// if w.IsFuzzy {
// whereClauses = append(whereClauses, fmt.Sprintf("%s ILike ?", w.Key))
// whereValues = append(whereValues, fmt.Sprintf("%%%s%%", w.Value))
// } else if w.isNot {
// whereClauses = append(whereClauses, fmt.Sprintf("%s != ?", w.Key))
// whereValues = append(whereValues, w.Value)
// } else if w.isIn {
// whereClauses = append(whereClauses, fmt.Sprintf("%s in (?)", w.Key))
// whereValues = append(whereValues, w.Value)
// } else {
// whereClauses = append(whereClauses, fmt.Sprintf("%s = ?", w.Key))
// whereValues = append(whereValues, w.Value)
// }
// }
// whereClause := strings.Join(whereClauses, " AND ")
whereClause, whereValues, errx := where.Build()
if errx != nil {
return nil, 0, errx
}
dataTx := GetDB().Model(new(T))
if orderBy != nil {
for _, order := range *orderBy {
// fmt.Println("order by:", order.Key, order.IsDESC)
orderMod := "ASC"
if order.IsDESC {
orderMod = "DESC"
}
orderStr := fmt.Sprintf("%s %s", order.Key, orderMod)
dataTx = dataTx.Order(orderStr)
}
}
if whereClause != "" {
dataTx = dataTx.Where(whereClause, whereValues...)
}
err = dataTx.
Count(&total).
Offset(offset).
Limit(limit).
Find(&data).
Error
return
}