ActivityConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Module\Activity\Models;
  3. use App\Module\Activity\Enums\ACTIVITY_STATUS;
  4. use App\Module\Activity\Enums\ACTIVITY_TYPE;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use UCore\ModelCore;
  7. /**
  8. * 活动基础配置
  9. *
  10. * field start
  11. * @property int $id
  12. * @property string $name 活动名称
  13. * @property int $type 活动类型
  14. * @property string $description 活动描述
  15. * @property string $start_time 开始时间
  16. * @property string $end_time 结束时间
  17. * @property int $status 活动状态
  18. * @property int $display_order 显示顺序
  19. * @property string $icon 活动图标URL
  20. * @property string $banner 活动横幅URL
  21. * @property int $reward_group_id 奖励组ID(关联game_reward_groups表)
  22. * @property string $reward_group_code 奖励组编码(关联game_reward_groups表的code字段)
  23. * @property object|array $config_params 活动特定配置参数
  24. * @property \Carbon\Carbon $created_at
  25. * @property \Carbon\Carbon $updated_at
  26. * field end
  27. */
  28. class ActivityConfig extends ModelCore
  29. {
  30. /**
  31. * 与模型关联的表名
  32. *
  33. * @var string
  34. */
  35. protected $table = 'activity_config';
  36. /**
  37. * 可批量赋值的属性
  38. *
  39. * @var array
  40. */
  41. // attrlist start
  42. protected $fillable = [
  43. 'id',
  44. 'name',
  45. 'type',
  46. 'description',
  47. 'start_time',
  48. 'end_time',
  49. 'status',
  50. 'display_order',
  51. 'icon',
  52. 'banner',
  53. 'reward_group_id',
  54. 'reward_group_code',
  55. 'config_params',
  56. ];
  57. // attrlist end
  58. /**
  59. * 应该被转换为原生类型的属性
  60. *
  61. * @var array
  62. */
  63. protected $casts = [
  64. 'type' => 'integer',
  65. 'status' => 'integer',
  66. 'display_order' => 'integer',
  67. 'reward_group_id' => 'integer',
  68. 'config_params' => 'json',
  69. 'start_time' => 'datetime',
  70. 'end_time' => 'datetime',
  71. ];
  72. /**
  73. * 获取活动类型名称
  74. *
  75. * @return string
  76. */
  77. public function getTypeName(): string
  78. {
  79. return ACTIVITY_TYPE::getName($this->type);
  80. }
  81. /**
  82. * 获取活动状态名称
  83. *
  84. * @return string
  85. */
  86. public function getStatusName(): string
  87. {
  88. return ACTIVITY_STATUS::getName($this->status);
  89. }
  90. /**
  91. * 获取活动的参与记录
  92. *
  93. * @return HasMany
  94. */
  95. public function participations(): HasMany
  96. {
  97. return $this->hasMany(ActivityParticipation::class, 'activity_id', 'id');
  98. }
  99. /**
  100. * 获取活动的用户数据
  101. *
  102. * @return HasMany
  103. */
  104. public function userData(): HasMany
  105. {
  106. return $this->hasMany(UserActivityData::class, 'activity_id', 'id');
  107. }
  108. /**
  109. * 获取活动的条件
  110. *
  111. * @return HasMany
  112. */
  113. public function conditions(): HasMany
  114. {
  115. return $this->hasMany(ActivityCondition::class, 'activity_id', 'id');
  116. }
  117. /**
  118. * 检查活动是否进行中
  119. *
  120. * @return bool
  121. */
  122. public function isInProgress(): bool
  123. {
  124. return $this->status === ACTIVITY_STATUS::IN_PROGRESS->value;
  125. }
  126. /**
  127. * 检查活动是否已结束
  128. *
  129. * @return bool
  130. */
  131. public function isEnded(): bool
  132. {
  133. return $this->status === ACTIVITY_STATUS::ENDED->value || $this->status === ACTIVITY_STATUS::CLOSED->value;
  134. }
  135. /**
  136. * 检查活动是否未开始
  137. *
  138. * @return bool
  139. */
  140. public function isNotStarted(): bool
  141. {
  142. return $this->status === ACTIVITY_STATUS::NOT_STARTED->value;
  143. }
  144. }