SubjectService.php 5.4 KB

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