diff --git a/app/Controller/OrderServicePackageController.php b/app/Controller/OrderServicePackageController.php index ec51334..15f3712 100644 --- a/app/Controller/OrderServicePackageController.php +++ b/app/Controller/OrderServicePackageController.php @@ -61,5 +61,4 @@ class OrderServicePackageController extends AbstractController $data = $OrderServicePackageService->addServiceInquiryOrder(); return $this->response->json($data); } - } \ No newline at end of file diff --git a/app/Controller/PatientOrderController.php b/app/Controller/PatientOrderController.php index a7c1523..76c0e15 100644 --- a/app/Controller/PatientOrderController.php +++ b/app/Controller/PatientOrderController.php @@ -297,5 +297,36 @@ class PatientOrderController extends AbstractController return $this->response->json($data); } + /** + * 服务包订单取消支付-1未支付 + * @return ResponseInterface + */ + public function putPatientServiceOrderCancelPay(): ResponseInterface + { + $PatientOrderService = new PatientOrderService(); + $data = $PatientOrderService->putPatientServiceOrderCancelPay(); + return $this->response->json($data); + } + /** + * 订单取消支付-1未支付 + * @return ResponseInterface + */ + public function putPatientOrderCancelPay(): ResponseInterface + { + $PatientOrderService = new PatientOrderService(); + $data = $PatientOrderService->putPatientOrderCancelPay(); + return $this->response->json($data); + } + + /** + * 取消订单-问诊/检测/服务包 + * @return ResponseInterface + */ + public function putCancelPatientOrder(): ResponseInterface + { + $PatientOrderService = new PatientOrderService(); + $data = $PatientOrderService->putCancelPatientOrder(); + return $this->response->json($data); + } } \ No newline at end of file diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 5d81e2a..54706c3 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Constants\HttpEnumCode; use App\Exception\BusinessException; use App\Model\Order; use App\Model\OrderCoupon; @@ -14,6 +15,7 @@ use App\Model\OrderProduct; use App\Model\OrderProductRefund; use App\Model\OrderRefund; use App\Model\OrderServicePackage; +use App\Model\OrderServicePackageInquiry; use App\Model\OrderServicePackageRefund; use App\Utils\Log; use Extend\Wechat\WechatPay; @@ -729,4 +731,273 @@ class OrderService extends BaseService throw new BusinessException("订单支付状态错误"); } } + + /** + * 用户主动取消订单 + * @param string|int $order_no + * @return array + */ + public function PatientActiveCancelOrder(string|int $order_no): array + { + try { + $result = array(); + $result['status'] = 1; + $result['message'] = "成功"; + + // 获取订单数据 + $params = array(); + $params['order_no'] = $order_no; + $order = Order::getOne($params); + if (empty($order)) { + $result['status'] = 0; + $result['message'] = "未查询到对应订单数据"; + return $result; + } + + // 检测订单状态 + if ($order['cancel_status'] == 1) { + // 取消状态(0:否 1:是) + $result['status'] = 0; + $result['message'] = "订单已取消"; + return $result; + } + + if (!in_array($order['refund_status'], [0, 4, 5])) { + // 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + $result['status'] = 0; + $result['message'] = "订单正在退款中"; + return $result; + } + + // 设置锁 + $redis = $this->container->get(Redis::class); + $redis_key = "active_cancel_order_lock_" . $order_no; + $redis_lock = $redis->setnx($redis_key, 1); + if (!$redis_lock) { + // 设置失败,表示已经设置该值 + $result['status'] = 0; + $result['message'] = "请稍后再试"; + return $result; + } + + // 设置过期时间 + $redis->expire($redis_key, 3); + + // 修改订单为取消 + $data = array(); + $data['cancel_status'] = 1; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_remarks'] = "主动取消"; // 取消订单备注 + $data['updated_at'] = date("Y-m-d H:i:s", time()); + + $params = array(); + $params['order_no'] = $order_no; + Order::edit($params, $data); + + // 处理对应订单 + switch ($order['order_type']) { + case 1: // 问诊订单 + // 获取订单数据 + $params = array(); + $params['inquiry_no'] = $order_no; + $order_inquiry = OrderInquiry::getOne($params); + if (empty($order_inquiry)) { + $result['status'] = 0; + $result['message'] = "订单数据为空"; + return $result; + } + + // 检测订单状态 + if (!in_array($order_inquiry['inquiry_status'], [1, 2, 3])) { + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + $result['status'] = 0; + $result['message'] = "订单无法取消"; + return $result; + } + + if ($order_inquiry['inquiry_refund_status'] == 1) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单申请退款中,请您稍后取消"; + return $result; + } + + if ($order_inquiry['inquiry_refund_status'] == 2) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单正在退款中,请您稍后取消"; + return $result; + } + + // 修改问诊订单为取消 + $data = array(); + $data['inquiry_status'] = 7; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_reason'] = 2; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时) + $data['updated_at'] = date("Y-m-d H:i:s", time()); + + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + OrderInquiry::edit($params, $data); + + break; + case 3: // 检测订单 + // 获取订单数据 + $params = array(); + $params['detection_no'] = $order_no; + $order_detection = OrderDetection::getOne($params); + if (empty($order_detection)) { + $result['status'] = 0; + $result['message'] = "订单数据为空"; + return $result; + } + + // 检测订单状态 + if (!in_array($order_detection['detection_status'], [2])) { + // 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消) + $result['status'] = 0; + $result['message'] = "订单无法取消"; + return $result; + } + + if ($order_detection['detection_refund_status'] == 1) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单申请退款中,请您稍后取消"; + return $result; + } + + if ($order_detection['detection_refund_status'] == 2) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单正在退款中,请您稍后取消"; + return $result; + } + + // 修改检测订单为取消 + $data = array(); + $data['detection_status'] = 5; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_reason'] = 1; // 取消订单原因(1:主动取消 2:客服取消 3:支付超时) + $data['updated_at'] = date("Y-m-d H:i:s", time()); + + $params = array(); + $params['order_detection_id'] = $order_detection['order_detection_id']; + OrderDetection::editOrderDetection($params, $data); + + break; + case 4: // 服务包订单 + // 获取订单数据 + $params = array(); + $params['order_service_no'] = $order_no; + $order_service_package = OrderServicePackage::getOne($params); + if (empty($order_service_package)) { + $result['status'] = 0; + $result['message'] = "订单数据为空"; + return $result; + } + + // 检测订单状态 + if (!in_array($order_service_package['order_service_status'], [2])) { + // 订单状态(1:待支付 2:未开始 3:服务中 4:服务完成 5:服务取消) + $result['status'] = 0; + $result['message'] = "订单无法取消"; + return $result; + } + + if ($order_service_package['refund_status'] == 1) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单申请退款中,请您稍后取消"; + return $result; + } + + if ($order_service_package['refund_status'] == 2) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "订单正在退款中,请您稍后取消"; + return $result; + } + + // 修改服务包订单为取消 + $data = array(); + $data['order_service_status'] = 5; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_remarks'] = "主动取消"; + $data['updated_at'] = date("Y-m-d H:i:s", time()); + + $params = array(); + $params['order_service_id'] = $order_service_package['order_service_id']; + OrderServicePackage::edit($params, $data); + + // 获取问诊订单数据-此处只会存在一个 + $params = array(); + $params['order_service_id'] = $order_service_package['order_service_id']; + $order_service_package_inquiry = OrderServicePackageInquiry::getOne($params); + if (empty($order_service_package_inquiry)){ + $result['status'] = 0; + $result['message'] = "取消失败"; + return $result; + } + + // 检测问诊订单数据 + // 获取订单数据 + $params = array(); + $params['inquiry_no'] = $order_service_package_inquiry['inquiry_no']; + $order_inquiry = OrderInquiry::getOne($params); + if (empty($order_inquiry)) { + $result['status'] = 0; + $result['message'] = "订单数据为空"; + return $result; + } + + // 检测订单状态 + if (!in_array($order_inquiry['inquiry_status'], [4, 5, 6])) { + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + $result['status'] = 0; + $result['message'] = "已存在问诊订单,无法手动取消"; + return $result; + } + + if ($order_inquiry['inquiry_refund_status'] == 1) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "问诊订单申请退款中,请您稍后取消"; + return $result; + } + + if ($order_inquiry['inquiry_refund_status'] == 2) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + $result['status'] = 0; + $result['message'] = "问诊订单正在退款中,请您稍后取消"; + return $result; + } + + // 修改问诊订单为取消 + $data = array(); + $data['inquiry_status'] = 7; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_reason'] = 2; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时) + $data['updated_at'] = date("Y-m-d H:i:s", time()); + + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + OrderInquiry::edit($params, $data); + + break; + default: + throw new BusinessException("订单类型错误"); + } + + // 删除锁 + $redis->del($redis_key); + + } catch (\Throwable $e) { + $result['status'] = 0; + $result['message'] = $e->getMessage(); + } + + return $result; + } + } \ No newline at end of file diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index bb36102..b27367b 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -2394,6 +2394,35 @@ class PatientOrderService extends BaseService return success(); } + /** + * 取消订单-问诊/检测/服务包 + * @return array + */ + public function putCancelPatientOrder(): array + { + $user_info = $this->request->getAttribute("userInfo") ?? []; + + $order_no = $this->request->route('order_no'); + + Db::beginTransaction(); + + try { + $OrderService = new OrderService(); + $result = $OrderService->PatientActiveCancelOrder($order_no); + if ($result['status'] != 1) { + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, $result['message']); + } + + Db::commit(); + } catch (\Exception $e) { + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); + } + + return success(); + } + /** * 获取患者未完成订单 * @param string $patient_id diff --git a/config/routes.php b/config/routes.php index a84ed7f..cf97776 100644 --- a/config/routes.php +++ b/config/routes.php @@ -472,6 +472,7 @@ Router::addGroup('/patient', function () { // 订单 Router::addGroup('/order', function () { + // 问诊订单 Router::addGroup('/inquiry', function () { // 获取患者问诊订单列表 Router::get('', [PatientOrderController::class, 'getPatientInquiryOrderList']); @@ -543,11 +544,23 @@ Router::addGroup('/patient', function () { Router::put('/cancel-pay/{order_detection_id:\d+}', [PatientOrderController::class, 'putPatientDetectionOrderCancelPay']); }); + // 服务包订单 + Router::addGroup('/service', function () { + // 服务包订单取消支付-1未支付 + Router::put('/cancel-pay/{order_no}', [PatientOrderController::class, 'putPatientServiceOrderCancelPay']); + }); + // 获取患者订单支付数据 Router::get('/pay', [PatientOrderController::class, 'getPatientOrderPayInfo']); // 模拟支付成功-金额为0时使用 Router::post('/pay', [PatientOrderController::class, 'addPatientOrderPay']); + + // 订单取消支付-1未支付 + Router::put('/cancel-pay/{order_no}', [PatientOrderController::class, 'putPatientOrderCancelPay']); + + // 取消订单-问诊/检测/服务包 + Router::put('/cancel/{order_no}', [PatientOrderController::class, 'putCancelPatientOrder']); }); // 消息通知 @@ -590,6 +603,9 @@ Router::addGroup('/patient', function () { // 创建服务包问诊订单 Router::post('/inquiry/{order_no}', [OrderServicePackageController::class, 'addServiceInquiryOrder']); + + // 服务包订单取消支付-1未支付 + Router::put('/cancel-pay/{order_no:\d+}', [OrderServicePackageController::class, 'putPatientServiceOrderCancelPay']); }); });