page.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package base
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type IPage interface {
  7. GetTotal() int64 //获取总记录数
  8. SetTotal(int64) //设置总记录数
  9. GetCurrent() int64 //获取当前页
  10. SetCurrent(int64) //设置当前页
  11. GetPageSize() int64 //获取每页显示大小
  12. SetPageSize(int64) //设置每页显示大小
  13. AddOrderItem(OrderItem) // 设置排序条件
  14. AddOrderItems([]OrderItem) // 批量设置排序条件
  15. GetOrderItemsString() string // 将排序条件拼接成字符串
  16. Offset() int64 // 获取偏移量
  17. GetPages() int64 // 获取总的分页数
  18. }
  19. type OrderItem struct {
  20. Column string // 需要排序的字段
  21. Asc bool // 是否正序排列,默认true
  22. }
  23. type BaseData struct {
  24. Page *Page `json:"page"`
  25. List interface{} `json:"list"` // 查询数据列表
  26. }
  27. type Page struct {
  28. Total int64 `json:"total"` // 总的记录数
  29. Pages int64 `json:"pages"` //总页数
  30. PageSize int64 `json:"page_size"` // 每页显示的大小
  31. Current int64 `json:"current"` // 当前页
  32. orders []OrderItem `json:"orders"` // 排序条件
  33. }
  34. type PageReq struct {
  35. PageSize int64 `json:"page_size" form:"page_size,default=20"`
  36. Current int64 `json:"current" form:"current,default=1"`
  37. }
  38. func (b *BaseData) GetList() interface{} {
  39. return b.List
  40. }
  41. func (b *BaseData) SetList(list interface{}) {
  42. b.List = list
  43. }
  44. func (b *BaseData) GetPage() *Page {
  45. return b.Page
  46. }
  47. func (b *BaseData) SetPage(page *Page) {
  48. b.Page = page
  49. }
  50. func (page *Page) GetTotal() int64 {
  51. return page.Total
  52. }
  53. func (page *Page) SetTotal(total int64) {
  54. page.Total = total
  55. page.SetPages()
  56. }
  57. func (page *Page) GetCurrent() int64 {
  58. return page.Current
  59. }
  60. func (page *Page) SetCurrent(current int64) {
  61. page.Current = current
  62. }
  63. func (page *Page) GetPageSize() int64 {
  64. return page.PageSize
  65. }
  66. func (page *Page) SetPageSize(size int64) {
  67. page.PageSize = size
  68. }
  69. func (page *Page) AddOrderItem(orderItem OrderItem) {
  70. page.orders = append(page.orders, orderItem)
  71. }
  72. func (page *Page) AddOrderItems(orderItems []OrderItem) {
  73. page.orders = append(page.orders, orderItems...)
  74. }
  75. func (page *Page) GetOrderItemsString() string {
  76. arr := make([]string, 0)
  77. var order string
  78. for _, val := range page.orders {
  79. if val.Asc {
  80. order = ""
  81. } else {
  82. order = "desc"
  83. }
  84. arr = append(arr, fmt.Sprintf("%s %s", val.Column, order))
  85. }
  86. return strings.Join(arr, ",")
  87. }
  88. func (page *Page) Offset() int64 {
  89. if page.GetCurrent() > 0 {
  90. return (page.GetCurrent() - 1) * page.GetPageSize()
  91. } else {
  92. return 0
  93. }
  94. }
  95. func (page *Page) GetPages() int64 {
  96. if page.GetPageSize() == 0 {
  97. return 0
  98. }
  99. pages := page.GetTotal() / page.GetPageSize()
  100. if page.GetTotal()%page.PageSize != 0 {
  101. pages++
  102. }
  103. return pages
  104. }
  105. func (page *Page) SetPages() {
  106. if page.GetPageSize() == 0 {
  107. page.Pages = 0
  108. } else {
  109. pages := page.GetTotal() / page.GetPageSize()
  110. if page.GetTotal()%page.PageSize != 0 {
  111. pages++
  112. }
  113. page.Pages = pages
  114. }
  115. }
  116. func BuildAsc(column string) OrderItem {
  117. return OrderItem{Column: column, Asc: true}
  118. }
  119. func BuildDesc(column string) OrderItem {
  120. return OrderItem{Column: column, Asc: false}
  121. }
  122. func BuildAscs(columns ...string) []OrderItem {
  123. items := make([]OrderItem, 0)
  124. for _, val := range columns {
  125. items = append(items, BuildAsc(val))
  126. }
  127. return items
  128. }
  129. func BuildDescs(columns ...string) []OrderItem {
  130. items := make([]OrderItem, 0)
  131. for _, val := range columns {
  132. items = append(items, BuildDesc(val))
  133. }
  134. return items
  135. }
  136. func NewPage(pageSize, current int64, orderItems ...OrderItem) *Page {
  137. return &Page{PageSize: pageSize, Current: current, orders: orderItems}
  138. }