SubjectService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Services\Exam;
  3. use App\Models\ExamSubjectsModel;
  4. use App\Models\ActionLogModel;
  5. use App\Services\BaseService;
  6. /**
  7. * 课程管理-服务类
  8. * Class SubjectService
  9. */
  10. class SubjectService extends BaseService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new ExamSubjectsModel();
  17. }
  18. /**
  19. * 静态入口
  20. */
  21. public static function make()
  22. {
  23. if (!self::$instance) {
  24. self::$instance = new SubjectService();
  25. }
  26. return self::$instance;
  27. }
  28. /**
  29. * 获取课程列表
  30. */
  31. public function customList($param, $pageSize = 15)
  32. {
  33. $query = $this->model->where('mark', operator: 1);
  34. if (!empty($param['type'])) {
  35. $query->where('type', $param['type']);
  36. }
  37. if (!empty($param['attr_type'])) {
  38. $query->where('attr_type', $param['attr_type']);
  39. }
  40. if (!empty($param['keyword'])) {
  41. $query->where(function ($q) use ($param) {
  42. $q->where('id', $param['keyword'])
  43. ->orWhere('subject_name', 'like', "%{$param['keyword']}%")
  44. ->orWhere('description', 'like', "%{$param['keyword']}%");
  45. });
  46. }
  47. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  48. $list = $list ? $list->toArray() : [];
  49. return [
  50. 'pageSize' => $pageSize,
  51. 'total' => isset($list['total']) ? $list['total'] : 0,
  52. 'list' => isset($list['data']) ? $list['data'] : []
  53. ];
  54. }
  55. /**
  56. * 获取课程详情
  57. */
  58. public function getInfo($id)
  59. {
  60. $data = $this->model->findOrFail($id);
  61. return message("操作成功", true, $data);
  62. }
  63. /**
  64. * 删除课程(逻辑删除)
  65. */
  66. public function remove($id)
  67. {
  68. $subject = $this->model->findOrFail($id);
  69. $subject->mark = 0;
  70. $subject->save();
  71. return message("删除成功", true);
  72. }
  73. /**
  74. * 启用/禁用课程
  75. */
  76. public function changeStatus($id, $status)
  77. {
  78. $subject = $this->model->findOrFail($id);
  79. $subject->status = $status;
  80. $subject->save();
  81. return message("状态更新成功", true, $subject);
  82. }
  83. /**
  84. * 用户选项
  85. * @return array
  86. */
  87. public function options()
  88. {
  89. // 获取参数
  90. $param = request()->all();
  91. // 用户ID
  92. $keyword = getter($param, "keyword");
  93. $sceneType = getter($param, "scene_type");
  94. $type = getter($param, "type");
  95. $attrType = getter($param, "attr_type");
  96. $datas = $this->model
  97. ->where('status', '=', 1)
  98. ->where('mark', '=', 1)
  99. ->where(function ($query) use ($keyword, $sceneType, $type, $attrType) {
  100. if ($keyword) {
  101. $query->where('subject_name', 'like', "%{$keyword}%");
  102. }
  103. if ($type) {
  104. $query->where('type', '=', $type);
  105. }
  106. if ($attrType) {
  107. $query->where('attr_type', '=', $attrType);
  108. }
  109. })
  110. ->select(['subject_name', 'id'])
  111. ->get();
  112. return $datas ? $datas->toArray() : [];
  113. }
  114. /**
  115. * 删除七天之前标记软删除的数据
  116. */
  117. public function delete()
  118. {
  119. // 设置日志标题
  120. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除科目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  121. ActionLogModel::record();
  122. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  123. return parent::delete();
  124. }
  125. }