From 532c9f909f865f3dcf154fdfbea671998ea9fb0c Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Tue, 11 Apr 2023 18:02:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8D=AF=E5=93=81=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Command/ReportPreProductOrderCommand.php | 22 ++- app/Controller/TestController.php | 5 +- app/Model/OrderProductRefund.php | 10 ++ app/Services/InquiryService.php | 3 +- app/Services/OrderProductService.php | 136 +++++++++++++++++++ 5 files changed, 168 insertions(+), 8 deletions(-) diff --git a/app/Command/ReportPreProductOrderCommand.php b/app/Command/ReportPreProductOrderCommand.php index 89a7c49..15abd71 100644 --- a/app/Command/ReportPreProductOrderCommand.php +++ b/app/Command/ReportPreProductOrderCommand.php @@ -6,13 +6,17 @@ namespace App\Command; use App\Model\OrderProduct; use App\Services\OrderPrescriptionService; +use App\Services\OrderProductService; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; +use Hyperf\DbConnection\Db; use Hyperf\Redis\Redis; +use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; /** - * 上报处方平台商品订单 + * 商品订单上报处方平台 * 此处单商品订单上报两次 */ #[Command] @@ -29,6 +33,10 @@ class ReportPreProductOrderCommand extends HyperfCommand $this->setDescription('上报处方平台商品订单'); } + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function handle() { $this->line("上报处方平台商品订单开始"); @@ -48,14 +56,21 @@ class ReportPreProductOrderCommand extends HyperfCommand } foreach ($order_product_ids as $item){ + Db::beginTransaction(); + $redis_key = "ReportPreProductOrder" . $item['order_product_id']; try { $redis_value = $redis->get($redis_key); if(!empty($redis_value)){ + // 超出最大执行次数 if ($redis_value > 2){ - // 超出最大执行次数 + // 存储上报失败 $this->savaReportFail($item['order_product_id']); + + // 退款 + $OrderProductService = new OrderProductService(); + $OrderProductService->OrderProductRefund($item['order_product_id'],"药品订单退款"); } } @@ -68,7 +83,10 @@ class ReportPreProductOrderCommand extends HyperfCommand // 清空缓存 $redis->del($redis_key); + + Db::commit(); } catch (\Exception $e) { + Db::rollBack(); // 记录失败次数 $redis->incr($redis_key); $this->line("上报处方平台商品订单失败:" . $e->getMessage()); diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index 0085061..5021215 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -25,6 +25,7 @@ use App\Services\ImService; use App\Services\InquiryService; use App\Services\MessagePush; use App\Services\OrderPrescriptionService; +use App\Services\OrderProductService; use App\Services\PatientOrderService; use App\Services\UserDoctorService; use App\Utils\Log; @@ -754,10 +755,6 @@ class TestController extends AbstractController } public function test_13(){ -// $RecentContact = new RecentContact(); -// $RecentContact->deleteRecentContact(); -// die; - $out_trade_no = $this->request->input('out_trade_no'); $params = array(); $params['order_inquiry_id'] = $out_trade_no; diff --git a/app/Model/OrderProductRefund.php b/app/Model/OrderProductRefund.php index 0c560fd..f5c3480 100644 --- a/app/Model/OrderProductRefund.php +++ b/app/Model/OrderProductRefund.php @@ -91,4 +91,14 @@ class OrderProductRefund extends Model { return self::where($params)->update($data); } + + /** + * 新增 + * @param array $data + * @return \Hyperf\Database\Model\Model|OrderProductRefund + */ + public static function addOrderProductRefund(array $data): \Hyperf\Database\Model\Model|OrderProductRefund + { + return self::create($data); + } } diff --git a/app/Services/InquiryService.php b/app/Services/InquiryService.php index 0babf78..2a14089 100644 --- a/app/Services/InquiryService.php +++ b/app/Services/InquiryService.php @@ -894,7 +894,7 @@ class InquiryService extends BaseService * @param string $order_inquiry_id * @param string $refund_reason 退款原因 * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface|GuzzleException + * @throws NotFoundExceptionInterface */ public function inquiryRefund(string $order_inquiry_id, string $refund_reason) { @@ -944,7 +944,6 @@ class InquiryService extends BaseService ]; $result = $WechatPay->refund($options); - dump($result); // 处理订单退款状态 // 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) diff --git a/app/Services/OrderProductService.php b/app/Services/OrderProductService.php index 5425447..39d3cba 100644 --- a/app/Services/OrderProductService.php +++ b/app/Services/OrderProductService.php @@ -3,13 +3,20 @@ namespace App\Services; use App\Constants\HttpEnumCode; +use App\Exception\BusinessException; +use App\Model\OrderInquiryRefund; use App\Model\OrderPrescription; use App\Model\OrderPrescriptionProduct; use App\Model\OrderProduct; use App\Model\OrderProductItem; +use App\Model\OrderProductRefund; use App\Model\Product; use App\Model\ProductPlatformAmount; +use Extend\Wechat\WechatPay; use Hyperf\DbConnection\Db; +use Hyperf\Snowflake\IdGeneratorInterface; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; class OrderProductService extends BaseService { @@ -171,4 +178,133 @@ class OrderProductService extends BaseService return $result; } + + /** + * 药品订单退款 + * @param string $order_product_id 药品订单id + * @param string $refund_reason 退款原因 + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function OrderProductRefund(string $order_product_id, string $refund_reason): void + { + // 获取药品订单数据 + $params = array(); + $params['order_product_id'] = $order_product_id; + $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['order_product_status'] == 2){ + // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + throw new BusinessException("订单退款中"); + } + + if ($order_product['order_product_status'] == 3){ + // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + throw new BusinessException("订单已退款成功"); + } + + if ($order_product['order_product_status'] == 5){ + // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + throw new BusinessException("订单退款关闭"); + } + + // 检测支付状态 + if ($order_product['pay_status'] != 5){ + // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款) + throw new BusinessException("订单支付状态错误,无法退款"); + } + + // 系统退款编号 + $generator = $this->container->get(IdGeneratorInterface::class); + $product_refund_no = $generator->generate(); + + // 检测订单金额 + if ($order_product['payment_amount_total'] > 0){ + // 发起退款 + $WechatPay = new WechatPay(1, 1); + + $options = array(); + $options['transaction_id'] = $order_product['escrow_trade_no']; + $options['out_refund_no'] = (string)$product_refund_no; + $options['reason'] = $refund_reason; + $options['amount'] = [ + 'refund' => (int)($order_product['payment_amount_total'] * 100), + 'total' => (int)($order_product['payment_amount_total'] * 100), + 'currency' => "CNY", + ]; + + $result = $WechatPay->refund($options); + + // 处理订单退款状态 + // 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭) + // 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常) + $success_time = ""; + if ($result['status'] == "SUCCESS") { + // 退款成功 + $refund_status = 3; + $success_time = $result['success_time']; + } elseif ($result['status'] == "CLOSED") { + // 退款关闭 + $refund_status = 5; + } elseif ($result['status'] == "PROCESSING") { + // 退款处理中 + $refund_status = 2; + } elseif ($result['status'] == "ABNORMAL") { + // 退款异常,此情况不处理,进行短信通知 + throw new BusinessException("订单退款状态异常"); + } else { + throw new BusinessException("订单退款状态错误"); + } + + $refund_id = $result['refund_id']; + + }else{ + // 模拟退款 + $refund_status = 3; + $refund_id = "模拟退款:" . $generator->generate(); + $success_time = date("Y-m-d H:i:s", time()); + } + + // 新增退款表 + $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'] = $product_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); + } } \ No newline at end of file