package models import ( "time" ) // CardKey 卡密表 type CardKey struct { ID int `json:"id"` Key string `json:"key"` // 格式: XXXX-XXXX-XXXX-XXXX MaxUses int `json:"max_uses"` UsedCount int `json:"used_count"` ValidityType string `json:"validity_type"` // month/quarter/year/custom ExpiresAt time.Time `json:"expires_at"` IsActive bool `json:"is_active"` CreatedByID int `json:"created_by_id"` CreatedAt time.Time `json:"created_at"` } // TableName 返回表名 func (CardKey) TableName() string { return "card_keys" } // IsExpired 检查卡密是否过期 func (c *CardKey) IsExpired() bool { return time.Now().After(c.ExpiresAt) } // IsUsable 检查卡密是否可用 func (c *CardKey) IsUsable() bool { return c.IsActive && !c.IsExpired() && c.UsedCount < c.MaxUses } // RemainingUses 返回剩余使用次数 func (c *CardKey) RemainingUses() int { remaining := c.MaxUses - c.UsedCount if remaining < 0 { return 0 } return remaining }