ActivityParticipation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Module\Activity\Models;
  3. use App\Module\Activity\Enums\PARTICIPATION_STATUS;
  4. use App\Module\Activity\Enums\REWARD_STATUS;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use UCore\ModelCore;
  7. /**
  8. * 活动参与记录
  9. *
  10. * field start
  11. * @property int $id 主键
  12. * @property int $user_id 用户ID
  13. * @property int $activity_id 活动ID
  14. * @property \Carbon\Carbon $participate_time 参与时间
  15. * @property int $reward_status 奖励状态(0:未领取, 1:已领取, 2:已过期)
  16. * @property int $completion_status 完成状态(0:进行中, 1:已完成, 2:已失败)
  17. * @property \Carbon\Carbon $completion_time 完成时间
  18. * @property \Carbon\Carbon $created_at 创建时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. * field end
  21. */
  22. class ActivityParticipation extends ModelCore
  23. {
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'activity_participation';
  30. /**
  31. * 可批量赋值的属性
  32. *
  33. * @var array
  34. */
  35. // attrlist start
  36. protected $fillable = [
  37. 'id',
  38. 'user_id',
  39. 'activity_id',
  40. 'participate_time',
  41. 'reward_status',
  42. 'completion_status',
  43. 'completion_time',
  44. ];
  45. // attrlist end
  46. /**
  47. * 应该被转换为原生类型的属性
  48. *
  49. * @var array
  50. */
  51. protected $casts = [
  52. 'user_id' => 'integer',
  53. 'activity_id' => 'integer',
  54. 'reward_status' => 'integer',
  55. 'completion_status' => 'integer',
  56. 'participate_time' => 'datetime',
  57. 'completion_time' => 'datetime',
  58. ];
  59. /**
  60. * 获取关联的活动
  61. *
  62. * @return BelongsTo
  63. */
  64. public function activity(): BelongsTo
  65. {
  66. return $this->belongsTo(ActivityConfig::class, 'activity_id', 'id');
  67. }
  68. /**
  69. * 获取奖励状态名称
  70. *
  71. * @return string
  72. */
  73. public function getRewardStatusName(): string
  74. {
  75. return REWARD_STATUS::getName($this->reward_status);
  76. }
  77. /**
  78. * 获取完成状态名称
  79. *
  80. * @return string
  81. */
  82. public function getCompletionStatusName(): string
  83. {
  84. return PARTICIPATION_STATUS::getName($this->completion_status);
  85. }
  86. /**
  87. * 检查是否已完成
  88. *
  89. * @return bool
  90. */
  91. public function isCompleted(): bool
  92. {
  93. return $this->completion_status === PARTICIPATION_STATUS::COMPLETED;
  94. }
  95. /**
  96. * 检查是否进行中
  97. *
  98. * @return bool
  99. */
  100. public function isInProgress(): bool
  101. {
  102. return $this->completion_status === PARTICIPATION_STATUS::IN_PROGRESS;
  103. }
  104. /**
  105. * 检查是否已失败
  106. *
  107. * @return bool
  108. */
  109. public function isFailed(): bool
  110. {
  111. return $this->completion_status === PARTICIPATION_STATUS::FAILED;
  112. }
  113. /**
  114. * 检查奖励是否已领取
  115. *
  116. * @return bool
  117. */
  118. public function isRewardClaimed(): bool
  119. {
  120. return $this->reward_status === REWARD_STATUS::CLAIMED;
  121. }
  122. /**
  123. * 检查奖励是否未领取
  124. *
  125. * @return bool
  126. */
  127. public function isRewardNotClaimed(): bool
  128. {
  129. return $this->reward_status === REWARD_STATUS::NOT_CLAIMED;
  130. }
  131. /**
  132. * 检查奖励是否已过期
  133. *
  134. * @return bool
  135. */
  136. public function isRewardExpired(): bool
  137. {
  138. return $this->reward_status === REWARD_STATUS::EXPIRED;
  139. }
  140. }