|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\API\H5;
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use Illuminate\Http\Request;
|
|
|
use App\Lib\Rs;
|
|
|
use App\Services\PatientAuthService;
|
|
|
use App\Services\PlanService;
|
|
|
use App\Services\AppointmentService;
|
|
|
use App\Services\BlockService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class AppointmentController extends Controller
|
|
|
{
|
|
|
private PatientAuthService $patientAuthService;
|
|
|
private AppointmentService $appointmentService;
|
|
|
|
|
|
public function __construct(
|
|
|
PatientAuthService $patientAuthService,
|
|
|
AppointmentService $appointmentService
|
|
|
) {
|
|
|
$this->patientAuthService = $patientAuthService;
|
|
|
$this->appointmentService = $appointmentService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建预约
|
|
|
* POST /api/h5/appointment/create
|
|
|
*/
|
|
|
public function create(Request $request)
|
|
|
{
|
|
|
$payload = $request->attributes->get('payload');
|
|
|
$idCardNo = $this->patientAuthService->getPatientIdCardNo($payload);
|
|
|
|
|
|
$applicationIds = $request->input('application_ids');
|
|
|
$planId = $request->input('plan_id');
|
|
|
$channelId = (int)$request->input('channel_id', 3); // 默认微信,自助机传4
|
|
|
|
|
|
if (empty($applicationIds) || !is_array($applicationIds)) {
|
|
|
return Rs::error('请选择要预约的申请单');
|
|
|
}
|
|
|
if (empty($planId)) {
|
|
|
return Rs::error('请选择号源');
|
|
|
}
|
|
|
|
|
|
// 校验:申请单必须属于当前患者
|
|
|
$count = DB::table('exam_application')
|
|
|
->whereIn('id', $applicationIds)
|
|
|
->where('patient_id_card_no', $idCardNo)
|
|
|
->where('status', 0)
|
|
|
->count();
|
|
|
|
|
|
if ($count != count($applicationIds)) {
|
|
|
return Rs::error('部分申请单不存在或状态不符合');
|
|
|
}
|
|
|
|
|
|
$result = $this->appointmentService->createAppointment(
|
|
|
0, // 患者自助,无系统用户ID
|
|
|
$applicationIds,
|
|
|
$channelId,
|
|
|
(int)$planId
|
|
|
);
|
|
|
|
|
|
if ($result['success']) {
|
|
|
return Rs::success($result['data'] ?? null, $result['message']);
|
|
|
} else {
|
|
|
$data = $result['data'] ?? [];
|
|
|
$data['conflicts'] = $result['conflicts'] ?? [];
|
|
|
return Rs::error($result['message'], 400, $data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消预约
|
|
|
* POST /api/h5/appointment/cancel
|
|
|
*/
|
|
|
public function cancel(Request $request)
|
|
|
{
|
|
|
$payload = $request->attributes->get('payload');
|
|
|
$idCardNo = $this->patientAuthService->getPatientIdCardNo($payload);
|
|
|
|
|
|
$appointmentId = $request->input('appointment_id');
|
|
|
$cancelReason = $request->input('cancel_reason', '患者主动取消');
|
|
|
|
|
|
if (empty($appointmentId)) {
|
|
|
return Rs::error('请提供预约ID');
|
|
|
}
|
|
|
|
|
|
// 校验:预约必须属于当前患者
|
|
|
$appointment = DB::table('exam_appointment as ea')
|
|
|
->leftJoin('exam_application as eapp', 'ea.exam_application_id', '=', 'eapp.id')
|
|
|
->where('ea.id', $appointmentId)
|
|
|
->where('eapp.patient_id_card_no', $idCardNo)
|
|
|
->where('ea.status', 1)
|
|
|
->first();
|
|
|
|
|
|
if (!$appointment) {
|
|
|
return Rs::error('预约记录不存在或无权操作');
|
|
|
}
|
|
|
|
|
|
$result = $this->appointmentService->cancelAppointment(0, (int)$appointmentId, $cancelReason);
|
|
|
|
|
|
if ($result['success']) {
|
|
|
return Rs::success(null, $result['message']);
|
|
|
}
|
|
|
return Rs::error($result['message']);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 改约
|
|
|
* POST /api/h5/appointment/reschedule
|
|
|
*/
|
|
|
public function reschedule(Request $request)
|
|
|
{
|
|
|
$payload = $request->attributes->get('payload');
|
|
|
$idCardNo = $this->patientAuthService->getPatientIdCardNo($payload);
|
|
|
|
|
|
$appointmentId = $request->input('appointment_id');
|
|
|
$cancelReason = $request->input('cancel_reason', '患者改约');
|
|
|
$newPlanId = $request->input('plan_id');
|
|
|
|
|
|
if (empty($appointmentId)) {
|
|
|
return Rs::error('请提供原预约ID');
|
|
|
}
|
|
|
if (empty($newPlanId)) {
|
|
|
return Rs::error('请选择新号源');
|
|
|
}
|
|
|
|
|
|
// 校验:预约必须属于当前患者
|
|
|
$appointment = DB::table('exam_appointment as ea')
|
|
|
->leftJoin('exam_application as eapp', 'ea.exam_application_id', '=', 'eapp.id')
|
|
|
->where('ea.id', $appointmentId)
|
|
|
->where('eapp.patient_id_card_no', $idCardNo)
|
|
|
->where('ea.status', 1)
|
|
|
->first();
|
|
|
|
|
|
if (!$appointment) {
|
|
|
return Rs::error('预约记录不存在或无权操作');
|
|
|
}
|
|
|
|
|
|
$result = $this->appointmentService->rescheduleAppointment(
|
|
|
0,
|
|
|
(int)$appointmentId,
|
|
|
$cancelReason,
|
|
|
(int)$newPlanId
|
|
|
);
|
|
|
|
|
|
if ($result['success']) {
|
|
|
return Rs::success($result['data'] ?? null, $result['message']);
|
|
|
} else {
|
|
|
$data = $result['data'] ?? [];
|
|
|
$data['conflicts'] = $result['conflicts'] ?? [];
|
|
|
return Rs::error($result['message'], 400, $data);
|
|
|
}
|
|
|
}
|
|
|
}
|