fix排行榜问题

This commit is contained in:
xiabin 2025-01-23 16:14:23 +08:00
parent 02e95503cf
commit 6502832392
7 changed files with 61 additions and 31 deletions

View File

@ -11,7 +11,7 @@ type RankingData {
Nickname string `json:"nickname" db:"nickname"` // 昵称
Avatar string `json:"avatar" db:"avatar"` // 头像
Score uint32 `json:"score" db:"score"` // 得分
UserId uint64 `json:"-" db:"app_user_id"` // 用户 ID
UserId uint64 `json:"userId" db:"app_user_id"` // 用户 ID
Rank uint32 `json:"rank" db:"t_rank"` // 排名
Self bool `json:"self" db:"-"` // 是否是自己
}

View File

@ -73,7 +73,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
rest.WithPrefix("/v1/game"),
rest.WithTimeout(30000*time.Millisecond),
rest.WithTimeout(3000*time.Millisecond),
rest.WithMaxBytes(1048576),
)

View File

@ -34,8 +34,19 @@ func (l *DouyinCode2tokenLogic) DouyinCode2token(req *types.DouyinCode2TokenRequ
return
}
req.AnonymousCode = ""
if req.Code == "" {
resp.Code = -1
resp.Message = "code is empty"
return
}
res, err := douyinCli.Api.Code2Session(req.Code, req.AnonymousCode)
if err != nil {
l.Logger.Errorf("douyin open api code2session error: %v,resp:%+v", err, res)
resp.Code = -1
resp.Message = err.Error()
err = nil
return
}

View File

@ -37,12 +37,16 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
return nil, err
}
var flag bool
var (
flag bool
userRank types.RankingData
)
for i := range resp.RankingData {
if resp.RankingData[i].UserId == at.UserId {
resp.RankingData[i].Self = true
d := resp.RankingData[i]
resp.RankingData = append(resp.RankingData, d)
userRank = resp.RankingData[i]
l.Logger.Debugf("userRank: %+v", userRank)
resp.RankingData = append(resp.RankingData, userRank)
flag = true
break
}
@ -51,11 +55,14 @@ func (l *RankingListLogic) RankingList(req *types.RankingListRequest) (resp *typ
if !flag {
userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId, req.Type)
if err != nil {
return nil, err
}
l.Logger.Debugf("userRank: %+v", userRank)
userRank.Self = true
resp.RankingData = append(resp.RankingData, userRank)
}
l.Logger.Debugf("resp: %+v", resp)
return
}

View File

@ -25,12 +25,12 @@ type PageBase struct {
}
type RankingData struct {
Nickname string `json:"nickname" db:"nickname"` // 昵称
Avatar string `json:"avatar" db:"avatar"` // 头像
Score uint32 `json:"score" db:"score"` // 得分
UserId uint64 `json:"-" db:"app_user_id"` // 用户 ID
Rank uint32 `json:"rank" db:"t_rank"` // 排名
Self bool `json:"self" db:"-"` // 是否是自己
Nickname string `json:"nickname" db:"nickname"` // 昵称
Avatar string `json:"avatar" db:"avatar"` // 头像
Score uint32 `json:"score" db:"score"` // 得分
UserId uint64 `json:"userId" db:"app_user_id"` // 用户 ID
Rank uint32 `json:"rank" db:"t_rank"` // 排名
Self bool `json:"self" db:"-"` // 是否是自己
}
type RankingListRequest struct {

View File

@ -23,6 +23,8 @@ type (
}
)
const cacheEcpmAppUserPrefix = "cache:ecpm:appUser:"
// NewAppUserModel returns a model for the database table.
func NewAppUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) AppUserModel {
return &customAppUserModel{
@ -31,11 +33,11 @@ func NewAppUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option)
}
func (m *customAppUserModel) FindOrCreate(ctx context.Context, data *AppUser) (err error) {
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, data.Id)
ecpmAppUserIdKey := fmt.Sprintf("%sappId:%d:openid:%s", cacheEcpmAppUserPrefix, data.AppAccountId, data.Openid)
var resp AppUser
err = m.QueryRowCtx(ctx, &resp, ecpmAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `openid` = ? limit 1", appUserRows, m.table)
return conn.QueryRowCtx(ctx, v, query, data.Openid)
query := "select * from app_user where `openid` = ? and `app_account_id`= ? limit 1"
return conn.QueryRowCtx(ctx, v, query, data.Openid, data.AppAccountId)
})
switch err {
case nil:

View File

@ -101,25 +101,35 @@ func (m *customGameScoreModel) GetUserRank(ctx context.Context, appId uint64, us
err = m.QueryRowCtx(ctx, &res, m.userRankCacheKey(userId, appId, t), func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := `
SELECT
app_user.nickname,
app_user.avatar,
gs.score,
gs.app_user_id,
gs.t_rank
app_user.nickname,
app_user.avatar,
gs.score,
gs.app_user_id,
gs.t_rank
FROM
(
SELECT
game_score.score,
game_score.app_user_id,
game_score.app_account,
rank() over (ORDER BY game_score.score DESC) t_rank
FROM
game_score
WHERE
game_score.t = ?) AS gs
(
SELECT
game_score.score,
game_score.app_user_id,
game_score.app_account,
-- 使用窗口函数rank对分数进行降序排名
rank() OVER (ORDER BY game_score.score DESC) t_rank
FROM
game_score
-- 在子查询中先根据t = 0过滤数据
WHERE
game_score.t = ?
-- 在子查询中根据app_account = 2过滤数据
AND game_score.app_account = ?
) AS gs
-- 使用LEFT JOIN将子查询结果和app_user表进行连接
LEFT JOIN app_user ON app_user.id = gs.app_user_id
AND gs.app_account = ? AND game_score.app_user_id = ? LIMIT 1`
return conn.QueryRowsPartialCtx(ctx, v, query, t, userId, appId)
-- 在外部查询中根据app_user_id = 59过滤数据
WHERE
gs.app_user_id = ?
LIMIT 1;
`
return conn.QueryRowsPartialCtx(ctx, v, query, t, appId, userId)
})
switch {
case err == nil: