diff --git a/app/player/api/internal/logic/player/listPlayersLogic.go b/app/player/api/internal/logic/player/listPlayersLogic.go index be91844..e1d63ca 100644 --- a/app/player/api/internal/logic/player/listPlayersLogic.go +++ b/app/player/api/internal/logic/player/listPlayersLogic.go @@ -5,12 +5,15 @@ package player import ( "context" - "juwan-backend/app/player/rpc/pb" + "encoding/json" + "strconv" + "time" "juwan-backend/app/player/api/internal/svc" "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" ) @@ -38,17 +41,89 @@ func (l *ListPlayersLogic) ListPlayers(req *types.PlayerListReq) (resp *types.Pl if err != nil { 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)) for _, v := range p.Players { - temp := types.PlayerProfile{} - err = copier.Copy(&temp, &v) - if err != nil { - logx.Errorf("ListPlayersLogic.ListPlayers copier.Copy err: %v", err) - continue + profile := types.PlayerProfile{ + Id: v.Id, + Rating: v.Rating, + TotalOrders: v.TotalOrders, + 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)) + } + profile.Games = games + + 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) } - resp = &types.PlayerListResp{} - resp.Items = list - return + + return &types.PlayerListResp{Items: list}, nil }