Files
hospital-applets-api/app/Services/OrderService.php
T
2024-04-12 17:00:38 +08:00

1003 lines
43 KiB
PHP

<?php
namespace App\Services;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\Order;
use App\Model\OrderCoupon;
use App\Model\OrderDetection;
use App\Model\OrderDetectionRefund;
use App\Model\OrderInquiry;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund;
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;
use Hyperf\Amqp\Result;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Snowflake\IdGeneratorInterface;
/**
* 订单处理
*/
class OrderService extends BaseService
{
/**
* 取消未支付的订单
* @param string|int $order_no
* @param string|int $cancel_reason
* @param string|int $cancel_remarks
* @return array
*/
public function cancelUnpayOrder(string|int $order_no, string|int $cancel_reason, string|int $cancel_remarks): 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;
}
if ($order['pay_status'] == 2) {
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
$result['status'] = 0;
$result['message'] = "订单已支付";
return $result;
}
$data = array();
if ($cancel_remarks == "支付超时") {
$data['pay_status'] = 5;
}
$data['cancel_status'] = 1;
$data['cancel_time'] = date("Y-m-d H:i:s", time());
$data['cancel_remarks'] = $cancel_remarks; // 取消订单备注
$data['updated_at'] = date("Y-m-d H:i:s", time());
$params = array();
$params['order_id'] = $order['order_id'];
Order::edit($params, $data);
// 取消对应订单
if ($order['order_type'] == 1) {
// 问诊订单
$InquiryService = new InquiryService();
$result = $InquiryService->cancelUnpayInquiryOrder($order['order_no'], $cancel_reason, $cancel_remarks);
} elseif ($order['order_type'] == 2) {
// 药品订单
$OrderProductService = new OrderProductService();
$result = $OrderProductService->cancelUnpayProductOrder($order['order_no'], $cancel_reason, $cancel_remarks);
} elseif ($order['order_type'] == 3) {
// 检测订单
$DetectionService = new DetectionService();
$result = $DetectionService->cancelUnpayDetectionOrder($order['order_no'], $cancel_reason, $cancel_remarks);
} elseif ($order['order_type'] == 4 ||$order['order_type'] == 5 ) {
// 服务包订单
$OrderServicePackageService = new OrderServicePackageService();
$result = $OrderServicePackageService->cancelUnpayServiceOrder($order['order_no'], $cancel_reason, $cancel_remarks);
} else{
$result['status'] = 0;
$result['message'] = "订单类型错误";
}
} catch (\Throwable $e) {
$result['status'] = 0;
$result['message'] = $e->getMessage();
}
return $result;
}
/**
* 发起订单退款
* @param string $order_no
* @param string $refund_reason
* @return void
*/
public function orderRefund(string $order_no, string $refund_reason): void
{
// 检测参数
try {
// 获取订单数据
$params = array();
$params['order_no'] = $order_no;
$order = Order::getOne($params);
if (empty($order)) {
throw new BusinessException("未查询到对应订单数据");
}
// 检测订单退款状态
if (in_array($order['refund_status'], [2, 3, 5])) {
// 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单退款状态错误");
}
// 检测支付状态
if ($order['pay_status'] != 2) {
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
throw new BusinessException("订单支付状态错误");
}
// 取消订单
$order_data = array();
$order_data['cancel_status'] = 1;
$order_data['cancel_time'] = date("Y-m-d H:i:s", time());
$order_data['cancel_remarks'] = "医生未接诊";
$order_data['updated_at'] = date("Y-m-d H:i:s", time());
$params = array();
$params['order_id'] = $order['order_id'];
Order::edit($params, $order_data);
// 处理对应订单
if ($order['order_type'] == 1) {
// 检测问诊订单可退款状态
$this->checkOrderInquiryStatusForRefund($order['order_no']);
} elseif ($order['order_type'] == 2) {
// 检测药品订单可退款状态
$this->checkOrderProductStatusForRefund($order['order_no']);
} elseif ($order['order_type'] == 3) {
// 检测检测订单可退款状态
$this->checkOrderDetectionStatusForRefund($order['order_no']);
} elseif ($order['order_type'] == 4 || $order['order_type'] == 5) {
// 检测服务包订单可退款状态
$this->checkOrderServiceStatusForRefund($order['order_no']);
} else {
throw new BusinessException("订单类型错误");
}
}catch (\Throwable $e){
throw new BusinessException($e->getMessage());
}
try {
// 系统退款编号
$generator = $this->container->get(IdGeneratorInterface::class);
$refund_no = $generator->generate();
// 检测订单金额
if ($order['payment_amount_total'] > 0) {
// 发起退款
$WechatPay = new WechatPay(1, $order['order_type']);
$options = array();
$options['transaction_id'] = $order['escrow_trade_no'];
$options['out_refund_no'] = (string)$refund_no;
$options['reason'] = $refund_reason;
$options['amount'] = [
'refund' => (int)round($order['payment_amount_total'] * 100),
'total' => (int)round($order['payment_amount_total'] * 100),
'currency' => "CNY",
];
$refund_result = $WechatPay->refund($options);
// 处理订单退款状态
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
$success_time = "";
if ($refund_result['status'] == "SUCCESS") {
// 退款成功
$refund_status = 3;
$success_time = $refund_result['success_time'];
} elseif ($refund_result['status'] == "CLOSED") {
// 退款关闭
$refund_status = 5;
} elseif ($refund_result['status'] == "PROCESSING") {
// 退款处理中
$refund_status = 2;
} elseif ($refund_result['status'] == "ABNORMAL") {
// 退款异常,此情况不处理,进行短信通知
throw new BusinessException("订单退款状态异常");
} else {
throw new BusinessException("订单退款状态错误");
}
$refund_id = $refund_result['refund_id'];
} else {
// 药品订单不存在模拟退款
if ($order['order_type'] == 2){
throw new BusinessException("订单金额错误");
}
// 模拟退款
$refund_status = 3;
$generator = $this->container->get(IdGeneratorInterface::class);
$refund_id = "模拟退款:" . $generator->generate();
$success_time = date("Y-m-d H:i:s", time());
// 模拟退款时手动退还优惠卷
if (!empty($order['coupon_amount_total']) && $order['coupon_amount_total'] > 0) {
// 获取该订单全部优惠卷数据
$params = array();
$params['order_id'] = $order['order_id'];
$order_coupons = OrderCoupon::getList($params);
if (!empty($order_coupons)) {
$userCouponService = new UserCouponService();
foreach ($order_coupons as $order_coupon) {
// 退还优惠卷
$userCouponService->returnUserCoupon($order_coupon['user_coupon_id']);
// 发送站内消息-优惠卷退还
$MessagePush = new MessagePush($order_coupon['user_id']);
$MessagePush->patientRefundCoupon($order_coupon['coupon_name']);
}
}
}
}
// 新增退款表
$data = array();
$data['order_id'] = $order['order_id'];
$data['patient_id'] = $order['patient_id'];
$data['order_no'] = $order['order_no'];
$data['refund_no'] = $refund_no;
$data['inquiry_refund_no'] = $refund_no;
$data['refund_id'] = $refund_id;
$data['refund_status'] = $refund_status;
$data['refund_total'] = $order['payment_amount_total'];
$data['refund_reason'] = $refund_reason;
if ($refund_status == 3 && !empty($success_time)) {
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
}
$order_refund = OrderRefund::addOrderRefund($data);
if (empty($order_refund)) {
throw new BusinessException("添加退款表失败");
}
switch ($order['order_type']) {
case 1: // 问诊订单
// 获取订单数据
$params = array();
$params['inquiry_no'] = $order_no;
$order_inquiry = OrderInquiry::getOne($params);
if (empty($order_inquiry)) {
throw new BusinessException("订单数据为空");
}
// 新增退款表
$data = array();
$data['patient_id'] = $order['patient_id'];
$data['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
$data['inquiry_no'] = $order_inquiry['inquiry_no'];
$data['inquiry_refund_no'] = $refund_no;
$data['refund_id'] = $refund_id;
$data['inquiry_refund_status'] = $refund_status;
$data['refund_total'] = $order_inquiry['payment_amount_total'];
$data['refund_reason'] = $refund_reason;
if ($refund_status == 3 && !empty($success_time)) {
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
}
$order_inquiry_refund = OrderInquiryRefund::addOrderInquiryRefund($data);
if (empty($order_inquiry_refund)) {
throw new BusinessException("添加退款表失败");
}
// 修改问诊订单表状态
$data = array();
$data['inquiry_refund_status'] = $refund_status;
$params = array();
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
OrderInquiry::edit($params, $data);
break;
case 2: // 药品订单
// 获取药品订单数据
$params = array();
$params['order_product_no'] = $order['order_no'];
$order_product = OrderProduct::getOne($params);
if (empty($order_product)){
throw new BusinessException("订单数据为空");
}
$data = array();
$data['patient_id'] = $order_product['patient_id'];
$data['order_product_id'] = $order_product['order_product_id'];
$data['order_product_no'] = $order_product['order_product_no'];
$data['product_refund_no'] = $refund_no;
$data['refund_id'] = $refund_id;
$data['product_refund_status'] = $refund_status;
$data['refund_total'] = $order_product['payment_amount_total'];
$data['refund_reason'] = $refund_reason;
if ($refund_status == 3 && !empty($success_time)) {
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
}
$order_product_refund = OrderProductRefund::addOrderProductRefund($data);
if (empty($order_product_refund)) {
throw new BusinessException("添加退款表失败");
}
// 修改药品订单表状态
$data = array();
$data['refund_status'] = $refund_status;
$params = array();
$params['order_product_id'] = $order_product['order_product_id'];
OrderProduct::edit($params,$data);
break;
case 3: // 检测订单
// 获取订单数据
$params = array();
$params['detection_no'] = $order_no;
$order_detection = OrderDetection::getOne($params);
if (empty($order_detection)) {
throw new BusinessException("订单数据为空");
}
// 新增退款表
$data = array();
$data['patient_id'] = $order['patient_id'];
$data['order_detection_id'] = $order_detection['order_detection_id'];
$data['detection_no'] = $order_detection['detection_no'];
$data['detection_refund_no'] = $refund_no;
$data['refund_id'] = $refund_id;
$data['detection_refund_status'] = $refund_status;
$data['refund_total'] = $order_detection['payment_amount_total'];
$data['refund_reason'] = $refund_reason;
if ($refund_status == 3 && !empty($success_time)) {
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
}
$order_detection_refund = OrderDetectionRefund::add($data);
if (empty($order_detection_refund)) {
throw new BusinessException("添加退款表失败");
}
// 修改问诊订单表状态
$data = array();
$data['detection_refund_status'] = $refund_status;
$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)) {
throw new BusinessException("订单数据为空");
}
// 新增退款表
$data = array();
$data['patient_id'] = $order['patient_id'];
$data['order_service_id'] = $order_service_package['order_service_id'];
$data['order_service_no'] = $order_service_package['order_service_no'];
$data['service_refund_no'] = $refund_no;
$data['refund_id'] = $refund_id;
$data['detection_refund_status'] = $refund_status;
$data['refund_total'] = $order_service_package['payment_amount_total'];
$data['refund_reason'] = $refund_reason;
if ($refund_status == 3 && !empty($success_time)) {
$data['success_time'] = date("Y-m-d H:i:s", strtotime($success_time)); // 退款成功时间
}
$order_service_package_refund = OrderServicePackageRefund::addOrderServicePackageRefund($data);
if (empty($order_service_package_refund)) {
throw new BusinessException("添加退款表失败");
}
// 修改问诊订单表状态
$data = array();
$data['refund_status'] = $refund_status;
$params = array();
$params['order_service_id'] = $order_service_package['order_service_id'];
OrderServicePackage::edit($params, $data);
break;
default:
throw new BusinessException("订单类型错误");
}
} catch (\Throwable $e) {
throw new BusinessException($e->getMessage());
}
}
/**
* 处理订单退款回调结果
* @param string $order_no
* @param string|int $refund_status
* @param string $success_time
* @return void
*/
public function orderRefundResult(string $order_no,string|int $refund_status,string $success_time): void
{
// 获取订单数据
$params = array();
$params['order_no'] = $order_no;
$order = Order::getOne($params);
if (empty($order)) {
throw new BusinessException("未查询到对应订单数据");
}
// 退款状态
if ($refund_status == "SUCCESS") {
// 退款成功
$refund_status = 3;
} elseif ($refund_status == "CLOSED") {
// 退款关闭
$refund_status = 5;
} elseif ($refund_status == "ABNORMAL") {
// 退款异常
$refund_status = 6;
} else{
// 错误,无退款状态
throw new BusinessException("退款状态错误");
}
// 修改订单
$data = array();
$data['refund_status'] = $refund_status;
$params = array();
$params['order_no'] = $order['order_no'];
Order::edit($params,$data);
// 修改订单退款表
$data = array();
$data['refund_status'] = $refund_status;
$data['success_time'] = $success_time;
$params = array();
$params['order_no'] = $order['order_no'];
OrderRefund::edit($params, $data);
// 处理对应订单
switch ($order['order_type']) {
case 1: // 问诊订单
// 修改订单
$data = array();
$data['inquiry_refund_status'] = $refund_status;
$params = array();
$params['inquiry_no'] = $order['order_no'];
OrderInquiry::edit($params, $data);
// 修改退款订单
$data = array();
$data['inquiry_refund_status'] = $refund_status;
$data['success_time'] = $success_time;
$params = array();
$params['inquiry_no'] = $order['order_no'];
OrderInquiryRefund::edit($params, $data);
break;
case 2: // 药品订单
// 修改订单
$data = array();
$data['refund_status'] = $refund_status;
$params = array();
$params['order_product_no'] = $order['order_no'];
OrderProduct::edit($params, $data);
// 修改退款订单
$data = array();
$data['product_refund_status'] = $refund_status;
$data['success_time'] = $success_time;
$params = array();
$params['order_product_no'] = $order['order_no'];
OrderProductRefund::edit($params, $data);
break;
case 3: // 检测订单
// 修改订单
$data = array();
$data['detection_refund_status'] = $refund_status;
$params = array();
$params['detection_no'] = $order['order_no'];
OrderDetection::editOrderDetection($params, $data);
// 修改退款订单
$data = array();
$data['detection_refund_status'] = $refund_status;
$data['success_time'] = $success_time;
$params = array();
$params['detection_no'] = $order['order_no'];
OrderDetectionRefund::edit($params, $data);
break;
case 4: // 服务包订单
// 修改订单
$data = array();
$data['refund_status'] = $refund_status;
$params = array();
$params['order_service_no'] = $order['order_no'];
OrderServicePackage::edit($params, $data);
// 修改退款订单
$data = array();
$data['detection_refund_status'] = $refund_status;
$data['success_time'] = $success_time;
$params = array();
$params['order_service_no'] = $order['order_no'];
OrderServicePackageRefund::edit($params, $data);
break;
default:
throw new BusinessException("订单类型错误");
}
// 恢复优惠卷
if ($refund_status == 3) {
if (!empty($order['coupon_amount_total']) && $order['coupon_amount_total'] > 0) {
// 获取该订单全部优惠卷数据
$params = array();
$params['order_id'] = $order['order_id'];
$order_coupons = OrderCoupon::getList($params);
if (!empty($order_coupons)){
$userCouponService = new UserCouponService();
foreach ($order_coupons as $order_coupon){
// 退还优惠卷
$userCouponService->returnUserCoupon($order_coupon['user_coupon_id']);
try {
// 发送站内消息-优惠卷退还
$MessagePush = new MessagePush($order_coupon['user_id']);
$MessagePush->patientRefundCoupon($order_coupon['coupon_name']);
}catch (\Throwable $e){
// 此处不处理错误
Log::getInstance("CallBack-wxPayInquiryRefund")->error("推送消息失败:" . $e->getMessage());
}
}
}
}
}
}
/**
* 检测问诊订单可退款状态
* @param string $order_no
*/
public function checkOrderInquiryStatusForRefund(string $order_no): void
{
// 获取订单数据
$params = array();
$params['inquiry_no'] = $order_no;
$order_inquiry = OrderInquiry::getOne($params);
if (empty($order_inquiry)) {
throw new BusinessException("订单数据为空");
}
// 检测问诊订单状态
if (!in_array($order_inquiry['inquiry_status'], [2, 3, 4, 5, 7])) {
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
throw new BusinessException("订单状态错误");
}
// 检测订单退款状态
if (in_array($order_inquiry['inquiry_refund_status'], [2, 3, 5])) {
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
throw new BusinessException("订单退款状态错误");
}
// 检测支付状态
if ($order_inquiry['inquiry_pay_status'] != 2) {
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
throw new BusinessException("订单支付状态错误");
}
}
/**
* 检测药品订单可退款状态
* @param string $order_no
*/
public function checkOrderProductStatusForRefund(string $order_no): void
{
// 获取药品订单数据
$params = array();
$params['order_product_no'] = $order_no;
$order_product = OrderProduct::getOne($params);
if (empty($order_product)){
throw new BusinessException("订单数据为空");
}
// 检测药品订单数据状态
if ($order_product['order_product_status'] == 1){
throw new BusinessException("订单未支付");
}
if ($order_product['order_product_status'] == 4){
throw new BusinessException("订单已签收");
}
// 检测商品订单退款状态
if ($order_product['refund_status'] == 2){
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单退款中");
}
if ($order_product['refund_status'] == 3){
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单已退款成功");
}
if ($order_product['refund_status'] == 5){
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单退款关闭");
}
// 检测支付状态
if ($order_product['pay_status'] != 2){
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
throw new BusinessException("订单支付状态错误,无法退款");
}
}
/**
* 检测检测订单可退款状态
* @param string $order_no
*/
public function checkOrderDetectionStatusForRefund(string $order_no): void
{
// 获取订单数据
$params = array();
$params['detection_no'] = $order_no;
$order_detection = OrderDetection::getOne($params);
if (empty($order_detection)) {
throw new BusinessException("订单数据为空");
}
// 检测问诊订单状态
if (!in_array($order_detection['detection_status'], [2, 3])) {
// 检测订单状态(1:待支付 2:待绑定 3:检测中 4:检测完成 5:已取消)
throw new BusinessException("订单状态错误");
}
// 检测订单退款状态
if (in_array($order_detection['detection_refund_status'], [2, 3, 5, 6])) {
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单退款状态错误");
}
// 检测支付状态
if ($order_detection['detection_pay_status'] != 2) {
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
throw new BusinessException("订单支付状态错误");
}
}
/**
* 检测服务包订单可退款状态
* @param string $order_no
*/
public function checkOrderServiceStatusForRefund(string $order_no): void
{
// 获取订单数据
$params = array();
$params['order_service_no'] = $order_no;
$order_service_package = OrderServicePackage::getOne($params);
if (empty($order_service_package)) {
throw new BusinessException("订单数据为空");
}
// 检测问诊订单状态
if (!in_array($order_service_package['order_service_status'], [2, 3])) {
// 订单状态(1:待支付 2:未开始 3:服务中 4:服务完成 5:服务取消)
throw new BusinessException("订单状态错误");
}
// 检测订单退款状态
if (in_array($order_service_package['refund_status'], [2, 3, 5, 6])) {
// 检测订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常)
throw new BusinessException("订单退款状态错误");
}
// 检测支付状态
if ($order_service_package['pay_status'] != 2) {
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
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;
}
}