topfans/backend/services/userService/service/converter.go
2026-05-16 02:42:32 +08:00

111 lines
2.2 KiB
Go

package service
import (
"github.com/topfans/backend/pkg/models"
pb "github.com/topfans/backend/pkg/proto/user"
)
// ModelToProtoUser 转换 User Model 到 Proto
func ModelToProtoUser(user *models.User) *pb.User {
if user == nil {
return nil
}
pbUser := &pb.User{
Id: user.ID,
Mobile: user.Mobile,
IsActive: user.IsActive,
CreatedAt: user.CreatedAt,
}
if user.AvatarURL != nil {
pbUser.AvatarUrl = *user.AvatarURL
}
if user.GlobalWalletAddr != nil {
pbUser.GlobalWalletAddress = *user.GlobalWalletAddr
}
return pbUser
}
// ModelToProtoFanProfile 转换 FanProfile Model 到 Proto
func ModelToProtoFanProfile(profile *models.FanProfile) *pb.FanProfile {
if profile == nil {
return nil
}
pbProfile := &pb.FanProfile{
Id: profile.ID,
UserId: profile.UserID,
StarId: profile.StarID,
Nickname: profile.Nickname,
Level: profile.Level,
Times: profile.Times,
Social: profile.Social,
CoinBalance: profile.CoinBalance,
CrystalBalance: profile.CrystalBalance,
Tags: []string(profile.Tags),
CreatedAt: profile.CreatedAt,
// 新增字段
StarbookLimit: profile.StarbookLimit,
SlotLimit: profile.SlotLimit,
AssetsCount: profile.AssetsCount,
}
// 头像
if profile.AvatarURL != nil {
pbProfile.AvatarUrl = *profile.AvatarURL
}
// 链地址
if profile.ChainAddress != nil {
pbProfile.ChainAddress = *profile.ChainAddress
}
return pbProfile
}
// ModelToProtoStar 转换 Star Model 到 Proto
func ModelToProtoStar(star *models.Star) *pb.Star {
if star == nil {
return nil
}
pbStar := &pb.Star{
StarId: star.StarID,
Name: star.Name,
IdentityId: star.IdentityID,
IsActive: star.IsActive,
CreatedAt: star.CreatedAt,
}
if star.NameEn != nil {
pbStar.NameEn = *star.NameEn
}
if star.PicURL != nil {
pbStar.PicUrl = *star.PicURL
}
if star.Description != nil {
pbStar.Description = *star.Description
}
// 新增 tag 字段
if star.Tag != nil {
pbStar.Tag = *star.Tag
}
return pbStar
}
// getStringValue 安全获取字符串指针的值
func getStringValue(ptr *string) string {
if ptr == nil {
return ""
}
return *ptr
}