fix获取排行榜初始化数据问题,sql执行问题
This commit is contained in:
parent
7f7a881d46
commit
9cc97b5c06
@ -19,4 +19,5 @@ Log:
|
||||
|
||||
DWCache:
|
||||
Host: redis:6379
|
||||
Password:
|
||||
IdleTimeout: 60
|
@ -19,5 +19,6 @@ Log:
|
||||
|
||||
DWCache:
|
||||
Host: 127.0.0.1:6379
|
||||
Password:
|
||||
IdleTimeout: 60
|
||||
EcpmLogPath: ./logs/ecpm.log
|
@ -5,6 +5,7 @@ import (
|
||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/logic/rankings"
|
||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc"
|
||||
"gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/types"
|
||||
"strconv"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@ -40,29 +41,31 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
||||
|
||||
var flag bool
|
||||
|
||||
resp.RankingData = make([]types.RankingData, len(cacheData))
|
||||
resp.RankingData = make([]types.RankingData, 0, len(cacheData))
|
||||
var userRank types.RankingData
|
||||
|
||||
for i, datum := range cacheData {
|
||||
if id, ok := datum.Member.(uint64); ok {
|
||||
//查询用户数据,FindOne带缓存
|
||||
user, err := l.svcCtx.AppUser.FindOne(l.ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := types.RankingData{
|
||||
Nickname: user.Nickname,
|
||||
Avatar: user.Avatar,
|
||||
Score: uint32(datum.Score),
|
||||
Rank: uint32(i), //todo
|
||||
Self: user.Id == at.UserId,
|
||||
}
|
||||
if user.Id == at.UserId {
|
||||
flag = true
|
||||
userRank = data
|
||||
}
|
||||
resp.RankingData = append(resp.RankingData, data)
|
||||
userId, err := strconv.Atoi(datum.Member.(string))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
//查询用户数据,FindOne带缓存
|
||||
user, err := l.svcCtx.AppUser.FindOne(l.ctx, uint64(userId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data := types.RankingData{
|
||||
Nickname: user.Nickname,
|
||||
Avatar: user.Avatar,
|
||||
Score: uint32(datum.Score),
|
||||
Rank: uint32(i) + 1,
|
||||
Self: user.Id == at.UserId,
|
||||
}
|
||||
if user.Id == at.UserId {
|
||||
flag = true
|
||||
userRank = data
|
||||
}
|
||||
resp.RankingData = append(resp.RankingData, data)
|
||||
}
|
||||
|
||||
if !flag {
|
||||
@ -71,11 +74,11 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
|
||||
|
||||
return nil, err
|
||||
}
|
||||
l.Logger.Debugf("userRank: %+v", userRank)
|
||||
userRank.Self = true
|
||||
resp.RankingData = append(resp.RankingData, userRank)
|
||||
}
|
||||
l.Logger.Debugf("resp: %+v", resp)
|
||||
resp.RankingData = append(resp.RankingData, userRank)
|
||||
|
||||
l.Logger.Debugf("userRank: %+v", userRank)
|
||||
l.Logger.Debugf("resp: %+v", resp)
|
||||
return
|
||||
}
|
||||
|
@ -26,20 +26,20 @@ func GetRankingsCacheKey(appId, t uint64) string {
|
||||
|
||||
// SetList 向排行榜中添加成员及其分数
|
||||
func (r *Ranking) SetList(ctx context.Context, key string, data ...redis.Z) {
|
||||
r.c.ZAdd(ctx, EcpmRankingsListPrefix+key, data...)
|
||||
r.c.ZAdd(ctx, key, data...)
|
||||
}
|
||||
|
||||
// GetList 获取排行榜,按照分数从高到低排序
|
||||
func (r *Ranking) GetList(ctx context.Context, key string, start, stop int64) (data []redis.Z, err error) {
|
||||
return r.c.ZRevRangeWithScores(ctx, EcpmRankingsListPrefix+key, start, stop).Result()
|
||||
return r.c.ZRevRangeWithScores(ctx, key, start, stop).Result()
|
||||
}
|
||||
|
||||
// GetRank 获取指定成员在排行榜中的排名(排名从 0 开始,分数越高排名越靠前)
|
||||
func (r *Ranking) GetRank(ctx context.Context, key, member string) (rank int64, err error) {
|
||||
return r.c.ZRevRank(ctx, EcpmRankingsListPrefix+key, member).Result()
|
||||
return r.c.ZRevRank(ctx, key, member).Result()
|
||||
}
|
||||
|
||||
// GetScore 获取指定成员在排行榜中的分数
|
||||
func (r *Ranking) GetScore(ctx context.Context, key, member string) (score float64, err error) {
|
||||
return r.c.ZScore(ctx, EcpmRankingsListPrefix+key, member).Result()
|
||||
return r.c.ZScore(ctx, key, member).Result()
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
//添加排行榜数据
|
||||
for s, scores := range rankList {
|
||||
data := make([]redis.Z, len(scores))
|
||||
data := make([]redis.Z, 0, len(scores))
|
||||
for _, score := range scores {
|
||||
data = append(data, redis.Z{
|
||||
Score: float64(score.Score),
|
||||
|
@ -73,7 +73,7 @@ func (m *customGameScoreModel) userRankCacheKey(userId, appId, t uint64) string
|
||||
func (m *customGameScoreModel) FindAllRankList(ctx context.Context) (resp map[string][]GameScore, err error) {
|
||||
list := make([]GameScore, 0)
|
||||
resp = make(map[string][]GameScore)
|
||||
err = m.QueryRowNoCacheCtx(ctx, &list, "select DISTINCT app_account,t from game_score")
|
||||
err = m.QueryRowsPartialNoCacheCtx(ctx, &list, "select DISTINCT app_account,t from game_score")
|
||||
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
@ -91,7 +91,7 @@ func (m *customGameScoreModel) FindAllRankList(ctx context.Context) (resp map[st
|
||||
}
|
||||
|
||||
func (m *customGameScoreModel) FindAllScore(ctx context.Context, appId uint64, t uint64) (resp []GameScore, err error) {
|
||||
err = m.QueryRowNoCacheCtx(ctx, &resp, "select * from game_score where app_account = ? and t = ?", appId, t)
|
||||
err = m.QueryRowsPartialNoCacheCtx(ctx, &resp, "select * from game_score where app_account = ? and t = ? order by score desc", appId, t)
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user