238 lines
8.7 KiB
PHP
238 lines
8.7 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCoupon;
|
||
use App\Model\OrderProduct;
|
||
use App\Model\UserCoupon;
|
||
use App\Utils\Log;
|
||
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
||
use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
||
use Hyperf\Amqp\Result;
|
||
use Hyperf\Amqp\Annotation\Consumer;
|
||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||
use Hyperf\DbConnection\Db;
|
||
use Hyperf\Snowflake\IdGeneratorInterface;
|
||
use PhpAmqpLib\Message\AMQPMessage;
|
||
use Hyperf\Amqp\Message\Type;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
/**
|
||
* 取消未支付订单消费者-延迟队列
|
||
*/
|
||
#[Consumer(nums: 1)]
|
||
class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage
|
||
{
|
||
use ProducerDelayedMessageTrait;
|
||
use ConsumerDelayedMessageTrait;
|
||
|
||
protected string $exchange = 'amqp.delay.direct';
|
||
|
||
protected ?string $queue = 'cancel.unpay.orders.delay.queue';
|
||
|
||
protected string $type = Type::DIRECT; //Type::FANOUT;
|
||
|
||
protected string|array $routingKey = 'CancelUnpayOrders';
|
||
|
||
public function consumeMessage($data, AMQPMessage $message): string
|
||
{
|
||
Log::getInstance()->error("开始执行 取消未支付订单 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
||
Db::beginTransaction();
|
||
|
||
try {
|
||
if ($data['order_type'] == 1){
|
||
// 问诊订单取消
|
||
$result = $this->cancelUnpayInquiryOrder($data['order_no']);
|
||
if ($result['status'] == 0){
|
||
Db::rollBack();
|
||
Log::getInstance()->error("取消未支付订单失败:" . $result['message']);
|
||
return Result::DROP;// 销毁
|
||
}elseif ($result['status'] == 2){
|
||
Db::rollBack();
|
||
Log::getInstance()->error("取消未支付订单失败:" . $result['message']);
|
||
return Result::ACK;// 销毁
|
||
}
|
||
}elseif ($data['order_type'] == 2){
|
||
// 药品订单取消
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
Db::commit();
|
||
Log::getInstance()->error("取消未支付订单 队列执行成功");
|
||
return Result::ACK;
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("取消未支付订单执行失败:" . $e->getMessage());
|
||
return Result::REQUEUE; // 重回队列
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取消未支付的问诊订单
|
||
* @param string $order_no 系统订单编号
|
||
* @return array
|
||
*/
|
||
public function cancelUnpayInquiryOrder(string $order_no): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
|
||
// 获取订单数据
|
||
$params = array();
|
||
$params['inquiry_no'] = $order_no;
|
||
$order_inquiry = OrderInquiry::getOne($params);
|
||
if (empty($order_inquiry)) {
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单数据";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_inquiry['inquiry_status'] == 7) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
$result['status'] = 2;
|
||
$result['message'] = "取消未支付的问诊订单:订单已取消";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_inquiry['inquiry_status'] != 1) {
|
||
// 问诊订单状态(1:待支付 2:待分配 3:待接诊 4:已接诊 5:已完成 6:已结束 7:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单:订单状态为" . $order_inquiry['inquiry_status'] . "无法执行";
|
||
return $result;
|
||
}
|
||
|
||
if (!in_array($order_inquiry['inquiry_refund_status'],[0,4,5])) {
|
||
// 问诊订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单:订单正在退款中";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_inquiry['inquiry_pay_status'] == 2) {
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$result['status'] = 2;
|
||
$result['message'] = "取消未支付的问诊订单:订单已支付";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单删除状态
|
||
if ($order_inquiry['is_delete'] == 1) {
|
||
// 删除状态(0:否 1:是)
|
||
$result['status'] = 2;
|
||
$result['message'] = "取消未支付的问诊订单:订单已被删除";
|
||
return $result;
|
||
}
|
||
|
||
// 取消问诊订单
|
||
$data = array();
|
||
$data['inquiry_status'] = 7;
|
||
$data['inquiry_pay_status'] = 5;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 5; // 取消订单原因(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);
|
||
|
||
// 处理订单优惠卷
|
||
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);
|
||
}
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
|
||
public function cancelUnpayProductOrder(string $order_no): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['order_product_no'] = $order_no;
|
||
$order_product = OrderInquiry::getOne($params);
|
||
if (empty($order_product)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单数据";
|
||
return $result;
|
||
}
|
||
|
||
// 检测订单状态
|
||
if ($order_product['order_product_status'] == 5){
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
$result['status'] = 2;
|
||
$result['message'] = "取消未支付的问诊订单:订单已取消";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_product['order_product_status'] != 1){
|
||
// 订单状态(1:待支付 2:待发货 3:已发货 4:已签收 5:已取消)
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单:订单状态为" . $order_product['order_product_status'] . "无法执行";
|
||
return $result;
|
||
}
|
||
|
||
if (!in_array($order_product['refund_status'],[0,4,5])) {
|
||
// 商品订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭)
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单:订单正在退款中";
|
||
return $result;
|
||
}
|
||
|
||
if ($order_product['inquiry_pay_status'] == 2) {
|
||
// 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
$result['status'] = 2;
|
||
$result['message'] = "取消未支付的问诊订单:订单已支付";
|
||
return $result;
|
||
}
|
||
|
||
// 取消药品订单
|
||
$data = array();
|
||
$data['order_product_status'] = 5;
|
||
$data['cancel_time'] = date("Y-m-d H:i:s",time());
|
||
$data['cancel_reason'] = 3; // 订单取消原因(1:主动取消 2:复核失败/库存不足 3:支付超时
|
||
$data['cancel_remarks'] = 5; // 取消订单原因(1:医生未接诊 2:主动取消 3:无可分配医生 4:客服取消 5:支付超时)
|
||
$data['updated_at'] = date("Y-m-d H:i:s",time());
|
||
|
||
$params = array();
|
||
$params['order_inquiry_id'] = $order_product['order_inquiry_id'];
|
||
OrderProduct::edit($params,$data);
|
||
|
||
//
|
||
}
|
||
}
|