123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package utils
- import (
- "fmt"
- "git.i2edu.net/i2/i2-bill-api/internal/types"
- "github.com/xormplus/xorm"
- "html/template"
- )
- // 分页查询
- func PageSearch(db *xorm.Engine, controllername string, apiname string, tableName string, params map[string]interface{}) (*types.PageResult, error) {
- page, _ := params["page"].(int)
- rows, _ := params["rows"].(int)
- if page == 0 {
- page = 1
- }
- if rows == 0 {
- rows = 10
- params["rows"] = rows
- }
- offset := (page - 1) * rows
- params["offset"] = offset
- params["ge"] = template.HTML(">=")
- params["le"] = template.HTML("<=")
- stplkey := fmt.Sprintf("%s_%s_select.tpl", controllername, apiname)
- result, err := db.SqlTemplateClient(stplkey, ¶ms).Query().List()
- if err != nil {
- fmt.Println(err)
- return nil, err
- }
- stplkey = fmt.Sprintf("%s_%s_count.tpl", controllername, apiname)
- cresult, err := db.SqlTemplateClient(stplkey, ¶ms).Query().List()
- if err != nil {
- fmt.Println(err)
- return nil, err
- }
- if result == nil {
- presult := types.PageResult{}
- content := []map[string]interface{}{}
- presult.Page = (page)
- presult.Rows = (rows)
- presult.Content = content
- presult.TotalSize = 0
- presult.TotalPageSize = 0
- return &presult, nil
- }
- var records int64
- if len(cresult) > 0 {
- records = cresult[0]["records"].(int64)
- }
- var totalPageSize int64 = 0
- if records < int64(rows) {
- totalPageSize = 1
- } else if records%int64(rows) == 0 {
- totalPageSize = records / int64(rows)
- } else {
- totalPageSize = records/int64(rows) + 1
- }
- presult := types.PageResult{}
- presult.Page = (page)
- presult.Rows = (rows)
- presult.Content = result
- presult.TotalSize = records
- presult.TotalPageSize = totalPageSize
- return &presult, nil
- }
|