page.go 748 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package page
  2. import (
  3. "github.com/druidcaesa/gotool"
  4. "xorm.io/xorm"
  5. )
  6. // Page 分页结构体
  7. type Page struct {
  8. Size int `json:"size"` //显示条数
  9. Total int64 `json:"total"` //总条数
  10. List interface{} `json:"list"` //数据
  11. }
  12. type Start struct {
  13. }
  14. // StartSize 获取分页偏移量
  15. func StartSize(pageNum int, size int) int {
  16. if pageNum == 0 {
  17. pageNum = 1
  18. }
  19. if size == 0 {
  20. size = 10
  21. }
  22. num := (pageNum - 1) * size
  23. return num
  24. }
  25. // GetTotal 获取总条数
  26. func GetTotal(engine *xorm.Session, args ...interface{}) (int64, error) {
  27. if args != nil {
  28. engine.Table(args)
  29. }
  30. count, err := engine.Count()
  31. if err != nil {
  32. gotool.Logs.ErrorLog().Println(err.Error())
  33. return 0, err
  34. }
  35. return count, nil
  36. }