systemModel = new ScheduleOverviewSystem(); $this->courseModel = new ScheduleOverviewCourse(); $this->scheduleModel = new ScheduleOverviewSchedule(); } public function overview() { $year = request('year', date('Y')); return $this->success([ 'systems' => $this->getSystems(), 'courses' => $this->getCourses(), 'schedules' => $this->getSchedules($year), ]); } public function systemIndex() { return $this->success($this->getSystems()); } public function systemShow() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '体系ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $detail = $this->systemModel->with('courses')->find($all['id']); if (empty($detail)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']); } return $this->success($detail); } public function systemSave() { $all = request()->all(); $validator = Validator::make($all, [ 'name' => 'required|string|max:255', 'sort' => 'nullable|integer', ], [ 'name.required' => '体系名称必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } if (!empty($all['id'])) { $model = $this->systemModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']); } } else { $model = new ScheduleOverviewSystem(); $model->admin_id = $this->getUserId(); $model->department_id = optional($this->getUser())->department_id; } DB::beginTransaction(); try { $model->name = $all['name']; $model->sort = (int) ($all['sort'] ?? 0); $model->save(); DB::commit(); return $this->success($model); } catch (\Throwable $throwable) { DB::rollBack(); return $this->fail([ResponseCode::ERROR_INSIDE, $throwable->getMessage()]); } } public function systemDestroy() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '体系ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $model = $this->systemModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']); } DB::beginTransaction(); try { $courseIds = $this->courseModel->where('system_id', $model->id)->pluck('id')->toArray(); $this->scheduleModel->where('system_id', $model->id)->delete(); if (!empty($courseIds)) { $this->scheduleModel->whereIn('course_id', $courseIds)->delete(); $this->courseModel->whereIn('id', $courseIds)->delete(); } $model->delete(); DB::commit(); return $this->success('删除成功'); } catch (\Throwable $throwable) { DB::rollBack(); return $this->fail([ResponseCode::ERROR_INSIDE, $throwable->getMessage()]); } } public function courseIndex() { return $this->success($this->getCourses()); } public function courseShow() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '课程ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $detail = $this->courseModel->with('system')->find($all['id']); if (empty($detail)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不存在']); } return $this->success($detail); } public function courseSave() { $all = request()->all(); $validator = Validator::make($all, [ 'system_id' => 'required|integer', 'name' => 'required|string|max:255', 'sort' => 'nullable|integer', ], [ 'system_id.required' => '所属体系必填', 'name.required' => '课程名称必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $system = $this->systemModel->find($all['system_id']); if (empty($system)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '所属体系不存在']); } if (!empty($all['id'])) { $model = $this->courseModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不存在']); } } else { $model = new ScheduleOverviewCourse(); $model->admin_id = $this->getUserId(); $model->department_id = optional($this->getUser())->department_id; } DB::beginTransaction(); try { $originSystemId = $model->system_id; $model->system_id = (int) $all['system_id']; $model->name = $all['name']; $model->sort = (int) ($all['sort'] ?? 0); $model->save(); if (!empty($originSystemId) && (int) $originSystemId !== (int) $model->system_id) { $this->scheduleModel->where('course_id', $model->id)->update([ 'system_id' => $model->system_id, ]); } DB::commit(); return $this->success($model->load('system')); } catch (\Throwable $throwable) { DB::rollBack(); return $this->fail([ResponseCode::ERROR_INSIDE, $throwable->getMessage()]); } } public function courseDestroy() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '课程ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $model = $this->courseModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不存在']); } DB::beginTransaction(); try { $this->scheduleModel->where('course_id', $model->id)->delete(); $model->delete(); DB::commit(); return $this->success('删除成功'); } catch (\Throwable $throwable) { DB::rollBack(); return $this->fail([ResponseCode::ERROR_INSIDE, $throwable->getMessage()]); } } public function scheduleIndex() { $year = request('year', date('Y')); return $this->success($this->getSchedules($year)); } public function scheduleShow() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '编排ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $detail = $this->scheduleModel->with(['system', 'course'])->find($all['id']); if (empty($detail)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '编排不存在']); } return $this->success($detail); } public function scheduleSave() { $all = request()->all(); $validator = Validator::make($all, [ 'year' => 'required|string|size:4', 'system_id' => 'required|integer', 'course_id' => 'required|integer', 'month' => 'required|integer|min:1|max:12', 'title' => 'required|string|max:255', 'owner' => 'required|string|max:255', 'location' => 'required|string|max:255', 'count_text' => 'nullable|string|max:255', ], [ 'year.required' => '年份必填', 'system_id.required' => '体系必填', 'course_id.required' => '课程必填', 'month.required' => '月份必填', 'title.required' => '编排标题必填', 'owner.required' => '负责人必填', 'location.required' => '地点必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $system = $this->systemModel->find($all['system_id']); if (empty($system)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']); } $course = $this->courseModel->find($all['course_id']); if (empty($course)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不存在']); } if ((int) $course->system_id !== (int) $system->id) { return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不属于当前体系']); } if (!empty($all['id'])) { $model = $this->scheduleModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '编排不存在']); } } else { $model = new ScheduleOverviewSchedule(); $model->admin_id = $this->getUserId(); $model->department_id = optional($this->getUser())->department_id; } DB::beginTransaction(); try { $model->year = $all['year']; $model->system_id = (int) $all['system_id']; $model->course_id = (int) $all['course_id']; $model->month = (int) $all['month']; $model->title = $all['title']; $model->owner = $all['owner']; $model->location = $all['location']; $model->count_text = $all['count_text'] ?? ''; $model->save(); DB::commit(); return $this->success($model->load(['system', 'course'])); } catch (\Throwable $throwable) { DB::rollBack(); return $this->fail([ResponseCode::ERROR_INSIDE, $throwable->getMessage()]); } } public function scheduleDestroy() { $all = request()->all(); $validator = Validator::make($all, [ 'id' => 'required', ], [ 'id.required' => '编排ID必填', ]); if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } $model = $this->scheduleModel->find($all['id']); if (empty($model)) { return $this->fail([ResponseCode::ERROR_BUSINESS, '编排不存在']); } $model->delete(); return $this->success('删除成功'); } protected function getSystems() { return $this->systemModel ->orderBy('sort') ->orderBy('id') ->get(); } protected function getCourses() { return $this->courseModel ->with('system') ->orderBy('sort') ->orderBy('id') ->get(); } protected function getSchedules($year) { return $this->scheduleModel ->with(['system', 'course']) ->where('year', $year) ->orderBy('month') ->orderBy('id') ->get(); } }