310 lines
12 KiB
PHP
310 lines
12 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Amqp\Consumer;
|
||
|
||
use App\Model\OrderInquiry;
|
||
use App\Model\OrderInquiryCoupon;
|
||
use App\Model\OrderPrescription;
|
||
use App\Model\OrderPrescriptionProduct;
|
||
use App\Model\OrderProduct;
|
||
use App\Model\OrderProductItem;
|
||
use App\Model\Product;
|
||
use App\Model\ProductPlatformAmount;
|
||
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']);
|
||
}elseif ($data['order_type'] == 2){
|
||
// 药品订单取消
|
||
$result = $this->cancelUnpayProductOrder($data['order_no']);
|
||
}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()->error("取消未支付订单失败:" . $result['message']);
|
||
return Result::ACK;// 销毁
|
||
}
|
||
|
||
Db::commit();
|
||
Log::getInstance()->error("取消未支付订单 队列执行成功");
|
||
return Result::ACK;
|
||
} catch (\Exception $e) {
|
||
Db::rollBack();
|
||
Log::getInstance()->error("取消未支付订单执行失败:" . $e->getMessage());
|
||
return Result::ACK; // 重回队列
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取消未支付的问诊订单
|
||
* @param string|int $order_no 系统订单编号
|
||
* @return array
|
||
*/
|
||
public function cancelUnpayInquiryOrder(string|int $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;
|
||
}
|
||
|
||
/**
|
||
* 取消未支付的药品订单
|
||
* @param string|int $order_no 系统订单编号
|
||
* @return array
|
||
*/
|
||
public function cancelUnpayProductOrder(string|int $order_no): array
|
||
{
|
||
$result = array();
|
||
$result['status'] = 1;
|
||
$result['message'] = "成功";
|
||
|
||
// 获取药品订单数据
|
||
$params = array();
|
||
$params['order_product_no'] = $order_no;
|
||
$order_product = OrderProduct::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);
|
||
|
||
// 获取订单商品订单列表
|
||
$params = array();
|
||
$params['order_product_id'] = $order_product['order_product_id'];
|
||
$order_product_item = OrderProductItem::getList($params);
|
||
if (empty($order_product_item)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单商品订单列表";
|
||
return $result;
|
||
}
|
||
|
||
foreach ($order_product_item as $item){
|
||
// 释放锁定库存
|
||
$params = array();
|
||
$params['product_id'] = $item['product_id'];
|
||
$product = Product::getWithAmountOne($params);
|
||
if (empty($product)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单商品订单列表";
|
||
return $result;
|
||
}
|
||
|
||
// 库存+1
|
||
$params = array();
|
||
$params['amount_id'] = $product['ProductPlatformAmount']['amount_id'];
|
||
ProductPlatformAmount::inc($params, 'stock', (float)$item['amount']);
|
||
|
||
// 锁定库存-1
|
||
ProductPlatformAmount::dec($params, 'lock_stock', (float)$item['amount']);
|
||
}
|
||
|
||
// 获取处方数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_product['order_prescription_id'];
|
||
$order_prescription = OrderPrescription::getOne($params);
|
||
if (empty($order_prescription)){
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单处方";
|
||
return $result;
|
||
}
|
||
|
||
// 修改处方状态为未使用
|
||
if ($order_prescription['prescription_status'] == 4){
|
||
$data = array();
|
||
$data['prescription_status'] = 2;
|
||
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
OrderPrescription::edit($params, $data);
|
||
}
|
||
|
||
// 获取处方商品数据
|
||
$params = array();
|
||
$params['order_prescription_id'] = $order_prescription['order_prescription_id'];
|
||
$order_prescription_product = OrderPrescriptionProduct::getList($params);
|
||
if (empty($order_prescription_product)) {
|
||
$result['status'] = 0;
|
||
$result['message'] = "取消未支付的问诊订单失败:未查询到对应订单处方商品数据";
|
||
return $result;
|
||
}
|
||
|
||
// 修改处方商品为未使用
|
||
foreach ($order_prescription_product as $item){
|
||
$data = array();
|
||
$data['use_status'] = 0;
|
||
|
||
$params = array();
|
||
$params['prescription_product_id'] = $item['prescription_product_id'];
|
||
OrderPrescriptionProduct::edit($params, $data);
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
}
|