topfans/backend/services/activityService/client/user_rpc_client.go
2026-04-07 22:29:48 +08:00

132 lines
3.4 KiB
Go

package client
import (
"context"
"dubbo.apache.org/dubbo-go/v3/client"
_ "dubbo.apache.org/dubbo-go/v3/imports"
pbUser "github.com/topfans/backend/pkg/proto/user"
"github.com/topfans/backend/pkg/logger"
"go.uber.org/zap"
)
// FanProfile 粉丝档案信息
type FanProfile struct {
UserID int64
StarID int64
Nickname string
AvatarUrl 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(dubboClient *client.Client) (UserRPCClient, error) {
userService, err := pbUser.NewUserSocialService(dubboClient)
if err != nil {
logger.Logger.Error("Failed to create UserSocialService client", zap.Error(err))
return nil, err
}
return &userRPCClient{
client: userService,
}, nil
}
// 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
}
if resp.Base.Code != 200 {
logger.Logger.Warn("UserService.GetFanProfile returned error",
zap.Int64("user_id", userID),
zap.Int64("star_id", starID),
zap.String("message", resp.Base.Message),
)
return nil, nil
}
if resp.Profile == nil {
return nil, nil
}
return &FanProfile{
UserID: resp.Profile.UserId,
StarID: resp.Profile.StarId,
Nickname: resp.Profile.Nickname,
AvatarUrl: resp.Profile.AvatarUrl,
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
}
if resp.Base.Code != 200 {
logger.Logger.Warn("UserService.UpdateCrystalBalance returned error",
zap.Int64("user_id", userID),
zap.Int64("star_id", starID),
zap.Int64("delta", delta),
zap.String("message", resp.Base.Message),
)
return 0, nil
}
return resp.NewBalance, nil
}