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

124 lines
4.0 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"
)
// CollectionService 典藏服务接口
type CollectionService interface {
// Create 创建典藏藏品
Create(asset *models.CollectionAsset) error
// GetByID 根据ID获取
GetByID(id int64) (*models.CollectionAsset, error)
// GetByAssetID 根据asset_id获取
GetByAssetID(assetID int64) (*models.CollectionAsset, error)
// GetByOwner 获取用户的典藏藏品
GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.CollectionAsset, error)
// GetByOwnerAndCategory 获取用户指定分类的典藏藏品
GetByOwnerAndCategory(ownerUID, starID int64, category string, limit, offset int) ([]*models.CollectionAsset, error)
// CountByOwner 统计用户的典藏藏品数量
CountByOwner(ownerUID, starID int64) (int64, error)
// CountByOwnerAndCategory 统计用户指定分类的典藏藏品数量
CountByOwnerAndCategory(ownerUID, starID int64, category string) (int64, error)
// IncrementLikeCount 增加点赞数
IncrementLikeCount(id int64) error
// DecrementLikeCount 减少点赞数
DecrementLikeCount(id int64) error
}
// collectionService 典藏服务实现
type collectionService struct {
collectionRepo repository.CollectionRepository
registryRepo repository.AssetRegistryRepository
}
// NewCollectionService 创建典藏服务实例
func NewCollectionService(
collectionRepo repository.CollectionRepository,
registryRepo repository.AssetRegistryRepository,
) CollectionService {
return &collectionService{
collectionRepo: collectionRepo,
registryRepo: registryRepo,
}
}
// Create 创建典藏藏品
func (s *collectionService) Create(asset *models.CollectionAsset) error {
if asset == nil {
return appErrors.ErrInvalidAssetStatus
}
// 创建典藏藏品记录
if err := s.collectionRepo.Create(asset); err != nil {
return err
}
// 同步写入 asset_registry
registry := &models.AssetRegistry{
AssetID: asset.AssetID,
AssetType: models.AssetTypeCollection,
OwnerUID: asset.OwnerUID,
StarID: asset.StarID,
CollectionCategory: &asset.Category,
Status: asset.Status,
LikeCount: asset.LikeCount,
CreatedAt: time.Now().UnixMilli(),
UpdatedAt: time.Now().UnixMilli(),
}
return s.registryRepo.Create(registry)
}
// GetByID 根据ID获取
func (s *collectionService) GetByID(id int64) (*models.CollectionAsset, error) {
return s.collectionRepo.GetByID(id)
}
// GetByAssetID 根据asset_id获取
func (s *collectionService) GetByAssetID(assetID int64) (*models.CollectionAsset, error) {
return s.collectionRepo.GetByAssetID(assetID)
}
// GetByOwner 获取用户的典藏藏品
func (s *collectionService) GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.CollectionAsset, error) {
return s.collectionRepo.GetByOwner(ownerUID, starID, limit, offset)
}
// GetByOwnerAndCategory 获取用户指定分类的典藏藏品
func (s *collectionService) GetByOwnerAndCategory(ownerUID, starID int64, category string, limit, offset int) ([]*models.CollectionAsset, error) {
return s.collectionRepo.GetByOwnerAndCategory(ownerUID, starID, category, limit, offset)
}
// CountByOwner 统计用户的典藏藏品数量
func (s *collectionService) CountByOwner(ownerUID, starID int64) (int64, error) {
return s.collectionRepo.CountByOwner(ownerUID, starID)
}
// CountByOwnerAndCategory 统计用户指定分类的典藏藏品数量
func (s *collectionService) CountByOwnerAndCategory(ownerUID, starID int64, category string) (int64, error) {
return s.collectionRepo.CountByOwnerAndCategory(ownerUID, starID, category)
}
// IncrementLikeCount 增加点赞数
func (s *collectionService) IncrementLikeCount(id int64) error {
return s.collectionRepo.IncrementLikeCount(id)
}
// DecrementLikeCount 减少点赞数
func (s *collectionService) DecrementLikeCount(id int64) error {
return s.collectionRepo.DecrementLikeCount(id)
}