topfans/backend/services/moderationService/client/user_client.go
2026-06-22 17:19:48 +08:00

50 lines
1.2 KiB
Go

package client
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"github.com/topfans/backend/pkg/models"
)
// UserClient 验证 user 目标存在 + 返回 star_id
type UserClient struct {
db *gorm.DB
}
func NewUserClient(db *gorm.DB) *UserClient {
return &UserClient{db: db}
}
// GetUserForReport 验证用户存在 + 返回 (star_id, snapshot)
// user.nickname/star_id 在 fan_profiles 表里(多对一关系)
func (c *UserClient) GetUserForReport(ctx context.Context, userID int64) (starID int64, snapshot map[string]interface{}, err error) {
var user models.User
if err = c.db.WithContext(ctx).First(&user, userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, nil, ErrTargetNotFound
}
return 0, nil, err
}
// 从 fan_profiles 拉 nickname + star_id
var fp models.FanProfile
if err = c.db.WithContext(ctx).
Where("user_id = ?", userID).
Order("star_id ASC").
First(&fp).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return 0, nil, err
}
starID = fp.StarID
snapshot = map[string]interface{}{
"user_id": user.ID,
"nickname": fp.Nickname,
"avatar_url": user.AvatarURL,
"star_id": starID,
"snapshot_at": time.Now().UnixMilli(),
}
return starID, snapshot, nil
}