paging.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package utils
  2. type PagingReq struct {
  3. CurrentIndex int `description:"当前页码" json:"_page"`
  4. PageSize int `description:"每页数据条数,如果不传,默认每页20条" json:"_page_size"`
  5. }
  6. type PagingItem struct {
  7. IsStart bool `description:"是否首页" json:"is_start"`
  8. IsEnd bool `description:"是否最后一页" json:"is_end"`
  9. PreviousIndex int `description:"上一页页码" json:"previous_index"`
  10. NextIndex int `description:"下一页页码" json:"next_index"`
  11. CurrentIndex int `description:"当前页页码" json:"current_index"`
  12. Pages int `description:"总页数" json:"pages"`
  13. Totals int `description:"总数据量" json:"totals"`
  14. PageSize int `description:"每页数据条数" json:"page_size"`
  15. }
  16. func GetPaging(currentIndex,pageSize,total int)(item *PagingItem) {
  17. if pageSize<=0 {
  18. pageSize=PageSize20
  19. }
  20. if currentIndex<=0 {
  21. currentIndex=1
  22. }
  23. item=new(PagingItem)
  24. item.PageSize=pageSize
  25. item.Totals=total
  26. item.CurrentIndex=currentIndex
  27. if total<=0 {
  28. item.IsStart=true
  29. item.IsEnd=true
  30. return
  31. }
  32. pages:=PageCount(total,pageSize)
  33. item.Pages=pages
  34. if pages<=1 {
  35. item.IsStart=true
  36. item.IsEnd=true
  37. item.PreviousIndex=1
  38. item.NextIndex=1
  39. return
  40. }
  41. if pages == currentIndex {
  42. item.IsStart=false
  43. item.IsEnd=true
  44. item.PreviousIndex=currentIndex-1
  45. item.NextIndex=currentIndex
  46. return
  47. }
  48. if currentIndex==1 {
  49. item.IsStart=true
  50. item.IsEnd=false
  51. item.PreviousIndex=1
  52. item.NextIndex=currentIndex+1
  53. return
  54. }
  55. item.IsStart=false
  56. item.IsEnd=false
  57. item.PreviousIndex=currentIndex-1
  58. item.NextIndex=currentIndex+1
  59. return
  60. }
  61. func StartIndex2(page, pagesize int) int {
  62. if page > 1 {
  63. return (page - 1) * pagesize
  64. }
  65. return 0
  66. }
  67. func PageCount2(count, pagesize int) int {
  68. if count%pagesize > 0 {
  69. return count/pagesize + 1
  70. } else {
  71. return count / pagesize
  72. }
  73. }