topfans/backend/services/starbookService/service/activity_asset_service.go
2026-04-20 16:00:10 +08:00

133 lines
4.6 KiB
Go

package service
import (
"time"
appErrors "github.com/topfans/backend/pkg/errors"
"github.com/topfans/backend/pkg/models"
"github.com/topfans/backend/services/starbookService/repository"
)
// ActivityAssetService 活动藏品服务接口
type ActivityAssetService interface {
// Create 创建活动藏品
Create(asset *models.ActivityAsset) error
// GetByID 根据ID获取
GetByID(id int64) (*models.ActivityAsset, error)
// GetByAssetID 根据asset_id获取
GetByAssetID(assetID int64) (*models.ActivityAsset, error)
// GetByOwner 获取用户的活动藏品
GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.ActivityAsset, error)
// GetByOwnerAndActivityType 获取用户指定活动类型的活动藏品
GetByOwnerAndActivityType(ownerUID, starID int64, activityType string, limit, offset int) ([]*models.ActivityAsset, error)
// GetByOwnerAndActivityID 获取用户指定活动的活动藏品
GetByOwnerAndActivityID(ownerUID, starID int64, activityID int64, limit, offset int) ([]*models.ActivityAsset, error)
// CountByOwner 统计用户的活动藏品数量
CountByOwner(ownerUID, starID int64) (int64, error)
// CountByOwnerAndActivityType 统计用户指定活动类型的活动藏品数量
CountByOwnerAndActivityType(ownerUID, starID int64, activityType string) (int64, error)
// IncrementLikeCount 增加点赞数
IncrementLikeCount(id int64) error
// DecrementLikeCount 减少点赞数
DecrementLikeCount(id int64) error
}
// activityAssetService 活动藏品服务实现
type activityAssetService struct {
activityRepo repository.ActivityAssetRepository
registryRepo repository.AssetRegistryRepository
}
// NewActivityAssetService 创建活动藏品服务实例
func NewActivityAssetService(
activityRepo repository.ActivityAssetRepository,
registryRepo repository.AssetRegistryRepository,
) ActivityAssetService {
return &activityAssetService{
activityRepo: activityRepo,
registryRepo: registryRepo,
}
}
// Create 创建活动藏品
func (s *activityAssetService) Create(asset *models.ActivityAsset) error {
if asset == nil {
return appErrors.ErrInvalidAssetStatus
}
// 创建活动藏品记录
if err := s.activityRepo.Create(asset); err != nil {
return err
}
// 同步写入 asset_registry
registry := &models.AssetRegistry{
AssetID: asset.AssetID,
AssetType: models.AssetTypeActivity,
OwnerUID: asset.OwnerUID,
StarID: asset.StarID,
ActivityID: &asset.ActivityID,
ActivityType: &asset.ActivityType,
Status: asset.Status,
LikeCount: asset.LikeCount,
CreatedAt: time.Now().UnixMilli(),
UpdatedAt: time.Now().UnixMilli(),
}
return s.registryRepo.Create(registry)
}
// GetByID 根据ID获取
func (s *activityAssetService) GetByID(id int64) (*models.ActivityAsset, error) {
return s.activityRepo.GetByID(id)
}
// GetByAssetID 根据asset_id获取
func (s *activityAssetService) GetByAssetID(assetID int64) (*models.ActivityAsset, error) {
return s.activityRepo.GetByAssetID(assetID)
}
// GetByOwner 获取用户的活动藏品
func (s *activityAssetService) GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.ActivityAsset, error) {
return s.activityRepo.GetByOwner(ownerUID, starID, limit, offset)
}
// GetByOwnerAndActivityType 获取用户指定活动类型的活动藏品
func (s *activityAssetService) GetByOwnerAndActivityType(ownerUID, starID int64, activityType string, limit, offset int) ([]*models.ActivityAsset, error) {
return s.activityRepo.GetByOwnerAndActivityType(ownerUID, starID, activityType, limit, offset)
}
// GetByOwnerAndActivityID 获取用户指定活动的活动藏品
func (s *activityAssetService) GetByOwnerAndActivityID(ownerUID, starID int64, activityID int64, limit, offset int) ([]*models.ActivityAsset, error) {
return s.activityRepo.GetByOwnerAndActivityID(ownerUID, starID, activityID, limit, offset)
}
// CountByOwner 统计用户的活动藏品数量
func (s *activityAssetService) CountByOwner(ownerUID, starID int64) (int64, error) {
return s.activityRepo.CountByOwner(ownerUID, starID)
}
// CountByOwnerAndActivityType 统计用户指定活动类型的活动藏品数量
func (s *activityAssetService) CountByOwnerAndActivityType(ownerUID, starID int64, activityType string) (int64, error) {
return s.activityRepo.CountByOwnerAndActivityType(ownerUID, starID, activityType)
}
// IncrementLikeCount 增加点赞数
func (s *activityAssetService) IncrementLikeCount(id int64) error {
return s.activityRepo.IncrementLikeCount(id)
}
// DecrementLikeCount 减少点赞数
func (s *activityAssetService) DecrementLikeCount(id int64) error {
return s.activityRepo.DecrementLikeCount(id)
}