|
|
@@ -0,0 +1,268 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 官方网站: http://www.laravel.cn
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Author: laravel开发员 <laravel.qq.com>
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace App\Services\Api;
|
|
|
+
|
|
|
+use App\Models\ExamAnswerModel;
|
|
|
+use App\Models\ExamPaperModel;
|
|
|
+use App\Models\ExamTopicModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+use App\Services\ConfigService;
|
|
|
+use App\Services\RedisService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 试卷服务-服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * @package App\Services\Api
|
|
|
+ */
|
|
|
+class PaperService extends BaseService
|
|
|
+{
|
|
|
+ // 静态对象
|
|
|
+ protected static $instance = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new ExamPaperModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = new static();
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 15)
|
|
|
+ {
|
|
|
+ $page = isset($params['page'])? $params['page'] : 1;
|
|
|
+ $cacheKey = "caches:paper:list_{$page}_{$pageSize}:".md5(json_encode($params));
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ $query = $this->getQuery($params);
|
|
|
+ $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'])
|
|
|
+ ->orderBy('a.create_time','desc')
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ $list = $list? $list->toArray() :[];
|
|
|
+ if($list){
|
|
|
+ foreach($list['data'] as &$item){
|
|
|
+ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $rows = isset($list['data'])? $list['data'] : [];
|
|
|
+ $datas = [
|
|
|
+ 'pageSize'=> $pageSize,
|
|
|
+ 'total'=> isset($list['total'])? $list['total'] : 0,
|
|
|
+ 'list'=> $rows
|
|
|
+ ];
|
|
|
+ if($rows){
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300, 600));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询
|
|
|
+ * @param $params
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getQuery($params)
|
|
|
+ {
|
|
|
+ $where = ['b.status'=>1,'b.mark'=>1,'a.mark' => 1];
|
|
|
+ $status = isset($params['status'])? $params['status'] : 0;
|
|
|
+ $type = isset($params['type'])? $params['type'] : 0;
|
|
|
+ $sceneType = isset($params['scene_type'])? $params['scene_type'] : 0;
|
|
|
+ $subjectId = isset($params['subject_id'])? $params['subject_id'] : 0;
|
|
|
+ if($status>0){
|
|
|
+ $where['a.status'] = $status;
|
|
|
+ }
|
|
|
+ if($type>0){
|
|
|
+ $where['b.type'] = $type;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($sceneType>0){
|
|
|
+ $where['b.scene_type'] = $sceneType;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($subjectId>0){
|
|
|
+ $where['b.subject_id'] = $subjectId;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->model->from('exam_answers as a')
|
|
|
+ ->leftJoin('exam_papers as b','b.id','=','a.paper_id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function ($query) use($params){
|
|
|
+ $keyword = isset($params['keyword'])? $params['keyword'] : '';
|
|
|
+ if($keyword){
|
|
|
+ $query->where('b.name','like',"%{$keyword}%");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 最近是否答过该题
|
|
|
+ * @param $userId 用户ID
|
|
|
+ * @param $paperId 试卷ID
|
|
|
+ * @param int $submit 是否已交卷,1-是,0-否
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getLastAnswer($userId, $paperId, $submit=0)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:paper:answer_last_{$userId}:{$paperId}_{$submit}";
|
|
|
+ $data = RedisService::get($cacheKey);
|
|
|
+ if($data){
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ $lastTime = ConfigService::make()->getConfigByCode('submit_paper_time', 30);
|
|
|
+ $lastTime = $lastTime>=1 && $lastTime <= 150? $lastTime : 30;
|
|
|
+ $data = ExamAnswerModel::where(['user_id'=>$userId,'is_submit'=> $submit,'paper_id'=> $paperId,'status'=>1,'mark'=>1])
|
|
|
+ ->where(function($query) use($lastTime, $submit){
|
|
|
+ if($submit<=0){
|
|
|
+ // 未交卷
|
|
|
+ $query->where('is_submit', 0)->orWhere('answer_last_at','>=', time() - $lastTime * 60);
|
|
|
+ }else{
|
|
|
+ $query->where('is_submit', $submit)->orWhere('answer_last_at','<', time() - $lastTime * 60);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['id','paper_id','score','answer_last_at'])
|
|
|
+ ->first();
|
|
|
+ $data = $data? $data->toArray() : [];
|
|
|
+ if($data){
|
|
|
+ RedisService::set($cacheKey, $data, rand(5, 10));
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取详情
|
|
|
+ * @param $id
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getInfo($userId, $paperId, $tid=0, $rid=0)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:paper:info_{$userId}:p{$paperId}_t{$tid}_r{$rid}";
|
|
|
+ $info = RedisService::get($cacheKey);
|
|
|
+ if($info){
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 若进行答题
|
|
|
+ if($rid<=0){
|
|
|
+ // 判断N分钟内是否有未交卷的答题
|
|
|
+ $lastAnswerInfo = $this->getLastAnswer($userId, $paperId, 0);
|
|
|
+ $rid = isset($lastAnswerInfo['id'])? $lastAnswerInfo['id'] : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $where = ['a.id'=> $paperId,'a.status'=>1,'a.mark'=>1];
|
|
|
+ $info = $this->model->from('exam_papers as a')
|
|
|
+ ->leftJoin('exam_answers as b','b.paper_id','=','a.id')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function($query) use($rid){
|
|
|
+ if($rid>0){
|
|
|
+ $query->where(['b.id'=>$rid]);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['a.id as paper_id','b.id as rid','b.score','b.accurate_count','b.answer_times','a.name','a.type','a.scene_type','a.subject_id','a.score_total','a.topic_count','a.is_charge','a.create_time','a.status'])
|
|
|
+ ->first();
|
|
|
+ $info = $info? $info->toArray() : [];
|
|
|
+ if($info){
|
|
|
+ $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
|
|
|
+
|
|
|
+ // 当前题目
|
|
|
+ //$prefix = env('DB_PREFIX','_lev');
|
|
|
+ $info['topic'] = ExamTopicModel::from('exam_topics as a')
|
|
|
+ ->leftJoin('exam_answers_topics as b', function($join) use($rid){
|
|
|
+ // 是否有最近答题记录
|
|
|
+ $join->on('b.topic_id','=',"a.id")->where("b.answer_log_id",'=', $rid);
|
|
|
+ })
|
|
|
+ ->where(['a.paper_id'=> $paperId,'a.status'=>1,'a.mark'=>1])
|
|
|
+ ->where(function($query) use($tid){
|
|
|
+ // 答题卡选择的题目,否则默认按题目排序返回第一题
|
|
|
+ if($tid>0){
|
|
|
+ $query->where('a.id', $tid);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['a.*','b.id as answer_topic_id','b.answer_log_id','b.answer as submit_answer','b.answer_type as submit_answer_type','b.score as submit_score','b.accurate'])
|
|
|
+ ->orderBy('a.sort','desc')
|
|
|
+ ->orderBy('a.id','asc')
|
|
|
+ ->first();
|
|
|
+ if($info['topic']) {
|
|
|
+ if($info['topic']['show_type'] == 1){
|
|
|
+ $info['topic']['topic_name'] = format_content($info['topic']['topic_name']);
|
|
|
+ if($rid<=0){
|
|
|
+ $info['topic']['correct_answer'] = crypt_answer($info['topic']['id'], $info['topic']['correct_answer']);
|
|
|
+ $info['topic']['topic_analysis'] = '';
|
|
|
+ }
|
|
|
+ }else if($info['topic']['show_type'] == 2){
|
|
|
+ $info['topic']['topic_name'] = get_image_url($info['topic']['topic_name']);
|
|
|
+
|
|
|
+ // 已经答题,返回答案
|
|
|
+ if($rid>0){
|
|
|
+ $info['topic']['topic_analysis'] = get_image_url($info['topic']['topic_analysis']);
|
|
|
+ if(preg_match("/images/", $info['topic']['correct_answer'])){
|
|
|
+ $info['topic']['correct_answer'] = get_image_url($info['topic']['correct_answer']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_A'])){
|
|
|
+ $info['topic']['answer_A'] = get_image_url($info['topic']['answer_A']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_B'])){
|
|
|
+ $info['topic']['answer_B'] = get_image_url($info['topic']['answer_B']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_C'])){
|
|
|
+ $info['topic']['answer_C'] = get_image_url($info['topic']['answer_C']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_D'])){
|
|
|
+ $info['topic']['answer_D'] = get_image_url($info['topic']['answer_D']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_E'])){
|
|
|
+ $info['topic']['answer_E'] = get_image_url($info['topic']['answer_E']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(preg_match("/images/", $info['topic']['answer_F'])){
|
|
|
+ $info['topic']['answer_F'] = get_image_url($info['topic']['answer_F']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ RedisService::set($cacheKey, $info, rand(10, 20));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|