升级ListAndTotal

This commit is contained in:
白茶清欢 2025-04-25 11:18:21 +08:00
parent 1b049b5bdc
commit 7d3c63175d
2 changed files with 14 additions and 11 deletions

View File

@ -27,7 +27,7 @@ type IDatabase interface {
// List 查询数据列表 // List 查询数据列表
List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error List(dbInstance *gorm.DB, result any, optionFuncList ...define.SetOption) error
// ListAndTotal 查询列表并返回满足条件数据总数 // 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 删除数据
Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error) Delete(dbInstance *gorm.DB, dataModel any, optionFuncList ...define.SetOption) (int64, error)
// Detail 数据详情 // Detail 数据详情

23
base.go
View File

@ -74,30 +74,33 @@ func (b *BaseDao) List(dbInstance *gorm.DB, result any, optionFuncList ...define
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:04 2025/2/26 // Date : 16:04 2025/2/26
func (b *BaseDao) ListAndTotal(dbInstance *gorm.DB, listRes any, disableTotal bool, optionFuncList ...define.SetOption) (int64, error) { func (b *BaseDao) ListAndTotal(dbInstance *gorm.DB, listRes any, totalRes *int64, disableTotal bool, optionFuncList ...define.SetOption) error {
if err := b.receiverTypeValid(listRes); nil != err {
return 0, err
}
var ( var (
cnt int64
err error 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...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
if err = dbInstance.Find(listRes).Error; nil != err { if err = dbInstance.Find(listRes).Error; nil != err {
// 列表查询失败 // 列表查询失败
return 0, err return err
} }
if disableTotal { if disableTotal {
// 禁用查询总数 // 禁用查询总数
return int64(reflect.ValueOf(listRes).Elem().Len()), nil *totalRes = int64(reflect.ValueOf(listRes).Elem().Len())
return nil
} }
optionFuncList = append(optionFuncList, WithClearLimit()) optionFuncList = append(optionFuncList, WithClearLimit())
dbInstance = b.setTxCondition(dbInstance, optionFuncList...) dbInstance = b.setTxCondition(dbInstance, optionFuncList...)
if err = dbInstance.Count(&cnt).Error; nil != err { if err = dbInstance.Count(totalRes).Error; nil != err {
return 0, err return err
} }
return cnt, nil return nil
} }
// Delete 删除数据, 硬删除, 对应 delete语句 // Delete 删除数据, 硬删除, 对应 delete语句