LessonSchedule.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Repositories\Models\School;
  3. use App\Repositories\Models\Base\Admin;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Prettus\Repository\Contracts\Transformable;
  7. use Prettus\Repository\Traits\TransformableTrait;
  8. /**
  9. * Class LessonSchedule.
  10. *
  11. * @package namespace App\Repositories\Models\School;
  12. */
  13. class LessonSchedule extends Model implements Transformable
  14. {
  15. use TransformableTrait;
  16. protected $table = 'school_lesson_schedules';
  17. /**
  18. * The attributes that are mass assignable.
  19. *
  20. * @var array
  21. */
  22. protected $guarded = [];
  23. public function setTeachTeacherIdsAttribute($val)
  24. {
  25. $this->attributes['teach_teacher_ids'] = '-' . arr2str($val, '-') . '-';
  26. }
  27. public function getTeachTeacherIdsAttribute($val)
  28. {
  29. return str2arr(trim($val, '-'), '-');
  30. }
  31. public function getGroupNameAttribute()
  32. {
  33. $group = (int)$this->attributes['group'];
  34. return chr(65 + $group) . '组';
  35. }
  36. public function lesson()
  37. {
  38. return $this->belongsTo(Lesson::class)->select(['id', 'name']);
  39. }
  40. // public function teacher()
  41. // {
  42. // return $this->belongsTo(Teacher::class)->select(['id', 'name', 'account']);
  43. // }
  44. public function teacher()
  45. {
  46. return $this->belongsTo(Admin::class, 'teacher_admin_id')->select(['id', 'name', 'username']);
  47. }
  48. public function grade()
  49. {
  50. return $this->belongsTo(Grade::class)->select(['id', 'name']);
  51. }
  52. public function room()
  53. {
  54. return $this->belongsTo(Room::class)->select(['id', 'name']);
  55. }
  56. public function time()
  57. {
  58. return $this->belongsTo(Time::class)->select(['id', 'name']);
  59. }
  60. public static function byTimeGetDate($term_id, $week, $week_index)
  61. {
  62. return Carbon::parse(optional(Term::byId($term_id))->start_date)->addWeeks($week - 1)->addDays($week_index)->toDateString();
  63. }
  64. public static function byDateGetWeek($term_id, $day)
  65. {
  66. // $day = Carbon::parse($day)->toDateString();
  67. // $week = ceil(Carbon::parse($day)->diffInDays(optional(Term::byId($term_id))->start_date) / 7);
  68. $week = ceil((Carbon::parse($day)->diffInDays(optional(Term::byId(1))->start_date) + 1) / 7);
  69. $w = Carbon::parse($day)->dayOfWeek;
  70. switch ($w) {
  71. case 0:
  72. $week_index = 6;
  73. break;
  74. case 1:
  75. case 2:
  76. case 3:
  77. case 4:
  78. case 5:
  79. case 6:
  80. $week_index = $w - 1;
  81. break;
  82. }
  83. return [
  84. 'week' => $week,
  85. 'week_index' => $week_index,
  86. ];
  87. }
  88. public static function toWeekIndex($w)
  89. {
  90. switch ($w) {
  91. case 0:
  92. $week_index = 6;
  93. break;
  94. case 1:
  95. case 2:
  96. case 3:
  97. case 4:
  98. case 5:
  99. case 6:
  100. $week_index = $w - 1;
  101. break;
  102. }
  103. return $week_index;
  104. }
  105. public static function weekInt2name($week_index)
  106. {
  107. $map = [
  108. '一', '二', '三', '四', '五', '六', '日',
  109. ];
  110. return $map[$week_index];
  111. }
  112. public static function groupInt2Name($group)
  113. {
  114. return chr(65 + $group) . '组';
  115. }
  116. }