SubjectService.php 3.0 KB

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