CollegeCoursesController.php 5.8 KB

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