| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- /**
- * 取消限购消费者
- * 处理取消限购操作
- */
- namespace common\components\rabbitmq;
- use bizGhs\product\classes\ProductClass;
- use common\components\noticeUtil;
- use mikemadisonweb\rabbitmq\components\ConsumerInterface;
- use PhpAmqpLib\Message\AMQPMessage;
- class cancelLimitBuyConsumer extends baseConsumer
- {
- /**
- * 执行消费者逻辑
- *
- * @param AMQPMessage $msg 消息对象
- * @return string 消息处理结果
- *
- * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
- * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
- * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
- */
- public function execute(AMQPMessage $msg)
- {
- try {
- $this->ensureDbConnection();
- // 反序列化消息体
- $data = unserialize($msg->body);
- if (!is_array($data)) {
- noticeUtil::push("取消限购的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
- return ConsumerInterface::MSG_REJECT;
- }
- print_r($data);
- // 根据操作类型分发处理
- $type = $data['type'] ?? null;
- $result = $this->runWithDbReconnect(function () use ($type, $data) {
- switch ($type) {
- case 'limit_buy_clear':
- return $this->clearOrderItemLimitBuy($data);
- default:
- noticeUtil::push("取消限购的消费者报错,未知 type: {$type}");
- return false;
- }
- });
- if ($result) {
- return ConsumerInterface::MSG_ACK;
- } else {
- noticeUtil::push("取消限购的消费者报错:Stock message processing failed");
- return ConsumerInterface::MSG_REQUEUE;
- }
- } catch (\Exception $e) {
- noticeUtil::push("取消限购的消费者报错:" . $e->getMessage());
- //return ConsumerInterface::MSG_REQUEUE;
- return ConsumerInterface::MSG_ACK;
- }
- }
- /**
- * 清空订单项限购值
- *
- * @param array $data
- * @return bool
- */
- private function clearOrderItemLimitBuy($data)
- {
- $productId = intval($data['productId'] ?? 0);
- if ($productId <= 0) {
- noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 productId', '15280215347');
- return true;
- }
- $clearAt = intval($data['clearAt'] ?? 0);
- if ($clearAt <= 0) {
- noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 clearAt', '15280215347');
- return true;
- }
- // 旧消息直接忽略,避免“先到期的旧消息”提前清空
- if (!ProductClass::checkLimitBuyClearMessage($productId, $clearAt)) {
- return true;
- }
- $result = ProductClass::clearLimitBuyByProductId($productId);
- if ($result) {
- ProductClass::clearLimitBuyClearMark($productId);
- }
- return $result;
- }
- }
|