2025-07-15 13:21:34 +08:00
|
|
|
|
package entities
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-07-20 20:53:26 +08:00
|
|
|
|
"github.com/google/uuid"
|
2025-07-15 13:21:34 +08:00
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
|
|
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:"软删除时间"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-20 20:53:26 +08:00
|
|
|
|
// BeforeCreate GORM钩子:创建前自动生成UUID
|
|
|
|
|
|
func (s *Subscription) BeforeCreate(tx *gorm.DB) error {
|
|
|
|
|
|
if s.ID == "" {
|
|
|
|
|
|
s.ID = uuid.New().String()
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-15 13:21:34 +08:00
|
|
|
|
// IsValid 检查订阅是否有效
|
|
|
|
|
|
func (s *Subscription) IsValid() bool {
|
|
|
|
|
|
return s.DeletedAt.Time.IsZero()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IncrementAPIUsage 增加API使用次数
|
|
|
|
|
|
func (s *Subscription) IncrementAPIUsage(count int64) {
|
|
|
|
|
|
s.APIUsed += count
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ResetAPIUsage 重置API使用次数
|
|
|
|
|
|
func (s *Subscription) ResetAPIUsage() {
|
|
|
|
|
|
s.APIUsed = 0
|
|
|
|
|
|
}
|