更新base dao

This commit is contained in:
白茶清欢 2022-05-15 11:40:48 +08:00
parent 815806777e
commit 0fab8b3296

27
base.go
View File

@ -13,7 +13,6 @@ import "gorm.io/gorm"
//
// Date : 8:05 下午 2021/8/8
type Option struct {
DBTable string `json:"db_table"` // 数据表
Limit int `json:"limit"` // 限制数量
Offset int `json:"offset"` // 偏移量
}
@ -26,7 +25,7 @@ type BaseDao struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, data interface{}, o *Option) error {
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, table string, data interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
@ -36,7 +35,7 @@ func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, data interface{}, o *Option
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Create(data).Error
return dbInstance.Table(table).Create(data).Error
}
// UpdateData 更新数据
@ -44,7 +43,7 @@ func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, data interface{}, o *Option
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, data interface{}, condition interface{}, o *Option) error {
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, data interface{}, condition interface{}, o *Option) error {
if nil != o {
if o.Limit > 0 {
@ -55,7 +54,7 @@ func (b *BaseDao) UpdateData(dbInstance *gorm.DB, data interface{}, condition in
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Where(condition).Updates(data).Error
return dbInstance.Table(table).Where(condition).Updates(data).Error
}
// Select 查询数据列表
@ -63,7 +62,7 @@ func (b *BaseDao) UpdateData(dbInstance *gorm.DB, data interface{}, condition in
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
func (b *BaseDao) Select(dbInstance *gorm.DB, condition interface{}, result interface{}, o *Option) error {
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)
@ -73,7 +72,7 @@ func (b *BaseDao) Select(dbInstance *gorm.DB, condition interface{}, result inte
dbInstance.Offset(o.Offset)
}
}
return dbInstance.Find(result, condition).Error
return dbInstance.Table(table).Find(result, condition).Error
}
// Detail 查询详情
@ -81,11 +80,8 @@ func (b *BaseDao) Select(dbInstance *gorm.DB, condition interface{}, result inte
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Detail(dbInstance *gorm.DB, condition interface{}, result interface{}, o *Option) error {
func (b *BaseDao) Detail(dbInstance *gorm.DB,table string, condition interface{}, result interface{}, o *Option) error {
if nil != o {
if len(o.DBTable) > 0 {
dbInstance.Table(o.DBTable)
}
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
@ -94,7 +90,7 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, condition interface{}, result inte
dbInstance.Offset(o.Offset)
}
}
return dbInstance.First(result, condition).Error
return dbInstance.Table(table).First(result, condition).Error
}
// Count 查询数量
@ -102,11 +98,8 @@ func (b *BaseDao) Detail(dbInstance *gorm.DB, condition interface{}, result inte
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:25 下午 2021/8/8
func (b *BaseDao) Count(dbInstance *gorm.DB, condition interface{}, o *Option) (int64, error) {
func (b *BaseDao) Count(dbInstance *gorm.DB, table string,condition interface{}, o *Option) (int64, error) {
if nil != o {
if len(o.DBTable) > 0 {
dbInstance.Table(o.DBTable)
}
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
@ -116,7 +109,7 @@ func (b *BaseDao) Count(dbInstance *gorm.DB, condition interface{}, o *Option) (
}
}
var cnt int64
return cnt, dbInstance.Where(condition).Count(&cnt).Error
return cnt, dbInstance.Table(table).Where(condition).Count(&cnt).Error
}
// Begin 开启事务