| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package utils
- import (
- "fmt"
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
- "github.com/xormplus/xorm"
- )
- // 分页查询
- func PageSearch(engine *xorm.Engine, controllername string, apiname string, tableName string, paramMap map[string]interface{}) (*models.PageResult, error) {
- page := paramMap["page"].(int)
- rows := paramMap["rows"].(int)
- offset := (page - 1) * rows
- paramMap["offset"] = offset
- stplkey := fmt.Sprintf("%s_%s_select.tpl", controllername, apiname)
- result, err := engine.SqlTemplateClient(stplkey, ¶mMap).Query().List()
- if err != nil {
- fmt.Println(err)
- return nil, err
- }
- stplkey = fmt.Sprintf("%s_%s_count.tpl", controllername, apiname)
- cresult, err := engine.SqlTemplateClient(stplkey, ¶mMap).Query().List()
- if err != nil {
- fmt.Println(err)
- return nil, err
- }
- records := cresult[0]["records"].(int64)
- var totalSize int64 = 0
- if totalSize < int64(rows) {
- totalSize = 1
- } else if records%int64(rows) == 0 {
- totalSize = records / int64(rows)
- } else {
- totalSize = records / int64(rows+1)
- }
- presult := models.PageResult{}
- presult.Page = (page)
- presult.Rows = (rows)
- presult.Content = result
- presult.TotalSize = records
- return &presult, nil
- }
|