| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\ExamAccessLogModel;
- use App\Models\ExamAnswerModel;
- use App\Models\ExamPaperModel;
- use App\Models\ExamTopicModel;
- use App\Models\MemberAnswerRankModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 答题服务-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class ExamService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new ExamAnswerModel();
- }
- /**
- * 静态入口
- */
- 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:exams: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;
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- $subjectId = isset($params['subject_id']) ? $params['subject_id'] : 0;
- if ($userId > 0) {
- $where['a.user_id'] = $userId;
- }
- 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 $params
- * @param int $pageSize
- * @return array|mixed
- */
- public function getHistoryList($params, $pageSize = 10)
- {
- $page = isset($params['page']) ? $params['page'] : 1;
- $cacheKey = "caches:exams:history_{$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(10, 20));
- }
- return $datas;
- }
- /**
- * 每日一练目录数据
- * @param $userId 用户ID
- * @param $params
- * @param $pageSize
- * @return array|mixed
- */
- public function getPracticeList($userId, $params, $pageSize = 10)
- {
- $page = isset($params['page']) ? $params['page'] : 1;
- $type = isset($params['type']) ? $params['type'] : 1;
- $cacheKey = "caches:exams:practice_{$userId}:{$page}_" . md5(json_encode($params));
- $datas = RedisService::get($cacheKey);
- // 每日一练访问次数统计
- if(empty($sc)){
- ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 1);
- }
- if ($datas) {
- return $datas;
- }
- $list = $this->model->from('exam_answers as a')
- ->leftJoin('exam_papers as b', 'b.id', '=', 'a.paper_id')
- ->where(['b.scene_type' => 1, 'b.status' => 1, 'b.mark' => 1, 'a.mark' => 1])
- ->where(function ($query) use ($params) {
- $type = isset($params['type']) && $params['type'] ? intval($params['type']) : 1;
- if ($type > 0) {
- $query->where('b.type', $type);
- }
- })
- ->select(['a.*', 'b.name', 'b.scene_type', 'b.type', 'b.score_total', 'b.topic_count'])
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['date'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d') : '';
- }
- }
- // 今日是否练习过
- $rows = isset($list['data']) ? $list['data'] : [];
- $first = isset($rows[0]) ? $rows[0] : [];
- $firstTime = isset($first['date']) ? $first['date'] : 0;
- if ($page == 1 && strtotime($firstTime) < strtotime(date('Y-m-d'))) {
- $type = isset($params['type']) && $params['type'] ? intval($params['type']) : 1;
- $data = PaperService::make()->getRandomPaper($userId, $type, 1);
- if($data){
- $rows = array_merge([$data], $rows);
- }
- }
- $datas = [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => $rows
- ];
- if ($rows) {
- RedisService::set($cacheKey, $datas, rand(10, 20));
- }
- return $datas;
- }
- /**
- * 答题排行榜
- * @param $type 1-日,2-周(7天),3-月
- * @param int $num
- * @return array|mixed
- */
- public function getRankByType($type, $num = 0)
- {
- $num = $num ? $num : ConfigService::make()->getConfigByCode('rank_num', 10);
- $cacheKey = "caches:exams:ranks:{$type}_{$num}";
- $datas = RedisService::get($cacheKey);
- if ($datas) {
- return $datas;
- }
- $prefix = env('DB_PREFIX', 'lev_');
- $datas = MemberAnswerRankModel::from('member_answer_ranks as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where(['a.status' => 1, 'a.mark' => 1])
- ->where(function ($query) use ($type) {
- if ($type == 1) {
- // 日
- $query->where('a.date', date('Y-m-d'));
- } else if ($type == 2) {
- // 周
- $query->where('a.date', '>=', date('Y-m-d', time() - 7 * 86400));
- } else if ($type == 3) {
- // 月
- $query->where('a.date', '>=', date('Y-m-01'));
- }
- })
- ->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")])
- ->groupBy('a.user_id')
- ->orderByRaw("sum({$prefix}a.answer_time)/3600 desc")
- ->take($num)
- ->get();
- $datas = $datas ? $datas->toArray() : [];
- if ($datas) {
- foreach ($datas as &$item) {
- $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
- }
- RedisService::set($cacheKey, $datas, rand(20, 30));
- }
- return $datas;
- }
- /**
- * 重新答题,清除答题记录数据
- * @param $id
- * @return bool
- */
- public function reset($id)
- {
- $log = $this->model->where(['id'=> $id,'mark'=>1])->first();
- if(empty($log)){
- return true;
- }
- $updateData = ['score'=>0,'accurate_count'=>0,'answer_count'=>0,'answer_times'=>0,'answer_last_id'=>0,'is_submit'=>0,'status'=>1];
- $this->model->where(['id'=> $id,'mark'=>1])->update($updateData);
- return true;
- }
- public function answer($userId, $params)
- {
- $paperId = isset($params['id'])? $params['id'] : 0;
- $rid = isset($params['rid'])? $params['rid'] : 0;
- $tid = isset($params['tid'])? $params['tid'] : 0;
- $type = isset($params['type'])? $params['type'] : 1;
- $isSubmit = isset($params['is_submit'])? $params['is_submit'] : 1;
- $answer = isset($params['answer'])? $params['answer'] : '';
- $answerImage = isset($params['answer_image'])? $params['answer_image'] : '';
- $answerType = isset($params['answer_type']) && $params['answer_type']? $params['answer_type'] : 2;
- if($isSubmit<=0 && empty($answer)){
- $this->error = '请先提交答案';
- return false;
- }
- if($answerType==1 && empty($answerImage)){
- $this->error = '请先上传图片答案';
- return false;
- }
- // 试卷数据
- $paperInfo = ExamPaperModel::where(['id'=> $paperId,'type'=>$type,'status'=>1,'mark'=>1])
- ->first();
- $correctAnswer = isset($paperInfo['correct_answer'])? $paperInfo['correct_answer'] : '';
- $sceneType = isset($paperInfo['scene_type']) && $paperInfo['scene_type']? $paperInfo['scene_type'] : 1;
- if(empty($paperInfo)){
- $this->error = '试题数据错误,请返回刷新重试';
- return false;
- }
- // 题目数据
- $topicInfo = ExamTopicModel::where(['id'=> $tid,'paper_id'=>$paperId,'status'=>1,'mark'=>1])->first();
- $topicType = isset($topicInfo['topic_type'])? trim($topicInfo['topic_type']) : '';
- if(empty($topicInfo) || empty($topicType)){
- $this->error = '题库已更新,请返回刷新重试';
- return false;
- }
- // 答题记录
- $submit = 0;
- if($rid){
- $answerInfo = ExamAnswerModel::where(['id'=>$rid,'status'=>1,'mark'=>1])->first();
- $submit = isset($answerInfo['is_submit'])? $answerInfo['is_submit'] : 0;
- if(empty($answerInfo)){
- $rid = 0;
- }
- }
- // 验证答案内容类型和数据
- // 每日一练
- if($sceneType == 1 && $rid<=0){
- // 今日记录
- $answerInfo = ExamAnswerModel::where(['paper_id'=>$paperId,'status'=>1,'mark'=>1])->where('create_time','>=', strtotime(date('Y-m-d')))->first();
- $rid = isset($answerInfo['id'])? $answerInfo['id'] : 0;
- $submit = isset($answerInfo['is_submit'])? $answerInfo['is_submit'] : 0;
- }
- // 是否已交卷
- if($submit == 1){
- $this->error = '您已交卷';
- return false;
- }
- /* TODO 验证答案 */
- if(in_array($topicType,['选择题','单选题'])){
- } else if (in_array($topicType, ['简答题','计算题','阅读理解'])){
- // 图片答案AI验证
- if($answerType == 1){
- }else {
- }
- }
- if($rid){
- $data = [
- ''
- ];
- }
- }
- }
|