You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/core/page/pagination.go

80 lines
1.7 KiB
Go

1 year ago
package page
import "fmt"
const (
DefaultPageSize = 10
DefaultTotal = 0
DefaultCurrent = 1
)
// Pagination 物理分页模型
type Pagination struct {
Size int64 `json:"size"` // 每页条目个数
Total int64 `json:"total"` // 数据条目总数
Current int64 `json:"current"` // 当前页码
}
// NewPagination 构建分页器
func NewPagination(current, size, total int64) *Pagination {
return &Pagination{
Size: size,
Total: total,
Current: current,
}
}
// DefaultPagination 构建默认分页器
func DefaultPagination() *Pagination {
return NewPagination(DefaultCurrent, DefaultPageSize, DefaultTotal)
}
// Clone 克隆 deep-clone
func (p *Pagination) Clone() *Pagination {
return NewPagination(p.Current, p.Size, p.Total)
}
// WithSize 设置每页个数
func (p *Pagination) WithSize(size int64) *Pagination {
p.Size = size
return p
}
// WithTotal 设置数据总数
func (p *Pagination) WithTotal(total int64) *Pagination {
p.Total = total
return p
}
// WithCurrent 设置当前页码
func (p *Pagination) WithCurrent(current int64) *Pagination {
p.Current = current
return p
}
// NextPage 下一页
func (p *Pagination) NextPage() *Pagination {
p.Current++
return p
}
// PrevPage 上一页
func (p *Pagination) PrevPage() *Pagination {
p.Current--
return p
}
// Offset 返回数据库需要的 Offset
func (p *Pagination) Offset() int64 {
return (p.Current - 1) * p.Size
}
// Limit 返回数据库需要的 Limit
func (p *Pagination) Limit() int64 {
return p.Size
}
func (p *Pagination) String() string {
return fmt.Sprintf("[Pagination current: %d, size: %d, total: %d]", p.Current, p.Size, p.Total)
}