| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?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\ExamAnswerTopicModel;
- 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','answer_last_id'])
- ->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, $params=[])
- {
- $lid = isset($params['lid'])? intval($params['lid']) : 0;
- $tid = isset($params['tid'])? intval($params['tid']) : 0;
- $rid = isset($params['rid'])? intval($params['rid']) : 0;
- $type = isset($params['type'])? intval($params['type']) : 1;
- $cacheKey = "caches:paper:info_{$userId}:p{$paperId}_t{$tid}_r{$rid}_l{$lid}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- // 若进行答题
- if($rid<=0 && $type != 1) {
- // 判断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',function($join) use($rid){
- $join->on('b.paper_id','=','a.id')->where(['b.id'=>$rid]);
- })
- ->where($where)
- ->select(['a.id as paper_id','b.id as rid','b.score','b.accurate_count','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'])
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
- $lastId = isset($info['answer_last_id'])? $info['answer_last_id'] : 0;
- $info['answer_count'] = isset($info['answer_count'])? intval($info['answer_count']) : 0;
- $info['accurate_count'] = isset($info['accurate_count'])? intval($info['accurate_count']) : 0;
- $info['score'] = isset($info['score'])? intval($info['score']) : 0;
- $info['rid'] = isset($info['rid'])? intval($info['rid']) : 0;
- // 剩余时间
- $totalTime = isset($info['total_time'])? intval($info['total_time']) : 0;
- $totalTime = $totalTime>0? $totalTime : ConfigService::make()->getConfigByCode('answer_total_time', 1800);
- $info['answer_times'] = isset($info['answer_times'])? intval($info['answer_times']) : 0;
- $info['remain_time'] = $totalTime>$info['answer_times']? $totalTime-$info['answer_times'] : 0;
- $info['remain_time_text'] = $info['remain_time']? format_times($info['remain_time']) : '00:00';
- // 当前题目
- //$prefix = env('DB_PREFIX','_lev');
- $model = 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("b.mark",'=', 1);
- })
- ->where(['a.paper_id'=> $paperId,'a.status'=>1,'a.mark'=>1])
- ->where(function($query) use($tid, $lastId){
- // 答题卡选择的题目,否则默认按题目排序返回第一题
- if($tid>0){
- $query->where('a.id', $tid);
- }else if($lastId){
- $query->where('a.id', '>', $lastId);
- }
- });
- $model1 = clone $model;
- if($model->value('a.id')<=0 && $lastId){
- $model = $model1->where('a.id', $lastId);
- }
- $info['topic'] = $model->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();
- $info['topic'] = $info['topic']? $info['topic']->toArray() : [];
- $topicId = isset($info['topic']['id'])? $info['topic']['id'] : 0;
- if($info['topic'] && $topicId) {
- $info['topic']['accurate'] = isset($info['topic']['accurate'])?$info['topic']['accurate'] : -1;
- $info['topic']['submit_answer'] = isset($info['topic']['submit_answer'])?$info['topic']['submit_answer'] : '';
- $info['topic']['submit_answer_type'] = isset($info['topic']['submit_answer_type'])?$info['topic']['submit_answer_type'] : 1;
- $info['topic']['submit_answers'] = '';
- $info['topic']['collect_answers'] = [];
- if($info['topic']['submit_answer_type'] == 3){
- $info['topic']['submit_answer'] = get_image_url($info['topic']['submit_answer']);
- }else if($info['topic']['submit_answer_type'] == 4){
- $info['topic']['submit_answer'] = $info['topic']['submit_answer']?json_decode($info['topic']['submit_answer'], true):[];
- }
- if($info['topic']['show_type'] == 1){
- $info['topic']['topic_name'] = format_content($info['topic']['topic_name']);
- }else if($info['topic']['show_type'] == 2){
- $info['topic']['topic_name'] = get_image_url($info['topic']['topic_name']);
- // 已经答题,返回答案
- if($rid>0 || $info['topic']['submit_answer']){
- $info['topic']['topic_analysis'] = get_image_url($info['topic']['topic_analysis']);
- if(preg_match("/(images|temp)/", $info['topic']['correct_answer'])){
- $info['topic']['correct_answer'] = get_image_url($info['topic']['correct_answer']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_A'])){
- $info['topic']['answer_A'] = get_image_url($info['topic']['answer_A']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_B'])){
- $info['topic']['answer_B'] = get_image_url($info['topic']['answer_B']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_C'])){
- $info['topic']['answer_C'] = get_image_url($info['topic']['answer_C']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_D'])){
- $info['topic']['answer_D'] = get_image_url($info['topic']['answer_D']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_E'])){
- $info['topic']['answer_E'] = get_image_url($info['topic']['answer_E']);
- }
- if(preg_match("/(images|temp)/", $info['topic']['answer_F'])){
- $info['topic']['answer_F'] = get_image_url($info['topic']['answer_F']);
- }
- }
- }
- // 未答题隐藏答案
- if(empty($info['topic']['submit_answer'])){
- $info['topic']['correct_answer'] = '未答题';
- $info['topic']['topic_analysis'] = '';
- }
- // 多选题
- $topicType = isset($info['topic']['topic_type'])? $info['topic']['topic_type'] : '';
- if($topicType == '多选题'){
- $info['topic']['submit_answers'] = $info['topic']['submit_answer']?explode(',', $info['topic']['submit_answer']) : '';
- $info['topic']['correct_answers'] = $info['topic']['correct_answer']?explode(',', $info['topic']['correct_answer']) : [];
- }
- $info['topic']['answers'] = [];
- if($info['topic']['answer_A']){
- $info['topic']['answers'][] = ['code'=>'A','value'=> $info['topic']['answer_A']];
- }
- if($info['topic']['answer_B']){
- $info['topic']['answers'][] = ['code'=>'B','value'=> $info['topic']['answer_B']];
- }
- if($info['topic']['answer_C']){
- $info['topic']['answers'][] = ['code'=>'C','value'=> $info['topic']['answer_C']];
- }
- if($info['topic']['answer_D']){
- $info['topic']['answers'][] = ['code'=>'D','value'=> $info['topic']['answer_D']];
- }
- if($info['topic']['answer_E']){
- $info['topic']['answers'][] = ['code'=>'E','value'=> $info['topic']['answer_E']];
- }
- if($info['topic']['answer_F']){
- $info['topic']['answers'][] = ['code'=>'F','value'=> $info['topic']['answer_F']];
- }
- // 上一题
- $info['last'] = ExamTopicModel::where(['paper_id'=> $paperId,'status'=>1,'mark'=>1])
- ->where('id','<', $topicId)
- ->select(['id','topic_name'])
- ->orderBy('sort','asc')
- ->orderBy('id','desc')
- ->first();
- $info['last'] = $info['last']? $info['last']->toArray() :['id'=>0];
- // 下一题
- $info['next'] = ExamTopicModel::where(['paper_id'=> $paperId,'status'=>1,'mark'=>1])
- ->where('id','>', $topicId)
- ->select(['id','topic_name'])
- ->orderBy('sort','desc')
- ->orderBy('id','asc')
- ->first();
- $info['next'] = $info['next']? $info['next']->toArray() :['id'=>0];
- }
- RedisService::set($cacheKey, $info, rand(10, 20));
- }
- return $info;
- }
- /**
- * 获取随机试卷
- * @param $userId 用户
- * @param $type 试卷类型
- * @param $sceneType 场景
- * @param false $refresh
- * @return array|mixed
- */
- public function getRandomPaper($userId, $type, $sceneType, $refresh=false)
- {
- $cacheKey = "caches:papers:random_{$userId}:{$type}_{$sceneType}";
- $data = RedisService::get($cacheKey);
- if($data && !$refresh){
- return $data;
- }
- $data = $this->model->where(['type'=>$type,'scene_type'=>$sceneType,'status'=>1,'mark'=>1])
- ->orderByRaw('RAND()')
- ->first();
- $data = $data? $data->toArray() : [];
- if($data){
- $data['score'] = 0;
- $data['paper_id'] = $data['id'];
- $data['accurate_count'] = 0;
- $data['date'] = date('Y-m-d');
- $data['answer_count'] = 0;
- $data['answer_times'] = 0;
- RedisService::set($cacheKey, $data, rand(10, 20));
- }
- return $data;
- }
- }
|