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 } // 无数据 if result == nil { presult := models.PageResult{} content := []interface{}{} presult.Page = (page) presult.Rows = (rows) presult.Content = content presult.TotalSize = 0 presult.TotalPageSize = 0 return &presult, nil } records := cresult[0]["records"].(int64) var totalPageSize int64 = 0 if totalPageSize < int64(rows) { totalPageSize = 1 } else if records%int64(rows) == 0 { totalPageSize = records / int64(rows) } else { totalPageSize = records / int64(rows+1) } presult := models.PageResult{} presult.Page = (page) presult.Rows = (rows) presult.Content = result presult.TotalSize = records presult.TotalPageSize = totalPageSize return &presult, nil }