PromotionReferralCode.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Module\Promotion\Models;
  3. use App\Module\Promotion\Enums\REFERRAL_CODE_STATUS;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use UCore\ModelCore;
  7. /**
  8. * 推荐码
  9. *
  10. * field start
  11. * field end
  12. */
  13. class PromotionReferralCode extends ModelCore
  14. {
  15. /**
  16. * 与模型关联的表名
  17. *
  18. * @var string
  19. */
  20. protected $table = 'promotion_referral_codes';
  21. // attrlist start
  22. protected $fillable = [
  23. ];
  24. // attrlist end
  25. /**
  26. * 应该被转换为日期的属性
  27. *
  28. * @var array
  29. */
  30. protected $dates = [
  31. 'expire_time',
  32. 'created_at',
  33. 'updated_at',
  34. ];
  35. /**
  36. * 应该被转换为原生类型的属性
  37. *
  38. * @var array
  39. */
  40. protected $casts = [
  41. 'status' => REFERRAL_CODE_STATUS::class,
  42. ];
  43. /**
  44. * 获取用户信息
  45. *
  46. * @return BelongsTo
  47. */
  48. public function user()
  49. {
  50. return $this->belongsTo('App\Models\User', 'user_id');
  51. }
  52. /**
  53. * 获取推荐码使用记录
  54. *
  55. * @return HasMany
  56. */
  57. public function usages()
  58. {
  59. return $this->hasMany(PromotionReferralCodeUsage::class, 'code', 'code');
  60. }
  61. /**
  62. * 判断推荐码是否有效
  63. *
  64. * @return bool
  65. */
  66. public function isValid(): bool
  67. {
  68. if ($this->status != REFERRAL_CODE_STATUS::ACTIVE) {
  69. return false;
  70. }
  71. if ($this->expire_time && $this->expire_time->isPast()) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. }