ActivityModel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Models\Fpdx;
  3. use App\Models\Model;
  4. use Illuminate\Database\Eloquent\Builder;
  5. /**
  6. * App\Models\Experience\ActivityModel
  7. *
  8. * @property int $stage_id
  9. * @property string $name
  10. * @property int $enroll_begin_time
  11. * @property int $enroll_end_time
  12. * @property int $activity_begin_time
  13. * @property int $activity_end_time
  14. * @property \Illuminate\Support\Carbon $created_at
  15. * @property \Illuminate\Support\Carbon $updated_at
  16. * @property string $activity_name 活动名称
  17. * @property int $activity_time 活动开始时间
  18. * @property int $signbegin_time 报名开始时间
  19. * @property int $signend_time 报名结束时间
  20. * @property int $open_time 开启聊天时间
  21. * @property int $close_time 活动结束时间
  22. * @property int $rematch_begin_at 重匹开始时间
  23. * @property int $rematched_at 重匹公布时间
  24. * @property int $create_time 创建时间
  25. * @property int $update_time 修改时间
  26. * @property int $qbj_stage_id 情报局活动id
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel currently()
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel enrolling()
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel newModelQuery()
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel newQuery()
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel query()
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereActivityName($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereActivityTime($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCloseTime($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCreateTime($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereCreatedAt($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereOpenTime($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereQbjStageId($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereRematchBeginAt($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereRematchedAt($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereSignbeginTime($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereSignendTime($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereStageId($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereUpdateTime($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\ActivityModel whereUpdatedAt($value)
  46. * @mixin \Eloquent
  47. */
  48. class ActivityModel extends Model
  49. {
  50. protected $table = 'kdgx_partner_charge_activity';
  51. protected $dateFormat = 'U';
  52. protected $fillable = [
  53. 'activity_name',
  54. 'activity_time',
  55. 'signbegin_time',
  56. 'signend_time',
  57. 'open_time',
  58. 'close_time',
  59. 'rematch_begin_at',
  60. 'rematched_at',
  61. 'qbj_stage_id'
  62. ];
  63. protected $primaryKey = "stage_id";
  64. protected $appends = ['id'];
  65. public const ENROLLING = 1; //报名中 周四12:00-下周四12:00
  66. public const MATCHING = 2; //匹配中 周四12:00-周四18:00
  67. public const CONFIRMING = 3; //确认中 周四18:00-周五10:00
  68. public const REMATCHING = 4; //重配中 周五10:00-周五12:00
  69. public const CHATTING = 5; //活动中 周四18:00-周日24:00
  70. public const END = 6; //活动已结束
  71. /**
  72. * 获取最近活动
  73. * @return array
  74. * [
  75. * 'last' => 上一期活动,
  76. * 'ing' => 正在进行中的活动(匹配结束到分销结束期间-1未不存在),
  77. * 'next' => 下一期活动(正在报名中的活动)
  78. * ]
  79. */
  80. public static function getActivitys()
  81. {
  82. /** @var ActivityModel $ing */
  83. $ing = self::where([['signend_time', '<', time()], ['close_time', '>', time()]])->first();
  84. if (collect($ing)->isEmpty()) {
  85. /** @var ActivityModel $next */
  86. $next = self::orderBy('stage_id', 'desc')->first();
  87. if (collect($next)->isEmpty()) {
  88. return array(
  89. 'last' => 0,
  90. 'ing' => 0,
  91. 'next' => 0
  92. );
  93. }
  94. return [
  95. 'last' => $next->stage_id - 1,
  96. 'ing' => -1,
  97. 'next' => $next->stage_id
  98. ];
  99. }
  100. return [
  101. 'last' => $ing->stage_id - 1,
  102. 'ing' => $ing->stage_id,
  103. 'next' => $ing->stage_id + 1
  104. ];
  105. }
  106. /**
  107. * 当前活动中的一期
  108. * @param $query
  109. * @return mixed
  110. */
  111. public function scopeCurrently($query)
  112. {
  113. /** @var Builder $query */
  114. return $query->where('close_time', '>', time())->orderBy($this->primaryKey, 'asc');
  115. }
  116. /**
  117. * 匹配中的一期
  118. * @param $query
  119. * @return mixed
  120. */
  121. public function scopeMatching($query)
  122. {
  123. $time = time();
  124. return $query->where('signend_time', '<=', $time)
  125. ->where('open_time', '>', $time);
  126. }
  127. /**
  128. * 当前报名中的一期
  129. */
  130. public function scopeEnrolling($query)
  131. {
  132. $time = time();
  133. return $query->where('signbegin_time', '<=', $time)
  134. ->where('signend_time', '>', $time);
  135. }
  136. /**
  137. * 活动ID
  138. * @return int
  139. */
  140. public function getIdAttribute()
  141. {
  142. return $this->stage_id;
  143. }
  144. /**
  145. * 获取当前活动的进度
  146. * @return int
  147. */
  148. public function getProgress()
  149. {
  150. if (time() >= $this->signbegin_time && time() < $this->signend_time) {
  151. return self::ENROLLING;
  152. } elseif (time() >= $this->signend_time && time() < $this->open_time) {
  153. return self::MATCHING;
  154. } elseif (time() >= $this->open_time && time() < $this->rematch_begin_at) {
  155. return self::CONFIRMING;
  156. } elseif (time() >= $this->rematch_begin_at && time() < $this->rematched_at) {
  157. return self::REMATCHING;
  158. } elseif (time() >= $this->rematched_at && time() < $this->close_time) {
  159. return self::CHATTING;
  160. } else {
  161. return self::END;
  162. }
  163. }
  164. }