From 670c7c7af932ab788073d24ecdfe7d6f78479a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 15 May 2022 18:04:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=B8=80=E7=89=88=20setTxCon?= =?UTF-8?q?dition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 106 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 27 deletions(-) diff --git a/base.go b/base.go index 9ee6c5e..4212e36 100644 --- a/base.go +++ b/base.go @@ -5,26 +5,19 @@ // Date : 8:04 下午 2021/8/8 package mysql -import "gorm.io/gorm" +import ( + "gorm.io/gorm" +) type BaseDao struct { } -// CreateNewData 创建新的数据 +// CreateNewRecord 创建新的数据 // // Author : go_developer@163.com<白茶清欢> // // Date : 8:06 下午 2021/8/8 -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) - } - - if o.Offset > 0 { - dbInstance.Offset(o.Offset) - } - } +func (b *BaseDao) CreateNewRecord(dbInstance *gorm.DB, table string, data interface{}) error { return dbInstance.Table(table).Create(data).Error } @@ -33,18 +26,8 @@ func (b *BaseDao) CreateNewData(dbInstance *gorm.DB, table string, data interfac // Author : go_developer@163.com<白茶清欢> // // Date : 8:12 下午 2021/8/8 -func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, 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.Table(table).Where(condition).Updates(data).Error +func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, updateDate interface{}, optionFuncList ...SetOption) error { + return dbInstance.Table(table).Updates(updateDate).Error } // Select 查询数据列表 @@ -52,7 +35,7 @@ func (b *BaseDao) UpdateData(dbInstance *gorm.DB, table string, data interface{} // 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 { +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) @@ -70,7 +53,7 @@ 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 { +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) @@ -88,7 +71,7 @@ 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) { +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) @@ -128,3 +111,72 @@ func (b *BaseDao) Commit(db *gorm.DB) error { func (b *BaseDao) Rollback(db *gorm.DB) error { return db.Rollback().Error } + +// 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) + } + } +}