|
@@ -6,10 +6,16 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
type PageParams struct {
|
|
type PageParams struct {
|
|
- Page int `json:"unity" form:"unity"`
|
|
|
|
|
|
+ Page int `json:"page" form:"page"`
|
|
Size int `json:"size" form:"size"`
|
|
Size int `json:"size" form:"size"`
|
|
Desc string `json:"desc" form:"desc"`
|
|
Desc string `json:"desc" form:"desc"`
|
|
}
|
|
}
|
|
|
|
+type QueryPageParams struct {
|
|
|
|
+ Page int `json:"page" form:"page"`
|
|
|
|
+ Size int `json:"size" form:"size"`
|
|
|
|
+ Query string `json:"query" form:"query"`
|
|
|
|
+ Desc string `json:"desc" form:"desc"`
|
|
|
|
+}
|
|
|
|
|
|
// Paginate 使用给定的DB连接执行分页查询
|
|
// Paginate 使用给定的DB连接执行分页查询
|
|
func Paginate[T any](params PageParams, model T) (result []T, total int64, current int, err error) {
|
|
func Paginate[T any](params PageParams, model T) (result []T, total int64, current int, err error) {
|
|
@@ -29,29 +35,23 @@ func Paginate[T any](params PageParams, model T) (result []T, total int64, curre
|
|
return result, count, params.Page, nil
|
|
return result, count, params.Page, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func PaginateWithCondition[T any](params PageParams, model T, queryCond interface{}, args ...interface{}) (result []T, total int64, err error) {
|
|
|
|
|
|
+func PaginateWithCondition[T any](params QueryPageParams, model T, queryCond interface{}) (result []T, total int64, err error) {
|
|
var count int64
|
|
var count int64
|
|
- // 根据模型和额外查询条件查询数据库中符合条件的数据总量
|
|
|
|
- //if err = global.DBLink.Model(model).Where(queryCond, args...).Count(&count).Error; err != nil {
|
|
|
|
- // return nil, 0, err
|
|
|
|
- //}
|
|
|
|
if queryCond != nil {
|
|
if queryCond != nil {
|
|
- if err = global.DBLink.Model(result).Where(queryCond, args...).Count(&count).Error; err != nil {
|
|
|
|
|
|
+ if err = global.DBLink.Model(model).Where(queryCond, params.Query).Count(&count).Error; err != nil {
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- if err = global.DBLink.Model(result).Count(&count).Error; err != nil {
|
|
|
|
|
|
+ if err = global.DBLink.Model(model).Count(&count).Error; err != nil {
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 计算查询的偏移量,并设置每次查询的记录数量
|
|
// 计算查询的偏移量,并设置每次查询的记录数量
|
|
offset := (params.Page - 1) * params.Size
|
|
offset := (params.Page - 1) * params.Size
|
|
-
|
|
|
|
// 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
|
|
// 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
|
|
- if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, args...).Find(&result).Error; err != nil {
|
|
|
|
|
|
+ if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Find(&result).Error; err != nil {
|
|
return nil, 0, err
|
|
return nil, 0, err
|
|
}
|
|
}
|
|
-
|
|
|
|
return result, count, nil
|
|
return result, count, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -66,7 +66,7 @@ func AddGenericItem[T any](item T, mapper func(T) interface{}) error {
|
|
// GetById 根据id查询
|
|
// GetById 根据id查询
|
|
func GetById[T any](id int, model T) (T, error) {
|
|
func GetById[T any](id int, model T) (T, error) {
|
|
tx := global.DBLink.Where("id = ?", id).First(&model)
|
|
tx := global.DBLink.Where("id = ?", id).First(&model)
|
|
- if tx.Error != nil {
|
|
|
|
|
|
+ if tx.RowsAffected <= 0 {
|
|
return model, errors.New("查询失败")
|
|
return model, errors.New("查询失败")
|
|
} else {
|
|
} else {
|
|
return model, nil
|
|
return model, nil
|
|
@@ -74,7 +74,7 @@ func GetById[T any](id int, model T) (T, error) {
|
|
}
|
|
}
|
|
|
|
|
|
// UpdateById 根据id进行修改
|
|
// UpdateById 根据id进行修改
|
|
-func UpdateById[T any](id uint, model T) (T, error) {
|
|
|
|
|
|
+func UpdateById[T any](id int, model T) (T, error) {
|
|
tx := global.DBLink.Where("id = ?", id).Updates(&model)
|
|
tx := global.DBLink.Where("id = ?", id).Updates(&model)
|
|
if tx.RowsAffected > 0 {
|
|
if tx.RowsAffected > 0 {
|
|
return model, nil
|
|
return model, nil
|
|
@@ -89,7 +89,7 @@ func Add[T any](model T) (T, error) {
|
|
if tx.RowsAffected > 0 {
|
|
if tx.RowsAffected > 0 {
|
|
return model, nil
|
|
return model, nil
|
|
} else {
|
|
} else {
|
|
- return model, errors.New("添加失败当前id不存在")
|
|
|
|
|
|
+ return model, errors.New("添加失败")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|