80 lines
2.3 KiB
PHP
80 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Amqp\Consumer;
|
|
|
|
use App\Model\Coupon;
|
|
use App\Model\CouponGrant;
|
|
use App\Services\CouponService;
|
|
use App\Utils\Log;
|
|
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
|
use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
|
use Hyperf\Amqp\Message\Type;
|
|
use Hyperf\Amqp\Result;
|
|
use Hyperf\Amqp\Annotation\Consumer;
|
|
use Hyperf\Amqp\Message\ConsumerMessage;
|
|
use Hyperf\DbConnection\Db;
|
|
use PhpAmqpLib\Message\AMQPMessage;
|
|
|
|
/**
|
|
* 发放用户优惠卷
|
|
*/
|
|
#[Consumer(nums: 1)]
|
|
class GrantUserCouponDelayDirectConsumer extends ConsumerMessage
|
|
{
|
|
use ProducerDelayedMessageTrait;
|
|
use ConsumerDelayedMessageTrait;
|
|
|
|
protected string $exchange = 'amqp.delay.direct';
|
|
|
|
protected ?string $queue = 'grant.user.coupon.delay.queue';
|
|
|
|
protected string $type = Type::DIRECT; //Type::FANOUT;
|
|
|
|
protected string|array $routingKey = 'GrantUserCoupon';
|
|
|
|
public function consumeMessage($data, AMQPMessage $message): string
|
|
{
|
|
Log::getInstance("queue-GrantUserCoupon")->info("开始执行 延迟发放用户优惠卷 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
|
|
// 检测入参参数
|
|
if (empty($data['coupon_id'])) {
|
|
Db::rollBack();
|
|
Log::getInstance("queue-GrantUserCoupon")->error("参数错误");
|
|
return Result::DROP;
|
|
}
|
|
|
|
if (empty($data['user_id'])) {
|
|
Db::rollBack();
|
|
Log::getInstance("queue-GrantUserCoupon")->error("参数错误");
|
|
return Result::DROP;
|
|
}
|
|
|
|
$grant_quantity = 1;
|
|
if (!empty($data['grant_quantity'])) {
|
|
$grant_quantity = $data['grant_quantity'];
|
|
}
|
|
|
|
Db::beginTransaction();
|
|
try {
|
|
$couponService = new CouponService();
|
|
$res = $couponService->GrantUserCoupon($data["coupon_id"],$data['user_id'],$grant_quantity);
|
|
if (!$res){
|
|
// 发放失败
|
|
Db::rollBack();
|
|
Log::getInstance("queue-GrantUserCoupon")->error("发放优惠卷失败");
|
|
return Result::DROP;
|
|
}
|
|
}catch (\Throwable $e){
|
|
Db::rollBack();
|
|
Log::getInstance("queue-GrantUserCoupon")->error($e->getMessage());
|
|
return Result::DROP;
|
|
}
|
|
|
|
Db::commit();
|
|
|
|
return Result::ACK;
|
|
}
|
|
}
|