SubjectService.php 5.2 KB

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