CollegeCoursesController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\validate\life\CollegeCoursesValidate;
  4. use app\controller\Curd;
  5. use app\model\CollegeCourses as CollegeCourseModel;
  6. use support\Request;
  7. use support\Response;
  8. class CollegeCoursesController extends Curd
  9. {
  10. const COURSES_TYPE = [
  11. 1 => '线上课程',
  12. 2 => '线下课程'
  13. ];
  14. public function __construct()
  15. {
  16. $this->model = new CollegeCourseModel();
  17. $this->validate = true;
  18. $this->validateClass = new CollegeCoursesValidate();
  19. }
  20. /**
  21. * @Desc 课程列表
  22. * @Author Gorden
  23. * @Date 2024/2/26 16:51
  24. *
  25. * @param Request $request
  26. * @return Response
  27. * @throws \support\exception\BusinessException
  28. */
  29. public function select(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $where['courses_is_del'] = 0;
  33. $query = $this->doSelect($where, $field, $order);
  34. return $this->doFormat($query, $format, $limit);
  35. }
  36. /**
  37. * @Desc 联表
  38. * @Author Gorden
  39. * @Date 2024/3/20 14:53
  40. *
  41. * @param array $where
  42. * @param string|null $field
  43. * @param string $order
  44. * @return \Illuminate\Database\Query\Builder
  45. */
  46. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  47. {
  48. $model = $this->model->with([
  49. 'category' => function ($query) {
  50. $query->select('category_id', 'category_name');
  51. },
  52. 'teacher' => function ($query) {
  53. $query->select('teacher_id', 'teacher_name');
  54. }
  55. ]);
  56. foreach ($where as $column => $value) {
  57. if (is_array($value)) {
  58. if ($value[0] === 'like' || $value[0] === 'not like') {
  59. $model = $model->where($column, $value[0], "%$value[1]%");
  60. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  61. $model = $model->where($column, $value[0], $value[1]);
  62. } elseif ($value[0] == 'in' && !empty($value[1])) {
  63. $valArr = $value[1];
  64. if (is_string($value[1])) {
  65. $valArr = explode(",", trim($value[1]));
  66. }
  67. $model = $model->whereIn($column, $valArr);
  68. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  69. $valArr = $value[1];
  70. if (is_string($value[1])) {
  71. $valArr = explode(",", trim($value[1]));
  72. }
  73. $model = $model->whereNotIn($column, $valArr);
  74. } elseif ($value[0] == 'null') {
  75. $model = $model->whereNull($column);
  76. } elseif ($value[0] == 'not null') {
  77. $model = $model->whereNotNull($column);
  78. } elseif ($value[0] !== '' || $value[1] !== '') {
  79. $model = $model->whereBetween($column, $value);
  80. }
  81. } else {
  82. $model = $model->where($column, $value);
  83. }
  84. }
  85. if ($field) {
  86. $model = $model->orderBy($field, $order);
  87. }
  88. return $model;
  89. }
  90. protected function afterQuery($items)
  91. {
  92. foreach ($items as &$item) {
  93. $item->courses_week = !empty($item->courses_week) ? explode(',', $item->courses_week) : '';
  94. $item->courses_label = !empty($item->courses_label) ? explode(',', $item->courses_label) : '';
  95. $item->courses_time = !empty($item->courses_time) ? explode('~', $item->courses_time) : '';
  96. $item->courses_type = self::COURSES_TYPE[$item->courses_type];
  97. }
  98. return $items;
  99. }
  100. /**
  101. * @Desc insert input
  102. * @Author Gorden
  103. * @Date 2024/3/20 14:15
  104. *
  105. * @param Request $request
  106. * @return array
  107. * @throws \support\exception\BusinessException
  108. */
  109. protected function insertInput(Request $request): array
  110. {
  111. $data = $this->inputFilter($request->post());
  112. $coursesTime = json_decode($data['courses_time'], true);
  113. $data['courses_time'] = $coursesTime['start'] . '~' . $coursesTime['end'];
  114. // 教学时长
  115. $timeCut = strtotime(date('Y-m-d') . $coursesTime['end']) - strtotime(date('Y-m-d') . $coursesTime['start']);
  116. $data['courses_long'] = $timeCut / 3600;
  117. return $data;
  118. }
  119. /**
  120. * @Desc update input
  121. * @Author Gorden
  122. * @Date 2024/3/20 14:15
  123. *
  124. * @param Request $request
  125. * @return array
  126. * @throws \support\exception\BusinessException
  127. */
  128. protected function updateInput(Request $request): array
  129. {
  130. $primary_key = $this->model->getKeyName();
  131. $id = $request->post($primary_key);
  132. $data = $this->inputFilter($request->post());
  133. $coursesTime = json_decode($data['courses_time'], true);
  134. $data['courses_time'] = $coursesTime['start'] . '~' . $coursesTime['end'];
  135. // 教学时长
  136. $timeCut = strtotime(date('Y-m-d') . $coursesTime['end']) - strtotime(date('Y-m-d') . $coursesTime['start']);
  137. $data['courses_long'] = $timeCut / 3600;
  138. $model = $this->model->find($id);
  139. if (!$model) {
  140. throw new BusinessException('记录不存在', 2);
  141. }
  142. unset($data[$primary_key]);
  143. return [$id, $data];
  144. }
  145. /**
  146. * @Desc 删除课程
  147. * @Author Gorden
  148. * @Date 2024/2/26 16:52
  149. *
  150. * @param Request $request
  151. * @return Response
  152. * @throws \support\exception\BusinessException
  153. */
  154. public function delete(Request $request): Response
  155. {
  156. $ids = $this->deleteInput($request);
  157. $this->doSoftDelete($ids, ['courses_is_del' => 1]);
  158. return json_success('success');
  159. }
  160. }