148 lines
3.0 KiB
Go
148 lines
3.0 KiB
Go
|
// Package mysql ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 8:04 下午 2021/8/8
|
||
|
package mysql
|
||
|
|
||
|
import "gorm.io/gorm"
|
||
|
|
||
|
// Option 扩展选项
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 8:05 下午 2021/8/8
|
||
|
type Option struct {
|
||
|
DBTable string `json:"db_table"` // 数据表
|
||
|
Limit int `json:"limit"` // 限制数量
|
||
|
Offset int `json:"offset"` // 偏移量
|
||
|
}
|
||
|
|
||
|
type BaseDao struct {
|
||
|
}
|
||
|
|
||
|
// CreateNewData 创建新的数据
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 8:06 下午 2021/8/8
|
||
|
func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, data 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.Create(data).Error
|
||
|
}
|
||
|
|
||
|
// UpdateData 更新数据
|
||
|
//
|
||
|
// 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 {
|
||
|
if nil != o {
|
||
|
|
||
|
if o.Limit > 0 {
|
||
|
dbInstance.Limit(o.Limit)
|
||
|
}
|
||
|
|
||
|
if o.Offset > 0 {
|
||
|
dbInstance.Offset(o.Offset)
|
||
|
}
|
||
|
}
|
||
|
return dbInstance.Where(condition).Updates(data).Error
|
||
|
}
|
||
|
|
||
|
// Select 查询数据列表
|
||
|
//
|
||
|
// 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 {
|
||
|
if nil != o {
|
||
|
if o.Limit > 0 {
|
||
|
dbInstance.Limit(o.Limit)
|
||
|
}
|
||
|
|
||
|
if o.Offset > 0 {
|
||
|
dbInstance.Offset(o.Offset)
|
||
|
}
|
||
|
}
|
||
|
return dbInstance.Find(result, condition).Error
|
||
|
}
|
||
|
|
||
|
// Detail 查询详情
|
||
|
//
|
||
|
// 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 {
|
||
|
if nil != o {
|
||
|
if len(o.DBTable) > 0 {
|
||
|
dbInstance.Table(o.DBTable)
|
||
|
}
|
||
|
if o.Limit > 0 {
|
||
|
dbInstance.Limit(o.Limit)
|
||
|
}
|
||
|
|
||
|
if o.Offset > 0 {
|
||
|
dbInstance.Offset(o.Offset)
|
||
|
}
|
||
|
}
|
||
|
return dbInstance.First(result, condition).Error
|
||
|
}
|
||
|
|
||
|
// Count 查询数量
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 8:25 下午 2021/8/8
|
||
|
func (b *BaseDao) Count(dbInstance *gorm.DB, 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)
|
||
|
}
|
||
|
|
||
|
if o.Offset > 0 {
|
||
|
dbInstance.Offset(o.Offset)
|
||
|
}
|
||
|
}
|
||
|
var cnt int64
|
||
|
return cnt, dbInstance.Where(condition).Count(&cnt).Error
|
||
|
}
|
||
|
|
||
|
// Begin 开启事务
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 3:09 下午 2021/8/9
|
||
|
func (b *BaseDao) Begin(dbInstance *gorm.DB) *gorm.DB {
|
||
|
return dbInstance.Begin()
|
||
|
}
|
||
|
|
||
|
// Commit 提交事务
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 3:10 下午 2021/8/9
|
||
|
func (b *BaseDao) Commit(db *gorm.DB) error {
|
||
|
return db.Commit().Error
|
||
|
}
|
||
|
|
||
|
// Rollback 回滚
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 3:12 下午 2021/8/9
|
||
|
func (b *BaseDao) Rollback(db *gorm.DB) error {
|
||
|
return db.Rollback().Error
|
||
|
}
|