cancelLimitBuyConsumer.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * 取消限购消费者
  4. * 处理取消限购操作
  5. */
  6. namespace common\components\rabbitmq;
  7. use bizGhs\product\classes\ProductClass;
  8. use common\components\noticeUtil;
  9. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  10. use PhpAmqpLib\Message\AMQPMessage;
  11. class cancelLimitBuyConsumer extends baseConsumer
  12. {
  13. /**
  14. * 执行消费者逻辑
  15. *
  16. * @param AMQPMessage $msg 消息对象
  17. * @return string 消息处理结果
  18. *
  19. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  20. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  21. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  22. */
  23. public function execute(AMQPMessage $msg)
  24. {
  25. try {
  26. $this->ensureDbConnection();
  27. // 反序列化消息体
  28. $data = unserialize($msg->body);
  29. if (!is_array($data)) {
  30. noticeUtil::push("取消限购的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  31. return ConsumerInterface::MSG_REJECT;
  32. }
  33. print_r($data);
  34. // 根据操作类型分发处理
  35. $type = $data['type'] ?? null;
  36. $result = $this->runWithDbReconnect(function () use ($type, $data) {
  37. switch ($type) {
  38. case 'limit_buy_clear':
  39. return $this->clearOrderItemLimitBuy($data);
  40. default:
  41. noticeUtil::push("取消限购的消费者报错,未知 type: {$type}");
  42. return false;
  43. }
  44. });
  45. if ($result) {
  46. return ConsumerInterface::MSG_ACK;
  47. } else {
  48. noticeUtil::push("取消限购的消费者报错:Stock message processing failed");
  49. return ConsumerInterface::MSG_REQUEUE;
  50. }
  51. } catch (\Exception $e) {
  52. noticeUtil::push("取消限购的消费者报错:" . $e->getMessage());
  53. //return ConsumerInterface::MSG_REQUEUE;
  54. return ConsumerInterface::MSG_ACK;
  55. }
  56. }
  57. /**
  58. * 清空订单项限购值
  59. *
  60. * @param array $data
  61. * @return bool
  62. */
  63. private function clearOrderItemLimitBuy($data)
  64. {
  65. $productId = intval($data['productId'] ?? 0);
  66. if ($productId <= 0) {
  67. noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 productId', '15280215347');
  68. return true;
  69. }
  70. $clearAt = intval($data['clearAt'] ?? 0);
  71. if ($clearAt <= 0) {
  72. noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 clearAt', '15280215347');
  73. return true;
  74. }
  75. // 旧消息直接忽略,避免“先到期的旧消息”提前清空
  76. if (!ProductClass::checkLimitBuyClearMessage($productId, $clearAt)) {
  77. return true;
  78. }
  79. $result = ProductClass::clearLimitBuyByProductId($productId);
  80. if ($result) {
  81. ProductClass::clearLimitBuyClearMark($productId);
  82. }
  83. return $result;
  84. }
  85. }