ExamService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ExamAccessLogModel;
  13. use App\Models\ExamAnswerModel;
  14. use App\Models\ExamPaperModel;
  15. use App\Models\ExamTopicModel;
  16. use App\Models\MemberAnswerRankModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 答题服务-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * @package App\Services\Api
  26. */
  27. class ExamService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @author laravel开发员
  34. * @since 2020/11/11
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new ExamAnswerModel();
  39. }
  40. /**
  41. * 静态入口
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = new static();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * @param $params
  52. * @param int $pageSize
  53. * @return array
  54. */
  55. public function getDataList($params, $pageSize = 15)
  56. {
  57. $page = isset($params['page']) ? $params['page'] : 1;
  58. $cacheKey = "caches:exams:list_{$page}_{$pageSize}:" . md5(json_encode($params));
  59. $datas = RedisService::get($cacheKey);
  60. if ($datas) {
  61. return $datas;
  62. }
  63. $query = $this->getQuery($params);
  64. $list = $query->select(['a.id', 'a.user_id', 'a.paper_id', 'a.score', 'a.accurate_count', 'b.name', 'b.type', 'b.topic_count', 'b.score_total', 'b.is_charge', 'a.create_time', 'a.answer_times', 'a.status'])
  65. ->orderBy('a.create_time', 'desc')
  66. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  67. $list = $list ? $list->toArray() : [];
  68. if ($list) {
  69. foreach ($list['data'] as &$item) {
  70. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  71. }
  72. }
  73. $rows = isset($list['data']) ? $list['data'] : [];
  74. $datas = [
  75. 'pageSize' => $pageSize,
  76. 'total' => isset($list['total']) ? $list['total'] : 0,
  77. 'list' => $rows
  78. ];
  79. if ($rows) {
  80. RedisService::set($cacheKey, $datas, rand(300, 600));
  81. }
  82. return $datas;
  83. }
  84. /**
  85. * 查询
  86. * @param $params
  87. * @return mixed
  88. */
  89. public function getQuery($params)
  90. {
  91. $where = ['b.status' => 1, 'b.mark' => 1, 'a.mark' => 1];
  92. $status = isset($params['status']) ? $params['status'] : 0;
  93. $type = isset($params['type']) ? $params['type'] : 0;
  94. $sceneType = isset($params['scene_type']) ? $params['scene_type'] : 0;
  95. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  96. $subjectId = isset($params['subject_id']) ? $params['subject_id'] : 0;
  97. if ($userId > 0) {
  98. $where['a.user_id'] = $userId;
  99. }
  100. if ($status > 0) {
  101. $where['a.status'] = $status;
  102. }
  103. if ($type > 0) {
  104. $where['b.type'] = $type;
  105. }
  106. if ($sceneType > 0) {
  107. $where['b.scene_type'] = $sceneType;
  108. }
  109. if ($subjectId > 0) {
  110. $where['b.subject_id'] = $subjectId;
  111. }
  112. return $this->model->from('exam_answers as a')
  113. ->leftJoin('exam_papers as b', 'b.id', '=', 'a.paper_id')
  114. ->where($where)
  115. ->where(function ($query) use ($params) {
  116. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  117. if ($keyword) {
  118. $query->where('b.name', 'like', "%{$keyword}%");
  119. }
  120. });
  121. }
  122. /**
  123. * 答题历史
  124. * @param $params
  125. * @param int $pageSize
  126. * @return array|mixed
  127. */
  128. public function getHistoryList($params, $pageSize = 10)
  129. {
  130. $page = isset($params['page']) ? $params['page'] : 1;
  131. $cacheKey = "caches:exams:history_{$page}_{$pageSize}:" . md5(json_encode($params));
  132. $datas = RedisService::get($cacheKey);
  133. if ($datas) {
  134. return $datas;
  135. }
  136. $query = $this->getQuery($params);
  137. $list = $query->select(['a.id', 'a.user_id', 'a.paper_id', 'a.score', 'a.accurate_count', 'b.name', 'b.type', 'b.topic_count', 'b.score_total', 'b.is_charge', 'a.create_time', 'a.answer_times', 'a.status'])
  138. ->orderBy('a.create_time', 'desc')
  139. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  140. $list = $list ? $list->toArray() : [];
  141. if ($list) {
  142. foreach ($list['data'] as &$item) {
  143. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  144. }
  145. }
  146. $rows = isset($list['data']) ? $list['data'] : [];
  147. $datas = [
  148. 'pageSize' => $pageSize,
  149. 'total' => isset($list['total']) ? $list['total'] : 0,
  150. 'list' => $rows
  151. ];
  152. if ($rows) {
  153. RedisService::set($cacheKey, $datas, rand(10, 20));
  154. }
  155. return $datas;
  156. }
  157. /**
  158. * 每日一练目录数据
  159. * @param $userId 用户ID
  160. * @param $params
  161. * @param $pageSize
  162. * @return array|mixed
  163. */
  164. public function getPracticeList($userId, $params, $pageSize = 10)
  165. {
  166. $page = isset($params['page']) ? $params['page'] : 1;
  167. $type = isset($params['type']) ? $params['type'] : 1;
  168. $cacheKey = "caches:exams:practice_{$userId}:{$page}_" . md5(json_encode($params));
  169. $datas = RedisService::get($cacheKey);
  170. // 每日一练访问次数统计
  171. if(empty($sc)){
  172. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 1);
  173. }
  174. if ($datas) {
  175. return $datas;
  176. }
  177. $list = $this->model->from('exam_answers as a')
  178. ->leftJoin('exam_papers as b', 'b.id', '=', 'a.paper_id')
  179. ->where(['b.scene_type' => 1, 'b.status' => 1, 'b.mark' => 1, 'a.mark' => 1])
  180. ->where(function ($query) use ($params) {
  181. $type = isset($params['type']) && $params['type'] ? intval($params['type']) : 1;
  182. if ($type > 0) {
  183. $query->where('b.type', $type);
  184. }
  185. })
  186. ->select(['a.*', 'b.name', 'b.scene_type', 'b.type', 'b.score_total', 'b.topic_count'])
  187. ->orderBy('a.create_time', 'desc')
  188. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  189. $list = $list ? $list->toArray() : [];
  190. if ($list) {
  191. foreach ($list['data'] as &$item) {
  192. $item['date'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d') : '';
  193. }
  194. }
  195. // 今日是否练习过
  196. $rows = isset($list['data']) ? $list['data'] : [];
  197. $first = isset($rows[0]) ? $rows[0] : [];
  198. $firstTime = isset($first['date']) ? $first['date'] : 0;
  199. if ($page == 1 && strtotime($firstTime) < strtotime(date('Y-m-d'))) {
  200. $type = isset($params['type']) && $params['type'] ? intval($params['type']) : 1;
  201. $data = PaperService::make()->getRandomPaper($userId, $type, 1);
  202. if($data){
  203. $rows = array_merge([$data], $rows);
  204. }
  205. }
  206. $datas = [
  207. 'pageSize' => $pageSize,
  208. 'total' => isset($list['total']) ? $list['total'] : 0,
  209. 'list' => $rows
  210. ];
  211. if ($rows) {
  212. RedisService::set($cacheKey, $datas, rand(10, 20));
  213. }
  214. return $datas;
  215. }
  216. /**
  217. * 答题排行榜
  218. * @param $type 1-日,2-周(7天),3-月
  219. * @param int $num
  220. * @return array|mixed
  221. */
  222. public function getRankByType($type, $num = 0)
  223. {
  224. $num = $num ? $num : ConfigService::make()->getConfigByCode('rank_num', 10);
  225. $cacheKey = "caches:exams:ranks:{$type}_{$num}";
  226. $datas = RedisService::get($cacheKey);
  227. if ($datas) {
  228. return $datas;
  229. }
  230. $prefix = env('DB_PREFIX', 'lev_');
  231. $datas = MemberAnswerRankModel::from('member_answer_ranks as a')
  232. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  233. ->where(['a.status' => 1, 'a.mark' => 1])
  234. ->where(function ($query) use ($type) {
  235. if ($type == 1) {
  236. // 日
  237. $query->where('a.date', date('Y-m-d'));
  238. } else if ($type == 2) {
  239. // 周
  240. $query->where('a.date', '>=', date('Y-m-d', time() - 7 * 86400));
  241. } else if ($type == 3) {
  242. // 月
  243. $query->where('a.date', '>=', date('Y-m-01'));
  244. }
  245. })
  246. ->select(['a.id', 'a.user_id', 'b.avatar', 'b.nickname', 'a.answer_time', 'a.answer_count', DB::raw("ROUND(sum({$prefix}a.answer_time)/3600,0) as answer_hour"), DB::raw("sum({$prefix}a.answer_count) as count")])
  247. ->groupBy('a.user_id')
  248. ->orderByRaw("sum({$prefix}a.answer_time)/3600 desc")
  249. ->take($num)
  250. ->get();
  251. $datas = $datas ? $datas->toArray() : [];
  252. if ($datas) {
  253. foreach ($datas as &$item) {
  254. $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
  255. }
  256. RedisService::set($cacheKey, $datas, rand(20, 30));
  257. }
  258. return $datas;
  259. }
  260. /**
  261. * 重新答题,清除答题记录数据
  262. * @param $id
  263. * @return bool
  264. */
  265. public function reset($id)
  266. {
  267. $log = $this->model->where(['id'=> $id,'mark'=>1])->first();
  268. if(empty($log)){
  269. return true;
  270. }
  271. $updateData = ['score'=>0,'accurate_count'=>0,'answer_count'=>0,'answer_times'=>0,'answer_last_id'=>0,'is_submit'=>0,'status'=>1];
  272. $this->model->where(['id'=> $id,'mark'=>1])->update($updateData);
  273. return true;
  274. }
  275. public function answer($userId, $params)
  276. {
  277. $paperId = isset($params['id'])? $params['id'] : 0;
  278. $rid = isset($params['rid'])? $params['rid'] : 0;
  279. $tid = isset($params['tid'])? $params['tid'] : 0;
  280. $type = isset($params['type'])? $params['type'] : 1;
  281. $isSubmit = isset($params['is_submit'])? $params['is_submit'] : 1;
  282. $answer = isset($params['answer'])? $params['answer'] : '';
  283. $answerImage = isset($params['answer_image'])? $params['answer_image'] : '';
  284. $answerType = isset($params['answer_type']) && $params['answer_type']? $params['answer_type'] : 2;
  285. if($isSubmit<=0 && empty($answer)){
  286. $this->error = '请先提交答案';
  287. return false;
  288. }
  289. if($answerType==1 && empty($answerImage)){
  290. $this->error = '请先上传图片答案';
  291. return false;
  292. }
  293. // 试卷数据
  294. $paperInfo = ExamPaperModel::where(['id'=> $paperId,'type'=>$type,'status'=>1,'mark'=>1])
  295. ->first();
  296. $correctAnswer = isset($paperInfo['correct_answer'])? $paperInfo['correct_answer'] : '';
  297. $sceneType = isset($paperInfo['scene_type']) && $paperInfo['scene_type']? $paperInfo['scene_type'] : 1;
  298. if(empty($paperInfo)){
  299. $this->error = '试题数据错误,请返回刷新重试';
  300. return false;
  301. }
  302. // 题目数据
  303. $topicInfo = ExamTopicModel::where(['id'=> $tid,'paper_id'=>$paperId,'status'=>1,'mark'=>1])->first();
  304. $topicType = isset($topicInfo['topic_type'])? trim($topicInfo['topic_type']) : '';
  305. if(empty($topicInfo) || empty($topicType)){
  306. $this->error = '题库已更新,请返回刷新重试';
  307. return false;
  308. }
  309. // 答题记录
  310. $submit = 0;
  311. if($rid){
  312. $answerInfo = ExamAnswerModel::where(['id'=>$rid,'status'=>1,'mark'=>1])->first();
  313. $submit = isset($answerInfo['is_submit'])? $answerInfo['is_submit'] : 0;
  314. if(empty($answerInfo)){
  315. $rid = 0;
  316. }
  317. }
  318. // 验证答案内容类型和数据
  319. // 每日一练
  320. if($sceneType == 1 && $rid<=0){
  321. // 今日记录
  322. $answerInfo = ExamAnswerModel::where(['paper_id'=>$paperId,'status'=>1,'mark'=>1])->where('create_time','>=', strtotime(date('Y-m-d')))->first();
  323. $rid = isset($answerInfo['id'])? $answerInfo['id'] : 0;
  324. $submit = isset($answerInfo['is_submit'])? $answerInfo['is_submit'] : 0;
  325. }
  326. // 是否已交卷
  327. if($submit == 1){
  328. $this->error = '您已交卷';
  329. return false;
  330. }
  331. /* TODO 验证答案 */
  332. if(in_array($topicType,['选择题','单选题'])){
  333. } else if (in_array($topicType, ['简答题','计算题','阅读理解'])){
  334. // 图片答案AI验证
  335. if($answerType == 1){
  336. }else {
  337. }
  338. }
  339. if($rid){
  340. $data = [
  341. ''
  342. ];
  343. }
  344. }
  345. }