promo_code.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package service
  2. import (
  3. "time"
  4. )
  5. // PromoCode 注册优惠码
  6. type PromoCode struct {
  7. ID int64
  8. Code string
  9. BonusAmount float64
  10. MaxUses int
  11. UsedCount int
  12. Status string
  13. ExpiresAt *time.Time
  14. Notes string
  15. CreatedAt time.Time
  16. UpdatedAt time.Time
  17. // 关联
  18. UsageRecords []PromoCodeUsage
  19. }
  20. // PromoCodeUsage 优惠码使用记录
  21. type PromoCodeUsage struct {
  22. ID int64
  23. PromoCodeID int64
  24. UserID int64
  25. BonusAmount float64
  26. UsedAt time.Time
  27. // 关联
  28. PromoCode *PromoCode
  29. User *User
  30. }
  31. // CanUse 检查优惠码是否可用
  32. func (p *PromoCode) CanUse() bool {
  33. if p.Status != PromoCodeStatusActive {
  34. return false
  35. }
  36. if p.ExpiresAt != nil && time.Now().After(*p.ExpiresAt) {
  37. return false
  38. }
  39. if p.MaxUses > 0 && p.UsedCount >= p.MaxUses {
  40. return false
  41. }
  42. return true
  43. }
  44. // IsExpired 检查是否已过期
  45. func (p *PromoCode) IsExpired() bool {
  46. return p.ExpiresAt != nil && time.Now().After(*p.ExpiresAt)
  47. }
  48. // CreatePromoCodeInput 创建优惠码输入
  49. type CreatePromoCodeInput struct {
  50. Code string
  51. BonusAmount float64
  52. MaxUses int
  53. ExpiresAt *time.Time
  54. Notes string
  55. }
  56. // UpdatePromoCodeInput 更新优惠码输入
  57. type UpdatePromoCodeInput struct {
  58. Code *string
  59. BonusAmount *float64
  60. MaxUses *int
  61. Status *string
  62. ExpiresAt *time.Time
  63. Notes *string
  64. }