Files
hyperf_service/app/Amqp/Consumer/RefundOrderConsumer.php
2025-03-21 15:14:43 +08:00

83 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Amqp\Consumer;
use App\Constants\Common\RefundCode;
use App\Lib\Log;
use App\Model\Order;
use App\Service\Amqp\Refund\FullRefundOrderService;
use App\Service\Amqp\Refund\PartialRefundOrderService;
use App\Service\Amqp\Refund\RefundService;
use App\Service\ServiceTrait\Api\OrderTrait;
use Exception;
use Hyperf\Amqp\Message\Type;
use Hyperf\Amqp\Result;
use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Di\Annotation\Inject;
use PhpAmqpLib\Message\AMQPMessage;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
#[Consumer(exchange: 'RefundOrder', routingKey: 'RefundOrder', queue: 'RefundOrder.direct', name: "RefundOrderConsumer", nums: 1)]
class RefundOrderConsumer extends ConsumerMessage
{
use OrderTrait;
/**
* @var Type|string 消息类型
*/
protected Type|string $type = Type::DIRECT;
/**
* @var Log $log
*/
#[Inject]
protected Log $log;
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @param $data
* @param AMQPMessage $message
* @return Result
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function consumeMessage($data, AMQPMessage $message): Result
{
if (!$data['order_id'] || !$data['type'] || !$data['reason']) {
$this->log->error('RefundOrderConsumer:error:NoData:'.json_encode($data));
return Result::ACK;
}
try {
$service = match ($data['type']) {
RefundCode::FULL_GOOD_REFUND => new FullRefundOrderService(),
// RefundCode::PARTIAL_GOOD_REFUND => new PartialRefundOrderService(),
RefundCode::PARTIAL_GOOD_REFUND => throw new Exception('部分退款直接调用后台退款接口'),
// RefundCode::BALANCE_REFUND => $service = new RefundService(),
};
$service->orderId = (int)$data['order_id'];
$service->reason = $data['reason'];
$service->type = $data['type'];
$service->handle();
} catch (Exception $e) {
$this->log->error('RefundOrderConsumer:error:'.$e->getMessage().':data:'.json_encode($data));
}
return Result::ACK;
}
}