diff --git a/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php index 3b71f48..70688f7 100644 --- a/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php +++ b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php @@ -89,7 +89,7 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage } catch (\Throwable $e) { Db::rollBack(); Log::getInstance("queue-CancelUnpayOrders")->error("取消未支付订单执行失败:" . $e->getMessage()); - return Result::ACK; // 重回队列 + return Result::ACK; } } diff --git a/app/Command/getPrescriptionOrderStatusCommand.php b/app/Command/getPrescriptionOrderStatusCommand.php index 93893b2..6600bd4 100644 --- a/app/Command/getPrescriptionOrderStatusCommand.php +++ b/app/Command/getPrescriptionOrderStatusCommand.php @@ -170,7 +170,7 @@ class getPrescriptionOrderStatusCommand extends HyperfCommand $this->line("获取处方平台订单数据结束:处理完毕"); Db::commit(); - } catch (\Exception $e) { + } catch (\Throwable $e) { Db::rollBack(); // 记录失败次数 $this->line("获取处方平台订单数据失败:" . $e->getMessage()); diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index 54e5c9d..d984eb4 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -23,6 +23,7 @@ use App\Model\OrderInquiryCoupon; use App\Model\OrderInquiryRefund; use App\Model\OrderPrescription; use App\Model\OrderProduct; +use App\Model\OrderProductCoupon; use App\Model\OrderProductItem; use App\Model\OrderProductLogistic; use App\Model\OrderProductRefund; @@ -39,6 +40,7 @@ use App\Services\ImService; use App\Services\InquiryService; use App\Services\MessagePush; use App\Services\OrderPrescriptionService; +use App\Services\UserCouponService; use App\Services\UserService; use App\Utils\Log; use Extend\Alibaba\Oss; @@ -250,10 +252,10 @@ class CallBackController extends AbstractController // 验证推送消息签名 $app->getValidator()->validate($app->getRequest()); - Log::getInstance()->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); if (empty($message['out_trade_no'])) { - Log::getInstance()->info("微信退款回调数据错误"); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("微信退款回调数据错误"); return $server->serve(); } @@ -262,28 +264,28 @@ class CallBackController extends AbstractController $params['inquiry_no'] = $message['out_trade_no']; $order_inquiry = OrderInquiry::getOne($params); if (empty($order_inquiry)) { - Log::getInstance()->info("非法订单"); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("非法订单"); return $server->serve(); } // 验证订单状态 if ($order_inquiry['inquiry_status'] == 1) { // 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) - Log::getInstance()->info("订单状态错误:当前为" . $order_inquiry['inquiry_status']); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("订单状态错误:当前为" . $order_inquiry['inquiry_status']); return $server->serve(); } // 验证订单退款状态 if ($order_inquiry['inquiry_refund_status'] == 3) { // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) - Log::getInstance()->info("订单退款状态错误:当前为" . $order_inquiry['inquiry_refund_status']); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("订单退款状态错误:当前为" . $order_inquiry['inquiry_refund_status']); return $server->serve(); } // 验证订单支付状态 if (in_array($order_inquiry['inquiry_pay_status'], [1, 3, 4, 5, 6, 7])) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) - Log::getInstance()->info("订单支付状态错误:当前为" . $order_inquiry['inquiry_pay_status']); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("订单支付状态错误:当前为" . $order_inquiry['inquiry_pay_status']); return $server->serve(); } @@ -301,7 +303,7 @@ class CallBackController extends AbstractController if (empty($inquiry_refund_status)) { // 错误,无退款状态 - Log::getInstance()->info("订单支付状态错误:未接收到退款状态"); + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("订单支付状态错误:未接收到退款状态"); return $this->wxPayErrorReturn("退款状态错误"); } @@ -325,8 +327,25 @@ class CallBackController extends AbstractController // 恢复优惠卷 if ($inquiry_refund_status == 3) { if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { - $InquiryService = new InquiryService(); - $InquiryService->returnInquiryCoupon($order_inquiry['order_inquiry_id'], $order_inquiry['user_id']); + // 获取该订单全部优惠卷数据 + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + $order_inquiry_coupons = OrderInquiryCoupon::getList($params); + if (!empty($order_inquiry_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_inquiry_coupons as $order_inquiry_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_inquiry_coupon['user_coupon_id']); + + try { + // 发送站内消息-优惠卷退还 + $MessagePush = new MessagePush($order_inquiry['user_id']); + $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); + }catch (\Throwable $e){ + Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->error("推送消息失败:" . $e->getMessage()); + } + } + } } } @@ -348,18 +367,6 @@ class CallBackController extends AbstractController // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时) $MessagePush->refundInquirySuccess($order_inquiry['cancel_reason']); - - 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)){ - // 发送站内消息-优惠卷退还 - $MessagePush = new MessagePush($order_inquiry['user_id'], $order_inquiry['order_inquiry_id']); - $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); - } - } } catch (\Exception $e) { // 验证失败 Log::getInstance()->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage()); @@ -505,10 +512,10 @@ class CallBackController extends AbstractController // 验证推送消息签名 $app->getValidator()->validate($app->getRequest()); - Log::getInstance()->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("微信退款回调数据:" . json_encode($message->toArray(), JSON_UNESCAPED_UNICODE)); if (empty($message['out_trade_no'])) { - Log::getInstance()->info("药品微信退款回调数据处理失败,缺少外部订单号"); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,缺少外部订单号"); return $server->serve(); } @@ -518,28 +525,38 @@ class CallBackController extends AbstractController $order_product = OrderProduct::getOne($params); if (empty($order_product)) { Db::rollBack(); - Log::getInstance()->info("药品微信退款回调数据处理失败,无订单数据"); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,无订单数据"); + return $server->serve(); + } + + // 查询患者数据 + $params = array(); + $params['patient_id'] = $order_product["patient_id"]; + $user_patient = UserPatient::getOne($params); + if (empty($user_patient)) { + Db::rollBack(); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,无患者数据"); return $server->serve(); } // 验证订单状态 if ($order_product['order_product_status'] == 1) { // 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消) - Log::getInstance()->info("药品微信退款回调数据处理失败,订单状态错误:当前为" . $order_product['order_product_status']); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,订单状态错误:当前为" . $order_product['order_product_status']); return $server->serve(); } // 验证订单退款状态 if ($order_product['refund_status'] == 3) { // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) - Log::getInstance()->info("药品微信退款回调数据处理失败,订单退款状态为" . $order_product['refund_status']); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,订单退款状态为" . $order_product['refund_status']); return $server->serve(); } // 验证订单支付状态 if (in_array($order_product['pay_status'], [1, 3, 4, 5, 6, 7])) { // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) - Log::getInstance()->error("药品微信退款回调数据处理失败:订单未支付"); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:订单未支付"); return $server->serve(); } @@ -557,7 +574,7 @@ class CallBackController extends AbstractController if (empty($refund_status)) { // 错误,无退款状态 - Log::getInstance()->error("药品微信退款回调数据处理失败:订单未支付"); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:订单未支付"); return $this->wxPayErrorReturn("退款状态错误"); } @@ -578,15 +595,40 @@ class CallBackController extends AbstractController $params['order_product_id'] = $order_product['order_product_id']; OrderProductRefund::edit($params, $data); + // 恢复优惠卷 + if ($refund_status == 3) { + if (!empty($order_product['coupon_amount_total']) && $order_product['coupon_amount_total'] > 0) { + // 获取该订单全部优惠卷数据 + $params = array(); + $params['order_product_id'] = $order_product['order_product_id']; + $order_product_coupons = OrderProductCoupon::getList($params); + if (!empty($order_product_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_product_coupons as $order_product_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_product_coupon['user_coupon_id']); + + // 发送站内消息-优惠卷退还 + try { + $MessagePush = new MessagePush($user_patient['user_id']); + $MessagePush->patientRefundCoupon($order_product_coupon['coupon_name']); + }catch (\Throwable $e){ + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("推送消息失败:" . $e->getMessage()); + } + } + } + } + } + Db::commit(); } catch (\Exception $e) { // 验证失败 Db::rollBack(); - Log::getInstance()->error("药品微信退款回调数据处理失败:" . $e->getMessage()); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:" . $e->getMessage()); return $this->wxPayErrorReturn($e->getMessage()); } - Log::getInstance()->info("药品微信退款回调数据处理成功"); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理成功"); // 发送推送消息 try { @@ -601,7 +643,7 @@ class CallBackController extends AbstractController } } catch (\Exception $e) { // 验证失败 - Log::getInstance()->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage()); + Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage()); return $server->serve(); } diff --git a/app/Services/InquiryService.php b/app/Services/InquiryService.php index c13d5b5..a603a5e 100644 --- a/app/Services/InquiryService.php +++ b/app/Services/InquiryService.php @@ -21,6 +21,7 @@ use App\Model\OrderInquiryCase; use App\Model\OrderInquiryCoupon; use App\Model\OrderInquiryRefund; use App\Model\OrderPrescription; +use App\Model\OrderProductCoupon; use App\Model\PatientFamily; use App\Model\PatientFamilyHealth; use App\Model\PatientFamilyPersonal; @@ -1264,8 +1265,21 @@ class InquiryService extends BaseService // 模拟退款时手动退还优惠卷 if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { - $InquiryService = new InquiryService(); - $InquiryService->returnInquiryCoupon($order_inquiry['order_inquiry_id'], $order_inquiry['user_id']); + // 获取该订单全部优惠卷数据 + $params = array(); + $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; + $order_inquiry_coupons = OrderInquiryCoupon::getList($params); + if (!empty($order_inquiry_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_inquiry_coupons as $order_inquiry_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_inquiry_coupon['user_coupon_id']); + + // 发送站内消息-优惠卷退还 + $MessagePush = new MessagePush($order_inquiry['user_id']); + $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); + } + } } } @@ -1378,17 +1392,20 @@ class InquiryService extends BaseService // 处理订单优惠卷 if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { - $InquiryService = new InquiryService(); - $InquiryService->returnInquiryCoupon($order_inquiry['order_inquiry_id'], $order_inquiry['user_id']); - - // 获取用户优惠卷信息 + // 获取该订单全部优惠卷数据 $params = array(); $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; - $order_inquiry_coupon = OrderInquiryCoupon::getOne($params); - if (!empty($order_inquiry_coupon)) { - // 发送站内消息-优惠卷退还 - $MessagePush = new MessagePush($order_inquiry['user_id'], $order_inquiry['order_inquiry_id']); - $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); + $order_inquiry_coupons = OrderInquiryCoupon::getList($params); + if (!empty($order_inquiry_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_inquiry_coupons as $order_inquiry_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_inquiry_coupon['user_coupon_id']); + + // 发送站内消息-优惠卷退还 + $MessagePush = new MessagePush($order_inquiry['user_id']); + $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); + } } } @@ -1456,58 +1473,6 @@ class InquiryService extends BaseService return true; } - /** - * 退还问诊订单优惠卷 - * @param string $order_inquiry_id 问诊订单id - * @param string $patient_user_id 患者用户id - * @return bool - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ - public function returnInquiryCoupon(string $order_inquiry_id, string $patient_user_id): bool - { - try { - // 获取用户优惠卷信息 - $params = array(); - $params['order_inquiry_id'] = $order_inquiry_id; - $order_inquiry_coupon = OrderInquiryCoupon::getOne($params); - if (empty($order_inquiry_coupon)) { - // 订单未使用优惠卷,无需退还 - return true; - } - - // 获取用户优惠卷数据 - $params = array(); - $params['user_coupon_id'] = $order_inquiry_coupon['user_coupon_id']; - $user_coupon = UserCoupon::getOne($params); - if (empty($user_coupon)) { - // 无该优惠卷数据,无需处理 - return true; - } - - // 恢复优惠卷 - $data = array(); - - // 检测优惠卷过期时间。判断是否需要退还 - if (strtotime($user_coupon['valid_end_time']) <= time()) { - // 超出过期时间,置为已过期 - $data['user_coupon_status'] = 3; - } else { - $data['user_coupon_status'] = 0; - $data['coupon_use_date'] = null; - } - - $params = array(); - $params['user_coupon_id'] = $order_inquiry_coupon['user_coupon_id']; - UserCoupon::edit($params, $data); - - return true; - } catch (\Exception $e) { - Log::getInstance()->error("退还用户优惠卷失败" . $e->getMessage()); - return false; - } - } - /** * 检测当前是否符合系统问诊时间 * @param string $inquiry_type 接诊类型(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药) diff --git a/app/Services/OrderProductService.php b/app/Services/OrderProductService.php index 0c63f39..72cd6ed 100644 --- a/app/Services/OrderProductService.php +++ b/app/Services/OrderProductService.php @@ -4,14 +4,17 @@ namespace App\Services; use App\Constants\HttpEnumCode; use App\Exception\BusinessException; +use App\Model\OrderInquiryCoupon; use App\Model\OrderInquiryRefund; use App\Model\OrderPrescription; use App\Model\OrderPrescriptionProduct; use App\Model\OrderProduct; +use App\Model\OrderProductCoupon; use App\Model\OrderProductItem; use App\Model\OrderProductRefund; use App\Model\Product; use App\Model\ProductPlatformAmount; +use App\Model\UserPatient; use Extend\Wechat\WechatPay; use Hyperf\DbConnection\Db; use Hyperf\Snowflake\IdGeneratorInterface; @@ -41,6 +44,8 @@ class OrderProductService extends BaseService * @param string|int $cancel_reason 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时 * @param string $cancel_remarks 订单取消原因 * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function cancelUnpayProductOrder(string|int $order_no,string|int $cancel_reason,string $cancel_remarks): array { @@ -87,6 +92,16 @@ class OrderProductService extends BaseService return $result; } + // 获取患者数据 + $params = array(); + $params['patient_id'] = $order_product['patient_id']; + $user_patient = UserPatient::getOne($params); + if (empty($user_patient)){ + $result['status'] = 0; + $result['message'] = "取消未支付的问诊订单失败:未查询到对应用户数据"; + return $result; + } + // 取消药品订单 $data = array(); $data['order_product_status'] = 5; @@ -134,7 +149,6 @@ class OrderProductService extends BaseService return $result; } - // 库存+订单商品数量 $params = array(); $params['amount_id'] = $product_platform_amount['amount_id']; @@ -181,6 +195,25 @@ class OrderProductService extends BaseService OrderPrescriptionProduct::edit($params, $data); } + // 处理订单优惠卷 + if (!empty($order_product['coupon_amount_total']) && $order_product['coupon_amount_total'] > 0) { + // 获取该订单全部优惠卷数据 + $params = array(); + $params['order_product_id'] = $order_product['order_product_id']; + $order_product_coupons = OrderProductCoupon::getList($params); + if (!empty($order_product_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_product_coupons as $order_product_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_product_coupon['user_coupon_id']); + + // 发送站内消息-优惠卷退还 + $MessagePush = new MessagePush($user_patient['user_id']); + $MessagePush->patientRefundCoupon($order_product_coupon['coupon_name']); + } + } + } + return $result; } @@ -233,6 +266,14 @@ class OrderProductService extends BaseService throw new BusinessException("订单支付状态错误,无法退款"); } + // 获取患者数据 + $params = array(); + $params['patient_id'] = $order_product['patient_id']; + $user_patient = UserPatient::getOne($params); + if (empty($user_patient)){ + throw new BusinessException("未查询到对应用户数据"); + } + // 系统退款编号 $generator = $this->container->get(IdGeneratorInterface::class); $product_refund_no = $generator->generate(); @@ -281,6 +322,25 @@ class OrderProductService extends BaseService $refund_status = 3; $refund_id = "模拟退款:" . $generator->generate(); $success_time = date("Y-m-d H:i:s", time()); + + // 模拟退款时手动退还优惠卷 + if (!empty($order_product['coupon_amount_total']) && $order_product['coupon_amount_total'] > 0) { + // 获取该订单全部优惠卷数据 + $params = array(); + $params['order_product_id'] = $order_product['order_product_id']; + $order_product_coupons = OrderProductCoupon::getList($params); + if (!empty($order_product_coupons)){ + $userCouponService = new UserCouponService(); + foreach ($order_product_coupons as $order_product_coupon){ + // 退还优惠卷 + $userCouponService->returnUserCoupon($order_product_coupon['user_coupon_id']); + + // 发送站内消息-优惠卷退还 + $MessagePush = new MessagePush($user_patient['user_id']); + $MessagePush->patientRefundCoupon($order_product_coupon['coupon_name']); + } + } + } } // 新增退款表 diff --git a/app/Services/PatientOrderService.php b/app/Services/PatientOrderService.php index e200a0f..7602b7b 100644 --- a/app/Services/PatientOrderService.php +++ b/app/Services/PatientOrderService.php @@ -641,7 +641,7 @@ class PatientOrderService extends BaseService } Db::commit(); - } catch (\Exception $e) { + } catch (\Throwable $e) { Db::rollBack(); return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); } diff --git a/app/Services/UserCouponService.php b/app/Services/UserCouponService.php index cabcc5f..80c7246 100644 --- a/app/Services/UserCouponService.php +++ b/app/Services/UserCouponService.php @@ -2,8 +2,10 @@ namespace App\Services; +use App\Model\OrderInquiryCoupon; use App\Model\Product; use App\Model\UserCoupon; +use App\Utils\Log; /** * 用户优惠卷 @@ -209,4 +211,44 @@ class UserCouponService extends BaseService return $selected_coupons; } + + /** + * 退还用户优惠卷 + * @param string|int $user_coupon_id + * @return bool + */ + public function returnUserCoupon(string|int $user_coupon_id): bool + { + try { + // 获取用户优惠卷数据 + $params = array(); + $params['user_coupon_id'] = $user_coupon_id; + $user_coupon = UserCoupon::getOne($params); + if (empty($user_coupon)) { + // 无该优惠卷数据,无需处理 + return true; + } + + // 恢复优惠卷 + $data = array(); + + // 检测优惠卷过期时间。判断是否需要退还 + if (strtotime($user_coupon['valid_end_time']) <= time()) { + // 超出过期时间,置为已过期 + $data['user_coupon_status'] = 3; + } else { + $data['user_coupon_status'] = 0; + $data['coupon_use_date'] = null; + } + + $params = array(); + $params['user_coupon_id'] = $user_coupon_id; + UserCoupon::edit($params, $data); + }catch (\Throwable $e){ + Log::getInstance("UserCouponService-returnUserCoupon")->error($e->getMessage()); + return false; + } + + return true; + } } \ No newline at end of file