-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpagehelper.go
116 lines (99 loc) · 2.84 KB
/
pagehelper.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
/*
* Licensed to the AcmeStack under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
* 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 pagehelper
import (
"context"
"fmt"
)
type OrderByInfo struct {
Field string
Order string
}
type PageInfo struct {
Page int64
PageSize int64
countColumn string
total int64
}
func GetPageInfo(ctx context.Context) *PageInfo {
if ctx == nil {
return nil
}
p := ctx.Value(pageHelperValue)
if p != nil {
if param, ok := p.(*PageInfo); ok {
return param
}
}
return nil
}
func GetTotal(ctx context.Context) int64 {
p := GetPageInfo(ctx)
if p != nil {
return p.total
}
return 0
}
// 分页
// page 页码
// pageSize 分页大小
// ctx 初始context
func StartPage(ctx context.Context, page, pageSize int64) context.Context {
return context.WithValue(ctx, pageHelperValue, &PageInfo{Page: page, PageSize: pageSize, total: 0})
}
// 分页(包含total信息)
// page 页码
// pageSize 分页大小
// 用于统计的列名称,如果为空,则默认使用COUNT(0)统计
// ctx 初始context
func StartPageWithCount(ctx context.Context, page, pageSize int64, countColumn string) context.Context {
return context.WithValue(ctx, pageHelperValue, &PageInfo{Page: page, PageSize: pageSize, total: -1, countColumn: countColumn})
}
//排序
//field 字段
//order 排序 [ASC | DESC]
//ctx 初始context
func OrderBy(ctx context.Context, field, order string) context.Context {
return context.WithValue(ctx, orderHelperValue, &OrderByInfo{Field: field, Order: order})
}
//获得总记录数
func (p *PageInfo) GetTotal() int64 {
return p.total
}
//设置总记录数,用户手动查询总记录数时使用
func (p *PageInfo) SetTotal(total int64) *PageInfo {
p.total = total
return p
}
//获得当前页码
func (p *PageInfo) GetPageNum() int64 {
return p.Page
}
//获得每页的记录数
func (p *PageInfo) GetPageSize() int64 {
return p.PageSize
}
//获得总页码
func (p *PageInfo) GetTotalPage() int64 {
if p.PageSize <= 0 {
return 0
}
return (p.total + int64(p.PageSize) - 1) / int64(p.PageSize)
}
func (p *PageInfo) String() string {
return fmt.Sprintf("pageNum: %d , pageSize: %d , Total: %d , TotalPage: %d", p.GetPageNum(), p.GetPageSize(), p.GetTotal(), p.GetTotalPage())
}