diff --git a/common/svmysql/index.go b/common/svmysql/index.go index 7624fc8..acfdc75 100644 --- a/common/svmysql/index.go +++ b/common/svmysql/index.go @@ -4,8 +4,11 @@ import ( "apigame/common/svconst" "apigame/service/constd" "apigame/util/utdto" + "apigame/util/util-lx/lxalilog" "fmt" "github.com/astaxie/beego" + "gorm.io/gorm" + "strings" ) func Init() bool { @@ -32,3 +35,85 @@ func Init() bool { return true } + +func InitTable(obj IMysqlData, gameId string) { + info := obj.MysqlInfo(gameId) + utdto.InitTable(info.DbMysql, obj, info.TableName) +} + +func Insert(obj IMysqlData, gameId string) (err error) { + info := obj.MysqlInfo(gameId) + db := info.DbMysql + stmt := db.Session(&gorm.Session{DryRun: true}).Create(obj).Statement + stmtSQL := stmt.SQL.String() + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) + result := db.Exec(sql, stmt.Vars...) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Updates(obj IMysqlData, gameId string) (err error) { + info := obj.MysqlInfo(gameId) + db := info.DbMysql + stmt := db.Session(&gorm.Session{DryRun: true}).Updates(obj).Statement + stmtSQL := stmt.SQL.String() + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) + result := db.Exec(sql, stmt.Vars...) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Save(obj IMysqlData, gameId string) (err error) { + info := obj.MysqlInfo(gameId) + db := info.DbMysql + stmt := db.Session(&gorm.Session{DryRun: true}).Save(obj).Statement + stmtSQL := stmt.SQL.String() + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) + result := db.Exec(sql, stmt.Vars...) + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func First(obj IMysqlData, gameId string) (has bool, err error) { + info := obj.MysqlInfo(gameId) + db := info.DbMysql + stmt := db.Session(&gorm.Session{DryRun: true}).First(obj).Statement + stmtSQL := stmt.SQL.String() + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) + result := db.Raw(sql, stmt.Vars...).Scan(obj) + has = result.RowsAffected != 0 + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} + +func Find(obj IMysqlData, gameId string) (has bool, err error) { + info := obj.MysqlInfo(gameId) + db := info.DbMysql + stmt := db.Session(&gorm.Session{DryRun: true}).Find(obj).Statement + stmtSQL := stmt.SQL.String() + sql := strings.Replace(stmtSQL, stmt.Table, info.TableName, -1) + result := db.Raw(sql, stmt.Vars...).Scan(obj) + has = result.RowsAffected != 0 + err = result.Error + if err != nil { + lxalilog.Errors(err, gameId) + return + } + return +} diff --git a/common/svmysql/interface.go b/common/svmysql/interface.go index 0c6b64e..fba3d4d 100644 --- a/common/svmysql/interface.go +++ b/common/svmysql/interface.go @@ -1 +1,15 @@ package svmysql + +import "gorm.io/gorm" + +// MysqlInfo mysql存储信息 +type MysqlInfo struct { + DbMysql *gorm.DB + TableName string +} + +// IMysqlData mysql存储对象 +type IMysqlData interface { + // MysqlInfo mysql存储信息 + MysqlInfo(gameId string) *MysqlInfo +} diff --git a/common/svredis/index.go b/common/svredis/index.go index 42b0904..92f43c7 100644 --- a/common/svredis/index.go +++ b/common/svredis/index.go @@ -35,12 +35,12 @@ func Init() bool { return true } -func SaveData(gameId string, obj IRedisInfo) { +func SaveData(gameId string, obj IRedisData) { info := obj.RedisInfo(gameId) _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) } -func LoadData[T IRedisInfo](gameId string, obj T) (has bool) { +func LoadData[T IRedisData](gameId string, obj T) (has bool) { has = true info := obj.RedisInfo(gameId) text, err := zredis.Get(zredis.GetConn(), info.CacheKey) diff --git a/common/svredis/interface.go b/common/svredis/interface.go index 47dedae..0c24d5d 100644 --- a/common/svredis/interface.go +++ b/common/svredis/interface.go @@ -6,8 +6,8 @@ type RedisInfo struct { CacheTime int } -// IRedisInfo redis存储信息 -type IRedisInfo interface { +// IRedisData redis存储对象 +type IRedisData interface { // RedisInfo redis存储信息 RedisInfo(gameId string) *RedisInfo } diff --git a/configs/feat-api.go b/configs/feat-api.go index 3064700..6efb06a 100644 --- a/configs/feat-api.go +++ b/configs/feat-api.go @@ -3,6 +3,7 @@ package configs import ( "apigame/common/svconst" "apigame/common/svdto" + "apigame/common/svmysql" "apigame/common/svredis" "apigame/util/utdto" "fmt" @@ -36,3 +37,11 @@ func (c *ApiGameConfig) RedisInfo(gameId string) *svredis.RedisInfo { CacheTime: 300, } } + +func (c *ApiGameConfig) MysqlInfo(gameId string) *svmysql.MysqlInfo { + tableName := "s_game_config" + return &svmysql.MysqlInfo{ + DbMysql: svconst.DbApi.Where("gameid = ?", gameId), + TableName: tableName, + } +} diff --git a/configs/registry.go b/configs/registry.go index ba84136..6456448 100644 --- a/configs/registry.go +++ b/configs/registry.go @@ -1,9 +1,8 @@ package configs import ( + "apigame/common/svmysql" "apigame/common/svredis" - "apigame/util/utdto" - "apigame/util/util-lx/lxalilog" "fmt" ) @@ -15,14 +14,19 @@ func GetApiGame(gameId string) (conf *ApiGameConfig, err error) { fmt.Println("dwjw GetApiGame use cache") return } - rule := conf.GetRule(gameId) - result := utdto.First(rule.DbMysql, conf, rule.TableName) - has = result.RowsAffected != 0 - err = result.Error + has, err = svmysql.First(conf, gameId) if err != nil { - lxalilog.Errors(err, gameId) return } + + //rule := conf.GetRule(gameId) + //result := utdto.First(rule.DbMysql, conf, rule.TableName) + //has = result.RowsAffected != 0 + //err = result.Error + //if err != nil { + // lxalilog.Errors(err, gameId) + // return + //} fmt.Println("dwjw GetApiGame save cache") svredis.SaveData(gameId, conf) -- libgit2 0.21.0