RoomGradeModel.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\admin\model\exam;
  3. use addons\exam\enum\RoomSignupStatus;
  4. use addons\exam\model\BaseModel;
  5. use addons\exam\model\RoomModel;
  6. use app\admin\model\User;
  7. use think\Db;
  8. class RoomGradeModel extends BaseModel
  9. {
  10. // 表名
  11. protected $name = 'exam_room_grade';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = false;
  18. // 追加属性
  19. protected $append
  20. = [
  21. 'is_pass_text',
  22. 'is_makeup_text',
  23. 'grade_time_text'
  24. ];
  25. public function getIsPassList()
  26. {
  27. return ['0' => __('Is_pass 0'), '1' => __('Is_pass 1')];
  28. }
  29. public function getIsMakeupList()
  30. {
  31. return ['0' => __('Is_makeup 0'), '1' => __('Is_makeup 1')];
  32. }
  33. public function getIsPassTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['is_pass']) ? $data['is_pass'] : '');
  36. $list = $this->getIsPassList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getIsMakeupTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['is_makeup']) ? $data['is_makeup'] : '');
  42. $list = $this->getIsMakeupList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. public function getGradeTimeTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['grade_time']) ? $data['grade_time'] : '');
  48. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  49. }
  50. protected function setGradeTimeAttr($value)
  51. {
  52. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  53. }
  54. public function user()
  55. {
  56. return $this->belongsTo(User::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  57. }
  58. public function belongsUser()
  59. {
  60. return $this->belongsTo(User::class);
  61. }
  62. public function paper()
  63. {
  64. return $this->belongsTo(PaperModel::class, 'paper_id', 'id', [], 'LEFT')->setEagerlyType(0);
  65. }
  66. public function cate()
  67. {
  68. return $this->belongsTo(CateModel::class, 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
  69. }
  70. public function cates()
  71. {
  72. return $this->belongsTo(CateModel::class, 'cate_id', 'id');
  73. }
  74. public function room()
  75. {
  76. return $this->belongsTo(RoomModel::class, 'room_id', 'id', [], 'LEFT')->setEagerlyType(0);
  77. }
  78. public function signup()
  79. {
  80. return $this->belongsTo(RoomSignupModel::class, 'user_id', 'user_id', [])->where('status', RoomSignupStatus::ACCEPT);
  81. }
  82. public function signup1()
  83. {
  84. return $this->belongsTo(RoomSignupModel::class, 'user_id', 'user_id', [], 'LEFT')->setEagerlyType(0)->where('status', RoomSignupStatus::ACCEPT);
  85. }
  86. /**
  87. * 统计考场排名
  88. * @param $room_id
  89. * @return array
  90. */
  91. public static function rankData($room_id)
  92. {
  93. if (!$room = RoomModel::get($room_id)) {
  94. fail('考场信息不存在');
  95. }
  96. // +----------------------------------------------------------------------
  97. // 已统计排名
  98. // +----------------------------------------------------------------------
  99. // if ($room['is_rank']) {
  100. // return [
  101. // 'summary' => [
  102. // 'grade_count' => $room['grade_count'],
  103. // 'pass_count' => $room['pass_count'],
  104. // 'pass_rate' => $room['pass_rate'],
  105. // 'cache_time' => date('Y-m-d H:i:s', $room['updatetime']),
  106. // ],
  107. // 'list' => \addons\exam\model\RoomGradeModel::with(
  108. // [
  109. // 'user' => BaseModel::withSimpleUser(),
  110. // ]
  111. // )
  112. // ->where('room_id', $room['id'])
  113. // ->where('paper_id', $room['paper_id'])
  114. // ->where('is_makeup', 0) // 补考不参与排名
  115. // ->group('user_id')
  116. // ->order('rank')
  117. // ->limit(10)
  118. // ->select(),
  119. // ];
  120. // }
  121. // +----------------------------------------------------------------------
  122. // 未统计排名
  123. // +----------------------------------------------------------------------
  124. $grade_count = RoomGradeModel::where('room_id', $room['id'])->where('paper_id', $room['paper_id'])->group('user_id')->count();
  125. $pass_count = RoomGradeModel::where('room_id', $room['id'])->where('is_pass', 1)->group('user_id')->count();
  126. $pass_rate = round(($pass_count / $grade_count) * 100, 2) . '%';
  127. // $pass_rate = bcmul(bcdiv($pass_count, $grade_count, 4), 100, 2);
  128. $list = !$grade_count ? [] : \addons\exam\model\RoomGradeModel::with(
  129. [
  130. // 'belongs_user' => BaseModel::withSimpleUser(),
  131. 'user' => BaseModel::withSimpleUser(),
  132. ]
  133. )
  134. ->where('room_id', $room['id'])
  135. ->where('paper_id', $room['paper_id'])
  136. ->where('is_makeup', 0) // 补考不参与排名
  137. ->group('user_id')
  138. ->order('score desc, grade_time asc')
  139. ->select();
  140. // 保存数据,下次无需再次统计
  141. Db::transaction(function () use ($room, $grade_count, $pass_count, $pass_rate, &$list) {
  142. // 记录统计数据
  143. $room->grade_count = $grade_count;
  144. $room->pass_count = $pass_count;
  145. $room->pass_rate = $pass_rate;
  146. $room->is_rank = 1;
  147. $room->save();
  148. // 记录排名数据
  149. foreach ($list as $index => &$grade) {
  150. $grade['rank'] = $index + 1;
  151. $grade->save();
  152. }
  153. });
  154. return [
  155. 'summary' => [
  156. 'grade_count' => $grade_count,
  157. 'pass_count' => $pass_count,
  158. 'pass_rate' => $pass_rate . '%',
  159. 'cache_time' => date('Y-m-d H:i:s'),
  160. ],
  161. // 只返回10条
  162. 'list' => count($list) > 10 ? array_slice($list, 0, 10) : $list,
  163. ];
  164. }
  165. }