You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
6.1 KiB
91 lines
6.1 KiB
package configDatabase
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
type MysqlSetUp struct {
|
|
MasterMysql MasterMysqlSetUp `mapstructure:"master" json:"master" yaml:"master"` //主数据库
|
|
AppPlatformDatabase MasterMysqlSetUp `mapstructure:"appPlatformDatabase" json:"appPlatformDatabase" yaml:"appPlatformDatabase"` //应用平台
|
|
//其他数据库依次添加
|
|
WechatMysql MasterMysqlSetUp `mapstructure:"wechat" json:"wechat" yaml:"wechat"` //微信数据库
|
|
HrMysql MasterMysqlSetUp `mapstructure:"hrdatabase" json:"hrdatabase" yaml:"hrdatabase"` //HR数据库
|
|
FileBookDate MasterMysqlSetUp `mapstructure:"fileBookDate" json:"fileBookDate" yaml:"fileBookDate"` //文档属性数据库
|
|
ErrorSubjectDate MasterMysqlSetUp `mapstructure:"errorSubjectDate" json:"errorSubjectDate" yaml:"errorSubjectDate"` //错题库
|
|
MyTestDate MasterMysqlSetUp `mapstructure:"myTestDate" json:"myTestDate" yaml:"myTestDate"` //自我测验
|
|
ImageTextDate MasterMysqlSetUp `mapstructure:"imageTextDate" json:"imageTextDate" yaml:"imageTextDate"` //图文信息数据库
|
|
ScoringDetailsDate MasterMysqlSetUp `mapstructure:"scoringDetailsDate" json:"scoringDetailsDate" yaml:"scoringDetailsDate"` //计分明细数据库
|
|
QuestionsAnswersDate MasterMysqlSetUp `mapstructure:"questionsAnswersDate" json:"questionsAnswersDate" yaml:"questionsAnswersDate"` //趣味问答
|
|
BillboardDate MasterMysqlSetUp `mapstructure:"billboardDate" json:"billboardDate" yaml:"billboardDate"` //风云榜统计数据库
|
|
HealthReportDate MasterMysqlSetUp `mapstructure:"healthReportDate" json:"healthReportDate" yaml:"healthReportDate"` //健康上报数据库
|
|
KpiDate MasterMysqlSetUp `mapstructure:"kpiDate" json:"kpiDate" yaml:"kpiDate"` //绩效考核数据库
|
|
WechatCallBackLogDate MasterMysqlSetUp `mapstructure:"wechatCallBackLogDate" json:"wechatCallBackLogDate" yaml:"wechatCallBackLogDate"` //企业微信回调记录
|
|
Managearchives MasterMysqlSetUp `mapstructure:"managearchives" json:"managearchives" yaml:"managearchives"` //管理档案
|
|
SystemPermission MasterMysqlSetUp `mapstructure:"systempermission" json:"systempermission" yaml:"systempermission"` //系统权限配置数据库
|
|
Storage MasterMysqlSetUp `mapstructure:"storage" json:"storage" yaml:"storage"` //仓储系统数据库
|
|
ServerMaster MasterMysqlSetUp `mapstructure:"servermaster" json:"servermaster" yaml:"servermaster"` //仓储系统数据库
|
|
TidbrMaster MasterMysqlSetUp `mapstructure:"tidbrmaster" json:"tidbrmaster" yaml:"tidbrmaster"` //仓储系统数据库
|
|
CustomerForm MasterMysqlSetUp `mapstructure:"customerFormDatabase" json:"customerFormDatabase" yaml:"customerFormDatabase"` //自定义表单数据库
|
|
}
|
|
|
|
type MasterMysqlSetUp struct {
|
|
UrlPath string `mapstructure:"url_path" json:"url_path" yaml:"url_path"` // 服务器地址
|
|
Port int `mapstructure:"port" json:"port" yaml:"port"` // 端口
|
|
Charset string `mapstructure:"charset" json:"charset" yaml:"charset"` // 编码方式
|
|
ParseTime bool `mapstructure:"parseTime" json:"parseTime" yaml:"parseTime"` // 是否自动转换时间
|
|
Loc string `mapstructure:"loc" json:"loc" yaml:"loc"` // 时区
|
|
Name string `mapstructure:"name" json:"name" yaml:"name"` // 数据库名称
|
|
UserName string `mapstructure:"username" json:"username" yaml:"username"` // 账号
|
|
PassWord string `mapstructure:"password" json:"password" yaml:"password"` // 密码
|
|
MaxIdleConns int `mapstructure:"max_idle_conns" json:"max_idle_conns" yaml:"max_idle_conns"` // 最大空闲数
|
|
MaxOpenConns int `mapstructure:"max_open_conns" json:"max_open_conns" yaml:"max_open_conns"` // 最大链接数
|
|
GormLog bool `mapstructure:"gorm_log" json:"gorm_log" yaml:"gorm_log"` // 是否开启Gorm全局日志
|
|
}
|
|
|
|
func (m *MasterMysqlSetUp) SqlDsn() (dsnStr string) {
|
|
dsnStr = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v", m.UserName, m.PassWord, m.UrlPath, m.Port, m.Name, m.Charset)
|
|
if m.ParseTime {
|
|
dsnStr = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=%v&parseTime=%v&loc=%v", m.UserName, m.PassWord, m.UrlPath, m.Port, m.Name, m.Charset, m.ParseTime, m.Loc)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (m *MasterMysqlSetUp) OpenSql() *gorm.DB {
|
|
sqlConfig := mysql.Config{
|
|
DSN: m.SqlDsn(), // DSN
|
|
DefaultStringSize: 255, // string 类型字段的默认长度
|
|
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
|
|
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
|
|
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
|
|
SkipInitializeWithVersion: false, // 根据版本自动配置
|
|
}
|
|
if m.GormLog {
|
|
if opDb, err := gorm.Open(mysql.New(sqlConfig), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
}); err != nil {
|
|
return nil
|
|
} else {
|
|
sqlDb, _ := opDb.DB()
|
|
sqlDb.SetMaxIdleConns(m.MaxIdleConns)
|
|
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
|
sqlDb.SetConnMaxLifetime(5 * time.Minute)
|
|
return opDb
|
|
}
|
|
} else {
|
|
if opDb, err := gorm.Open(mysql.New(sqlConfig)); err != nil {
|
|
return nil
|
|
} else {
|
|
sqlDb, _ := opDb.DB()
|
|
sqlDb.SetMaxIdleConns(m.MaxIdleConns)
|
|
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
|
sqlDb.SetConnMaxLifetime(5 * time.Minute)
|
|
return opDb
|
|
}
|
|
}
|
|
|
|
}
|
|
|