master
cody 3 weeks ago
parent f402975d2b
commit 979859f8da

@ -279,20 +279,37 @@ class ScheduleOverviewController extends CommonController
public function scheduleSave() public function scheduleSave()
{ {
$all = request()->all(); $all = request()->all();
$validator = Validator::make($all, [ $monthValue = $all['month'] ?? null;
$monthList = is_array($monthValue) ? $monthValue : [$monthValue];
$rules = [
'year' => 'required|string|size:4', 'year' => 'required|string|size:4',
'system_id' => 'required|integer', 'system_id' => 'required|integer',
'course_id' => 'required|integer', 'course_id' => 'required|integer',
'month' => 'required|integer|min:1|max:12',
'title' => 'required|string|max:255', 'title' => 'required|string|max:255',
'owner' => 'required|string|max:255', 'owner' => 'required|string|max:255',
'location' => 'required|string|max:255', 'location' => 'required|string|max:255',
'count_text' => 'nullable|string|max:255', 'count_text' => 'nullable|string|max:255',
], [ ];
if (!empty($all['id'])) {
$rules['month'] = 'required|integer|between:1,12';
} else {
$rules['month'] = 'required|array|min:1';
$rules['month.*'] = 'required|integer|between:1,12';
}
$validator = Validator::make($all, $rules, [
'year.required' => '年份必填', 'year.required' => '年份必填',
'system_id.required' => '体系必填', 'system_id.required' => '体系必填',
'course_id.required' => '课程必填', 'course_id.required' => '课程必填',
'month.required' => '月份必填', 'month.required' => '月份必填',
'month.array' => '月份格式不正确',
'month.min' => '请至少选择一个月份',
'month.integer' => '月份格式不正确',
'month.between' => '月份范围需在1到12之间',
'month.*.required' => '月份必填',
'month.*.integer' => '月份格式不正确',
'month.*.between' => '月份范围需在1到12之间',
'title.required' => '编排标题必填', 'title.required' => '编排标题必填',
'owner.required' => '负责人必填', 'owner.required' => '负责人必填',
'location.required' => '地点必填', 'location.required' => '地点必填',
@ -302,6 +319,16 @@ class ScheduleOverviewController extends CommonController
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
} }
if (empty($monthList)) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '月份必填']);
}
foreach ($monthList as $month) {
if (!is_numeric($month) || (int) $month < 1 || (int) $month > 12) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '月份范围需在1到12之间']);
}
}
$system = $this->systemModel->find($all['system_id']); $system = $this->systemModel->find($all['system_id']);
if (empty($system)) { if (empty($system)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']); return $this->fail([ResponseCode::ERROR_BUSINESS, '体系不存在']);
@ -333,15 +360,33 @@ class ScheduleOverviewController extends CommonController
DB::beginTransaction(); DB::beginTransaction();
try { try {
if (!empty($all['id'])) {
$model->year = $all['year']; $model->year = $all['year'];
$model->system_id = (int) $all['system_id']; $model->system_id = (int) $all['system_id'];
$model->course_id = (int) $all['course_id']; $model->course_id = (int) $all['course_id'];
$model->month = (int) $all['month']; $model->month = (int) $monthList[0];
$model->title = $all['title']; $model->title = $all['title'];
$model->owner = $all['owner']; $model->owner = $all['owner'];
$model->location = $all['location']; $model->location = $all['location'];
$model->count_text = $all['count_text'] ?? ''; $model->count_text = $all['count_text'] ?? '';
$model->save(); $model->save();
} else {
foreach ($monthList as $month) {
$schedule = new ScheduleOverviewSchedule();
$schedule->admin_id = $this->getUserId();
$schedule->department_id = optional($this->getUser())->department_id;
$schedule->year = $all['year'];
$schedule->system_id = (int) $all['system_id'];
$schedule->course_id = (int) $all['course_id'];
$schedule->month = (int) $month;
$schedule->title = $all['title'];
$schedule->owner = $all['owner'];
$schedule->location = $all['location'];
$schedule->count_text = $all['count_text'] ?? '';
$schedule->save();
$model = $schedule;
}
}
DB::commit(); DB::commit();
return $this->success($model->load(['system', 'course'])); return $this->success($model->load(['system', 'course']));

Loading…
Cancel
Save