page_util.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package utils
  2. import (
  3. "fmt"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  5. "github.com/xormplus/xorm"
  6. )
  7. // 分页查询
  8. func PageSearch(engine *xorm.Engine, controllername string, apiname string, tableName string, paramMap map[string]interface{}) (*models.PageResult, error) {
  9. page := paramMap["page"].(int)
  10. rows := paramMap["rows"].(int)
  11. offset := (page - 1) * rows
  12. paramMap["offset"] = offset
  13. stplkey := fmt.Sprintf("%s_%s_select.tpl", controllername, apiname)
  14. result, err := engine.SqlTemplateClient(stplkey, &paramMap).Query().List()
  15. if err != nil {
  16. fmt.Println(err)
  17. return nil, err
  18. }
  19. stplkey = fmt.Sprintf("%s_%s_count.tpl", controllername, apiname)
  20. cresult, err := engine.SqlTemplateClient(stplkey, &paramMap).Query().List()
  21. if err != nil {
  22. fmt.Println(err)
  23. return nil, err
  24. }
  25. records := cresult[0]["records"].(int64)
  26. var totalSize int64 = 0
  27. if records%int64(rows) == 0 {
  28. totalSize = records / int64(rows)
  29. } else {
  30. totalSize = records / int64(rows+1)
  31. }
  32. presult := models.PageResult{}
  33. presult.Page = (page)
  34. presult.Rows = (rows)
  35. presult.Content = result
  36. presult.TotalSize = totalSize
  37. return &presult, nil
  38. }