ActivityReward.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Models;
  3. use App\Constants\Util;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use App\Support\ActivityCatalog;
  6. /**
  7. * @property string $title
  8. * @property string sub_title
  9. * @property string detail_image
  10. * @property string start_time
  11. * @property string end_time
  12. * @property string status
  13. *
  14. */
  15. class ActivityReward extends BaseModel
  16. {
  17. const STATUS_UP = 1;
  18. const STATUS_DOWN = 0;
  19. protected $table = 'activity_rewards';
  20. protected $hidden = ['created_at', 'updated_at', 'detail_banner', 'pc_banner'];
  21. protected $fillable = ['title', 'sub_title', 'detail_image', 'start_time', 'end_time', 'status','type','lang','content', 'params','pc_image','detail_banner', 'pc_banner',
  22. 'publish_platforms', 'budget', 'reward_quota', 'sort', 'register_time_limited', 'register_start_time', 'register_end_time',
  23. 'register_platforms', 'deposit_limited', 'deposit_min', 'deposit_max', 'show_ineligible', 'game_category', 'click_type', 'link', 'click_position'];
  24. protected $appends = ['type_name', 'game_category_name', 'click_type', 'link', 'click_position', 'can_jump'];
  25. protected function getClickTypeAttribute($value): string
  26. {
  27. $value = $value ?? ($this->attributes['click_type'] ?? 'none');
  28. return in_array($value, ['none', 'external', 'internal'], true) ? $value : 'none';
  29. }
  30. protected function getLinkAttribute($value): ?string
  31. {
  32. return in_array($this->click_type, ['external', 'internal'], true) ? ($value ?? ($this->attributes['link'] ?? null)) : null;
  33. }
  34. protected function getCanJumpAttribute(): bool
  35. {
  36. return in_array($this->click_type, ['external', 'internal'], true) && !empty($this->link) && $this->click_position !== null;
  37. }
  38. protected function getClickPositionAttribute($value): ?string
  39. {
  40. $value = $value ?? ($this->attributes['click_position'] ?? null);
  41. return $this->click_type !== 'none' && in_array($value, ['cover', 'detail'], true) ? $value : null;
  42. }
  43. protected $casts = [
  44. 'publish_platforms' => 'array', 'register_platforms' => 'array',
  45. 'register_time_limited' => 'boolean', 'deposit_limited' => 'boolean',
  46. 'show_ineligible' => 'boolean', 'reward_quota' => 'integer', 'rewarded_count' => 'integer',
  47. 'quota_enabled' => 'boolean', 'budget' => 'decimal:2', 'budget_used' => 'decimal:2',
  48. 'deposit_min' => 'decimal:2', 'deposit_max' => 'decimal:2', 'sort' => 'integer',
  49. ];
  50. public function activityUser(): HasMany
  51. {
  52. return $this->hasMany(ActivityUser::class, 'activity_id', 'id');
  53. }
  54. protected function getTypeNameAttribute(): ?string
  55. {
  56. return ActivityCatalog::activityTypes()[(int) ($this->attributes['type'] ?? 0)] ?? null;
  57. }
  58. protected function getGameCategoryNameAttribute(): ?string
  59. {
  60. return ActivityCatalog::gameTypes()[$this->attributes['game_category'] ?? ''] ?? null;
  61. }
  62. protected function getDetailImageAttribute($value)
  63. {
  64. return Util::ensureUrl($value);
  65. }
  66. protected function setDetailImageAttribute($value)
  67. {
  68. $this->attributes['detail_image'] = Util::replacePartInUrl($value);
  69. }
  70. protected function getPcImageAttribute($value)
  71. {
  72. return Util::ensureUrl($value);
  73. }
  74. protected function setPcImageAttribute($value)
  75. {
  76. $this->attributes['pc_image'] = Util::replacePartInUrl($value);
  77. }
  78. protected function getStartTimeAttribute($value): string
  79. {
  80. return date('Y-m-d', $value);
  81. }
  82. protected function getEndTimeAttribute($value): string
  83. {
  84. return date('Y-m-d', $value);
  85. }
  86. }