SubjectService.php 5.4 KB

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