2024-07-05 14:41:35 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
|
|
|
|
|
func Paginate(current, size int64) func(db *gorm.DB) *gorm.DB {
|
2024-11-06 09:10:30 +08:00
|
|
|
|
// 如果页码是-1,就不分页👋
|
2024-07-05 14:41:35 +08:00
|
|
|
|
if current == -1 {
|
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
|
return db
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 分页
|
|
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
|
if current == 0 {
|
|
|
|
|
current = 1
|
|
|
|
|
}
|
|
|
|
|
if size < 1 {
|
|
|
|
|
size = 10
|
|
|
|
|
}
|
|
|
|
|
// 计算偏移量
|
|
|
|
|
offset := (current - 1) * size
|
|
|
|
|
// 返回组装结果
|
|
|
|
|
return db.Offset(int(offset)).Limit(int(size))
|
|
|
|
|
}
|
|
|
|
|
}
|