Active.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Models;
  3. use DB;
  4. use Carbon\Carbon;
  5. class Active extends BaseModel
  6. {
  7. protected $fillable = [
  8. 'tid', 'name', 'start_time', 'end_time',
  9. 'user_num', 'address', 'text'
  10. ];
  11. protected $tb_act_theme = 'actives_themes';
  12. public function getStartTimeAttribute($value)
  13. {
  14. if ($value) {
  15. return $this->timeFormat($value);
  16. }
  17. }
  18. public function getEndTimeAttribute($value)
  19. {
  20. if ($value) {
  21. return $this->timeFormat($value);
  22. }
  23. }
  24. public function theme()
  25. {
  26. return $this->belongsTo(ActivesTheme::class, 'tid');
  27. }
  28. public function activesUser()
  29. {
  30. return $this->hasMany(ActivesUser::class, 'aid');
  31. }
  32. public function getFontActives($activeType_ename)
  33. {
  34. $query = $this->join(
  35. $this->tb_act_theme,
  36. $this->getTable().'.tid', '=', $this->tb_act_theme.'.id'
  37. )
  38. ->select(
  39. $this->tb_act_theme.'.id as tid',
  40. $this->tb_act_theme.'.name as tname',
  41. $this->getTable().'.id',
  42. $this->getTable().'.name',
  43. 'start_time',
  44. 'end_time'
  45. );
  46. switch ($activeType_ename) {
  47. case 'act_ready':
  48. $query = $query->where('start_time', '>', Carbon::now());
  49. break;
  50. case 'act_start':
  51. $query = $query->where(function($query) {
  52. $query->where('start_time', '<=', Carbon::now())
  53. ->orWhere('start_time', null);
  54. })
  55. ->where(function($query) {
  56. $query->where('end_time', '>', Carbon::now())
  57. ->orWhere('end_time', null);
  58. });
  59. break;
  60. case 'act_end':
  61. $query = $query->where('end_time', '<=', Carbon::now());
  62. break;
  63. }
  64. $query = $query->orderBy($this->getTable().'.created_at', 'desc')
  65. ->get();
  66. return $query;
  67. }
  68. }