-
Notifications
You must be signed in to change notification settings - Fork 0
/
paging.go
138 lines (118 loc) · 2.96 KB
/
paging.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
/*
* * Copyright (C) 2023 The Developer bitstwinkle
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*/
package load
import (
"fmt"
)
const DefaultPagingSize = 100
const MaxPagingSize = 2000
const FirstPage = 1
type Page struct {
Size int64 `json:"page_size"` // Page Size, default 100
Current int64 `json:"page_no"` // From One
}
func PageALL() *Page {
return &Page{
Size: MaxPagingSize,
Current: FirstPage,
}
}
type Paging struct {
Size int64 `json:"page_size"` // Page Size, default 100
Current int64 `json:"page_no"` // From One
Total int64 `json:"page_total"` // The Page Count
ItemTotal int64 `json:"item_total"` // The Item Count
}
func (paging *Paging) ToString() string {
return fmt.Sprintf("size: %d|current: %d|total: %d|item_total: %d",
paging.Size, paging.Current, paging.Total, paging.ItemTotal)
}
func (paging *Paging) WithItemTotal(itemTotal int64) {
if paging.Size == 0 {
paging.Size = DefaultPagingSize
}
if paging.Current < FirstPage {
paging.Current = FirstPage
}
if itemTotal == 0 {
paging.Total = 0
paging.Current = FirstPage
return
}
paging.ItemTotal = itemTotal
paging.Total = paging.ItemTotal / paging.Size
if paging.ItemTotal%paging.Size != 0 {
paging.Total += 1
}
if paging.Current > paging.Total {
paging.Current = paging.Total
}
if paging.Current < 1 {
paging.Current = 1
}
}
func (paging *Paging) Skip() int64 {
return (paging.Current - 1) * paging.Size
}
func (paging *Paging) Limit() int64 {
return paging.Size
}
func PagingOfPage(page *Page) Paging {
if page == nil {
return PagingOf(DefaultPagingSize, FirstPage)
}
return PagingOf(page.Size, page.Current)
}
func PagingOf(size int64, current int64) Paging {
if size <= 0 {
size = DefaultPagingSize
}
if size > MaxPagingSize {
size = MaxPagingSize
}
if current < FirstPage {
current = FirstPage
}
return Paging{
Size: size,
Current: current,
Total: 0,
ItemTotal: 0,
}
}
func PagingALL() Paging {
return PagingOf(MaxPagingSize, FirstPage)
}
type Pagination struct {
Paging Paging `bson:"paging" json:"paging"`
Items []any `bson:"items" json:"items"`
}
func PagingWrap[T any](paging Paging, arr []*T, wrap func(m *T) any) Pagination {
if len(arr) == 0 {
return Pagination{
Paging: paging,
Items: []any{},
}
}
items := make([]any, len(arr))
for i, m := range arr {
items[i] = wrap(m)
}
return Pagination{
Paging: paging,
Items: items,
}
}