SubjectService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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['keyword'])) {
  38. $query->where(function ($q) use ($param) {
  39. $q->where('id', $param['keyword'])
  40. ->orWhere('subject_name', 'like', "%{$param['keyword']}%");
  41. });
  42. }
  43. $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
  44. $list = $list ? $list->toArray() : [];
  45. return [
  46. 'pageSize' => $pageSize,
  47. 'total' => isset($list['total']) ? $list['total'] : 0,
  48. 'list' => isset($list['data']) ? $list['data'] : []
  49. ];
  50. return message("操作成功", true, $list);
  51. }
  52. /**
  53. * 获取课程详情
  54. */
  55. public function getInfo($id)
  56. {
  57. $data = $this->model->findOrFail($id);
  58. return message("操作成功", true, $data);
  59. }
  60. /**
  61. * 删除课程(逻辑删除)
  62. */
  63. public function remove($id)
  64. {
  65. $subject = $this->model->findOrFail($id);
  66. $subject->mark = 0;
  67. $subject->save();
  68. return message("删除成功", true);
  69. }
  70. /**
  71. * 启用/禁用课程
  72. */
  73. public function changeStatus($id, $status)
  74. {
  75. $subject = $this->model->findOrFail($id);
  76. $subject->status = $status;
  77. $subject->save();
  78. return message("状态更新成功", true, $subject);
  79. }
  80. /**
  81. * 用户选项
  82. * @return array
  83. */
  84. public function options()
  85. {
  86. // 获取参数
  87. $param = request()->all();
  88. // 用户ID
  89. $keyword = getter($param, "keyword");
  90. $sceneType = getter($param, "scene_type");
  91. $type = getter($param, "type");
  92. $datas = $this->model
  93. ->where('status', '=', 1)
  94. ->where('mark', '=', 1)
  95. ->where(function ($query) use ($keyword, $sceneType, $type) {
  96. if ($keyword) {
  97. $query->where('subject_name', 'like', "%{$keyword}%");
  98. }
  99. if ($type) {
  100. $query->where('type', '=', $type);
  101. }
  102. })
  103. ->select(['subject_name', 'id'])
  104. ->get();
  105. return $datas ? $datas->toArray() : [];
  106. }
  107. /**
  108. * 删除七天之前标记软删除的数据
  109. */
  110. public function delete()
  111. {
  112. // 设置日志标题
  113. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除科目信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  114. ActionLogModel::record();
  115. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  116. return parent::delete();
  117. }
  118. }