248 lines
7.6 KiB
Go
248 lines
7.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
|
|
appErrors "github.com/topfans/backend/pkg/errors"
|
|
"github.com/topfans/backend/pkg/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ActivityAssetRepository 活动藏品Repository接口
|
|
type ActivityAssetRepository interface {
|
|
// Create 创建活动藏品
|
|
Create(asset *models.ActivityAsset) error
|
|
|
|
// GetByID 根据ID查询
|
|
GetByID(id int64) (*models.ActivityAsset, error)
|
|
|
|
// GetByAssetID 根据asset_id查询
|
|
GetByAssetID(assetID int64) (*models.ActivityAsset, error)
|
|
|
|
// GetByAssetIDs 批量查询
|
|
GetByAssetIDs(assetIDs []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)
|
|
|
|
// UpdateLikeCount 更新点赞数
|
|
UpdateLikeCount(id int64, likeCount int32) error
|
|
|
|
// IncrementLikeCount 增加点赞数
|
|
IncrementLikeCount(id int64) error
|
|
|
|
// DecrementLikeCount 减少点赞数
|
|
DecrementLikeCount(id int64) error
|
|
}
|
|
|
|
// activityAssetRepository 活动藏品Repository实现
|
|
type activityAssetRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewActivityAssetRepository 创建活动藏品Repository实例
|
|
func NewActivityAssetRepository(db *gorm.DB) ActivityAssetRepository {
|
|
return &activityAssetRepository{db: db}
|
|
}
|
|
|
|
// Create 创建活动藏品
|
|
func (r *activityAssetRepository) Create(asset *models.ActivityAsset) error {
|
|
if asset == nil {
|
|
return errors.New("activity asset cannot be nil")
|
|
}
|
|
if asset.OwnerUID <= 0 {
|
|
return errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if asset.StarID <= 0 {
|
|
return errors.New("star_id must be greater than 0")
|
|
}
|
|
return r.db.Create(asset).Error
|
|
}
|
|
|
|
// GetByID 根据ID查询
|
|
func (r *activityAssetRepository) GetByID(id int64) (*models.ActivityAsset, error) {
|
|
if id <= 0 {
|
|
return nil, errors.New("id must be greater than 0")
|
|
}
|
|
var asset models.ActivityAsset
|
|
if err := r.db.Where("id = ?", id).First(&asset).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, appErrors.ErrActivityAssetNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &asset, nil
|
|
}
|
|
|
|
// GetByAssetID 根据asset_id查询
|
|
func (r *activityAssetRepository) GetByAssetID(assetID int64) (*models.ActivityAsset, error) {
|
|
if assetID <= 0 {
|
|
return nil, errors.New("asset_id must be greater than 0")
|
|
}
|
|
var asset models.ActivityAsset
|
|
if err := r.db.Where("asset_id = ?", assetID).First(&asset).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, appErrors.ErrActivityAssetNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &asset, nil
|
|
}
|
|
|
|
// GetByAssetIDs 批量查询
|
|
func (r *activityAssetRepository) GetByAssetIDs(assetIDs []int64) ([]*models.ActivityAsset, error) {
|
|
if len(assetIDs) == 0 {
|
|
return []*models.ActivityAsset{}, nil
|
|
}
|
|
var assets []*models.ActivityAsset
|
|
if err := r.db.Where("asset_id IN ?", assetIDs).Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// GetByOwner 查询用户的活动藏品列表
|
|
func (r *activityAssetRepository) GetByOwner(ownerUID, starID int64, limit, offset int) ([]*models.ActivityAsset, error) {
|
|
if ownerUID <= 0 {
|
|
return nil, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
var assets []*models.ActivityAsset
|
|
query := r.db.Where("owner_uid = ? AND star_id = ?", ownerUID, starID).
|
|
Order("created_at DESC")
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
if err := query.Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// GetByOwnerAndActivityType 查询用户指定活动类型的活动藏品
|
|
func (r *activityAssetRepository) GetByOwnerAndActivityType(ownerUID, starID int64, activityType string, limit, offset int) ([]*models.ActivityAsset, error) {
|
|
if ownerUID <= 0 {
|
|
return nil, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
var assets []*models.ActivityAsset
|
|
query := r.db.Where("owner_uid = ? AND star_id = ? AND activity_type = ?", ownerUID, starID, activityType).
|
|
Order("created_at DESC")
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
if err := query.Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// GetByOwnerAndActivityID 查询用户指定活动的活动藏品
|
|
func (r *activityAssetRepository) GetByOwnerAndActivityID(ownerUID, starID int64, activityID int64, limit, offset int) ([]*models.ActivityAsset, error) {
|
|
if ownerUID <= 0 {
|
|
return nil, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return nil, errors.New("star_id must be greater than 0")
|
|
}
|
|
var assets []*models.ActivityAsset
|
|
query := r.db.Where("owner_uid = ? AND star_id = ? AND activity_id = ?", ownerUID, starID, activityID).
|
|
Order("created_at DESC")
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
if offset > 0 {
|
|
query = query.Offset(offset)
|
|
}
|
|
if err := query.Find(&assets).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return assets, nil
|
|
}
|
|
|
|
// CountByOwner 统计用户的活动藏品数量
|
|
func (r *activityAssetRepository) CountByOwner(ownerUID, starID int64) (int64, error) {
|
|
if ownerUID <= 0 {
|
|
return 0, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return 0, errors.New("star_id must be greater than 0")
|
|
}
|
|
var count int64
|
|
if err := r.db.Model(&models.ActivityAsset{}).
|
|
Where("owner_uid = ? AND star_id = ?", ownerUID, starID).
|
|
Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// CountByOwnerAndActivityType 统计用户指定活动类型的活动藏品数量
|
|
func (r *activityAssetRepository) CountByOwnerAndActivityType(ownerUID, starID int64, activityType string) (int64, error) {
|
|
if ownerUID <= 0 {
|
|
return 0, errors.New("owner_uid must be greater than 0")
|
|
}
|
|
if starID <= 0 {
|
|
return 0, errors.New("star_id must be greater than 0")
|
|
}
|
|
var count int64
|
|
if err := r.db.Model(&models.ActivityAsset{}).
|
|
Where("owner_uid = ? AND star_id = ? AND activity_type = ?", ownerUID, starID, activityType).
|
|
Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// UpdateLikeCount 更新点赞数
|
|
func (r *activityAssetRepository) UpdateLikeCount(id int64, likeCount int32) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.ActivityAsset{}).
|
|
Where("id = ?", id).
|
|
Update("like_count", likeCount).Error
|
|
}
|
|
|
|
// IncrementLikeCount 增加点赞数
|
|
func (r *activityAssetRepository) IncrementLikeCount(id int64) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.ActivityAsset{}).
|
|
Where("id = ?", id).
|
|
UpdateColumn("like_count", gorm.Expr("like_count + ?", 1)).Error
|
|
}
|
|
|
|
// DecrementLikeCount 减少点赞数
|
|
func (r *activityAssetRepository) DecrementLikeCount(id int64) error {
|
|
if id <= 0 {
|
|
return errors.New("id must be greater than 0")
|
|
}
|
|
return r.db.Model(&models.ActivityAsset{}).
|
|
Where("id = ? AND like_count > ?", id, 0).
|
|
UpdateColumn("like_count", gorm.Expr("like_count - ?", 1)).Error
|
|
}
|