123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\admin\controller\life;
- use app\admin\validate\life\CollegeCoursesValidate;
- use app\controller\Curd;
- use app\model\CollegeCourses as CollegeCourseModel;
- use support\Request;
- use support\Response;
- class CollegeCoursesController extends Curd
- {
- const COURSES_TYPE = [
- 1 => '线上课程',
- 2 => '线下课程'
- ];
- public function __construct()
- {
- $this->model = new CollegeCourseModel();
- $this->validate = true;
- $this->validateClass = new CollegeCoursesValidate();
- }
- /**
- * @Desc 课程列表
- * @Author Gorden
- * @Date 2024/2/26 16:51
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $where['courses_is_del'] = 0;
- $order = $request->get('order', 'desc');
- $field = $field ?? 'courses_addTime';
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- /**
- * @Desc 联表
- * @Author Gorden
- * @Date 2024/3/20 14:53
- *
- * @param array $where
- * @param string|null $field
- * @param string $order
- * @return \Illuminate\Database\Query\Builder
- */
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'category' => function ($query) {
- $query->select('category_id', 'category_name');
- },
- 'teacher' => function ($query) {
- $query->select('teacher_id', 'teacher_name');
- }
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- protected function afterQuery($items)
- {
- foreach ($items as &$item) {
- $item->courses_week = !empty($item->courses_week) ? explode(',', $item->courses_week) : [];
- $item->courses_label = !empty($item->courses_label) ? explode(',', $item->courses_label) : '';
- $item->courses_time = !empty($item->courses_time) ? explode('~', $item->courses_time) : '';
- $item->courses_type = $item->courses_type;
- }
- return $items;
- }
- /**
- * @Desc insert input
- * @Author Gorden
- * @Date 2024/3/20 14:15
- *
- * @param Request $request
- * @return array
- * @throws \support\exception\BusinessException
- */
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- $coursesTime = json_decode($data['courses_time'], true);
- $data['courses_time'] = $coursesTime['start'] . '~' . $coursesTime['end'];
- // 教学时长
- $timeCut = strtotime(date('Y-m-d') . $coursesTime['end']) - strtotime(date('Y-m-d') . $coursesTime['start']);
- $data['courses_long'] = $timeCut / 3600;
- return $data;
- }
- /**
- * @Desc update input
- * @Author Gorden
- * @Date 2024/3/20 14:15
- *
- * @param Request $request
- * @return array
- * @throws \support\exception\BusinessException
- */
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $coursesTime = json_decode($data['courses_time'], true);
- $data['courses_time'] = $coursesTime['start'] . '~' . $coursesTime['end'];
- // 教学时长
- $timeCut = strtotime(date('Y-m-d') . $coursesTime['end']) - strtotime(date('Y-m-d') . $coursesTime['start']);
- $data['courses_long'] = $timeCut / 3600;
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- /**
- * @Desc 删除课程
- * @Author Gorden
- * @Date 2024/2/26 16:52
- *
- * @param Request $request
- * @return Response
- * @throws \support\exception\BusinessException
- */
- public function delete(Request $request): Response
- {
- $ids = $this->deleteInput($request);
- $this->doSoftDelete($ids, ['courses_is_del' => 1]);
- return json_success('success');
- }
- }
|