Student.php 3.8 KB

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