You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
5.6 KiB
206 lines
5.6 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Niucloud-admin 企业快速开发的多应用管理平台
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\api\controller\student;
|
|
|
|
use app\service\api\student\CourseBookingService;
|
|
use core\base\BaseController;
|
|
use think\Response;
|
|
|
|
/**
|
|
* 课程预约控制器
|
|
*/
|
|
class CourseBookingController extends BaseController
|
|
{
|
|
/**
|
|
* 获取可预约课程列表
|
|
* @param int $student_id
|
|
* @return Response
|
|
*/
|
|
public function getAvailableCourses($student_id)
|
|
{
|
|
$data = $this->request->params([
|
|
['date', ''],
|
|
['start_date', ''],
|
|
['end_date', ''],
|
|
['coach_id', ''],
|
|
['venue_id', ''],
|
|
['course_type', ''],
|
|
['page', 1],
|
|
['limit', 20]
|
|
]);
|
|
$data['student_id'] = $student_id;
|
|
|
|
// 验证student_id参数
|
|
if (empty($student_id) || !is_numeric($student_id) || $student_id <= 0) {
|
|
return fail('学员ID参数无效');
|
|
}
|
|
|
|
// 清理"undefined"参数
|
|
foreach (['date', 'start_date', 'end_date', 'coach_id', 'venue_id', 'course_type'] as $field) {
|
|
if (isset($data[$field]) && ($data[$field] === 'undefined' || $data[$field] === null)) {
|
|
$data[$field] = '';
|
|
}
|
|
}
|
|
|
|
// 构建验证规则,只对非空值进行验证
|
|
$rules = [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'page' => 'integer|egt:1',
|
|
'limit' => 'integer|between:1,50'
|
|
];
|
|
|
|
// 只对非空的日期字段进行验证
|
|
if (!empty($data['date'])) {
|
|
$rules['date'] = 'date';
|
|
}
|
|
if (!empty($data['start_date'])) {
|
|
$rules['start_date'] = 'date';
|
|
}
|
|
if (!empty($data['end_date'])) {
|
|
$rules['end_date'] = 'date';
|
|
}
|
|
|
|
$this->validate($data, $rules);
|
|
|
|
try {
|
|
$service = new CourseBookingService();
|
|
$result = $service->getAvailableCourses($data);
|
|
|
|
return success($result, '获取可预约课程成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建课程预约
|
|
* @return Response
|
|
*/
|
|
public function createBooking()
|
|
{
|
|
$data = $this->request->params([
|
|
['student_id', 0],
|
|
['schedule_id', 0],
|
|
['course_date', ''],
|
|
['time_slot', ''],
|
|
['remark', '']
|
|
]);
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'schedule_id' => 'require|integer|gt:0',
|
|
'course_date' => 'require|date',
|
|
'time_slot' => 'require|length:1,50'
|
|
]);
|
|
|
|
try {
|
|
$service = new CourseBookingService();
|
|
$result = $service->createBooking($data);
|
|
|
|
return success($result, '预约成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取我的预约列表
|
|
* @param int $student_id
|
|
* @return Response
|
|
*/
|
|
public function getMyBookingList($student_id)
|
|
{
|
|
$data = $this->request->params([
|
|
['status', ''],
|
|
['start_date', ''],
|
|
['end_date', ''],
|
|
['page', 1],
|
|
['limit', 20]
|
|
]);
|
|
$data['student_id'] = $student_id;
|
|
|
|
// 验证student_id参数
|
|
if (empty($student_id) || !is_numeric($student_id) || $student_id <= 0) {
|
|
return fail('学员ID参数无效');
|
|
}
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'start_date' => 'date',
|
|
'end_date' => 'date',
|
|
'page' => 'integer|egt:1',
|
|
'limit' => 'integer|between:1,50'
|
|
]);
|
|
|
|
try {
|
|
$service = new CourseBookingService();
|
|
$result = $service->getMyBookingList($data);
|
|
|
|
return success($result, '获取预约列表成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 取消课程预约
|
|
* @return Response
|
|
*/
|
|
public function cancelBooking()
|
|
{
|
|
$data = $this->request->params([
|
|
['booking_id', 0],
|
|
['cancel_reason', '']
|
|
]);
|
|
|
|
$this->validate($data, [
|
|
'booking_id' => 'require|integer|gt:0',
|
|
'cancel_reason' => 'length:0,255'
|
|
]);
|
|
|
|
try {
|
|
$service = new CourseBookingService();
|
|
$result = $service->cancelBooking($data);
|
|
|
|
return success($result, '取消预约成功');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查预约冲突
|
|
* @return Response
|
|
*/
|
|
public function checkBookingConflict()
|
|
{
|
|
$data = $this->request->params([
|
|
['student_id', 0],
|
|
['course_date', ''],
|
|
['time_slot', '']
|
|
]);
|
|
|
|
$this->validate($data, [
|
|
'student_id' => 'require|integer|gt:0',
|
|
'course_date' => 'require|date',
|
|
'time_slot' => 'require|length:1,50'
|
|
]);
|
|
|
|
try {
|
|
$service = new CourseBookingService();
|
|
$result = $service->checkBookingConflict($data);
|
|
|
|
return success($result, '检查完成');
|
|
|
|
} catch (\Exception $e) {
|
|
return fail($e->getMessage());
|
|
}
|
|
}
|
|
}
|