10、问诊订单退款

11、药品订单退款
12、问诊订单模拟退款
13、药品订单模拟退款
This commit is contained in:
wucongxing 2023-11-08 20:01:39 +08:00
parent 2336d96915
commit fa2d402aa0
7 changed files with 207 additions and 98 deletions

View File

@ -89,7 +89,7 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
} catch (\Throwable $e) { } catch (\Throwable $e) {
Db::rollBack(); Db::rollBack();
Log::getInstance("queue-CancelUnpayOrders")->error("取消未支付订单执行失败:" . $e->getMessage()); Log::getInstance("queue-CancelUnpayOrders")->error("取消未支付订单执行失败:" . $e->getMessage());
return Result::ACK; // 重回队列 return Result::ACK;
} }
} }

View File

@ -170,7 +170,7 @@ class getPrescriptionOrderStatusCommand extends HyperfCommand
$this->line("获取处方平台订单数据结束:处理完毕"); $this->line("获取处方平台订单数据结束:处理完毕");
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollBack(); Db::rollBack();
// 记录失败次数 // 记录失败次数
$this->line("获取处方平台订单数据失败:" . $e->getMessage()); $this->line("获取处方平台订单数据失败:" . $e->getMessage());

View File

@ -23,6 +23,7 @@ use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund; use App\Model\OrderInquiryRefund;
use App\Model\OrderPrescription; use App\Model\OrderPrescription;
use App\Model\OrderProduct; use App\Model\OrderProduct;
use App\Model\OrderProductCoupon;
use App\Model\OrderProductItem; use App\Model\OrderProductItem;
use App\Model\OrderProductLogistic; use App\Model\OrderProductLogistic;
use App\Model\OrderProductRefund; use App\Model\OrderProductRefund;
@ -39,6 +40,7 @@ use App\Services\ImService;
use App\Services\InquiryService; use App\Services\InquiryService;
use App\Services\MessagePush; use App\Services\MessagePush;
use App\Services\OrderPrescriptionService; use App\Services\OrderPrescriptionService;
use App\Services\UserCouponService;
use App\Services\UserService; use App\Services\UserService;
use App\Utils\Log; use App\Utils\Log;
use Extend\Alibaba\Oss; use Extend\Alibaba\Oss;
@ -250,10 +252,10 @@ class CallBackController extends AbstractController
// 验证推送消息签名 // 验证推送消息签名
$app->getValidator()->validate($app->getRequest()); $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'])) { if (empty($message['out_trade_no'])) {
Log::getInstance()->info("微信退款回调数据错误"); Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("微信退款回调数据错误");
return $server->serve(); return $server->serve();
} }
@ -262,28 +264,28 @@ class CallBackController extends AbstractController
$params['inquiry_no'] = $message['out_trade_no']; $params['inquiry_no'] = $message['out_trade_no'];
$order_inquiry = OrderInquiry::getOne($params); $order_inquiry = OrderInquiry::getOne($params);
if (empty($order_inquiry)) { if (empty($order_inquiry)) {
Log::getInstance()->info("非法订单"); Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("非法订单");
return $server->serve(); return $server->serve();
} }
// 验证订单状态 // 验证订单状态
if ($order_inquiry['inquiry_status'] == 1) { if ($order_inquiry['inquiry_status'] == 1) {
// 问诊订单状态1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消) // 问诊订单状态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(); return $server->serve();
} }
// 验证订单退款状态 // 验证订单退款状态
if ($order_inquiry['inquiry_refund_status'] == 3) { if ($order_inquiry['inquiry_refund_status'] == 3) {
// 问诊订单退款状态0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) // 问诊订单退款状态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(); return $server->serve();
} }
// 验证订单支付状态 // 验证订单支付状态
if (in_array($order_inquiry['inquiry_pay_status'], [1, 3, 4, 5, 6, 7])) { if (in_array($order_inquiry['inquiry_pay_status'], [1, 3, 4, 5, 6, 7])) {
// 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) // 支付状态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(); return $server->serve();
} }
@ -301,7 +303,7 @@ class CallBackController extends AbstractController
if (empty($inquiry_refund_status)) { if (empty($inquiry_refund_status)) {
// 错误,无退款状态 // 错误,无退款状态
Log::getInstance()->info("订单支付状态错误:未接收到退款状态"); Log::getInstance("CallBackController-wxPayInquiryRefundCallBack")->info("订单支付状态错误:未接收到退款状态");
return $this->wxPayErrorReturn("退款状态错误"); return $this->wxPayErrorReturn("退款状态错误");
} }
@ -325,8 +327,25 @@ class CallBackController extends AbstractController
// 恢复优惠卷 // 恢复优惠卷
if ($inquiry_refund_status == 3) { if ($inquiry_refund_status == 3) {
if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { 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:支付超时) // 取消订单原因1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
$MessagePush->refundInquirySuccess($order_inquiry['cancel_reason']); $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) { } catch (\Exception $e) {
// 验证失败 // 验证失败
Log::getInstance()->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage()); Log::getInstance()->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage());
@ -505,10 +512,10 @@ class CallBackController extends AbstractController
// 验证推送消息签名 // 验证推送消息签名
$app->getValidator()->validate($app->getRequest()); $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'])) { if (empty($message['out_trade_no'])) {
Log::getInstance()->info("药品微信退款回调数据处理失败,缺少外部订单号"); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理失败,缺少外部订单号");
return $server->serve(); return $server->serve();
} }
@ -518,28 +525,38 @@ class CallBackController extends AbstractController
$order_product = OrderProduct::getOne($params); $order_product = OrderProduct::getOne($params);
if (empty($order_product)) { if (empty($order_product)) {
Db::rollBack(); 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(); return $server->serve();
} }
// 验证订单状态 // 验证订单状态
if ($order_product['order_product_status'] == 1) { if ($order_product['order_product_status'] == 1) {
// 订单状态1:待支付 2:待发货 3:已发货 4:已签收 5:已取消) // 订单状态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(); return $server->serve();
} }
// 验证订单退款状态 // 验证订单退款状态
if ($order_product['refund_status'] == 3) { if ($order_product['refund_status'] == 3) {
// 商品订单退款状态0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) // 商品订单退款状态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(); return $server->serve();
} }
// 验证订单支付状态 // 验证订单支付状态
if (in_array($order_product['pay_status'], [1, 3, 4, 5, 6, 7])) { if (in_array($order_product['pay_status'], [1, 3, 4, 5, 6, 7])) {
// 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) // 支付状态1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
Log::getInstance()->error("药品微信退款回调数据处理失败:订单未支付"); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:订单未支付");
return $server->serve(); return $server->serve();
} }
@ -557,7 +574,7 @@ class CallBackController extends AbstractController
if (empty($refund_status)) { if (empty($refund_status)) {
// 错误,无退款状态 // 错误,无退款状态
Log::getInstance()->error("药品微信退款回调数据处理失败:订单未支付"); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:订单未支付");
return $this->wxPayErrorReturn("退款状态错误"); return $this->wxPayErrorReturn("退款状态错误");
} }
@ -578,15 +595,40 @@ class CallBackController extends AbstractController
$params['order_product_id'] = $order_product['order_product_id']; $params['order_product_id'] = $order_product['order_product_id'];
OrderProductRefund::edit($params, $data); 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(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
// 验证失败 // 验证失败
Db::rollBack(); Db::rollBack();
Log::getInstance()->error("药品微信退款回调数据处理失败:" . $e->getMessage()); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("药品微信退款回调数据处理失败:" . $e->getMessage());
return $this->wxPayErrorReturn($e->getMessage()); return $this->wxPayErrorReturn($e->getMessage());
} }
Log::getInstance()->info("药品微信退款回调数据处理成功"); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->info("药品微信退款回调数据处理成功");
// 发送推送消息 // 发送推送消息
try { try {
@ -601,7 +643,7 @@ class CallBackController extends AbstractController
} }
} catch (\Exception $e) { } catch (\Exception $e) {
// 验证失败 // 验证失败
Log::getInstance()->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage()); Log::getInstance("CallBackController-wxPayProductRefundCallBack")->error("微信退款回调处理成功,推送消息失败:" . $e->getMessage());
return $server->serve(); return $server->serve();
} }

