TeamReferralCode.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\Team\Models;
  3. use App\Module\Team\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. * @property int $id 主键ID
  12. * @property int $user_id 用户ID
  13. * @property string $code 推荐码
  14. * @property int $usage_count 使用次数
  15. * @property \App\Module\Team\Enums\REFERRAL_CODE_STATUS $status 状态:1有效,0无效
  16. * @property string $expire_time 过期时间,NULL表示永不过期
  17. * @property \Carbon\Carbon $created_at 创建时间
  18. * @property \Carbon\Carbon $updated_at 更新时间
  19. * field end
  20. */
  21. class TeamReferralCode extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'team_referral_codes';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'user_id',
  33. 'code',
  34. 'usage_count',
  35. 'status',
  36. 'expire_time',
  37. ];
  38. // attrlist end
  39. /**
  40. * 应该被转换为日期的属性
  41. *
  42. * @var array
  43. */
  44. protected $dates = [
  45. 'expire_time',
  46. 'created_at',
  47. 'updated_at',
  48. ];
  49. /**
  50. * 应该被转换为原生类型的属性
  51. *
  52. * @var array
  53. */
  54. protected $casts = [
  55. 'status' => REFERRAL_CODE_STATUS::class,
  56. ];
  57. /**
  58. * 获取用户信息
  59. *
  60. * @return BelongsTo
  61. */
  62. public function user()
  63. {
  64. return $this->belongsTo('App\Models\User', 'user_id');
  65. }
  66. /**
  67. * 获取推荐码使用记录
  68. *
  69. * @return HasMany
  70. */
  71. public function usages()
  72. {
  73. return $this->hasMany(TeamReferralCodeUsage::class, 'code', 'code');
  74. }
  75. /**
  76. * 判断推荐码是否有效
  77. *
  78. * @return bool
  79. */
  80. public function isValid(): bool
  81. {
  82. if ($this->status != REFERRAL_CODE_STATUS::ACTIVE) {
  83. return false;
  84. }
  85. if ($this->expire_time && $this->expire_time->isPast()) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. }