database/base.go

183 lines
3.6 KiB
Go
Raw Normal View History

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:04:06 +08:00
// CreateNewRecord 创建新的数据
2022-05-15 11:27:28 +08:00
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:06 下午 2021/8/8
2022-05-15 18:04:06 +08:00
func (b *BaseDao) CreateNewRecord(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
}
// UpdateData 更新数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:12 下午 2021/8/8
2022-05-15 18:04:06 +08:00
func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error {
return dbInstance.Table(table).Updates(updateDate).Error
2022-05-15 11:27:28 +08:00
}
// Select 查询数据列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:14 下午 2021/8/8
2022-05-15 18:04:06 +08:00
func (b *BaseDao) Select(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).Find(result, condition).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:04:06 +08:00
func (b *BaseDao) Detail(dbInstance *gorm.DB, table string, condition interface{}, result interface{}, o *Option) error {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
2022-05-15 11:40:48 +08:00
return dbInstance.Table(table).First(result, condition).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:04:06 +08:00
func (b *BaseDao) Count(dbInstance *gorm.DB, table string, condition interface{}, o *Option) (int64, error) {
2022-05-15 11:27:28 +08:00
if nil != o {
if o.Limit > 0 {
dbInstance.Limit(o.Limit)
}
if o.Offset > 0 {
dbInstance.Offset(o.Offset)
}
}
var cnt int64
2022-05-15 11:40:48 +08:00
return cnt, dbInstance.Table(table).Where(condition).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)
}
}
}