View File

@ -21,6 +21,7 @@ use App\Model\OrderInquiryCase;
use App\Model\OrderInquiryCoupon; use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund; use App\Model\OrderInquiryRefund;
use App\Model\OrderPrescription; use App\Model\OrderPrescription;
use App\Model\OrderProductCoupon;
use App\Model\PatientFamily; use App\Model\PatientFamily;
use App\Model\PatientFamilyHealth; use App\Model\PatientFamilyHealth;
use App\Model\PatientFamilyPersonal; 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) { 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,19 +1392,22 @@ class InquiryService extends BaseService
// 处理订单优惠卷 // 处理订单优惠卷
if (!empty($order_inquiry['coupon_amount_total']) && $order_inquiry['coupon_amount_total'] > 0) { 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 = array();
$params['order_inquiry_id'] = $order_inquiry['order_inquiry_id']; $params['order_inquiry_id'] = $order_inquiry['order_inquiry_id'];
$order_inquiry_coupon = OrderInquiryCoupon::getOne($params); $order_inquiry_coupons = OrderInquiryCoupon::getList($params);
if (!empty($order_inquiry_coupon)) { 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'], $order_inquiry['order_inquiry_id']); $MessagePush = new MessagePush($order_inquiry['user_id']);
$MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']); $MessagePush->patientRefundCoupon($order_inquiry_coupon['coupon_name']);
} }
} }
}
return $result; return $result;
} }
@ -1456,58 +1473,6 @@ class InquiryService extends BaseService
return true; 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:问诊购药) * @param string $inquiry_type 接诊类型1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)

