fix: 补全 players 列表接口的用户、服务、游戏关联数据

This commit is contained in:
zetaloop
2026-04-24 06:49:12 +08:00
parent 1936533244
commit c62d743320
@@ -5,12 +5,15 @@ package player
import ( import (
"context" "context"
"juwan-backend/app/player/rpc/pb" "encoding/json"
"strconv"
"time"
"juwan-backend/app/player/api/internal/svc" "juwan-backend/app/player/api/internal/svc"
"juwan-backend/app/player/api/internal/types" "juwan-backend/app/player/api/internal/types"
"juwan-backend/app/player/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@@ -38,17 +41,89 @@ func (l *ListPlayersLogic) ListPlayers(req *types.PlayerListReq) (resp *types.Pl
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(p.Players) == 0 {
return &types.PlayerListResp{Items: []types.PlayerProfile{}}, nil
}
userIds := make([]int64, 0, len(p.Players))
for _, v := range p.Players {
userIds = append(userIds, v.UserId)
}
usersResp, err := l.svcCtx.UserRpc.GetUsersByIds(l.ctx, &usercenter.GetUsersByIdsReq{Ids: userIds})
if err != nil {
return nil, err
}
userMap := make(map[int64]*usercenter.Users, len(usersResp.Users))
for _, u := range usersResp.Users {
userMap[u.Id] = u
}
list := make([]types.PlayerProfile, 0, len(p.Players)) list := make([]types.PlayerProfile, 0, len(p.Players))
for _, v := range p.Players { for _, v := range p.Players {
temp := types.PlayerProfile{} profile := types.PlayerProfile{
err = copier.Copy(&temp, &v) Id: v.Id,
if err != nil { Rating: v.Rating,
logx.Errorf("ListPlayersLogic.ListPlayers copier.Copy err: %v", err) TotalOrders: v.TotalOrders,
continue Status: v.Status,
Gender: v.Gender,
Services: []types.PlayerService{},
Tags: append([]string{}, v.Tags...),
} }
list = append(list, temp)
games := make([]string, 0, len(v.Games))
for _, gameID := range v.Games {
games = append(games, strconv.FormatInt(gameID, 10))
} }
resp = &types.PlayerListResp{} profile.Games = games
resp.Items = list
return if v.ShopId != 0 {
profile.ShopId = strconv.FormatInt(v.ShopId, 10)
}
if u, ok := userMap[v.UserId]; ok {
verificationStatus := map[string]string{}
if u.VerificationStatus != "" {
_ = json.Unmarshal([]byte(u.VerificationStatus), &verificationStatus)
}
profile.User = types.UserProfile{
Id: strconv.FormatInt(u.Id, 10),
Username: u.Username,
Nickname: u.Nickname,
Avatar: u.Avatar,
Role: u.CurrentRole,
VerifiedRoles: append([]string{}, u.VerifiedRoles...),
VerificationStatus: verificationStatus,
Phone: u.Phone,
Bio: u.Bio,
CreatedAt: time.Unix(u.CreatedAt, 0).Format(time.DateTime),
}
}
svcResp, svcErr := l.svcCtx.PlayerRpc.SearchPlayerServices(l.ctx, &pb.SearchPlayerServicesReq{
PlayerId: v.Id,
Limit: 100,
})
if svcErr != nil {
logx.Errorf("ListPlayers SearchPlayerServices player=%d err: %v", v.Id, svcErr)
} else {
for _, s := range svcResp.PlayerServices {
profile.Services = append(profile.Services, types.PlayerService{
Id: s.Id,
PlayerId: s.PlayerId,
GameId: s.GameId,
Title: s.Title,
Description: s.Description,
Price: s.Price,
Unit: s.Unit,
RankRange: s.RankRange,
Availability: s.Availability,
})
}
}
list = append(list, profile)
}
return &types.PlayerListResp{Items: list}, nil
} }