- 支持创建/编辑/删除优惠码,设置赠送金额和使用限制 - 注册页面实时验证优惠码并显示赠送金额 - 支持 URL 参数自动填充 (?promo=CODE) - 添加优惠码验证接口速率限制 - 使用数据库行锁防止并发超限 - 新增后台优惠码管理页面,支持复制注册链接
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package service
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// PromoCode 注册优惠码
|
|
type PromoCode struct {
|
|
ID int64
|
|
Code string
|
|
BonusAmount float64
|
|
MaxUses int
|
|
UsedCount int
|
|
Status string
|
|
ExpiresAt *time.Time
|
|
Notes string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
|
|
// 关联
|
|
UsageRecords []PromoCodeUsage
|
|
}
|
|
|
|
// PromoCodeUsage 优惠码使用记录
|
|
type PromoCodeUsage struct {
|
|
ID int64
|
|
PromoCodeID int64
|
|
UserID int64
|
|
BonusAmount float64
|
|
UsedAt time.Time
|
|
|
|
// 关联
|
|
PromoCode *PromoCode
|
|
User *User
|
|
}
|
|
|
|
// CanUse 检查优惠码是否可用
|
|
func (p *PromoCode) CanUse() bool {
|
|
if p.Status != PromoCodeStatusActive {
|
|
return false
|
|
}
|
|
if p.ExpiresAt != nil && time.Now().After(*p.ExpiresAt) {
|
|
return false
|
|
}
|
|
if p.MaxUses > 0 && p.UsedCount >= p.MaxUses {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsExpired 检查是否已过期
|
|
func (p *PromoCode) IsExpired() bool {
|
|
return p.ExpiresAt != nil && time.Now().After(*p.ExpiresAt)
|
|
}
|
|
|
|
// CreatePromoCodeInput 创建优惠码输入
|
|
type CreatePromoCodeInput struct {
|
|
Code string
|
|
BonusAmount float64
|
|
MaxUses int
|
|
ExpiresAt *time.Time
|
|
Notes string
|
|
}
|
|
|
|
// UpdatePromoCodeInput 更新优惠码输入
|
|
type UpdatePromoCodeInput struct {
|
|
Code *string
|
|
BonusAmount *float64
|
|
MaxUses *int
|
|
Status *string
|
|
ExpiresAt *time.Time
|
|
Notes *string
|
|
}
|