1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
page.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-06-28 20:47 . 页码 limit
package wrapper
import (
"context"
"gitee.com/h79/goutils/common"
"gorm.io/gorm"
"time"
)
type Page struct {
Base
Total int64
size int
offset int
}
func NewPage(db *gorm.DB, timeout time.Duration) *Page {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
return &Page{
Base: Base{
DB: db,
ctx: ctx,
cancel: cancel,
},
}
}
func (page *Page) WithSize(size int) *Page {
page.size = size
return page
}
func (page *Page) WithOffset(offset int) *Page {
page.offset = offset
return page
}
// FindPage
// param results := make([]object, 0)
func (page *Page) FindPage(model interface{}, where IQuery, sel ISelect) error {
db := page.DB.Model(model)
if where != nil && where.Is() {
db = db.Where(where.Query(), where.Value()...)
}
if sel != nil && sel.Is() {
db = db.Select(sel.Query(), sel.Value()...)
}
db.Count(&page.Total)
if !common.IsNil(page.order) {
db = db.Order(page.order)
}
if page.size > 0 {
db = db.Limit(page.size)
db = db.Offset(page.offset)
}
return db.Find(page.Results).Error
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.99

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385