一波更新
This commit is contained in:
@@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"framework/sqlAsyncMgr"
|
||||
"goutil/logUtilPlus"
|
||||
"time"
|
||||
|
||||
goredis "github.com/go-redis/redis/v8"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"goutil/logUtilPlus"
|
||||
"time"
|
||||
|
||||
// _ "github.com/go-sql-driver/mysql"
|
||||
config "common/configsYaml"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package connection
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -29,3 +30,7 @@ func TestExecute(t *testing.T) {
|
||||
_ = result.Error // 返回 error
|
||||
_ = result.RowsAffected // 返回插入记录的条数
|
||||
}
|
||||
|
||||
func TestGetDBName(t *testing.T) {
|
||||
fmt.Print(time.Now().Format("200601"))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package connection
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
config "common/configsYaml"
|
||||
"common/rabbitmq"
|
||||
"gorm.io/gorm"
|
||||
"goutil/logUtilPlus"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// 存放实体结构
|
||||
@@ -32,10 +39,137 @@ func BuildDB() {
|
||||
for _, dbModel := range dbModelMap {
|
||||
|
||||
// 检查数据库中是否存在与dbModel对应的表
|
||||
tableExists := modelDB.Migrator().HasTable(dbModel)
|
||||
if !tableExists {
|
||||
// 如果表不存在,则进行自动迁移
|
||||
modelDB.AutoMigrate(dbModel)
|
||||
}
|
||||
CheckTableExists(modelDB, dbModel)
|
||||
}
|
||||
}
|
||||
|
||||
// CheckTableExists 检查表是否存在,不存在则添加
|
||||
func CheckTableExists(db *gorm.DB, value interface{}) bool {
|
||||
|
||||
// 检查数据库中是否存在与dbModel对应的表
|
||||
tableExists := db.Migrator().HasTable(value)
|
||||
if !tableExists {
|
||||
// 如果表不存在,则进行自动迁移
|
||||
err := db.AutoMigrate(value)
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("CheckTableExists is err: %v", err.Error())
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GetMonth 获取当前月份
|
||||
func GetMonth() int32 {
|
||||
month, err := strconv.Atoi(time.Now().Format("200601"))
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("GetMonth is err: %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return int32(month)
|
||||
}
|
||||
|
||||
// GetToMonthAdd 获取当前月份 加上指定个月
|
||||
func GetToMonthAdd(monthCount int32) int32 {
|
||||
month, err := strconv.Atoi(time.Now().AddDate(0, int(monthCount), 0).Format("200601"))
|
||||
if err != nil {
|
||||
logUtilPlus.ErrorLog("GetToMonthAdd is err: %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return int32(month)
|
||||
}
|
||||
|
||||
// Create 添加数据
|
||||
// @param db 数据库连接
|
||||
// @param value 值
|
||||
// @param dbIndex mq队列索引
|
||||
func Create(db *gorm.DB, value interface{}, dbIndex int32) *gorm.DB {
|
||||
|
||||
result := &gorm.DB{Error: nil}
|
||||
|
||||
//检查表是否存在
|
||||
CheckTableExists(db, value)
|
||||
|
||||
//转换sql mq远程执行
|
||||
if config.GetSqlUseMQ() {
|
||||
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Model(value).Create(value)
|
||||
})
|
||||
|
||||
//推送数据到mq
|
||||
rabbitmq.SendMqData(dbIndex, sql)
|
||||
|
||||
//直接返回
|
||||
return result
|
||||
}
|
||||
|
||||
result = db.Create(value)
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("数据创建失败:", result.Error.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Save 保存数据
|
||||
func Save(db *gorm.DB, value interface{}, dbIndex int32) *gorm.DB {
|
||||
|
||||
result := &gorm.DB{Error: nil}
|
||||
|
||||
//转换sql mq远程执行
|
||||
if config.GetSqlUseMQ() {
|
||||
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
|
||||
return tx.Model(value).Save(value)
|
||||
})
|
||||
|
||||
//推送数据到mq
|
||||
rabbitmq.SendMqData(dbIndex, sql)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
result = db.Save(value)
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("数据保存失败:", result.Error.Error())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// AsyncCreate 异步创建数据
|
||||
func AsyncCreate(db *gorm.DB, value interface{}) {
|
||||
|
||||
go func() {
|
||||
|
||||
//检查表是否存在
|
||||
CheckTableExists(db, value)
|
||||
result := db.Create(value)
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("AsyncCreate is err: %v", result.Error)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// AsyncSave 异步保存数据
|
||||
func AsyncSave(db *gorm.DB, value interface{}) {
|
||||
|
||||
go func() {
|
||||
result := db.Save(value)
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("AsyncSave is err : %v", result.Error)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// AsyncDelete 异步删除数据
|
||||
func AsyncDelete(db *gorm.DB, value interface{}) {
|
||||
|
||||
go func() {
|
||||
result := db.Delete(value)
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("AsyncDelete is err : %v", result.Error)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user