60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
|
|
package entities
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SubscriptionStatus 订阅状态枚举
|
||
|
|
type SubscriptionStatus string
|
||
|
|
|
||
|
|
const (
|
||
|
|
SubscriptionStatusActive SubscriptionStatus = "active" // 活跃
|
||
|
|
SubscriptionStatusInactive SubscriptionStatus = "inactive" // 非活跃
|
||
|
|
SubscriptionStatusExpired SubscriptionStatus = "expired" // 已过期
|
||
|
|
SubscriptionStatusCanceled SubscriptionStatus = "canceled" // 已取消
|
||
|
|
)
|
||
|
|
|
||
|
|
// Subscription 订阅实体
|
||
|
|
type Subscription struct {
|
||
|
|
ID string `gorm:"primaryKey;type:varchar(36)" comment:"订阅ID"`
|
||
|
|
UserID string `gorm:"type:varchar(36);not null;index" comment:"用户ID"`
|
||
|
|
ProductID string `gorm:"type:varchar(36);not null;index" comment:"产品ID"`
|
||
|
|
Status SubscriptionStatus `gorm:"type:varchar(20);not null;default:'active'" comment:"订阅状态"`
|
||
|
|
Price float64 `gorm:"type:decimal(10,2);not null" comment:"订阅价格"`
|
||
|
|
APIUsed int64 `gorm:"default:0" comment:"已使用API调用次数"`
|
||
|
|
|
||
|
|
// 关联关系
|
||
|
|
Product *Product `gorm:"foreignKey:ProductID" comment:"产品"`
|
||
|
|
|
||
|
|
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
||
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
||
|
|
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// IsValid 检查订阅是否有效
|
||
|
|
func (s *Subscription) IsValid() bool {
|
||
|
|
return s.DeletedAt.Time.IsZero()
|
||
|
|
}
|
||
|
|
|
||
|
|
// IncrementAPIUsage 增加API使用次数
|
||
|
|
func (s *Subscription) IncrementAPIUsage(count int64) {
|
||
|
|
s.APIUsed += count
|
||
|
|
}
|
||
|
|
|
||
|
|
// Activate 激活订阅
|
||
|
|
func (s *Subscription) Activate() {
|
||
|
|
s.Status = SubscriptionStatusActive
|
||
|
|
}
|
||
|
|
|
||
|
|
// Deactivate 停用订阅
|
||
|
|
func (s *Subscription) Deactivate() {
|
||
|
|
s.Status = SubscriptionStatusInactive
|
||
|
|
}
|
||
|
|
|
||
|
|
// ResetAPIUsage 重置API使用次数
|
||
|
|
func (s *Subscription) ResetAPIUsage() {
|
||
|
|
s.APIUsed = 0
|
||
|
|
}
|