重新初始化仓库
This commit is contained in:
		
							
								
								
									
										147
									
								
								dao/base.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								dao/base.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,147 @@
 | 
			
		||||
// Package dao ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 8:04 下午 2021/8/8
 | 
			
		||||
package dao
 | 
			
		||||
 | 
			
		||||
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 base struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateNewData 创建新的数据
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 8:06 下午 2021/8/8
 | 
			
		||||
func (b *base) 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 *base) 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 *base) 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 *base) 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 *base) 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 *base) Begin() *gorm.DB {
 | 
			
		||||
	return DBInstance.Begin()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Commit 提交事务
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 3:10 下午 2021/8/9
 | 
			
		||||
func (b *base) Commit(db *gorm.DB) error {
 | 
			
		||||
	return db.Commit().Error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Rollback 回滚
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 3:12 下午 2021/8/9
 | 
			
		||||
func (b *base) Rollback(db *gorm.DB) error {
 | 
			
		||||
	return db.Rollback().Error
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								dao/config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								dao/config.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
// Package dao ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 10:24 下午 2021/8/8
 | 
			
		||||
package dao
 | 
			
		||||
 | 
			
		||||
type config struct {
 | 
			
		||||
	base
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								dao/init.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								dao/init.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
// Package dao ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 1:14 下午 2021/8/7
 | 
			
		||||
package dao
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.zhangdeman.cn/zhangdeman/gopkg/middleware/mysql"
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	// DBInstance 收敛数据库操作
 | 
			
		||||
	DBInstance *gorm.DB
 | 
			
		||||
	// Namespace 命名空间的操作
 | 
			
		||||
	Namespace *namespace
 | 
			
		||||
	// Log 日志操作实例
 | 
			
		||||
	Log *log
 | 
			
		||||
	// Config 配置操作实例
 | 
			
		||||
	Config *config
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// InitDatabase 初始化数据库链接
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 1:15 下午 2021/8/7
 | 
			
		||||
func InitDatabase(conf *mysql.DBConfig, logConf *mysql.LogConfig) error {
 | 
			
		||||
	var err error
 | 
			
		||||
	if DBInstance, err = mysql.GetDatabaseClient(conf, logConf); nil != err {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	Namespace = &namespace{}
 | 
			
		||||
	Log = &log{}
 | 
			
		||||
	Config = &config{}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								dao/log.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								dao/log.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
			
		||||
// Package dao ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 8:50 下午 2021/8/7
 | 
			
		||||
package dao
 | 
			
		||||
 | 
			
		||||
type log struct {
 | 
			
		||||
	base
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								dao/namespace.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								dao/namespace.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
// Package dao ...
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 8:49 下午 2021/8/7
 | 
			
		||||
package dao
 | 
			
		||||
 | 
			
		||||
import "git.zhangdeman.cn/zhangdeman/center-config/define/model"
 | 
			
		||||
 | 
			
		||||
// namespace 命名空间相关操作
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 8:28 下午 2021/8/8
 | 
			
		||||
type namespace struct {
 | 
			
		||||
	base
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetDetailByNamespace 根据 namespace 获取命名空间详情
 | 
			
		||||
//
 | 
			
		||||
// Author : go_developer@163.com<白茶清欢>
 | 
			
		||||
//
 | 
			
		||||
// Date : 2:44 下午 2021/8/9
 | 
			
		||||
func (n *namespace) GetDetailByNamespace(namespace string) (*model.Namespace, error) {
 | 
			
		||||
	var (
 | 
			
		||||
		result model.Namespace
 | 
			
		||||
		err    error
 | 
			
		||||
	)
 | 
			
		||||
	if err = DBInstance.Table("namespace").Where("namespace = ?", namespace).First(&result).Error; nil != err {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return &result, nil
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user