| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | <?phpnamespace App\Models;use Carbon\Carbon;use function foo\func;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Log;class Student extends Model{    protected $table = 'students';    protected $guarded = [];    public $marry_list;    public function __construct(array $attributes = [])    {        parent::__construct($attributes);        $this->marry_list = collect([            ['key' => 1, 'value' => 1, 'show_value' => '未婚'],            ['key' => 2, 'value' => 2, 'show_value' => '已婚'],//            ['key' => 3, 'value' => 3, 'show_value' => '离婚'],        ]);    }    public function getMarryList()    {        return $this->marry_list;    }    public function getBindPhone()    {        return $this['bind_phone'] == 2 ? '已绑定' : '未绑定';    }    public function getMarry($key = null)    {        $key = empty($key) ? $this['marry'] : 1;        $item = $this->marry_list->where('key', $key)->first();        return empty($item) ? '未婚' : $item['show_value'];    }    public function course()    {        return $this->belongsTo('App\Models\Course');    }    public function getCheckCardDates()    {        return CheckCard::where('student_id', $this['id'])->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get()->pluck('begin_date_time')->map(function ($item) {            return substr($item, 0, 10);        })->unique();    }    public function getStudentCourse()    {        return StudentCourse::where('student_id', $this['id'])->first();    }    public function getCourseInfo()    {        $student_course = StudentCourse::where('student_id', $this['id'])->first();        if(empty($student_course)) {            return [                'course_name' => '',                'apply_date' => '',                'end_date' => '',                'teacher_names' => ''            ];        }        $course = Course::find($student_course->course_id);        if(empty($course)) {            $course_name = '';        } else {            $course_name = $course->name;        }        $apply_date = $student_course->apply_date;        $end_date = $student_course->computeEndDate();        $teacher_names = $student_course->getTeacherFullNames();        return [            'course_name' => $course_name,            'apply_date' => $apply_date,            'end_date' => $end_date,            'teacher_names' => $teacher_names        ];    }    public function getCourseName()    {        $student_course = StudentCourse::where('student_id', $this['id'])->first();        if(empty($student_course)) return '';        $course = Course::find($student_course->course_id);        return empty($course) ? '' : $course->name;    }    public function getIsNew()    {        $today = Carbon::today();        $this_week_begin = null;        while($today->dayOfWeekIso != 1) {            $today = $today->subDay();        }        $this_week_begin = $today->toDateTimeString();        $this_week_end = $today->addDays(7)->toDateTimeString();        $tmp = Remark::where([            ['student_id', '=', $this['id']],            ['updated_at', '>=', $this_week_begin],            ['updated_at', '<', $this_week_end],        ])->first();        return empty($tmp) ? true : false;    }    public function getThisWeekAverageScore(Remark $remark)    {        $today = Carbon::today();        $this_week_begin = null;        while($today->dayOfWeekIso != 1) {            $today = $today->subDay();        }        $this_week_begin = $today->toDateTimeString();        return RemarkDetail::where([            ['remark_id', '=', $remark['id']],            ['created_at', '>=', $this_week_begin],        ])->get()->avg('score');    }    public function getTodayCheckCardMinutes()    {        $today = Carbon::today()->toDateTimeString();        $items = CheckCard::where([            ['student_id', '=', $this['id']],            ['begin_date_time', '>=', $today]        ])->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get();        $total = 0;        foreach($items as $item) {            $duration = strtotime($item->end_date_time) - strtotime($item->begin_date_time);            $total += $duration;        }        return floor($total / 60);    }    public function getCheckCardTime($begin_date = null, $end_date = null)    {        $now = Carbon::now();        $begin_date_time = empty($begin_date) ? $now->subYears(10)->toDateTimeString() : Carbon::createFromTimestamp(strtotime($begin_date))->toDateTimeString();        $end_date_time = empty($end_date) ? $now->addYears(10)->toDateTimeString() : Carbon::createFromTimestamp(strtotime($end_date))->toDateTimeString();        $check_cards = CheckCard::where([            ['student_id', '=', $this['id']],            ['begin_date_time', '>=', $begin_date_time],            ['begin_date_time', '<', $end_date_time],        ])->get();        $total = 0;        foreach($check_cards as $check_card) {            if(!empty($check_card->begin_date_time) && !empty($check_card->end_date_time) && $check_card->end_date_time > $check_card->begin_date_time) {                $total += (strtotime($check_card->end_date_time) - strtotime($check_card->begin_date_time));            }        }        return [            'total' => $total,            'totalHuman' => $this->getHumanTime($total)        ];    }    public function getHumanTime($total)    {        $res = '';        $tmp = floor($total / 3600);        $diff_time = $total % 3600;        if(!empty($tmp)) {            $res .= $tmp . '小时';        }        $tmp = floor($diff_time / 60);        $diff_time = $diff_time % 60;        if(!empty($tmp)) {            $res .= $tmp . '分钟';        }        if(!empty($diff_time)) {            $res .= $diff_time . '秒';        }        return empty($res) ? 0 : $res;    }}
 |