SubjectService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamAccessLogModel;
  4. use App\Models\ExamSubjectsModel;
  5. use App\Models\ActionLogModel;
  6. use App\Services\BaseService;
  7. use App\Services\RedisService;
  8. /**
  9. * 课程管理-服务类
  10. * Class SubjectService
  11. */
  12. class SubjectService extends BaseService
  13. {
  14. protected static $instance = null;
  15. protected $model = null;
  16. public function __construct()
  17. {
  18. $this->model = new ExamSubjectsModel();
  19. }
  20. /**
  21. * 静态入口
  22. */
  23. public static function make()
  24. {
  25. if (!self::$instance) {
  26. self::$instance = new SubjectService();
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * 获取课程列表
  32. */
  33. public function customList($param, $pageSize = 15)
  34. {
  35. $query = $this->model->where('mark', operator: 1);
  36. if (!empty($param['type'])) {
  37. $query->where('type', $param['type']);
  38. }
  39. if (!empty($param['keyword'])) {
  40. $query->where(function ($q) use ($param) {
  41. $q->where('id', $param['keyword'])
  42. ->orWhere('subject_name', 'like', "%{$param['keyword']}%");
  43. });
  44. }
  45. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  46. $list = $list ? $list->toArray() : [];
  47. return [
  48. 'pageSize' => $pageSize,
  49. 'total' => isset($list['total']) ? $list['total'] : 0,
  50. 'list' => isset($list['data']) ? $list['data'] : []
  51. ];
  52. return message("操作成功", true, $list);
  53. }
  54. /**
  55. * 获取课程详情
  56. */
  57. public function getInfo($id)
  58. {
  59. $data = $this->model->findOrFail($id);
  60. return message("操作成功", true, $data);
  61. }
  62. /**
  63. * 删除课程(逻辑删除)
  64. */
  65. public function remove($id)
  66. {
  67. $subject = $this->model->findOrFail($id);
  68. $subject->mark = 0;
  69. $subject->save();
  70. return message("删除成功", true);
  71. }
  72. /**
  73. * 启用/禁用课程
  74. */
  75. public function changeStatus($id, $status)
  76. {
  77. $subject = $this->model->findOrFail($id);
  78. $subject->status = $status;
  79. $subject->save();
  80. return message("状态更新成功", true, $subject);
  81. }
  82. /**
  83. * 用户选项
  84. * @return array
  85. */
  86. public function options()
  87. {
  88. // 获取参数
  89. $param = request()->all();
  90. // 用户ID
  91. $keyword = getter($param, "keyword");
  92. $sceneType = getter($param, "scene_type");
  93. $type = getter($param, "type");
  94. $datas = $this->model
  95. ->where('status', '=', 1)
  96. ->where('mark', '=', 1)
  97. ->where(function ($query) use ($keyword, $sceneType, $type) {
  98. if ($keyword) {
  99. $query->where('subject_name', 'like', "%{$keyword}%");
  100. }
  101. if ($type) {
  102. $query->where('type', '=', $type);
  103. }
  104. })
  105. ->select(['subject_name', 'id'])
  106. ->orderBy('sort','desc')
  107. ->orderBy('id','asc')
  108. ->get();
  109. return $datas ? $datas->toArray() : [];
  110. }
  111. /**
  112. * 科目分组列表
  113. * @param int $type 2-对口,3-专升本
  114. * @return array|mixed
  115. */
  116. public function getListByAttrType($type=0,$sceneType=0, $sc=1)
  117. {
  118. $cacheKey = "caches:exam:subjects:{$type}";
  119. $datas = RedisService::get($cacheKey);
  120. // 分类入口访问统计
  121. if (empty($sc)) {
  122. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, $sceneType);
  123. }
  124. if($datas){
  125. return $datas;
  126. }
  127. $where = ['type'=>1,'status'=>1,'mark'=>1];
  128. if($type>0){
  129. $where['type'] = $type;
  130. }else{
  131. unset($where['type']);
  132. }
  133. $datas = $this->model->where($where)
  134. ->select(['id','subject_name','icon','pid','type','description','attr_type','status'])
  135. ->orderBy('sort','desc')
  136. ->orderBy('id','asc')
  137. ->get();
  138. $datas = $datas? $datas->toArray() : [];
  139. $list = [];
  140. if($datas){
  141. foreach ($datas as $item){
  142. $attrType = isset($item['attr_type'])? $item['attr_type'] : 0;
  143. $list[$attrType][] = $item;
  144. }
  145. RedisService::set($cacheKey, $list, rand(300, 600));
  146. }
  147. return $list;
  148. }
  149. /**
  150. * 删除七天之前标记软删除的数据
  151. */
  152. public function delete()
  153. {
  154. // 设置日志标题
  155. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除科目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  156. ActionLogModel::record();
  157. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  158. return parent::delete();
  159. }
  160. }