diff --git a/app/Http/Controllers/Admin/CalendarsController.php b/app/Http/Controllers/Admin/CalendarsController.php index e3d7d62..7130029 100644 --- a/app/Http/Controllers/Admin/CalendarsController.php +++ b/app/Http/Controllers/Admin/CalendarsController.php @@ -12,6 +12,7 @@ use App\Models\CourseContentEvaluationAsk; use App\Models\CourseContentEvaluationForm; use App\Models\CustomForm; use App\Models\CustomFormField; +use App\Models\HistoryCourse; use App\Models\SupplyDemand; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; @@ -90,7 +91,7 @@ class CalendarsController extends BaseController if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } - $detail = $this->model->with('courseContent')->find($all['id']); + $detail = $this->model->with(['courseContent', 'historyCourses'])->find($all['id']); return $this->success($detail); } @@ -113,6 +114,7 @@ class CalendarsController extends BaseController * @OA\Parameter(name="is_publish", in="query", @OA\Schema(type="string"), required=true, description="是否向用户发布0否1是"), * @OA\Parameter(name="address", in="query", @OA\Schema(type="string"), required=true, description="地址"), * @OA\Parameter(name="days", in="query", @OA\Schema(type="string"), required=true, description="天数"), + * @OA\Parameter(name="history_courses", in="query", @OA\Schema(type="array", @OA\Items(type="object")), required=false, description="历史课程数组,每项包含:type(课程体系ID), course_name(课程名称), course_type_signs_pass(培养人数未去重), course_type_signs_pass_unique(培养人数去重), course_signs_pass(课程培养人数), start_time(开始时间), end_time(结束时间)"), * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="认证token"), * @OA\Response( * response="200", @@ -122,7 +124,39 @@ class CalendarsController extends BaseController */ public function save() { - return parent::save(); + $all = \request()->all(); + DB::beginTransaction(); + try { + if (isset($all['id'])) { + $model = $this->model->find($all['id']); + if (empty($model)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); + } + } else { + $model = $this->model; + $all['admin_id'] = $this->getUserId(); + $all['department_id'] = $this->getUser()->department_id; + } + $original = $model->getOriginal(); + $model->fill($all); + $model->save(); + + // 处理历史课程数据 + if (isset($all['history_courses']) && is_array($all['history_courses'])) { + // 删除原有的历史课程数据 + $model->historyCourses()->delete(); + $model->historyCourses()->createMany($all['history_courses']); + } + DB::commit(); + // 记录日志 + $this->saveLogs($original, $model); + // 返回带有历史课程的数据 + $model->load('historyCourses'); + return $this->success($model); + } catch (\Exception $exception) { + DB::rollBack(); + return $this->fail([$exception->getCode(), $exception->getMessage()]); + } } /** @@ -141,7 +175,33 @@ class CalendarsController extends BaseController */ public function destroy() { - return parent::destroy(); + $all = \request()->all(); + $messages = [ + 'id.required' => 'Id必填', + ]; + $validator = Validator::make($all, [ + 'id' => 'required' + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + + DB::beginTransaction(); + try { + $model = $this->model->find($all['id']); + if (empty($model)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); + } + // 删除关联的历史课程数据 + $model->historyCourses()->delete(); + // 删除日历 + $model->delete(); + DB::commit(); + return $this->success([]); + } catch (\Exception $exception) { + DB::rollBack(); + return $this->fail([$exception->getCode(), $exception->getMessage()]); + } } } diff --git a/app/Models/Calendar.php b/app/Models/Calendar.php index 172b6ba..9cde227 100755 --- a/app/Models/Calendar.php +++ b/app/Models/Calendar.php @@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Cache; class Calendar extends SoftDeletesModel { - protected $appends = ['is_publish_text','type_text']; + protected $appends = ['is_publish_text', 'type_text', 'is_count_days_text', 'is_count_people_text']; public function getIsPublishTextAttribute() { @@ -22,6 +22,16 @@ class Calendar extends SoftDeletesModel return $array[$this->attributes['type']] ?? ''; } + public function getIsCountDaysTextAttribute() + { + return ($this->attributes['is_count_days'] ?? 1) == 1 ? '是' : '否'; + } + + public function getIsCountPeopleTextAttribute() + { + return ($this->attributes['is_count_people'] ?? 1) == 1 ? '是' : '否'; + } + public function course() { return $this->hasOne(Course::class, 'id', 'course_id'); @@ -32,5 +42,10 @@ class Calendar extends SoftDeletesModel return $this->hasMany(CourseContent::class, 'id', 'course_content_id'); } + public function historyCourses() + { + return $this->hasMany(HistoryCourse::class, 'calendar_id', 'id'); + } + } diff --git a/app/Models/HistoryCourse.php b/app/Models/HistoryCourse.php index 4eb14bf..8c5a203 100644 --- a/app/Models/HistoryCourse.php +++ b/app/Models/HistoryCourse.php @@ -9,5 +9,10 @@ class HistoryCourse extends SoftDeletesModel { return $this->hasOne(CourseType::class, 'id', 'type'); } + + public function calendar() + { + return $this->belongsTo(Calendar::class, 'calendar_id', 'id'); + } } diff --git a/database/migrations/2025_11_26_100000_add_calendar_id_to_history_courses_table.php b/database/migrations/2025_11_26_100000_add_calendar_id_to_history_courses_table.php new file mode 100644 index 0000000..20aafa5 --- /dev/null +++ b/database/migrations/2025_11_26_100000_add_calendar_id_to_history_courses_table.php @@ -0,0 +1,34 @@ +unsignedBigInteger('calendar_id')->nullable()->after('id')->comment('日历ID'); + $table->index('calendar_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('history_courses', function (Blueprint $table) { + $table->dropIndex(['calendar_id']); + $table->dropColumn('calendar_id'); + }); + } +}; + diff --git a/database/migrations/2025_11_26_100100_add_count_fields_to_calendars_table.php b/database/migrations/2025_11_26_100100_add_count_fields_to_calendars_table.php new file mode 100644 index 0000000..fe1df14 --- /dev/null +++ b/database/migrations/2025_11_26_100100_add_count_fields_to_calendars_table.php @@ -0,0 +1,33 @@ +tinyInteger('is_count_days')->default(1)->comment('是否统计天数 0否 1是'); + $table->tinyInteger('is_count_people')->default(1)->comment('是否统计人数 0否 1是'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('calendars', function (Blueprint $table) { + $table->dropColumn(['is_count_days', 'is_count_people']); + }); + } +}; +