diff --git a/configs/confbase/index.go b/configs/confbase/index.go index f2c1677..d2f8f01 100644 --- a/configs/confbase/index.go +++ b/configs/confbase/index.go @@ -2,9 +2,35 @@ package confbase import ( "apigame/service-common/svmysql" + "apigame/util/util-lx/lxalilog" "apigame/util/util-lx/lxtime" + "apigame/util/zjson" + "apigame/util/zredis" + "fmt" ) +func SaveCache(gameId string, obj IConfData) { + info := obj.ConfInfo(gameId) + _ = zredis.SetEx(zredis.GetConn(), info.CacheKey, zjson.Str(obj), info.CacheTime) +} + +func LoadCache[T IConfData](gameId string, obj T) (has bool) { + has = true + info := obj.ConfInfo(gameId) + text, err := zredis.Get(zredis.GetConn(), info.CacheKey) + if err != nil { + has = false + return + } + err = zjson.Obj(text, &obj) + if err != nil { + fmt.Println(err) + has = false + return + } + return +} + func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { info := obj.MysqlInfo(gameId) db := info.DbMysql @@ -13,3 +39,23 @@ func FindDuringTime(obj svmysql.IMysqlData, gameId string) (has bool) { has = result.RowsAffected != 0 return } + +func LoadData[T1 IConfData, T2 IConfRawData](gameId string, id any, obj T1) (has bool) { + confRaw := new(T2) + info := obj.ConfInfo(gameId) + db := info.DbMysql + result := db.Table(info.TableName).Where(fmt.Sprintf("%s = ?", info.KeyName), id).First(obj) + + has = result.RowsAffected != 0 + err := result.Error + if err != nil { + lxalilog.Errors(err, "confbase.LoadData error", gameId) + return + } + if !has { + return + } + + obj.Decode(gameId, confRaw) + return +} diff --git a/configs/confbase/interface.go b/configs/confbase/interface.go new file mode 100644 index 0000000..7e2a785 --- /dev/null +++ b/configs/confbase/interface.go @@ -0,0 +1,24 @@ +package confbase + +import "gorm.io/gorm" + +// ConfInfo 配置对象信息 +type ConfInfo struct { + DbMysql *gorm.DB + TableName string + KeyName string + CacheKey string + CacheTime int +} + +// IConfData 配置对象 +type IConfData interface { + // ConfInfo redis存储信息 + ConfInfo(suffix string) *ConfInfo + // Decode 解码 + Decode(gameId string, rawData any) +} + +// IConfRawData mysql存储对象 +type IConfRawData interface { +} diff --git a/configs/confdemo/1.go b/configs/confdemo/1.go new file mode 100644 index 0000000..73de153 --- /dev/null +++ b/configs/confdemo/1.go @@ -0,0 +1,42 @@ +package confdemo + +import ( + "apigame/configs/confbase" + "apigame/service-common/svconst" + "fmt" +) + +// DemoConfig 房间排行活动配置 分析后数据 +type DemoConfig struct { + Raw *DemoConfigRaw `json:"-"` + + Id int64 // ID + OpenLevel int // 开启等级 +} + +func (c *DemoConfig) ConfInfo(suffix string) *confbase.ConfInfo { + tableName := "s_demo" + return &confbase.ConfInfo{ + DbMysql: svconst.DbConfig, + TableName: tableName + suffix, + KeyName: "id", + CacheKey: fmt.Sprintf("%s:%s:%s", svconst.REDIS_CACHEP_REFIX, tableName, suffix), + CacheTime: 300, + } +} + +// DemoConfigRaw 房间排行活动配置 原始数据 +type DemoConfigRaw struct { + Id int64 `gorm:"column:id;primaryKey"` // ID + OpenLevel int // 开启等级 + PreviewTime int64 // 预告时间 + StartTime int64 // 开始时间 + EndTime int64 // 结束时间 + + Robot string // 机器人配置 + Room string // 房间配置 + + Ver string // 版本号 + Status int // 状态 0=关闭 1=开启 + UpdateTime int64 // 修改时间戳 +} diff --git a/configs/confdemo/decode.go b/configs/confdemo/decode.go new file mode 100644 index 0000000..9f171d0 --- /dev/null +++ b/configs/confdemo/decode.go @@ -0,0 +1,7 @@ +package confdemo + +// Decode 解析配置原始数据 +func (c *DemoConfig) Decode(gameId string, rawData any) { + raw := rawData.(*DemoConfigRaw) + c.Raw = raw +} diff --git a/controllers/demo.go b/controllers/demo.go index d9ffd69..3a25961 100644 --- a/controllers/demo.go +++ b/controllers/demo.go @@ -2,6 +2,8 @@ package controllers import ( "apigame/configs" + "apigame/configs/confbase" + "apigame/configs/confdemo" "apigame/models" "apigame/service/code-msg" "apigame/util/zjson" @@ -67,5 +69,13 @@ func (c *DemoController) Demo() { fmt.Println(zjson.Str(list)) } + { + gameId := "10149" + conf := new(confdemo.DemoConfig) + has := confbase.LoadData[*confdemo.DemoConfig, confdemo.DemoConfigRaw](gameId, 1, conf) + fmt.Println(has) + fmt.Println(conf) + } + c.RetRspCodeData(code_msg.RECODE_OK, rsp) } -- libgit2 0.21.0