Student.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $end_date = $student_course->computeEndDate();
  68. $teacher_names = $student_course->getTeacherFullNames();
  69. return [
  70. 'course_name' => $course_name,
  71. 'apply_date' => $apply_date,
  72. 'end_date' => $end_date,
  73. 'teacher_names' => $teacher_names
  74. ];
  75. }
  76. public function getCourseName()
  77. {
  78. $student_course = StudentCourse::where('student_id', $this['id'])->first();
  79. if(empty($student_course)) return '';
  80. $course = Course::find($student_course->course_id);
  81. return empty($course) ? '' : $course->name;
  82. }
  83. public function getIsNew()
  84. {
  85. $today = Carbon::today();
  86. $this_week_begin = null;
  87. while($today->dayOfWeekIso != 1) {
  88. $today = $today->subDay();
  89. }
  90. $this_week_begin = $today->toDateTimeString();
  91. $this_week_end = $today->addDays(7)->toDateTimeString();
  92. $tmp = Remark::where([
  93. ['student_id', '=', $this['id']],
  94. ['updated_at', '>=', $this_week_begin],
  95. ['updated_at', '<', $this_week_end],
  96. ])->first();
  97. return empty($tmp) ? true : false;
  98. }
  99. public function getThisWeekAverageScore(Remark $remark)
  100. {
  101. $today = Carbon::today();
  102. $this_week_begin = null;
  103. while($today->dayOfWeekIso != 1) {
  104. $today = $today->subDay();
  105. }
  106. $this_week_begin = $today->toDateTimeString();
  107. return RemarkDetail::where([
  108. ['remark_id', '=', $remark['id']],
  109. ['created_at', '>=', $this_week_begin],
  110. ])->get()->avg('score');
  111. }
  112. public function getTodayCheckCardMinutes()
  113. {
  114. $today = Carbon::today()->toDateTimeString();
  115. $items = CheckCard::where([
  116. ['student_id', '=', $this['id']],
  117. ['begin_date_time', '>=', $today]
  118. ])->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get();
  119. $total = 0;
  120. foreach($items as $item) {
  121. $duration = strtotime($item->end_date_time) - strtotime($item->begin_date_time);
  122. $total += $duration;
  123. }
  124. return floor($total / 60);
  125. }
  126. public function getCheckCardTime($begin_date = null, $end_date = null)
  127. {
  128. $now = Carbon::now();
  129. $begin_date_time = empty($begin_date) ? $now->subYears(10)->toDateTimeString() : Carbon::createFromTimestamp(strtotime($begin_date))->toDateTimeString();
  130. $end_date_time = empty($end_date) ? $now->addYears(10)->toDateTimeString() : Carbon::createFromTimestamp(strtotime($end_date))->toDateTimeString();
  131. $check_cards = CheckCard::where([
  132. ['student_id', '=', $this['id']],
  133. ['begin_date_time', '>=', $begin_date_time],
  134. ['begin_date_time', '<', $end_date_time],
  135. ])->get();
  136. $total = 0;
  137. foreach($check_cards as $check_card) {
  138. if(!empty($check_card->begin_date_time) && !empty($check_card->end_date_time) && $check_card->end_date_time > $check_card->begin_date_time) {
  139. $total += (strtotime($check_card->end_date_time) - strtotime($check_card->begin_date_time));
  140. }
  141. }
  142. return [
  143. 'total' => $total,
  144. 'totalHuman' => $this->getHumanTime($total)
  145. ];
  146. }
  147. public function getHumanTime($total)
  148. {
  149. $res = '';
  150. $tmp = floor($total / 3600);
  151. $diff_time = $total % 3600;
  152. if(!empty($tmp)) {
  153. $res .= $tmp . '小时';
  154. }
  155. $tmp = floor($diff_time / 60);
  156. $diff_time = $diff_time % 60;
  157. if(!empty($tmp)) {
  158. $res .= $tmp . '分钟';
  159. }
  160. if(!empty($diff_time)) {
  161. $res .= $diff_time . '秒';
  162. }
  163. return empty($res) ? 0 : $res;
  164. }
  165. }