package client import ( "context" "errors" "fmt" "github.com/topfans/backend/pkg/logger" pbCommon "github.com/topfans/backend/pkg/proto/common" pbUser "github.com/topfans/backend/pkg/proto/user" "go.uber.org/zap" ) // FanProfile 粉丝档案信息 type FanProfile struct { ID int64 UserID int64 StarID int64 Nickname string Level int32 CrystalBalance int64 } // UserRPCClient User Service RPC客户端接口 type UserRPCClient interface { // GetFanProfile 获取粉丝档案(等级、水晶余额等) GetFanProfile(userID, starID int64) (*FanProfile, error) // UpdateCrystalBalance 更新水晶余额(返回更新后的余额) UpdateCrystalBalance(userID, starID int64, delta int64) (int64, error) } // userRPCClient User Service RPC客户端实现 type userRPCClient struct { client pbUser.UserSocialService } // NewUserRPCClient 创建User Service RPC客户端 func NewUserRPCClient(client pbUser.UserSocialService) UserRPCClient { return &userRPCClient{ client: client, } } // GetFanProfile 获取粉丝档案(等级、水晶余额等) func (c *userRPCClient) GetFanProfile(userID, starID int64) (*FanProfile, error) { logger.Logger.Debug("Calling UserService.GetFanProfile", zap.Int64("user_id", userID), zap.Int64("star_id", starID), ) ctx := context.Background() resp, err := c.client.GetFanProfile(ctx, &pbUser.GetFanProfileRequest{ UserId: userID, StarId: starID, }) if err != nil { logger.Logger.Error("Failed to call UserService.GetFanProfile", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Error(err), ) return nil, err } // StatusCode_STATUS_OK = 200,不是 0 if resp.Base.Code != pbCommon.StatusCode_STATUS_OK { errorMsg := resp.Base.Message if errorMsg == "" { errorMsg = fmt.Sprintf("UserService返回错误码: %d", resp.Base.Code) } logger.Logger.Warn("UserService.GetFanProfile returned error", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int32("code", int32(resp.Base.Code)), zap.String("message", errorMsg), ) return nil, errors.New(errorMsg) } logger.Logger.Debug("UserService.GetFanProfile successful", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int32("level", resp.Profile.Level), zap.Int64("crystal_balance", resp.Profile.CrystalBalance), ) return &FanProfile{ ID: resp.Profile.Id, UserID: resp.Profile.UserId, StarID: resp.Profile.StarId, Nickname: resp.Profile.Nickname, Level: resp.Profile.Level, CrystalBalance: resp.Profile.CrystalBalance, }, nil } // UpdateCrystalBalance 更新水晶余额(返回更新后的余额) func (c *userRPCClient) UpdateCrystalBalance(userID, starID int64, delta int64) (int64, error) { logger.Logger.Debug("Calling UserService.UpdateCrystalBalance", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int64("delta", delta), ) ctx := context.Background() resp, err := c.client.UpdateCrystalBalance(ctx, &pbUser.UpdateCrystalBalanceRequest{ UserId: userID, StarId: starID, Delta: delta, }) if err != nil { logger.Logger.Error("Failed to call UserService.UpdateCrystalBalance", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int64("delta", delta), zap.Error(err), ) return 0, err } // StatusCode_STATUS_OK = 200,不是 0 if resp.Base.Code != pbCommon.StatusCode_STATUS_OK { errorMsg := resp.Base.Message if errorMsg == "" { errorMsg = fmt.Sprintf("UserService返回错误码: %d", resp.Base.Code) } logger.Logger.Warn("UserService.UpdateCrystalBalance returned error", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int32("code", int32(resp.Base.Code)), zap.String("message", errorMsg), ) return 0, errors.New(errorMsg) } logger.Logger.Debug("UserService.UpdateCrystalBalance successful", zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int64("new_balance", resp.NewBalance), ) return resp.NewBalance, nil }