Commit 10d0724c by zhengqiuyun86

网络通过

parent 3eb2feea
package PagePlus package PagePlus
import ( import (
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception" "git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/page" "git.168cad.top/zhengqiuyun/rym-util/page"
"git.168cad.top/zhengqiuyun/rym-util/util" "git.168cad.top/zhengqiuyun/rym-util/util"
...@@ -37,3 +38,27 @@ func Page(params []page.Param, pageNum int, pageSize int, m interface{}, data in ...@@ -37,3 +38,27 @@ func Page(params []page.Param, pageNum int, pageSize int, m interface{}, data in
} }
return &page return &page
} }
func PageSql(db *gorm.DB, data interface{}, pageNum int, pageSize int, sql string, sqlParams []interface{}) *page.Data {
var page = page.Data{}
page.PageSize = util.If(pageSize == 0, 10, pageSize)
page.PageNum = util.If(pageNum == 0, 1, pageNum)
tx := db.Session(&gorm.Session{PrepareStmt: false})
countTx := tx.Raw(fmt.Sprintf("select count(1) from (%s) _tmp_", sql), sqlParams...)
resultCount := countTx.Scan(&page.Total)
if resultCount.Error != nil {
exception.ThrowsErr(resultCount.Error)
}
if page.Total > 0 {
sql = fmt.Sprintf("%s limit ?,? ", sql)
sqlParams = append(sqlParams, (page.PageNum-1)*page.PageSize)
sqlParams = append(sqlParams, page.PageSize)
dataTx := tx.Raw(sql, sqlParams...)
resultList := dataTx.Scan(&data)
if resultList.Error != nil {
exception.ThrowsErr(resultList.Error)
}
}
page.List = data
return &page
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment