get.go 2.03 KB
package confcardholder

import (
	"apigame/service-common/svconst"
	"apigame/service-common/svmysql"
	"apigame/service-common/svredis"
	"apigame/util/util-lx/lxalilog"
	"apigame/util/util-lx/lxtime"
	"apigame/util/utstring"
	"apigame/util/zredis"
	"fmt"
)

// GetCurrent 获取 当前配置
func GetCurrent(gameId string) (conf *ActivityConfig, has bool) {
	currentKey := fmt.Sprintf("%s:%s:%s:current",
		svconst.REDIS_CACHEP_REFIX,
		svconst.MYSQL_TABLE_S_CARDHOLDER_CONFIG,
		gameId)
	currentId := zredis.GetInt64(zredis.GetConn(), currentKey)
	timeNow := lxtime.NowUninx()
	confRaw := new(ActivityConfigRaw)
	hasFind := false
	if currentId == 0 {
		hasFind = FindCurrent(confRaw, gameId)
	} else {
		conf, has = GetConfig(gameId, currentId)
		if has {
			if timeNow < conf.StartTime || timeNow > conf.EndTime {
				hasFind = FindCurrent(confRaw, gameId)
			}
		} else {
			hasFind = FindCurrent(confRaw, gameId)
		}
	}
	if hasFind {
		conf = new(ActivityConfig)
		conf.Decode(gameId, confRaw)
		svredis.SaveData(gameId, conf)
		has = true
		currentId = conf.Id
		_ = zredis.Set(zredis.GetConn(), currentKey, utstring.Int64ToString(currentId))
	}
	return
}

func FindCurrent(confRaw *ActivityConfigRaw, gameId string) (has bool) {
	info := confRaw.MysqlInfo(gameId)
	db := info.DbMysql
	timeNow := lxtime.NowUninx()
	result := db.Table(info.TableName).Where("start_time <= ? AND end_time >= ?", timeNow, timeNow).First(confRaw)
	has = result.RowsAffected != 0
	return
}

// GetConfig 获取 配置根据Id
func GetConfig(gameId string, confId int64) (conf *ActivityConfig, has bool) {
	var err error
	conf = &ActivityConfig{Id: confId}
	has = svredis.LoadData(gameId, conf)
	if has {
		fmt.Println("dwjw confcardholder.GetConfig use cache")
		return
	}
	confRaw := new(ActivityConfigRaw)
	has, err = svmysql.First(confRaw, gameId)
	if err != nil {
		lxalilog.Errors(err, "confcardholder.GetConfig error", gameId)
		return
	}
	if !has {
		return
	}

	conf.Decode(gameId, confRaw)

	fmt.Println("dwjw confcardholder.GetConfig save cache")
	svredis.SaveData(gameId, conf)

	return
}