page.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package common
  2. type PageParams struct {
  3. Page int64 `protobuf:"varint,1,opt,name=page,proto3" json:"page"`
  4. PageSize int64 `protobuf:"varint,2,opt,name=pageSize,proto3" json:"pageSize"`
  5. Total int64 `protobuf:"varint,3,opt,name=total,proto3" json:"total"`
  6. Last bool `protobuf:"varint,4,opt,name=last,proto3" json:"last"`
  7. }
  8. //从0开始的分页
  9. func (this *PageParams) GetPageParams() (limit int, offset int) {
  10. _offset := this.Page * this.PageSize
  11. if this.Total <= this.PageSize {
  12. this.Page = 0
  13. this.Last = true
  14. return int(this.Total), 0
  15. }
  16. _start := (this.Page + 1) * this.PageSize
  17. if this.Total-_start < 0 {
  18. this.Last = true
  19. pageCount := this.Total / this.PageSize
  20. this.Page = pageCount
  21. num := this.Total - pageCount*this.PageSize
  22. if num == 0 {
  23. pageCount = pageCount - 1
  24. return int(this.PageSize), int(pageCount * this.PageSize)
  25. }
  26. return int(num), int(pageCount * this.PageSize)
  27. } else if this.Total-_start == 0 {
  28. this.Last = true
  29. }
  30. return int(this.PageSize), int(_offset)
  31. }