162 lines
4.4 KiB
Go
162 lines
4.4 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
)
|
|
|
|
// UserServiceClient User Service RPC 客户端接口
|
|
type UserServiceClient interface {
|
|
// UpdateCrystalBalance 更新水晶余额
|
|
UpdateCrystalBalance(ctx context.Context, userID, starID int64, delta int64) (int64, error)
|
|
|
|
// UpdateAssetsCount 更新资产数量
|
|
UpdateAssetsCount(ctx context.Context, userID, starID int64, delta int32) (int32, error)
|
|
|
|
// GetFanProfile 获取粉丝档案信息
|
|
GetFanProfile(ctx context.Context, userID, starID int64) (*pbUser.FanProfile, error)
|
|
}
|
|
|
|
// userServiceClient User Service RPC 客户端实现
|
|
type userServiceClient struct {
|
|
client pbUser.UserSocialService
|
|
}
|
|
|
|
// NewUserServiceClient 创建 User Service RPC 客户端
|
|
func NewUserServiceClient(client pbUser.UserSocialService) UserServiceClient {
|
|
return &userServiceClient{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
// UpdateCrystalBalance 更新水晶余额
|
|
func (c *userServiceClient) UpdateCrystalBalance(ctx context.Context, 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),
|
|
)
|
|
|
|
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 != 0 {
|
|
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", resp.Base.Message),
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// UpdateAssetsCount 更新资产数量
|
|
func (c *userServiceClient) UpdateAssetsCount(ctx context.Context, userID, starID int64, delta int32) (int32, error) {
|
|
logger.Logger.Debug("Calling UserService.UpdateAssetsCount",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
zap.Int32("delta", delta),
|
|
)
|
|
|
|
resp, err := c.client.UpdateAssetsCount(ctx, &pbUser.UpdateAssetsCountRequest{
|
|
UserId: userID,
|
|
StarId: starID,
|
|
Delta: delta,
|
|
})
|
|
|
|
if err != nil {
|
|
logger.Logger.Error("Failed to call UserService.UpdateAssetsCount",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
zap.Int32("delta", delta),
|
|
zap.Error(err),
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
if resp.Base.Code != 0 {
|
|
logger.Logger.Warn("UserService.UpdateAssetsCount returned error",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
zap.Int32("code", int32(resp.Base.Code)),
|
|
zap.String("message", resp.Base.Message),
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
logger.Logger.Debug("UserService.UpdateAssetsCount successful",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
zap.Int32("new_count", resp.NewCount),
|
|
)
|
|
|
|
return resp.NewCount, nil
|
|
}
|
|
|
|
// GetFanProfile 获取粉丝档案信息
|
|
func (c *userServiceClient) GetFanProfile(ctx context.Context, userID, starID int64) (*pbUser.FanProfile, error) {
|
|
logger.Logger.Debug("Calling UserService.GetFanProfile",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
)
|
|
|
|
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 != pbCommon.StatusCode_STATUS_OK {
|
|
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", resp.Base.Message),
|
|
)
|
|
return nil, fmt.Errorf("failed to get fan profile: %s", resp.Base.Message)
|
|
}
|
|
|
|
logger.Logger.Debug("UserService.GetFanProfile successful",
|
|
zap.Int64("user_id", userID),
|
|
zap.Int64("star_id", starID),
|
|
zap.String("nickname", resp.Profile.Nickname),
|
|
)
|
|
|
|
return resp.Profile, nil
|
|
}
|