diff --git a/app/Http/Controllers/Admin/ScheduleOverviewController.php b/app/Http/Controllers/Admin/ScheduleOverviewController.php index 7c84fa4..2f0beda 100644 --- a/app/Http/Controllers/Admin/ScheduleOverviewController.php +++ b/app/Http/Controllers/Admin/ScheduleOverviewController.php @@ -27,15 +27,15 @@ class ScheduleOverviewController extends CommonController $year = request('year', date('Y')); return $this->success([ - 'systems' => $this->getSystems(), - 'courses' => $this->getCourses(), + 'systems' => $this->getSystems($year), + 'courses' => $this->getCourses($year), 'schedules' => $this->getSchedules($year), ]); } public function systemIndex() { - return $this->success($this->getSystems()); + return $this->success($this->getSystems(request('year', date('Y')))); } public function systemShow() @@ -63,9 +63,11 @@ class ScheduleOverviewController extends CommonController { $all = request()->all(); $validator = Validator::make($all, [ + 'year' => 'required|string|size:4', 'name' => 'required|string|max:255', 'sort' => 'nullable|integer', ], [ + 'year.required' => '年份必填', 'name.required' => '体系名称必填', ]); @@ -86,6 +88,7 @@ class ScheduleOverviewController extends CommonController DB::beginTransaction(); try { + $model->year = $all['year']; $model->name = $all['name']; $model->sort = (int) ($all['sort'] ?? 0); $model->save(); @@ -136,7 +139,7 @@ class ScheduleOverviewController extends CommonController public function courseIndex() { - return $this->success($this->getCourses()); + return $this->success($this->getCourses(request('year', date('Y')))); } public function courseShow() @@ -313,6 +316,10 @@ class ScheduleOverviewController extends CommonController return $this->fail([ResponseCode::ERROR_BUSINESS, '课程不属于当前体系']); } + if ((string) $system->year !== (string) $all['year']) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不属于当前年份']); + } + if (!empty($all['id'])) { $model = $this->scheduleModel->find($all['id']); if (empty($model)) { @@ -366,18 +373,22 @@ class ScheduleOverviewController extends CommonController return $this->success('删除成功'); } - protected function getSystems() + protected function getSystems($year) { return $this->systemModel + ->where('year', $year) ->orderBy('sort') ->orderBy('id') ->get(); } - protected function getCourses() + protected function getCourses($year) { return $this->courseModel ->with('system') + ->whereHas('system', function ($query) use ($year) { + $query->where('year', $year); + }) ->orderBy('sort') ->orderBy('id') ->get(); diff --git a/database/migrations/2026_03_13_160000_create_schedule_overview_tables.php b/database/migrations/2026_03_13_160000_create_schedule_overview_tables.php index 157c9a6..0433501 100644 --- a/database/migrations/2026_03_13_160000_create_schedule_overview_tables.php +++ b/database/migrations/2026_03_13_160000_create_schedule_overview_tables.php @@ -13,12 +13,15 @@ return new class extends Migration { Schema::create('schedule_overview_systems', function (Blueprint $table) { $table->id(); + $table->string('year', 4)->comment('年份'); $table->string('name')->comment('体系名称'); $table->integer('sort')->default(0)->comment('排序'); $table->bigInteger('admin_id')->nullable()->comment('创建管理员'); $table->bigInteger('department_id')->nullable()->comment('部门ID'); $table->timestamps(); $table->softDeletes(); + + $table->index('year'); }); Schema::create('schedule_overview_courses', function (Blueprint $table) { diff --git a/database/migrations/2026_03_13_173000_add_year_to_schedule_overview_systems_table.php b/database/migrations/2026_03_13_173000_add_year_to_schedule_overview_systems_table.php new file mode 100644 index 0000000..f317c15 --- /dev/null +++ b/database/migrations/2026_03_13_173000_add_year_to_schedule_overview_systems_table.php @@ -0,0 +1,46 @@ +string('year', 4)->default(date('Y'))->after('id')->comment('年份'); + $table->index('year'); + } + }); + + DB::table('schedule_overview_systems') + ->whereNull('year') + ->orWhere('year', '') + ->update(['year' => date('Y')]); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + if (!Schema::hasTable('schedule_overview_systems') || !Schema::hasColumn('schedule_overview_systems', 'year')) { + return; + } + + Schema::table('schedule_overview_systems', function (Blueprint $table) { + $table->dropIndex(['year']); + $table->dropColumn('year'); + }); + } +};