Student.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use function foo\func;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Log;
  7. class Student extends Model
  8. {
  9. protected $table = 'students';
  10. protected $guarded = [];
  11. public $marry_list;
  12. public function __construct(array $attributes = [])
  13. {
  14. parent::__construct($attributes);
  15. $this->marry_list = collect([
  16. ['key' => 1, 'value' => 1, 'show_value' => '未婚'],
  17. ['key' => 2, 'value' => 2, 'show_value' => '已婚'],
  18. // ['key' => 3, 'value' => 3, 'show_value' => '离婚'],
  19. ]);
  20. }
  21. public function getMarryList()
  22. {
  23. return $this->marry_list;
  24. }
  25. public function getBindPhone()
  26. {
  27. return $this['bind_phone'] == 2 ? '已绑定' : '未绑定';
  28. }
  29. public function getMarry($key = null)
  30. {
  31. $key = empty($key) ? $this['marry'] : 1;
  32. $item = $this->marry_list->where('key', $key)->first();
  33. return empty($item) ? '未婚' : $item['show_value'];
  34. }
  35. public function course()
  36. {
  37. return $this->belongsTo('App\Models\Course');
  38. }
  39. public function getCheckCardDates()
  40. {
  41. return CheckCard::where('student_id', $this['id'])->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get()->pluck('begin_date_time')->map(function ($item) {
  42. return substr($item, 0, 10);
  43. })->unique();
  44. }
  45. public function getStudentCourse()
  46. {
  47. return StudentCourse::where('student_id', $this['id'])->first();
  48. }
  49. public function getCourseInfo()
  50. {
  51. $student_course = StudentCourse::where('student_id', $this['id'])->first();
  52. if(empty($student_course)) {
  53. return [
  54. 'course_name' => '',
  55. 'apply_date' => '',
  56. 'end_date' => '',
  57. 'teacher_names' => ''
  58. ];
  59. }
  60. $course = Course::find($student_course->course_id);
  61. if(empty($course)) {
  62. $course_name = '';
  63. } else {
  64. $course_name = $course->name;
  65. }
  66. $apply_date = $student_course->apply_date;
  67. if(empty($apply_date) || empty($student_course->duration)) {
  68. $end_date = '';
  69. } else {
  70. $end_date = Carbon::createFromTimestamp(strtotime($apply_date))->addDays($student_course->duration)->toDateString();
  71. }
  72. $teacher_names = $student_course->getTeacherFullNames();
  73. return [
  74. 'course_name' => $course_name,
  75. 'apply_date' => $apply_date,
  76. 'end_date' => $end_date,
  77. 'teacher_names' => $teacher_names
  78. ];
  79. }
  80. public function getCourseName()
  81. {
  82. $student_course = StudentCourse::where('student_id', $this['id'])->first();
  83. if(empty($student_course)) return '';
  84. $course = Course::find($student_course->course_id);
  85. return empty($course) ? '' : $course->name;
  86. }
  87. public function getIsNew()
  88. {
  89. $today = Carbon::today();
  90. $this_week_begin = null;
  91. while($today->dayOfWeekIso != 1) {
  92. $today = $today->subDay();
  93. }
  94. $this_week_begin = $today->toDateTimeString();
  95. $this_week_end = $today->addDays(7)->toDateTimeString();
  96. $tmp = Remark::where([
  97. ['student_id', '=', $this['id']],
  98. ['updated_at', '>=', $this_week_begin],
  99. ['updated_at', '<', $this_week_end],
  100. ])->first();
  101. return empty($tmp) ? true : false;
  102. }
  103. public function getThisWeekAverageScore(Remark $remark)
  104. {
  105. $today = Carbon::today();
  106. $this_week_begin = null;
  107. while($today->dayOfWeekIso != 1) {
  108. $today = $today->subDay();
  109. }
  110. $this_week_begin = $today->toDateTimeString();
  111. return RemarkDetail::where([
  112. ['remark_id', '=', $remark['id']],
  113. ['created_at', '>=', $this_week_begin],
  114. ])->get()->avg('score');
  115. }
  116. }