View File

@ -4,14 +4,17 @@ namespace App\Services;
use App\Constants\HttpEnumCode; use App\Constants\HttpEnumCode;
use App\Exception\BusinessException; use App\Exception\BusinessException;
use App\Model\OrderInquiryCoupon;
use App\Model\OrderInquiryRefund; use App\Model\OrderInquiryRefund;
use App\Model\OrderPrescription; use App\Model\OrderPrescription;
use App\Model\OrderPrescriptionProduct; use App\Model\OrderPrescriptionProduct;
use App\Model\OrderProduct; use App\Model\OrderProduct;
use App\Model\OrderProductCoupon;
use App\Model\OrderProductItem; use App\Model\OrderProductItem;
use App\Model\OrderProductRefund; use App\Model\OrderProductRefund;
use App\Model\Product; use App\Model\Product;
use App\Model\ProductPlatformAmount; use App\Model\ProductPlatformAmount;
use App\Model\UserPatient;
use Extend\Wechat\WechatPay; use Extend\Wechat\WechatPay;
use Hyperf\DbConnection\Db; use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface; use Hyperf\Snowflake\IdGeneratorInterface;
@ -41,6 +44,8 @@ class OrderProductService extends BaseService
* @param string|int $cancel_reason 订单取消原因1:主动取消 2:复核失败/库存不足 3:支付超时 * @param string|int $cancel_reason 订单取消原因1:主动取消 2:复核失败/库存不足 3:支付超时
* @param string $cancel_remarks 订单取消原因 * @param string $cancel_remarks 订单取消原因
* @return array * @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function cancelUnpayProductOrder(string|int $order_no,string|int $cancel_reason,string $cancel_remarks): array 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; 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 = array();
$data['order_product_status'] = 5; $data['order_product_status'] = 5;
@ -134,7 +149,6 @@ class OrderProductService extends BaseService
return $result; return $result;
} }
// 库存+订单商品数量 // 库存+订单商品数量
$params = array(); $params = array();
$params['amount_id'] = $product_platform_amount['amount_id']; $params['amount_id'] = $product_platform_amount['amount_id'];
@ -181,6 +195,25 @@ class OrderProductService extends BaseService
OrderPrescriptionProduct::edit($params, $data); 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; return $result;
} }
@ -233,6 +266,14 @@ class OrderProductService extends BaseService
throw new BusinessException("订单支付状态错误,无法退款"); 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); $generator = $this->container->get(IdGeneratorInterface::class);
$product_refund_no = $generator->generate(); $product_refund_no = $generator->generate();
@ -281,6 +322,25 @@ class OrderProductService extends BaseService
$refund_status = 3; $refund_status = 3;
$refund_id = "模拟退款:" . $generator->generate(); $refund_id = "模拟退款:" . $generator->generate();
$success_time = date("Y-m-d H:i:s", time()); $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']);
}
}
}
} }
// 新增退款表 // 新增退款表

View File

@ -641,7 +641,7 @@ class PatientOrderService extends BaseService
} }
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Throwable $e) {
Db::rollBack(); Db::rollBack();
return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage());
} }

View File

@ -2,8 +2,10 @@
namespace App\Services; namespace App\Services;
use App\Model\OrderInquiryCoupon;
use App\Model\Product; use App\Model\Product;
use App\Model\UserCoupon; use App\Model\UserCoupon;
use App\Utils\Log;
/** /**
* 用户优惠卷 * 用户优惠卷
@ -209,4 +211,44 @@ class UserCouponService extends BaseService
return $selected_coupons; 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;
}
} }