|
|
@@ -0,0 +1,255 @@
|
|
|
+<?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\Services\BaseService;
|
|
|
+use App\Services\ConfigService;
|
|
|
+use App\Services\RedisService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 答题服务-服务类
|
|
|
+ * @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;
|
|
|
+ $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 $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(300, 600));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文章详情
|
|
|
+ * @param $id
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getInfo($id)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:articles:info_{$id}";
|
|
|
+ $info = RedisService::get($cacheKey);
|
|
|
+ if($info){
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = $this->model->where(['id'=> $id,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','title','type','cover','view_num','author','description','create_time','type','content'])
|
|
|
+ ->first();
|
|
|
+ $info = $info? $info->toArray() : [];
|
|
|
+ if($info){
|
|
|
+ $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
|
|
|
+ $info['cover'] = get_image_url($info['cover']);
|
|
|
+ $info['content'] = get_format_content($info['content']);
|
|
|
+ $this->model->where(['id'=> $id])->increment('view_num',1);
|
|
|
+ $info['view_num'] += intval($info['view_num']);
|
|
|
+ RedisService::set($cacheKey, $info, rand(5,10));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取分类文章推荐
|
|
|
+ * @param int $cateId 推荐分类ID
|
|
|
+ * @param int $type 类别:3-普通文章,4-客服回复
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getCustomRecommend($cateId=0, $type=4)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:articles:list_{$cateId}";
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ $limitNum = ConfigService::make()->getConfigByCode('custom_recommend_num', 6);
|
|
|
+ $limitNum = $limitNum? $limitNum : 6;
|
|
|
+ $datas = ArticleCateModel::where(function($query) use($cateId){
|
|
|
+ if($cateId){
|
|
|
+ $query->where('cate_id', $cateId);
|
|
|
+ }
|
|
|
+ })->where(['type'=>$type,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['id','cate_id','title','description','sort','type','status'])
|
|
|
+ ->limit($limitNum)
|
|
|
+ ->orderBy('sort','desc')
|
|
|
+ ->orderBy('create_time','desc')
|
|
|
+ ->get();
|
|
|
+ $datas = $datas? $datas->toArray() : [];
|
|
|
+ if($datas){
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300,600));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文章推荐分类
|
|
|
+ * @param int $type 1-普通文章分类
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getCateList($type=2)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:articles:cateList_{$type}";
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
+ if($datas){
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+
|
|
|
+ $limitNum = ConfigService::make()->getConfigByCode('custom_cate_num', 6);
|
|
|
+ $limitNum = $limitNum? $limitNum : 6;
|
|
|
+ $datas = ArticleCateModel::where(['type'=> $type,'status'=>1,'mark'=>1])
|
|
|
+ ->select(['cate_id','name','sort','type'])
|
|
|
+ ->limit($limitNum)
|
|
|
+ ->get();
|
|
|
+ $datas = $datas? $datas->toArray() : [];
|
|
|
+ if($datas){
|
|
|
+ RedisService::set($cacheKey, $datas, rand(300,600));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $datas;
|
|
|
+ }
|
|
|
+}
|