From ad91ec88c734cc1b9fbe2ed05979927fcf2a8034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 14 Aug 2021 16:43:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0base=20dao?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/mysql/base.go | 147 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 middleware/mysql/base.go diff --git a/middleware/mysql/base.go b/middleware/mysql/base.go new file mode 100644 index 0000000..dbfac02 --- /dev/null +++ b/middleware/mysql/base.go @@ -0,0 +1,147 @@ +// 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 +}