123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Repositories\Models\School;
- use App\Repositories\Models\Base\Admin;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Model;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class LessonSchedule.
- *
- * @package namespace App\Repositories\Models\School;
- */
- class LessonSchedule extends Model implements Transformable
- {
- use TransformableTrait;
- protected $table = 'school_lesson_schedules';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- public function setTeachTeacherIdsAttribute($val)
- {
- $this->attributes['teach_teacher_ids'] = '-' . arr2str($val, '-') . '-';
- }
- public function getTeachTeacherIdsAttribute($val)
- {
- return str2arr(trim($val, '-'), '-');
- }
- public function getGroupNameAttribute()
- {
- $group = (int)$this->attributes['group'];
- return chr(65 + $group) . '组';
- }
- public function lesson()
- {
- return $this->belongsTo(Lesson::class)->select(['id', 'name']);
- }
- // public function teacher()
- // {
- // return $this->belongsTo(Teacher::class)->select(['id', 'name', 'account']);
- // }
- public function teacher()
- {
- return $this->belongsTo(Admin::class, 'teacher_admin_id')->select(['id', 'name', 'username']);
- }
- public function grade()
- {
- return $this->belongsTo(Grade::class)->select(['id', 'name']);
- }
- public function room()
- {
- return $this->belongsTo(Room::class)->select(['id', 'name']);
- }
- public function time()
- {
- return $this->belongsTo(Time::class)->select(['id', 'name']);
- }
- public static function byTimeGetDate($term_id, $week, $week_index)
- {
- return Carbon::parse(optional(Term::byId($term_id))->start_date)->addWeeks($week - 1)->addDays($week_index)->toDateString();
- }
- public static function byDateGetWeek($term_id, $day)
- {
- // $day = Carbon::parse($day)->toDateString();
- // $week = ceil(Carbon::parse($day)->diffInDays(optional(Term::byId($term_id))->start_date) / 7);
- $week = ceil((Carbon::parse($day)->diffInDays(optional(Term::byId(1))->start_date) + 1) / 7);
- $w = Carbon::parse($day)->dayOfWeek;
- switch ($w) {
- case 0:
- $week_index = 6;
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- $week_index = $w - 1;
- break;
- }
- return [
- 'week' => $week,
- 'week_index' => $week_index,
- ];
- }
- public static function toWeekIndex($w)
- {
- switch ($w) {
- case 0:
- $week_index = 6;
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- $week_index = $w - 1;
- break;
- }
- return $week_index;
- }
- public static function weekInt2name($week_index)
- {
- $map = [
- '一', '二', '三', '四', '五', '六', '日',
- ];
- return $map[$week_index];
- }
- public static function groupInt2Name($group)
- {
- return chr(65 + $group) . '组';
- }
- }
|