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; } }