CollegeTeachingController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\validate\life\CollegeTeachingValidate;
  4. use app\controller\Curd;
  5. use app\model\CollegeTeaching as teachingModel;
  6. use support\Request;
  7. use support\Response;
  8. class CollegeTeachingController extends Curd
  9. {
  10. public function __construct()
  11. {
  12. $this->model = new teachingModel();
  13. $this->validate = true;
  14. $this->validateClass = new CollegeTeachingValidate();
  15. }
  16. /**
  17. * @Desc 列表
  18. * @Author Gorden
  19. * @Date 2024/2/27 10:29
  20. *
  21. * @param Request $request
  22. * @return Response
  23. * @throws \support\exception\BusinessException
  24. */
  25. public function select(Request $request): Response
  26. {
  27. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  28. $where['teaching_is_del'] = 0;
  29. $query = $this->doSelect($where, $field, $order);
  30. return $this->doFormat($query, $format, $limit);
  31. }
  32. /**
  33. * @Desc 联表
  34. * @Author Gorden
  35. * @Date 2024/3/20 14:53
  36. *
  37. * @param array $where
  38. * @param string|null $field
  39. * @param string $order
  40. * @return \Illuminate\Database\Query\Builder
  41. */
  42. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  43. {
  44. $model = $this->model->with([
  45. 'course' => function ($query) {
  46. $query->select('courses_id', 'courses_name');
  47. },
  48. ]);
  49. foreach ($where as $column => $value) {
  50. if (is_array($value)) {
  51. if ($value[0] === 'like' || $value[0] === 'not like') {
  52. $model = $model->where($column, $value[0], "%$value[1]%");
  53. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  54. $model = $model->where($column, $value[0], $value[1]);
  55. } elseif ($value[0] == 'in' && !empty($value[1])) {
  56. $valArr = $value[1];
  57. if (is_string($value[1])) {
  58. $valArr = explode(",", trim($value[1]));
  59. }
  60. $model = $model->whereIn($column, $valArr);
  61. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  62. $valArr = $value[1];
  63. if (is_string($value[1])) {
  64. $valArr = explode(",", trim($value[1]));
  65. }
  66. $model = $model->whereNotIn($column, $valArr);
  67. } elseif ($value[0] == 'null') {
  68. $model = $model->whereNull($column);
  69. } elseif ($value[0] == 'not null') {
  70. $model = $model->whereNotNull($column);
  71. } elseif ($value[0] !== '' || $value[1] !== '') {
  72. $model = $model->whereBetween($column, $value);
  73. }
  74. } else {
  75. $model = $model->where($column, $value);
  76. }
  77. }
  78. if ($field) {
  79. $model = $model->orderBy($field, $order);
  80. }
  81. return $model;
  82. }
  83. /**
  84. * @Desc 新增
  85. * @Author Gorden
  86. * @Date 2024/2/27 10:19
  87. *
  88. * @param Request $request
  89. * @return Response
  90. * @throws \support\exception\BusinessException
  91. */
  92. public function insert(Request $request): Response
  93. {
  94. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  95. return json_fail($this->validateClass->getError());
  96. }
  97. $data = $this->insertInput($request);
  98. $data['teaching_time'] = strtotime($data['teaching_time']);
  99. $this->doInsert($data);
  100. return json_success('success');
  101. }
  102. /**
  103. * @Desc 修改
  104. * @Author Gorden
  105. * @Date 2024/2/27 10:26
  106. *
  107. * @param Request $request
  108. * @return Response
  109. * @throws \support\exception\BusinessException
  110. */
  111. public function update(Request $request): Response
  112. {
  113. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  114. return json_fail($this->validateClass->getError());
  115. }
  116. [$id, $data] = $this->updateInput($request);
  117. $data['teaching_time'] = strtotime($data['teaching_time']);
  118. $this->doUpdate($id, $data);
  119. return json_success('success');
  120. }
  121. /**
  122. * @Desc 删除
  123. * @Author Gorden
  124. * @Date 2024/2/27 10:35
  125. *
  126. * @param Request $request
  127. * @return Response
  128. * @throws \support\exception\BusinessException
  129. */
  130. public function delete(Request $request): Response
  131. {
  132. $ids = $this->deleteInput($request);
  133. $this->doSoftDelete($ids, ['teaching_is_del' => 1]);
  134. return json_success('success');
  135. }
  136. /**
  137. * @Desc
  138. * @Author Gorden
  139. * @Date 2024/2/27 10:30
  140. *
  141. * @param $query
  142. * @param $format
  143. * @param $limit
  144. * @return Response
  145. */
  146. protected function doFormat($query, $format, $limit): Response
  147. {
  148. $methods = [
  149. 'select' => 'formatSelect',
  150. 'tree' => 'formatTree',
  151. 'table_tree' => 'formatTableTree',
  152. 'normal' => 'formatNormal',
  153. ];
  154. $paginator = $query->paginate($limit);
  155. $total = $paginator->total();
  156. $items = $paginator->items();
  157. if (method_exists($this, "afterQuery")) {
  158. $items = call_user_func([$this, "afterQuery"], $items);
  159. }
  160. $format_function = $methods[$format] ?? 'formatNormal';
  161. foreach ($items as &$item) {
  162. $item->teaching_time = date('Y-m-d H:i:s',$item->teaching_time);
  163. }
  164. return call_user_func([$this, $format_function], $items, $total);
  165. }
  166. }