69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
|
package logic
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/internal/logic/rankings"
|
||
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/user_service/user"
|
||
|
"github.com/redis/go-redis/v9"
|
||
|
"strconv"
|
||
|
|
||
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/internal/svc"
|
||
|
"gitea.youtukeji.com.cn/youtu/youtu_grpc/app/ranking_service/ranking"
|
||
|
|
||
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
)
|
||
|
|
||
|
type SettlementLogic struct {
|
||
|
ctx context.Context
|
||
|
svcCtx *svc.ServiceContext
|
||
|
logx.Logger
|
||
|
}
|
||
|
|
||
|
func NewSettlementLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SettlementLogic {
|
||
|
return &SettlementLogic{
|
||
|
ctx: ctx,
|
||
|
svcCtx: svcCtx,
|
||
|
Logger: logx.WithContext(ctx),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *SettlementLogic) Settlement(in *ranking.AtomicGetHigherUserRequest) (res *ranking.SettlementResponse, err error) {
|
||
|
res = &ranking.SettlementResponse{}
|
||
|
cacheKey := rankings.GetRankingsCacheKey(in.AppId, in.Type)
|
||
|
|
||
|
//查询高一名分数
|
||
|
higherLogic := NewAtomicGetHigherUserLogic(l.ctx, l.svcCtx)
|
||
|
res.Higher, err = higherLogic.AtomicGetHigherUser(in)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
//查询自己的数据
|
||
|
mineUser, err := l.svcCtx.UserServiceClient.GetUserById(l.ctx, &user.UserId{UserId: in.UserId})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
//自己最高分
|
||
|
_, maxScore, err := l.svcCtx.RedisRanking.GetRankAndScore(l.ctx, cacheKey, strconv.FormatUint(in.UserId, 10))
|
||
|
if err != nil && !errors.Is(err, redis.Nil) {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if uMax := uint32(uint64(maxScore) >> 32); uMax > in.Score {
|
||
|
res.MineMax = uMax
|
||
|
} else {
|
||
|
res.MineMax = in.Score
|
||
|
}
|
||
|
|
||
|
res.Mine = &ranking.RankingList{
|
||
|
Nickname: mineUser.Nickname,
|
||
|
Avatar: mineUser.Avatar,
|
||
|
Score: in.Score,
|
||
|
Self: true,
|
||
|
}
|
||
|
|
||
|
return res, nil
|
||
|
}
|