page.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 BaseOnlyData struct {
  28. List interface{} `json:"list"` // 查询数据列表
  29. }
  30. type Page struct {
  31. Total int64 `json:"total"` // 总的记录数
  32. Pages int64 `json:"pages"` //总页数
  33. PageSize int64 `json:"page_size"` // 每页显示的大小
  34. Current int64 `json:"current"` // 当前页
  35. orders []OrderItem `json:"orders"` // 排序条件
  36. }
  37. type PageReq struct {
  38. PageSize int64 `json:"page_size" form:"page_size,default=20"`
  39. Current int64 `json:"current" form:"current,default=1"`
  40. }
  41. func (b *BaseData) GetList() interface{} {
  42. return b.List
  43. }
  44. func (b *BaseData) SetList(list interface{}) {
  45. b.List = list
  46. }
  47. func (bo *BaseOnlyData) GetList() interface{} {
  48. return bo.List
  49. }
  50. func (bo *BaseOnlyData) SetList(list interface{}) {
  51. bo.List = list
  52. }
  53. func (b *BaseData) GetPage() *Page {
  54. return b.Page
  55. }
  56. func (b *BaseData) SetPage(page *Page) {
  57. b.Page = page
  58. }
  59. func (page *Page) GetTotal() int64 {
  60. return page.Total
  61. }
  62. func (page *Page) SetTotal(total int64) {
  63. page.Total = total
  64. page.SetPages()
  65. }
  66. func (page *Page) GetCurrent() int64 {
  67. return page.Current
  68. }
  69. func (page *Page) SetCurrent(current int64) {
  70. page.Current = current
  71. }
  72. func (page *Page) GetPageSize() int64 {
  73. return page.PageSize
  74. }
  75. func (page *Page) SetPageSize(size int64) {
  76. page.PageSize = size
  77. }
  78. func (page *Page) AddOrderItem(orderItem OrderItem) {
  79. page.orders = append(page.orders, orderItem)
  80. }
  81. func (page *Page) AddOrderItems(orderItems []OrderItem) {
  82. page.orders = append(page.orders, orderItems...)
  83. }
  84. func (page *Page) GetOrderItemsString() string {
  85. arr := make([]string, 0)
  86. var order string
  87. for _, val := range page.orders {
  88. if val.Asc {
  89. order = ""
  90. } else {
  91. order = "desc"
  92. }
  93. arr = append(arr, fmt.Sprintf("%s %s", val.Column, order))
  94. }
  95. return strings.Join(arr, ",")
  96. }
  97. func (page *Page) Offset() int64 {
  98. if page.GetCurrent() > 0 {
  99. return (page.GetCurrent() - 1) * page.GetPageSize()
  100. } else {
  101. return 0
  102. }
  103. }
  104. func (page *Page) GetPages() int64 {
  105. if page.GetPageSize() == 0 {
  106. return 0
  107. }
  108. pages := page.GetTotal() / page.GetPageSize()
  109. if page.GetTotal()%page.PageSize != 0 {
  110. pages++
  111. }
  112. return pages
  113. }
  114. func (page *Page) SetPages() {
  115. if page.GetPageSize() == 0 {
  116. page.Pages = 0
  117. } else {
  118. pages := page.GetTotal() / page.GetPageSize()
  119. if page.GetTotal()%page.PageSize != 0 {
  120. pages++
  121. }
  122. page.Pages = pages
  123. }
  124. }
  125. func BuildAsc(column string) OrderItem {
  126. return OrderItem{Column: column, Asc: true}
  127. }
  128. func BuildDesc(column string) OrderItem {
  129. return OrderItem{Column: column, Asc: false}
  130. }
  131. func BuildAscs(columns ...string) []OrderItem {
  132. items := make([]OrderItem, 0)
  133. for _, val := range columns {
  134. items = append(items, BuildAsc(val))
  135. }
  136. return items
  137. }
  138. func BuildDescs(columns ...string) []OrderItem {
  139. items := make([]OrderItem, 0)
  140. for _, val := range columns {
  141. items = append(items, BuildDesc(val))
  142. }
  143. return items
  144. }
  145. func NewPage(pageSize, current int64, orderItems ...OrderItem) *Page {
  146. return &Page{PageSize: pageSize, Current: current, orders: orderItems}
  147. }