优化基础方法的处理

This commit is contained in:
白茶清欢 2022-05-15 18:30:08 +08:00
parent 670c7c7af9
commit 520bf437cd
1 changed files with 16 additions and 39 deletions

55
base.go
View File

@ -12,40 +12,33 @@ import (
type BaseDao struct {
}
// CreateNewRecord 创建新的数据
// Create 创建新的数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
func (b *BaseDao) CreateNewRecord(dbInstance *gorm.DB, table string, data interface{}) error {
func (b *BaseDao) Create(dbInstance *gorm.DB, table string, data interface{}) error {
return dbInstance.Table(table).Create(data).Error
}
// UpdateData 更新数据
// Update 更新数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error {
return dbInstance.Table(table).Updates(updateDate).Error
func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error {
b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.Updates(updateDate).Error
}
// Select 查询数据列表
// List 查询数据列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
func (b *BaseDao) Select(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Table(table).Find(result, condition).Error
func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.Find(result).Error
}
// Detail 查询详情
@ -53,17 +46,9 @@ func (b *BaseDao) Select(dbInstance *gorm.DB, table string, condition interface{
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Table(table).First(result, condition).Error
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
b.setTxCondition(dbInstance, table, optionFuncList...)
return dbInstance.First(result).Error
}
// Count 查询数量
@ -71,18 +56,10 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, condition interface{
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, condition interface{}, o *Option) (int64, error) {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...SetOption) (int64, error) {
b.setTxCondition(dbInstance, table, optionFuncList...)
var cnt int64
return cnt, dbInstance.Table(table).Where(condition).Count(&cnt).Error
return cnt, dbInstance.Count(&cnt).Error
}
// Begin 开启事务