CollegeTeachingController.php 5.7 KB

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