| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace App\Services\Exam;
- use App\Models\ExamAccessLogModel;
- use App\Models\ExamSubjectsModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 课程管理-服务类
- * Class SubjectService
- */
- class SubjectService extends BaseService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new ExamSubjectsModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new SubjectService();
- }
- return self::$instance;
- }
- /**
- * 获取课程列表
- */
- public function customList($param, $pageSize = 15)
- {
- $query = $this->model->where('mark', operator: 1);
- if (!empty($param['type'])) {
- $query->where('type', $param['type']);
- }
- if (!empty($param['attr_type'])) {
- $query->where('attr_type', $param['attr_type']);
- }
- if (!empty($param['keyword'])) {
- $query->where(function ($q) use ($param) {
- $q->where('id', $param['keyword'])
- ->orWhere('subject_name', 'like', "%{$param['keyword']}%")
- ->orWhere('description', 'like', "%{$param['keyword']}%");
- });
- }
- $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 获取课程详情
- */
- public function getInfo($id)
- {
- $data = $this->model->findOrFail($id);
- return message("操作成功", true, $data);
- }
- /**
- * 删除课程(逻辑删除)
- */
- public function remove($id)
- {
- $subject = $this->model->findOrFail($id);
- $subject->mark = 0;
- $subject->save();
- return message("删除成功", true);
- }
- /**
- * 启用/禁用课程
- */
- public function changeStatus($id, $status)
- {
- $subject = $this->model->findOrFail($id);
- $subject->status = $status;
- $subject->save();
- return message("状态更新成功", true, $subject);
- }
- /**
- * 用户选项
- * @return array
- */
- public function options()
- {
- // 获取参数
- $param = request()->all();
- // 用户ID
- $keyword = getter($param, "keyword");
- $sceneType = getter($param, "scene_type");
- $type = getter($param, "type");
- $attrType = getter($param, "attr_type");
- $datas = $this->model
- ->where('status', '=', 1)
- ->where('mark', '=', 1)
- ->where(function ($query) use ($keyword, $sceneType, $type, $attrType) {
- if ($keyword) {
- $query->where('subject_name', 'like', "%{$keyword}%");
- }
- if ($type) {
- $query->where('type', '=', $type);
- }
- if ($attrType) {
- $query->where('attr_type', '=', $attrType);
- }
- })
- ->select(['subject_name', 'id'])
- ->orderBy('sort','desc')
- ->orderBy('id','asc')
- ->get();
- return $datas ? $datas->toArray() : [];
- }
- /**
- * 科目分组列表
- * @param int $type 2-对口,3-专升本
- * @return array|mixed
- */
- public function getListByAttrType($type=0,$sceneType=0, $sc=1)
- {
- $cacheKey = "caches:exam:subjects:{$type}";
- $datas = RedisService::get($cacheKey);
- // 分类入口访问统计
- if (empty($sc)) {
- ExamAccessLogModel::saveLog(date('Y-m-d'), $type, $sceneType);
- }
- if($datas){
- return $datas;
- }
- $where = ['type'=>1,'status'=>1,'mark'=>1];
- if($type>0){
- $where['type'] = $type;
- }else{
- unset($where['type']);
- }
- $datas = $this->model->where($where)
- ->select(['id','subject_name','icon','pid','type','description','attr_type','status'])
- ->orderBy('sort','desc')
- ->orderBy('id','asc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- $list = [];
- if($datas){
- foreach ($datas as $item){
- $attrType = isset($item['attr_type'])? $item['attr_type'] : 0;
- $list[$attrType][] = $item;
- }
- RedisService::set($cacheKey, $list, rand(300, 600));
- }
- return $list;
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除科目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|