2022-05-15 11:27:28 +08:00
|
|
|
// Package mysql ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:04 下午 2021/8/8
|
|
|
|
package mysql
|
|
|
|
|
2022-05-15 18:04:06 +08:00
|
|
|
import (
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2022-05-15 11:27:28 +08:00
|
|
|
|
|
|
|
type BaseDao struct {
|
|
|
|
}
|
|
|
|
|
2022-05-15 18:30:08 +08:00
|
|
|
// Create 创建新的数据
|
2022-05-15 11:27:28 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:06 下午 2021/8/8
|
2022-05-15 18:30:08 +08:00
|
|
|
func (b *BaseDao) Create(dbInstance *gorm.DB, table string, data interface{}) error {
|
2022-05-15 11:40:48 +08:00
|
|
|
return dbInstance.Table(table).Create(data).Error
|
2022-05-15 11:27:28 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 18:30:08 +08:00
|
|
|
// Update 更新数据
|
2022-05-15 11:27:28 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:12 下午 2021/8/8
|
2022-05-15 18:30:08 +08:00
|
|
|
func (b *BaseDao) Update(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error {
|
|
|
|
b.setTxCondition(dbInstance, table, optionFuncList...)
|
|
|
|
return dbInstance.Updates(updateDate).Error
|
2022-05-15 11:27:28 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 18:30:08 +08:00
|
|
|
// List 查询数据列表
|
2022-05-15 11:27:28 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:14 下午 2021/8/8
|
2022-05-15 18:30:08 +08:00
|
|
|
func (b *BaseDao) List(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
|
|
|
|
b.setTxCondition(dbInstance, table, optionFuncList...)
|
|
|
|
return dbInstance.Find(result).Error
|
2022-05-15 11:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detail 查询详情
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:25 下午 2021/8/8
|
2022-05-15 18:30:08 +08:00
|
|
|
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, result interface{}, optionFuncList ...SetOption) error {
|
|
|
|
b.setTxCondition(dbInstance, table, optionFuncList...)
|
|
|
|
return dbInstance.First(result).Error
|
2022-05-15 11:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count 查询数量
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 8:25 下午 2021/8/8
|
2022-05-15 18:30:08 +08:00
|
|
|
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, optionFuncList ...SetOption) (int64, error) {
|
|
|
|
b.setTxCondition(dbInstance, table, optionFuncList...)
|
2022-05-15 11:27:28 +08:00
|
|
|
var cnt int64
|
2022-05-15 18:30:08 +08:00
|
|
|
return cnt, dbInstance.Count(&cnt).Error
|
2022-05-15 11:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-05-15 18:04:06 +08:00
|
|
|
|
|
|
|
// setTxCondition 设置查询条件
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:38 2022/5/15
|
|
|
|
func (b *BaseDao) setTxCondition(tx *gorm.DB, table string, optionFuncList ...SetOption) {
|
|
|
|
// 指定查询的表
|
|
|
|
tx.Table(table)
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
o := &Option{}
|
|
|
|
for _, fn := range optionFuncList {
|
|
|
|
fn(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置where条件
|
|
|
|
if nil != o.Where {
|
|
|
|
tx.Where(o.Where)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置 limit offset
|
|
|
|
if o.Limit > 0 {
|
|
|
|
tx.Limit(o.Limit)
|
|
|
|
}
|
|
|
|
if o.Offset >= 0 {
|
|
|
|
tx.Offset(o.Offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// in 语句
|
|
|
|
if nil != o.In {
|
|
|
|
tx.Where(o.In)
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in 语句
|
|
|
|
if nil != o.NotIn {
|
|
|
|
for field, value := range o.NotIn {
|
|
|
|
tx.Where(field+" NOT IN ? ", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// like 语句
|
|
|
|
if nil != o.Like {
|
|
|
|
for field, value := range o.NotIn {
|
|
|
|
tx.Where(field+" LIKE ? ", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT LIKE 语句
|
|
|
|
if nil != o.NotLike {
|
|
|
|
for field, value := range o.NotLike {
|
|
|
|
tx.Where(field+" NOT LIKE ? ", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// >=
|
|
|
|
if nil != o.Start {
|
|
|
|
for field, value := range o.Start {
|
|
|
|
tx.Where(field+" >= ?", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// <
|
|
|
|
if nil != o.End {
|
|
|
|
for field, value := range o.End {
|
|
|
|
tx.Where(field+" < ?", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|