Commit 4a8e7043 by zhengqiuyun86

返回封装优化

parent 4fb31ddd
package PagePlus
import (
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/page"
"git.168cad.top/zhengqiuyun/rym-util/util"
"gorm.io/gorm"
)
func Page(params []page.Param, pageNum int, pageSize int, m interface{}, data interface{}, orderBy string, db *gorm.DB) *page.Data {
var page = page.Data{}
page.PageSize = util.IfInt(pageSize == 0, 10, pageSize)
page.PageNum = util.IfInt(pageNum == 0, 1, pageNum)
tx := db.Session(&gorm.Session{PrepareStmt: true})
countTx := tx.Model(&m).Select("count(1)")
if params != nil {
for _, p := range params {
countTx.Where(p.Column+" "+p.Op+" ?", p.Value)
}
}
resultCount := countTx.Scan(&page.Total)
if resultCount.Error != nil {
exception.ThrowsErr(resultCount.Error)
}
if page.Total > 0 {
dataTx := tx.Model(&m).Offset((page.PageNum - 1) * page.PageSize).Limit(page.PageSize).Order(orderBy)
if params != nil {
for _, p := range params {
dataTx.Where(p.Column+" "+p.Op+" ?", p.Value)
}
}
resultList := dataTx.Find(&data)
if resultList.Error != nil {
exception.ThrowsErr(resultList.Error)
}
page.List = data
}
return &page
}
package page
type Page struct {
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
}
type Data struct {
Page
List interface{} `json:"list"`
}
package page
type ParamPage struct {
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
}
type Param struct {
Column string
Op string
Value interface{}
}
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