Student.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 getIsNew()
  45. {
  46. $today = Carbon::today();
  47. $this_week_begin = null;
  48. while($today->dayOfWeekIso != 1) {
  49. $today = $today->subDay();
  50. }
  51. $this_week_begin = $today->toDateTimeString();
  52. $this_week_end = $today->addDays(7)->toDateTimeString();
  53. $tmp = Remark::where([
  54. ['student_id', '=', $this['id']],
  55. ['created_at', '>=', $this_week_begin],
  56. ['created_at', '<', $this_week_end],
  57. ])->first();
  58. return empty($tmp) ? true : false;
  59. }
  60. public function getThisWeekAverageScore(Remark $remark)
  61. {
  62. $today = Carbon::today();
  63. $this_week_begin = null;
  64. while($today->dayOfWeekIso != 1) {
  65. $today = $today->subDay();
  66. }
  67. $this_week_begin = $today->toDateTimeString();
  68. return RemarkDetail::where([
  69. ['remark_id', '=', $remark['id']],
  70. ['created_at', '>=', $this_week_begin],
  71. ])->get()->avg('score');
  72. }
  73. }