stockConsumer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * 库存管理消费者
  4. * 处理花材库存补充、扣减等库存操作
  5. */
  6. namespace common\components\rabbitmq;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizHd\product\classes\ProductClass as hdProductClass;
  9. use common\components\noticeUtil;
  10. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  11. use PhpAmqpLib\Message\AMQPMessage;
  12. use Yii;
  13. class stockConsumer extends baseConsumer
  14. {
  15. /**
  16. * 执行消费者逻辑
  17. *
  18. * @param AMQPMessage $msg 消息对象
  19. * @return string 消息处理结果
  20. *
  21. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  22. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  23. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  24. */
  25. public function execute(AMQPMessage $msg)
  26. {
  27. try {
  28. $this->ensureDbConnection();
  29. // 反序列化消息体
  30. $data = unserialize($msg->body);
  31. if (!is_array($data)) {
  32. noticeUtil::push("库存的消费者报错:Invalid notify message format: {$msg->body}");
  33. return ConsumerInterface::MSG_REJECT;
  34. }
  35. print_r($data);
  36. // 根据操作类型分发处理
  37. $type = $data['type'] ?? null;
  38. if ($type == 'limit_buy_clear') {
  39. Yii::info('限购延迟消息开始消费: ' . json_encode($data, JSON_UNESCAPED_UNICODE), __METHOD__);
  40. Yii::getLogger()->flush(true);
  41. }
  42. switch ($type) {
  43. case 'add':
  44. $result = true;
  45. echo '持久化OK---';
  46. break;
  47. case 'limit_buy_clear':
  48. echo 'limit_buy_clear---';
  49. $result = $this->runWithDbReconnect(function () use ($data) {
  50. return $this->clearOrderItemLimitBuy($data);
  51. });
  52. break;
  53. default:
  54. noticeUtil::push("库存的消费者报错,未知 type: {$type}");
  55. $result = false;
  56. }
  57. if ($result) {
  58. return ConsumerInterface::MSG_ACK;
  59. } else {
  60. noticeUtil::push("库存的消费者报错:Stock message processing failed");
  61. //return ConsumerInterface::MSG_REQUEUE;
  62. return ConsumerInterface::MSG_ACK;
  63. }
  64. } catch (\Exception $e) {
  65. noticeUtil::push("库存的消费者报错:" . $e->getMessage());
  66. //return ConsumerInterface::MSG_REQUEUE;
  67. return ConsumerInterface::MSG_ACK;
  68. }
  69. }
  70. /**
  71. * 清空订单项限购值
  72. *
  73. * @param array $data
  74. * @return bool
  75. */
  76. private function clearOrderItemLimitBuy($data)
  77. {
  78. $productId = intval($data['productId']);
  79. if ($productId <= 0) {
  80. noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 productId');
  81. return true;
  82. }
  83. $clearAt = intval($data['clearAt'] ?? 0);
  84. if ($clearAt <= 0) {
  85. noticeUtil::push('取消限购的消费者报错:limit_buy_clear 缺少 clearAt');
  86. return true;
  87. }
  88. // 旧消息直接忽略,避免“先到期的旧消息”提前清空
  89. if (!ProductClass::checkLimitBuyClearMessage($productId, $clearAt)) {
  90. return true;
  91. }
  92. $ptType = $data['ptType'];
  93. if ($ptType == 'ghs') {
  94. $result = ProductClass::clearLimitBuyByProductId($productId);
  95. if ($result) {
  96. ProductClass::clearLimitBuyClearMark($productId);
  97. } else {
  98. noticeUtil::push('限购字段清理失败: ' . json_encode([
  99. 'productId' => $productId,
  100. 'clearAt' => $clearAt,
  101. ], JSON_UNESCAPED_UNICODE));
  102. }
  103. } else {
  104. $result = hdProductClass::clearLimitBuyByProductId($productId);
  105. if ($result) {
  106. hdProductClass::clearLimitBuyClearMark($productId);
  107. } else {
  108. noticeUtil::push('限购字段清理失败: ' . json_encode([
  109. 'productId' => $productId,
  110. 'clearAt' => $clearAt,
  111. ], JSON_UNESCAPED_UNICODE));
  112. }
  113. }
  114. return $result;
  115. }
  116. }