123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "strings"
- "time"
- )
- // 游标分页
- type CursorPagination struct {
- Top string
- TopNum string
- TopExtra string
- Bottom string
- BottomNum string
- BottomExtra string
- }
- func NewCursorPagination() *CursorPagination {
- return &CursorPagination{}
- }
- // 分割分页的top/bottom
- func (c *CursorPagination) SplitTopBottomData(topParam, bottomParam string) {
- if topParam == "" {
- c.Top = "0"
- c.TopNum = "0"
- c.TopExtra = "0"
- } else {
- top := strings.Split(topParam, ",")
- c.Top = top[0]
- c.TopNum = top[1]
- c.TopExtra = top[2]
- }
- if bottomParam == "" {
- c.Bottom = "0"
- c.BottomNum = "0"
- c.BottomExtra = "0"
- } else {
- bottom := strings.Split(bottomParam, ",")
- c.Bottom = bottom[0]
- c.BottomNum = bottom[1]
- c.BottomExtra = bottom[2]
- }
- }
- // 传统分页
- type Pagination struct {
- CurrentPage int `json:"current_page"` // 当前页
- PerPage int `json:"per_page"` // 每一页多少条
- Total int `json:"total"` // 总条数
- FirstPage int `json:"first_page"` // 第一页
- LastPage int `json:"last_page"` // 最后页
- PrevPage int `json:"prev_page"` // 上一页
- NextPage int `json:"next_page"` // 下一页
- Limit int `json:"limit"`
- Offset int `json:"offset"`
- RequestTime int64 `json:"request_time"`
- }
- func NewPagination(currentPage, perPage int, requestTime int64) *Pagination {
- if currentPage == 0 {
- currentPage = 1
- }
- if requestTime == 0 {
- requestTime = time.Now().Unix()
- }
- return &Pagination{
- CurrentPage: currentPage,
- PerPage: perPage,
- PrevPage: -1,
- NextPage: -1,
- RequestTime: requestTime,
- }
- }
- // 根据当前页获取分页条数和偏移量
- func (p *Pagination) GetLimitOffset() *Pagination {
- var num int
- if p.CurrentPage < 1 {
- num = 0
- } else {
- num = p.CurrentPage - 1
- }
- p.Limit = p.PerPage
- p.Offset = num * p.PerPage
- return p
- }
- // 计算上、下、首、末页
- func (p *Pagination) CalPage(total int) {
- p.Total = total
- // 第一页
- if total != 0 {
- p.FirstPage = 1
- }
- // 最后页
- p.LastPage = total / p.PerPage
- if total%p.PerPage != 0 {
- p.LastPage += 1
- }
- // 上一页
- if p.CurrentPage > 1 {
- p.PrevPage = p.CurrentPage - 1
- }
- // 下一页
- if total > p.PerPage*p.CurrentPage {
- p.NextPage = p.CurrentPage + 1
- }
- }
|