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