package radio import ( "errors" "sundynix-go/global" common "sundynix-go/model/commom/request" "sundynix-go/model/radio" "time" "gorm.io/gorm" ) type SubscriptionService struct{} const MaxFreeSubscription = 2 // GetUserSubscription 获取用户订阅列表 func (s *SubscriptionService) GetUserSubscription(userId string, info common.PageInfo) ([]radio.RadioSubscription, int64, error) { var subscriptions []radio.RadioSubscription var total int64 db := global.DB // 替换为你实际的 GORM 变量 // 2. 统计该用户订阅的总数 (Status=1 表示订阅中) err := db.Model(&radio.RadioSubscription{}). Where("user_id = ? AND status = 1", userId). Count(&total).Error if err != nil { return nil, 0, err } // 3. 执行分页关联查询 err = db.Model(&radio.RadioSubscription{}). Where("user_id = ? AND status = 1", userId). Limit(info.PageSize). Offset((info.Current-1)*info.PageSize). Order("created_at DESC"). // 级联加载频道及其封面 Preload("Channel"). Preload("Channel.Cover"). // 关键:子查询过滤——只预加载每个频道 ID 最大的那一条节目 Preload("Channel.Programs", func(db *gorm.DB) *gorm.DB { // 子查询:找到每个频道下 ID 最大的节目(通常 ID 越大代表越新,也可以用 CreatedAt) subQuery := db.Table("sundynix_radio_program"). Select("id"). Where("status = 1"). // 只找上架的 Where("created_at = (SELECT MAX(created_at) FROM sundynix_radio_program AS rp WHERE rp.channel_id = sundynix_radio_program.channel_id AND rp.status = 1)") // 嵌套预加载:节目里的音频和封面也一并带出来 return db.Where("id IN (?)", subQuery). Preload("Cover"). Preload("Audio") }). Find(&subscriptions).Error if err != nil { return nil, total, err } return subscriptions, total, err } // GetUserSubscriptionHistory 获取用户历史订阅过的频道ID列表 func (s *SubscriptionService) GetUserSubscriptionHistory(userId string) ([]string, error) { var channelIds []string err := global.DB.Model(&radio.RadioSubscription{}). Where("user_id = ?", userId). Pluck("channel_id", &channelIds).Error return channelIds, err } // HasEverSubscribed 检查用户是否曾经订阅过该频道(包括已取消的) func (s *SubscriptionService) HasEverSubscribed(userId, channelId string) (bool, error) { var count int64 // 使用Unscoped查询包括已软删除的记录 err := global.DB.Unscoped().Model(&radio.RadioSubscription{}). Where("user_id = ? AND channel_id = ?", userId, channelId). Count(&count).Error return count > 0, err } // HasSubscription 检查用户当前是否订阅该频道(未取消的) func (s *SubscriptionService) HasSubscription(userId, channelId string) (bool, error) { var count int64 err := global.DB.Model(&radio.RadioSubscription{}). Where("user_id = ? AND channel_id = ?", userId, channelId). Count(&count).Error return count > 0, err } // CanSubscribe 检查是否可以订阅 // 规则: // 1. 如果用户是VIP且未过期,可以订阅任意频道 // 2. 如果用户曾经订阅过该频道(取消后再订阅),可以免费订阅 // 3. 否则检查当前有效订阅数量是否达到上限(2个) func (s *SubscriptionService) CanSubscribe(userId, channelId string) (bool, string, error) { // 检查是否已经是订阅用户(未取消的订阅) var existing radio.RadioSubscription err := global.DB.Where("user_id = ? AND channel_id = ?", userId, channelId).First(&existing).Error if err == nil { return false, "您已订阅该频道", nil } if !errors.Is(err, gorm.ErrRecordNotFound) { return false, "", err } // 获取radio_user检查VIP状态 var radioUser radio.RadioUser err = global.DB.Where("user_id = ?", userId).First(&radioUser).Error if err == nil && radioUser.IsVip == 1 { // 检查VIP是否过期 if radioUser.VipExpireAt != nil && *radioUser.VipExpireAt > time.Now().Unix() { return true, "", nil } } // 检查用户是否曾经订阅过该频道(取消后又订阅的情况) hasEverSubscribed, err := s.HasEverSubscribed(userId, channelId) if err != nil { return false, "", err } if hasEverSubscribed { // 曾今订阅过,可以免费再次订阅 return true, "", nil } // 非VIP用户,检查当前有效订阅数量(排除已取消的) var count int64 err = global.DB.Model(&radio.RadioSubscription{}).Where("user_id = ?", userId).Count(&count).Error if err != nil { return false, "", err } if count >= MaxFreeSubscription { return false, "免费订阅数量已达上限(2个),请开通VIP或订阅付费频道", nil } return true, "", nil } // Subscribe 订阅频道 func (s *SubscriptionService) Subscribe(userId, channelId string, subType int) error { subscription := radio.RadioSubscription{ UserId: userId, ChannelId: channelId, } return global.DB.Create(&subscription).Error } // Unsubscribe 退订频道(逻辑删除,更新删除时间表示已取消) func (s *SubscriptionService) Unsubscribe(userId, channelId string) error { // 软删除:将DeletedAt设置为当前时间,表示已取消订阅 // 这样用户可以再次免费订阅该频道 return global.DB.Model(&radio.RadioSubscription{}). Where("user_id = ? AND channel_id = ?", userId, channelId). Update("deleted_at", time.Now()).Error } // GetVipStatus 获取VIP状态 func (s *SubscriptionService) GetVipStatus(userId string) (bool, int64, error) { var radioUser radio.RadioUser err := global.DB.Where("user_id = ?", userId).First(&radioUser).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return false, 0, nil } return false, 0, err } if radioUser.IsVip == 1 && radioUser.VipExpireAt != nil && *radioUser.VipExpireAt > time.Now().Unix() { return true, *radioUser.VipExpireAt, nil } return false, 0, nil }