升级ListAndTotal
This commit is contained in:
parent
1b049b5bdc
commit
7d3c63175d
@ -27,7 +27,7 @@ type IDatabase interface {
|
||||
// List 查询数据列表
|
||||
List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error
|
||||
// ListAndTotal 查询列表并返回满足条件数据总数
|
||||
ListAndTotal(dbInstance *gorm.DB, listRes any, disableTotal bool, optionFuncList ...define.SetOption) (int64, error)
|
||||
ListAndTotal(dbInstance *gorm.DB, listRes any, totalRes *int64, disableTotal bool, optionFuncList ...define.SetOption) error
|
||||
// Delete 删除数据
|
||||
Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error)
|
||||
// Detail 数据详情
|
||||
|
23
base.go
23
base.go
@ -74,30 +74,33 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...define
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:04 2025/2/26
|
||||
func (b *BaseDao) ListAndTotal(dbInstance *gorm.DB, listRes any, disableTotal bool, optionFuncList ...define.SetOption) (int64, error) {
|
||||
if err := b.receiverTypeValid(listRes); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
func (b *BaseDao) ListAndTotal(dbInstance *gorm.DB, listRes any, totalRes *int64, disableTotal bool, optionFuncList ...define.SetOption) error {
|
||||
var (
|
||||
cnt int64
|
||||
err error
|
||||
)
|
||||
if err = b.receiverTypeValid(listRes); nil != err {
|
||||
return err
|
||||
}
|
||||
if err = b.receiverTypeValid(totalRes); nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
||||
if err = dbInstance.Find(listRes).Error; nil != err {
|
||||
// 列表查询失败
|
||||
return 0, err
|
||||
return err
|
||||
}
|
||||
if disableTotal {
|
||||
// 禁用查询总数
|
||||
return int64(reflect.ValueOf(listRes).Elem().Len()), nil
|
||||
*totalRes = int64(reflect.ValueOf(listRes).Elem().Len())
|
||||
return nil
|
||||
}
|
||||
optionFuncList = append(optionFuncList, WithClearLimit())
|
||||
dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
|
||||
if err = dbInstance.Count(&cnt).Error; nil != err {
|
||||
return 0, err
|
||||
if err = dbInstance.Count(totalRes).Error; nil != err {
|
||||
return err
|
||||
}
|
||||
return cnt, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除数据, 硬删除, 对应 delete语句
|
||||
|
Loading…
x
Reference in New Issue
Block a user