PaperService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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\ExamAnswerTopicModel;
  15. use App\Models\ExamErrorModel;
  16. use App\Models\ExamPaperModel;
  17. use App\Models\ExamTopicModel;
  18. use App\Services\BaseService;
  19. use App\Services\ConfigService;
  20. use App\Services\RedisService;
  21. use Illuminate\Support\Facades\DB;
  22. /**
  23. * 试卷服务-服务类
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. * @package App\Services\Api
  27. */
  28. class PaperService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. */
  37. public function __construct()
  38. {
  39. $this->model = new ExamPaperModel();
  40. }
  41. /**
  42. * 静态入口
  43. */
  44. public static function make()
  45. {
  46. if (!self::$instance) {
  47. self::$instance = new static();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * @param $params
  53. * @param int $pageSize
  54. * @return array
  55. */
  56. public function getDataList($params, $pageSize = 15)
  57. {
  58. $page = isset($params['page']) ? $params['page'] : 1;
  59. $cacheKey = "caches:paper:list_{$page}_{$pageSize}:" . md5(json_encode($params));
  60. $datas = RedisService::get($cacheKey);
  61. // 访问次数统计
  62. $sc = isset($params['sc']) ? $params['sc'] : 0;
  63. $type = isset($params['type']) ? $params['type'] : 0;
  64. $sceneType = isset($params['scene_type']) ? $params['scene_type'] : 0;
  65. if (empty($sc) && !in_array($sceneType, [3, 4, 6])) {
  66. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, $sceneType);
  67. }
  68. if ($datas) {
  69. return $datas;
  70. }
  71. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  72. $query = $this->getQuery($params);
  73. $list = $query->with(['answer' => function ($query) use ($userId) {
  74. $query->where('user_id', $userId);
  75. }])
  76. ->where('a.topic_count', '>', 0)
  77. ->select(['a.*', 'b.id as rid', 'b.score', 'b.accurate_count', 'b.user_id', 'b.answer_times', 'b.answer_count', 'b.is_submit'])
  78. ->groupBy('a.id')
  79. ->orderBy('a.sort', 'desc')->orderBy('a.id', 'desc')
  80. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  81. $list = $list ? $list->toArray() : [];
  82. if ($list) {
  83. foreach ($list['data'] as &$item) {
  84. $item['score'] = !empty($item['score']) ? $item['score'] : 0;
  85. $item['is_submit'] = !empty($item['is_submit']) ? $item['is_submit'] : 0;
  86. $item['answer_count'] = !empty($item['answer_count']) ? $item['answer_count'] : 0;
  87. $item['accurate_count'] = !empty($item['accurate_count']) ? $item['accurate_count'] : 0;
  88. $item['date'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d') : '';
  89. }
  90. }
  91. $rows = isset($list['data']) ? $list['data'] : [];
  92. $datas = [
  93. 'pageSize' => $pageSize,
  94. 'total' => isset($list['total']) ? $list['total'] : 0,
  95. 'list' => $rows
  96. ];
  97. if ($rows) {
  98. RedisService::set($cacheKey, $datas, rand(300, 600));
  99. }
  100. return $datas;
  101. }
  102. /**
  103. * 每日一练
  104. * @param $params
  105. * @param int $pageSize
  106. * @return array
  107. */
  108. public function getPracticeList($params, $pageSize = 15)
  109. {
  110. $page = isset($params['page']) ? $params['page'] : 1;
  111. $cacheKey = "caches:paper:practice_list_{$page}_{$pageSize}:" . md5(json_encode($params));
  112. $datas = RedisService::get($cacheKey);
  113. // 访问次数统计
  114. $sc = isset($params['sc']) ? $params['sc'] : 0;
  115. $type = isset($params['type']) ? $params['type'] : 0;
  116. if (empty($sc)) {
  117. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 1);
  118. }
  119. if ($datas) {
  120. return $datas;
  121. }
  122. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  123. $query = $this->getQuery($params);
  124. $list = $query->with(['answer' => function ($query) use ($userId) {
  125. $query->where('user_id', $userId);
  126. }])
  127. ->where('a.topic_count', '>', 0)
  128. ->select(['a.*', 'b.id as rid', 'b.score', 'b.accurate_count', 'b.user_id', 'b.answer_times', 'b.answer_count', 'b.is_submit'])
  129. ->groupBy('a.id')
  130. ->orderBy('a.sort', 'desc')->orderBy('a.id', 'desc')
  131. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  132. $list = $list ? $list->toArray() : [];
  133. if ($list) {
  134. foreach ($list['data'] as &$item) {
  135. $item['score'] = !empty($item['score']) ? $item['score'] : 0;
  136. $item['is_submit'] = !empty($item['is_submit']) ? $item['is_submit'] : 0;
  137. $item['answer_count'] = !empty($item['answer_count']) ? $item['answer_count'] : 0;
  138. $item['accurate_count'] = !empty($item['accurate_count']) ? $item['accurate_count'] : 0;
  139. $item['date'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d') : '';
  140. }
  141. }
  142. $rows = isset($list['data']) ? $list['data'] : [];
  143. $datas = [
  144. 'pageSize' => $pageSize,
  145. 'total' => isset($list['total']) ? $list['total'] : 0,
  146. 'list' => $rows
  147. ];
  148. if ($rows) {
  149. RedisService::set($cacheKey, $datas, rand(300, 600));
  150. }
  151. return $datas;
  152. }
  153. /**
  154. * 查询
  155. * @param $params
  156. * @return mixed
  157. */
  158. public function getQuery($params)
  159. {
  160. $where = ['a.status' => 1, 'a.mark' => 1];
  161. $status = isset($params['status']) ? $params['status'] : 0;
  162. $type = isset($params['type']) ? $params['type'] : 0;
  163. $sceneType = isset($params['scene_type']) ? $params['scene_type'] : 0;
  164. $subjectId = isset($params['subject_id']) ? $params['subject_id'] : 0;
  165. if ($status > 0) {
  166. $where['a.status'] = $status;
  167. }
  168. if ($type > 0) {
  169. $where['a.type'] = $type;
  170. }
  171. if ($sceneType > 0) {
  172. $where['a.scene_type'] = $sceneType;
  173. }
  174. if ($subjectId > 0) {
  175. $where['a.subject_id'] = $subjectId;
  176. }
  177. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  178. return $this->model->from('exam_papers as a')
  179. ->leftJoin('exam_answers as b', function ($join) use ($userId) {
  180. // 未完成交卷的答题数据
  181. $join->on('b.paper_id', '=', 'a.id')->where(['b.user_id' => $userId, 'b.status' => 1, 'b.mark' => 1]);
  182. })
  183. ->where($where)
  184. ->where(function ($query) use ($params) {
  185. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  186. if ($keyword) {
  187. $query->where('a.name', 'like', "%{$keyword}%");
  188. }
  189. });
  190. }
  191. /**
  192. * 最近是否答过该题
  193. * @param $userId 用户ID
  194. * @param $paperId 试卷ID
  195. * @param int $submit 是否已交卷,1-是,0-否
  196. * @return array|mixed
  197. */
  198. public function getLastAnswer($userId, $paperId, $submit = 0)
  199. {
  200. $cacheKey = "caches:paper:answer_last_{$userId}:{$paperId}_{$submit}";
  201. $data = RedisService::get($cacheKey);
  202. if ($data) {
  203. return $data;
  204. }
  205. $lastTime = ConfigService::make()->getConfigByCode('submit_paper_time', 30);
  206. $lastTime = $lastTime >= 1 && $lastTime <= 150 ? $lastTime : 30;
  207. $data = ExamAnswerModel::where(['user_id' => $userId, 'is_submit' => $submit, 'paper_id' => $paperId, 'status' => 1, 'mark' => 1])
  208. ->where(function ($query) use ($lastTime, $submit) {
  209. if ($submit <= 0) {
  210. // 未交卷
  211. $query->where('is_submit', 0)->orWhere('answer_last_at', '>=', time() - $lastTime * 60);
  212. } else {
  213. $query->where('is_submit', $submit)->orWhere('answer_last_at', '<', time() - $lastTime * 60);
  214. }
  215. })
  216. ->select(['id', 'paper_id', 'score', 'answer_last_at', 'answer_last_id'])
  217. ->first();
  218. $data = $data ? $data->toArray() : [];
  219. if ($data) {
  220. RedisService::set($cacheKey, $data, rand(5, 10));
  221. }
  222. return $data;
  223. }
  224. /**
  225. * 用户最近答题记录
  226. * @param $userId
  227. * @param $paperId
  228. * @return array|mixed
  229. */
  230. public function getLastAnswerLogId($userId, $paperId)
  231. {
  232. $cacheKey = "caches:paper:log_{$userId}_{$paperId}";
  233. $id = RedisService::get($cacheKey);
  234. if ($id) {
  235. return $id;
  236. }
  237. $id = ExamAnswerModel::where(['user_id' => $userId, 'paper_id' => $paperId, 'status' => 1, 'mark' => 1])
  238. ->where('create_time', '>=', strtotime(date('Y-m-d')))
  239. ->value('id');
  240. if ($id) {
  241. RedisService::set($cacheKey, $id, rand(5, 10));
  242. }
  243. return $id;
  244. }
  245. /**
  246. * 获取详情
  247. * @param $id
  248. * @return array|mixed
  249. */
  250. public function getInfo($userId, $paperId, $params = [])
  251. {
  252. $lid = isset($params['lid']) ? intval($params['lid']) : 0;
  253. $tid = isset($params['tid']) ? intval($params['tid']) : 0;
  254. $rid = isset($params['rid']) ? intval($params['rid']) : 0;
  255. $type = isset($params['type']) ? intval($params['type']) : 1;
  256. $sc = isset($params['sc']) ? intval($params['sc']) : 0;
  257. $cacheKey = "caches:paper:info_{$userId}:p{$paperId}_t{$tid}_r{$rid}_l{$lid}_{$sc}";
  258. $info = RedisService::get($cacheKey);
  259. if ($info) {
  260. return $info;
  261. }
  262. // 若进行答题
  263. if ($rid <= 0) {
  264. if ($type != 1) {
  265. // 判断N分钟内是否有未交卷的答题
  266. $lastAnswerInfo = $this->getLastAnswer($userId, $paperId, 0);
  267. $rid = isset($lastAnswerInfo['id']) ? $lastAnswerInfo['id'] : 0;
  268. } else {
  269. $rid = $this->getLastAnswerLogId($userId, $paperId);
  270. }
  271. }
  272. $where = ['a.id' => $paperId, 'a.status' => 1, 'a.mark' => 1];
  273. $info = $this->model->from('exam_papers as a')
  274. ->leftJoin('exam_answers as b', function ($join) use ($rid, $userId) {
  275. $join->on('b.paper_id', '=', 'a.id')->where(['b.id' => $rid, 'b.user_id' => $userId, 'b.status' => 1]);
  276. })
  277. ->leftJoin('exam_subjects as c','c.id','=','a.subject_id')
  278. ->where($where)
  279. ->select(['a.id as paper_id','c.answer_times as paper_answer_times', 'b.id as rid', 'b.score', 'b.accurate_count', 'b.is_submit', 'b.answer_count', 'b.answer_last_id', 'b.answer_times', 'a.name', 'a.type', 'a.scene_type', 'a.subject_id', 'a.score_total', 'a.topic_count', 'a.total_time', 'a.is_charge', 'a.create_time', 'a.status'])
  280. ->first();
  281. $info = $info ? $info->toArray() : [];
  282. if ($info) {
  283. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d') : '';
  284. $info['answer_count'] = isset($info['answer_count']) ? intval($info['answer_count']) : 0;
  285. $info['accurate_count'] = isset($info['accurate_count']) ? intval($info['accurate_count']) : 0;
  286. $info['score'] = isset($info['score']) ? floatval($info['score']) : 0;
  287. $info['rid'] = isset($info['rid']) ? intval($info['rid']) : 0;
  288. // 剩余时间
  289. $isSubmit = isset($info['is_submit']) ? $info['is_submit'] : 0;
  290. $totalTime = isset($info['paper_answer_times']) && $info['paper_answer_times']? intval($info['paper_answer_times']) : 0;
  291. $totalTime = $totalTime > 0 ? $totalTime : ConfigService::make()->getConfigByCode('answer_total_time', 60);
  292. $totalTime = $totalTime? $totalTime * 60 : 3600;
  293. $info['answer_times'] = isset($info['answer_times']) ? intval($info['answer_times']) : 0;
  294. $info['remain_time'] = $totalTime > $info['answer_times'] ? $totalTime - $info['answer_times'] : 0;
  295. $info['remain_time'] = $isSubmit ? 0 : $info['remain_time'];
  296. $info['remain_time_text'] = $info['remain_time'] ? format_times($info['remain_time']) : '00:00';
  297. $info['progress'] = $info['topic_count'] ? intval($info['answer_count'] / $info['topic_count'] * 100) : 0;
  298. $info['fee_num'] = (int)ConfigService::make()->getConfigByCode('fee_answer_num',10);
  299. // 当前题目
  300. //$prefix = env('DB_PREFIX','_lev');
  301. $model = ExamTopicModel::from('exam_topics as a')
  302. ->leftJoin('exam_answers_topics as b', function ($join) use ($rid, $userId) {
  303. // 是否有最近答题记录
  304. $join->on('b.topic_id', '=', "a.id")->where("b.answer_log_id", '=', $rid)->where(['b.user_id' => $userId, 'b.status' => 1, "b.mark" => 1]);
  305. })
  306. ->where(['a.paper_id' => $paperId, 'a.status' => 1, 'a.mark' => 1])
  307. ->where(function ($query) use ($tid) {
  308. // 答题卡选择的题目,否则默认按题目排序返回第一题
  309. if ($tid > 0) {
  310. $query->where('a.id', $tid);
  311. }
  312. });
  313. $info['topic'] = $model->select(['a.*', 'b.id as answer_topic_id', 'b.answer_log_id', 'b.answer as submit_answer', 'b.answer_analysis as submit_answer_analysis', 'b.answer_type as submit_answer_type', 'b.score as submit_score', 'b.accurate'])
  314. ->orderBy('a.sort', 'desc')
  315. ->orderBy('a.id', 'asc')
  316. ->first();
  317. $info['topic'] = $info['topic']? $info['topic']->toArray() : [];
  318. $topicId = isset($info['topic']['id'])? $info['topic']['id'] : 0;
  319. $sort = isset($info['topic']['sort'])? $info['topic']['sort'] : 0;
  320. if($info['topic'] && $topicId) {
  321. $info['topic']['accurate'] = isset($info['topic']['accurate'])?$info['topic']['accurate'] : -1;
  322. $info['topic']['submit_score'] = isset($info['topic']['submit_score'])?floatval($info['topic']['submit_score']) : 0;
  323. $info['topic']['topic_analysis'] = isset($info['topic']['topic_analysis'])?$info['topic']['topic_analysis'] : '';
  324. $info['topic']['answer_analysis'] = isset($info['topic']['answer_analysis'])?$info['topic']['answer_analysis'] : '';
  325. $info['topic']['topic_analysis'] = $info['topic']['topic_analysis']?$info['topic']['topic_analysis'] : $info['topic']['answer_analysis'];
  326. $info['topic']['submit_answer'] = isset($info['topic']['submit_answer'])?$info['topic']['submit_answer'] : '';
  327. $info['topic']['submit_answer_type'] = isset($info['topic']['submit_answer_type'])?$info['topic']['submit_answer_type'] : 1;
  328. $info['topic']['submit_answers'] = '';
  329. $info['topic']['correct_answers'] = [];
  330. if ($info['topic']['submit_answer_type'] == 3) {
  331. $info['topic']['submit_answer'] = get_image_url($info['topic']['submit_answer']);
  332. } else if ($info['topic']['submit_answer_type'] == 4) {
  333. $info['topic']['submit_answer'] = $info['topic']['submit_answer'] ? json_decode($info['topic']['submit_answer'], true) : [];
  334. }
  335. // else {
  336. // $info['topic']['submit_answer'] = format_content($info['topic']['submit_answer']);
  337. // }
  338. $info['topic']['correct_show_type'] = 2; // 文本
  339. if (preg_match("/(\/images\/|\/temp\/)/", $info['topic']['correct_answer'])) {
  340. $info['topic']['correct_show_type'] = 1; // 图片
  341. $info['topic']['correct_answer'] = get_image_url($info['topic']['correct_answer']);
  342. }
  343. $info['topic']['topic_analysis'] = format_content($info['topic']['topic_analysis']);
  344. // $info['topic']['topic_analysis'] = html_entity_decode($info['topic']['topic_analysis']);
  345. if ($info['topic']['show_type'] == 1) {
  346. $info['topic']['topic_name'] = format_content($info['topic']['topic_name']);
  347. } else if ($info['topic']['show_type'] == 2) {
  348. $info['topic']['topic_name'] = get_image_url($info['topic']['topic_name']);
  349. // 已经答题,返回答案
  350. if ($rid > 0 || $info['topic']['submit_answer']) {
  351. //$info['topic']['topic_analysis'] = get_image_url($info['topic']['topic_analysis']);
  352. if (preg_match("/(images|temp)/", $info['topic']['answer_A'])) {
  353. $info['topic']['answer_A'] = get_image_url($info['topic']['answer_A']);
  354. }
  355. if (preg_match("/(images|temp)/", $info['topic']['answer_B'])) {
  356. $info['topic']['answer_B'] = get_image_url($info['topic']['answer_B']);
  357. }
  358. if (preg_match("/(images|temp)/", $info['topic']['answer_C'])) {
  359. $info['topic']['answer_C'] = get_image_url($info['topic']['answer_C']);
  360. }
  361. if (preg_match("/(images|temp)/", $info['topic']['answer_D'])) {
  362. $info['topic']['answer_D'] = get_image_url($info['topic']['answer_D']);
  363. }
  364. if (preg_match("/(images|temp)/", $info['topic']['answer_E'])) {
  365. $info['topic']['answer_E'] = get_image_url($info['topic']['answer_E']);
  366. }
  367. if (preg_match("/(images|temp)/", $info['topic']['answer_F'])) {
  368. $info['topic']['answer_F'] = get_image_url($info['topic']['answer_F']);
  369. }
  370. }
  371. }
  372. // 多选题
  373. $topicType = isset($info['topic']['topic_type']) ? $info['topic']['topic_type'] : '';
  374. if ($topicType == '多选题') {
  375. $info['topic']['submit_answers'] = $info['topic']['submit_answer'] ? explode(',', $info['topic']['submit_answer']) : '';
  376. $info['topic']['correct_answer'] = str_replace([',',',','、'],['','',''], $info['topic']['correct_answer']);
  377. $info['topic']['correct_answers'] = $info['topic']['correct_answer'] ? str_split($info['topic']['correct_answer']) : [];
  378. }else if(in_array($topicType,['综合题','问答题'])){
  379. $info['topic']['correct_answer'] = format_content($info['topic']['correct_answer']);
  380. }
  381. $info['topic']['answers'] = [];
  382. if ($info['topic']['answer_A']!=='') {
  383. $info['topic']['answers'][] = ['code' => 'A', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_A']):$info['topic']['answer_A']];
  384. }
  385. if ($info['topic']['answer_B'] !== '') {
  386. $info['topic']['answers'][] = ['code' => 'B', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_B']):$info['topic']['answer_B']];
  387. }
  388. if ($info['topic']['answer_C'] !=='') {
  389. $info['topic']['answers'][] = ['code' => 'C', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_C']):$info['topic']['answer_C']];
  390. }
  391. if ($info['topic']['answer_D']!=='') {
  392. $info['topic']['answers'][] = ['code' => 'D', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_D']):$info['topic']['answer_D']];
  393. }
  394. if ($info['topic']['answer_E']!=='') {
  395. $info['topic']['answers'][] = ['code' => 'E', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_E']):$info['topic']['answer_E']];
  396. }
  397. if ($info['topic']['answer_F']!=='') {
  398. $info['topic']['answers'][] = ['code' => 'F', 'value' => in_array($topicType,['单选题','选择题','多选题'])?format_content($info['topic']['answer_F']):$info['topic']['answer_F']];
  399. }
  400. // 上一题
  401. $info['last'] = ExamTopicModel::where(['paper_id'=> $paperId,'status'=>1,'mark'=>1])
  402. ->where('sort','>', $sort)
  403. ->select(['id','sort','topic_name'])
  404. ->orderBy('sort','asc')
  405. ->orderBy('id','asc')
  406. ->first();
  407. $info['last'] = $info['last'] ? $info['last']->toArray() : ['id' => 0];
  408. // 下一题
  409. $info['next'] = ExamTopicModel::where(['paper_id'=> $paperId,'status'=>1,'mark'=>1])
  410. ->where('sort','<', $sort)
  411. ->select(['id','sort','topic_name'])
  412. ->orderBy('sort','desc')
  413. ->orderBy('id','desc')
  414. ->first();
  415. $info['next'] = $info['next'] ? $info['next']->toArray() : ['id' => 0];
  416. }
  417. RedisService::set($cacheKey, $info, rand(20, 30));
  418. }
  419. // $lockCacheKey = "caches:paper:read_{$userId}:p{$paperId}_lock";
  420. // if($sc<=0 && !RedisService::get($lockCacheKey)){
  421. // // 统计答题次数
  422. // $this->model->where(['id'=>$paperId])->update(['answer_counts'=>DB::raw("answer_counts + 1"),'update_time'=>time()]);
  423. // RedisService::set($lockCacheKey, ['id'=>$paperId,'user_id'=>$userId], rand(3,5));
  424. // }
  425. return $info;
  426. }
  427. /**
  428. * 获取随机试卷
  429. * @param $userId 用户
  430. * @param $type 试卷类型
  431. * @param $sceneType 场景
  432. * @param false $refresh
  433. * @return array|mixed
  434. */
  435. public function getRandomPaper($userId, $type, $sceneType, $refresh = false)
  436. {
  437. $cacheKey = "caches:papers:random_{$userId}:{$type}_{$sceneType}";
  438. $data = RedisService::get($cacheKey);
  439. if ($data && !$refresh) {
  440. return $data;
  441. }
  442. $data = $this->model->where(['type'=>$type,'scene_type'=>$sceneType,'status'=>1,'mark'=>1])
  443. ->where('topic_count','>',0)
  444. ->orderByRaw('RAND()')
  445. ->first();
  446. $data = $data ? $data->toArray() : [];
  447. if ($data) {
  448. $data['score'] = 0;
  449. $data['paper_id'] = $data['id'];
  450. $data['accurate_count'] = 0;
  451. $data['date'] = date('Y-m-d');
  452. $data['answer_count'] = 0;
  453. $data['answer_times'] = 0;
  454. RedisService::set($cacheKey, $data, rand(10, 20));
  455. }
  456. return $data;
  457. }
  458. /**
  459. * 纠错
  460. * @param $userId
  461. * @param $params
  462. * @return bool
  463. */
  464. public function submitError($userId, $params)
  465. {
  466. $id = isset($params['id']) ? $params['id'] : 0;
  467. $rid = isset($params['rid']) ? $params['rid'] : 0;
  468. $type = isset($params['type']) ? $params['type'] : 0;
  469. $moduleType = isset($params['module_type']) ? $params['module_type'] : 1;
  470. $description = isset($params['description']) ? $params['description'] : '';
  471. if ($id <= 0 || $moduleType <= 0) {
  472. $this->error = '参数错误';
  473. return false;
  474. }
  475. if ($type <= 0) {
  476. $this->error = '请选择错误类型';
  477. return false;
  478. }
  479. if (empty($description)) {
  480. $this->error = '请输入错误描述';
  481. return false;
  482. }
  483. $limitTime = ConfigService::make()->getConfigByCode('limit_exam_error_submit', 5);
  484. $cacheKey = "caches:paper:errors_{$userId}:{$id}_{$rid}";
  485. if (RedisService::get($cacheKey) && $limitTime > 0) {
  486. $this->error = '您近期已提交过该题错误,请稍后再试~';
  487. return false;
  488. }
  489. $paperInfo = $this->model->where(['id' => $id, 'mark' => 1])->first();
  490. if (empty($paperInfo)) {
  491. $this->error = '试题不存在';
  492. return false;
  493. }
  494. if ($limitTime > 0 && $logId = ExamErrorModel::where(['user_id' => $userId, 'rid' => $rid, 'paper_id' => $id, 'mark' => 1])->where('create_time', '>=', time() - $limitTime * 60)->value('id')) {
  495. $this->error = '您近期已提交过该题错误,请稍后再试~';
  496. RedisService::set($cacheKey, ['id' => $logId, 'paper_id' => $id], $limitTime * 60);
  497. return false;
  498. }
  499. $data = [
  500. 'user_id' => $userId,
  501. 'paper_id' => $id,
  502. 'rid' => $rid,
  503. 'type' => $type,
  504. 'module_type' => $moduleType,
  505. 'description' => $description,
  506. 'image_url' => isset($params['image_url']) && $params['image_url'] ? get_image_path($params['image_url']) : '',
  507. 'create_time' => time(),
  508. 'status' => 1,
  509. 'mark' => 1
  510. ];
  511. if (!$logId = ExamErrorModel::insertGetId($data)) {
  512. $this->error = '提交失败';
  513. return false;
  514. }
  515. $this->error = '提交成功';
  516. RedisService::set($cacheKey, $data, $limitTime * 60);
  517. return ['id' => $logId, 'paper_id' => $id];
  518. }
  519. }