diff --git a/app/Amqp/Consumer/AssignDoctorConsumer.php b/app/Amqp/Consumer/AssignDoctorConsumer.php index 9256dba..6f4900e 100644 --- a/app/Amqp/Consumer/AssignDoctorConsumer.php +++ b/app/Amqp/Consumer/AssignDoctorConsumer.php @@ -107,6 +107,7 @@ class AssignDoctorConsumer extends ConsumerMessage Log::getInstance()->error("分配医生队列执行失败:超出5分钟,执行退款"); // 检测问诊订单执行退款次数 + Log::getInstance()->info("检测问诊订单执行退款次数"); $res = $this->checkInquiryRefundCount($order_inquiry['order_inquiry_id']); if (!$res){ Db::rollBack(); diff --git a/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php b/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php new file mode 100644 index 0000000..0330d06 --- /dev/null +++ b/app/Amqp/Consumer/CancelUnInquiryOrdersDelayDirectConsumer.php @@ -0,0 +1,144 @@ +error("开始执行 取消未接诊问诊订单 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE)); + + Db::beginTransaction(); + + try { + // 获取问诊订单数据 + $params = array(); + $params['order_inquiry_id'] = $data['order_inquiry_id']; + $order_inquiry = OrderInquiry::getOne($params); + if (empty($order_inquiry)){ + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:未找到对应问诊订单" ); + return Result::ACK; + } + + // 检测订单状态 + if ($order_inquiry['inquiry_status'] == 7) { + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:订单已取消" ); + return Result::ACK; + } + + if ($order_inquiry['inquiry_status'] == 4) { + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:订单已接诊" ); + return Result::ACK; + } + + if ($order_inquiry['inquiry_status'] != 3) { + // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:订单状态非待接诊" ); + return Result::DROP; + } + + if (!in_array($order_inquiry['inquiry_refund_status'], [0, 4, 5])) { + // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:订单正在退款中" ); + return Result::DROP; + } + + // 取消问诊订单 + $data = array(); + $data['inquiry_status'] = 7; + $data['cancel_time'] = date("Y-m-d H:i:s", time()); + $data['cancel_reason'] = 1; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时) + $data['cancel_remarks'] = "医生未接诊"; // 取消订单备注 + $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); + + // 恢复优惠卷 + if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { + // 获取用户优惠卷信息 + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + $order_inquiry_coupon = OrderInquiryCoupon::getOne($params); + if (!empty($order_inquiry_coupon)) { + // 恢复优惠卷 + $data = array(); + $data['user_coupon_status'] = 0; + $data['coupon_use_date'] = date('Y-m-d H:i:s', time()); + + $params = array(); + $params['user_coupon_id'] = $order_inquiry_coupon['user_coupon_id']; + UserCoupon::edit($params, $data); + } + } + + // 支付状态 + if ($order_inquiry['inquiry_pay_status'] == 2) { + // 检测订单退款次数 + // 检测问诊订单执行退款次数 + $InquiryService = new InquiryService(); + $res = $InquiryService->checkInquiryRefundCount($order_inquiry['order_inquiry_i d']); + if (!$res){ + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:超出最大退款次数"); + return Result::ACK; + } + + $InquiryService->inquiryRefund($order_inquiry['order_inquiry_id'], "暂无医生接诊"); + + return Result::ACK; + } + // 订单退款 + + + Db::commit(); + } catch (\Exception $e) { + Db::rollBack(); + Log::getInstance()->error("取消未接诊问诊订单失败:" . $e->getMessage()); + return Result::ACK; + } + + + Log::getInstance()->info("取消未接诊问诊订单成功"); + return Result::ACK; + } +} diff --git a/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php new file mode 100644 index 0000000..5daa9e1 --- /dev/null +++ b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php @@ -0,0 +1,92 @@ +error("开始执行 取消未支付订单 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE)); + + Db::beginTransaction(); + + try { + if ($data['order_type'] == 1) { + // 问诊订单取消 + $InquiryService = new InquiryService(); + $result = $InquiryService->cancelUnpayInquiryOrder($data['order_no'], 5, "支付超时"); + + } elseif ($data['order_type'] == 2) { + // 药品订单取消 + $OrderProductService = new OrderProductService(); + $result = $OrderProductService->cancelUnpayProductOrder($data['order_no'], 3, "支付超时"); + + } else { + Log::getInstance()->error("取消未支付订单失败:order_type类型错误"); + return Result::DROP;// 销毁 + } + + if ($result['status'] == 0) { + Db::rollBack(); + Log::getInstance()->error("取消未支付订单失败:" . $result['message']); + return Result::DROP;// 销毁 + } elseif ($result['status'] == 2) { + Db::rollBack(); + Log::getInstance()->info("取消未支付订单成功:" . $result['message']); + return Result::ACK;// 销毁 + } + + Db::commit(); + Log::getInstance()->info("取消未支付订单 队列执行成功"); + return Result::ACK; + } catch (\Exception $e) { + Db::rollBack(); + Log::getInstance()->error("取消未支付订单执行失败:" . $e->getMessage()); + return Result::ACK; // 重回队列 + } + } + + +} diff --git a/app/Amqp/Producer/CancelUnInquiryOrdersDelayDirectProducer.php b/app/Amqp/Producer/CancelUnInquiryOrdersDelayDirectProducer.php new file mode 100644 index 0000000..3ad56a5 --- /dev/null +++ b/app/Amqp/Producer/CancelUnInquiryOrdersDelayDirectProducer.php @@ -0,0 +1,37 @@ +payload = $data; + } +} diff --git a/app/Amqp/Producer/CancelUnpayOrdersDelayDirectProducer.php b/app/Amqp/Producer/CancelUnpayOrdersDelayDirectProducer.php new file mode 100644 index 0000000..52bc4e7 --- /dev/null +++ b/app/Amqp/Producer/CancelUnpayOrdersDelayDirectProducer.php @@ -0,0 +1,31 @@ +payload = $data; + } +} diff --git a/app/Services/InquiryService.php b/app/Services/InquiryService.php index 322bf9d..d20b6a8 100644 --- a/app/Services/InquiryService.php +++ b/app/Services/InquiryService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Amqp\Producer\CancelUnPayInquiryOrderDelayProducer; use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer; use App\Constants\DoctorTitleCode; use App\Constants\HttpEnumCode; @@ -31,6 +32,7 @@ use App\Utils\PcreMatch; use Extend\Wechat\WechatPay; use Hyperf\Amqp\Producer; use Hyperf\DbConnection\Db; +use Hyperf\Redis\Redis; use Hyperf\Snowflake\IdGeneratorInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -972,4 +974,38 @@ class InquiryService extends BaseService return $order_inquiry->toArray(); } + + /** + * 检测问诊订单执行退款次数 + * @param string $order_inquiry_id + * @return bool + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function checkInquiryRefundCount(string $order_inquiry_id): bool + { + try { + $redis = $this->container->get(Redis::class); + + $redis_key = "inquiryRefund" . $order_inquiry_id; + $redis_value = $redis->get($redis_key); + if(empty($redis_value)){ + $redis->set($redis_key, 1,60 * 60 * 24 * 5); + return true; + } + + // 问诊订单执行退款次数过多 + if ($redis_value > 3) { + // 加入短信队列,通知管理员 + + return false; + } + + $redis->incr($redis_key); + } catch (\Exception $e) { + throw new BusinessException("检测问诊订单执行退款次数失败:" . $e->getMessage()); + } + + return true; + } } \ No newline at end of file diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index 793513c..bda30f7 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Amqp\Producer\AssignDoctorProducer; +use App\Amqp\Producer\CancelUnPayInquiryOrderDelayProducer; use App\Amqp\Producer\CancelUnpayOrdersDelayDirectProducer; use App\Constants\DoctorTitleCode; use App\Constants\HttpEnumCode; diff --git a/app/Services/UserDoctorService.php b/app/Services/UserDoctorService.php index d2c9034..6ca2b07 100644 --- a/app/Services/UserDoctorService.php +++ b/app/Services/UserDoctorService.php @@ -1082,7 +1082,7 @@ class UserDoctorService extends BaseService } // 检测药品库存数据 - if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['real_stock']) { + if ($item['prescription_product_num'] > $product['ProductPlatformAmount']['stock']) { // 库存不足 Db::rollBack(); return fail(HttpEnumCode::HTTP_ERROR, "药品" . $product['product_name'] . "库存不足");