-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginator.go
198 lines (180 loc) · 5.32 KB
/
paginator.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
package http_result
import (
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
"github.com/spf13/cast"
"gorm.io/gorm"
"math"
"strconv"
"strings"
)
type Meta struct {
TotalPage int `json:"total_page"`
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
Total int64 `json:"total"`
}
type Links struct {
First string `json:"first"`
Last string `json:"last"`
Prev string `json:"prev"`
Next string `json:"next"`
}
type PageResult struct {
Data any `json:"data"` // List of data
Total int64 `json:"total"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
func (h *HttpResult) SearchByIncludes(column string, values []any) *HttpResult {
// 再处理url查询
if h.Query != nil {
h.Query = func(q orm.Query) orm.Query {
//处理日期时间
// 先处理过滤条件
q = q.Where(column+" in ?", values).(orm.Query)
return q
}(h.Query)
return h
} else {
query := facades.Orm().Query()
h.Query = func(q orm.Query) orm.Query {
//处理日期时间
// 先处理过滤条件
q = q.Where(column+" in ?", values).(orm.Query)
return q
}(query)
return h
}
}
// SearchByParams
// example SearchByParams(map[string]{}{"name":"user"}, map[string]interface{}{"state",1}, []string{"age"}...)
// ?name=xxx&pageSize=1¤tPage=1&sort=xxx&order=xxx
func (h *HttpResult) SearchByParams(params map[string]string, conditionMap map[string]interface{}, excepts ...string) *HttpResult {
for _, except := range excepts {
delete(params, except)
}
if h.Query != nil {
query := facades.Orm().Query()
// 再处理url查询
h.Query = func(q orm.Query) orm.Query {
//处理日期时间
// 先处理过滤条件
for key, val := range conditionMap {
q = q.Where(key+" = ?", val).(orm.Query)
}
for key, value := range params {
//如果key包含了[]符号
if strings.Contains(key, "[]") || value == "" || key == "pageSize" || key == "total" || key == "currentPage" || key == "sort" || key == "order" {
continue
} else {
q = q.Where(gorm.Expr(key+" LIKE ?", "%"+value+"%"))
}
//则表示是日期时间范围
/**
created_at[]: 2024-10-21 00:00:00
created_at[]: 2024-10-21 23:59:59
*/
if strings.Contains(key, "[]") {
key = strings.Replace(key, "[]", "", -1)
if value == "" {
continue
}
//按照,拆分value
ranges := strings.Split(value, ",")
if len(ranges) == 2 {
q = q.Where(key+" BETWEEN ? AND ?", ranges[0], ranges[1])
} else {
continue
}
}
}
return q
}(query)
} else {
h.Query = func(q orm.Query) orm.Query {
//处理日期时间
// 先处理过滤条件
for key, val := range conditionMap {
q = q.Where(key+" = ?", val).(orm.Query)
}
for key, value := range params {
//如果key包含了[]符号
if strings.Contains(key, "[]") || value == "" || key == "pageSize" || key == "total" || key == "currentPage" || key == "sort" || key == "order" {
continue
} else {
q = q.Where(gorm.Expr(key+" LIKE ?", "%"+value+"%"))
}
//则表示是日期时间范围
/**
created_at[]: 2024-10-21 00:00:00
created_at[]: 2024-10-21 23:59:59
*/
if strings.Contains(key, "[]") {
key = strings.Replace(key, "[]", "", -1)
if value == "" {
continue
}
//按照,拆分value
ranges := strings.Split(value, ",")
if len(ranges) == 2 {
q = q.Where(key+" BETWEEN ? AND ?", ranges[0], ranges[1])
} else {
continue
}
}
}
return q
}(h.Query)
}
return h
}
func (r *HttpResult) ResultPagination(dest any, withes ...string) (http.Response, error) {
request := r.Context.Request()
pageSize := request.Query("pageSize", "10")
pageSizeInt := cast.ToInt(pageSize)
currentPage := request.Query("currentPage", "1")
currentPageInt := cast.ToInt(currentPage)
total := int64(0)
for _, with := range withes {
r.Query = r.Query.With(with)
}
r.Query = r.Query.OrderByDesc("id")
r.Query.Paginate(currentPageInt, pageSizeInt, dest, &total)
URL_PATH := r.Context.Request().Origin().URL.Path
var proto = "http://"
if request.Origin().TLS != nil {
proto = "https://"
}
// Corrected links generation
links := Links{
First: proto + request.Origin().Host + URL_PATH + "?pageSize=" + pageSize + "¤tPage=1",
Last: proto + request.Origin().Host + URL_PATH + "?pageSize=" + pageSize + "¤tPage=" + strconv.Itoa(int(total)/pageSizeInt),
Prev: proto + request.Origin().Host + URL_PATH + "?pageSize=" + pageSize + "¤tPage=" + strconv.Itoa(currentPageInt-1),
Next: proto + request.Origin().Host + URL_PATH + "?pageSize=" + pageSize + "¤tPage=" + strconv.Itoa(currentPageInt+1),
}
// Corrected total page calculation
totalPage := int(math.Ceil(float64(total) / float64(pageSizeInt)))
meta := Meta{
TotalPage: totalPage,
CurrentPage: currentPageInt,
PerPage: pageSizeInt,
Total: total,
}
pageResult := PageResult{
Data: dest,
Total: total,
Links: links,
Meta: meta,
}
// 返回构建好的分页结果
return r.Context.Response().Success().Json(pageResult), nil
}
func (r *HttpResult) List(dest any) (http.Response, error) {
err := r.Query.Find(dest)
if err != nil {
return nil, err
}
return r.Context.Response().Success().Json(dest), nil
}