stockConsumer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $ptType = $data['ptType'] ?? 'ghs';
  89. $productClass = $ptType == 'ghs' ? ProductClass::class : hdProductClass::class;
  90. // 旧消息直接忽略,避免“先到期的旧消息”提前清空
  91. if (!$productClass::checkLimitBuyClearMessage($productId, $clearAt)) {
  92. return true;
  93. }
  94. $recurring = intval($data['recurring'] ?? 0);
  95. if ($recurring == 1) {
  96. $result = $productClass::clearLimitBuyRecordByProductId($productId);
  97. if ($result) {
  98. $intervalDays = intval($data['intervalDays'] ?? 0);
  99. $clearHour = intval($data['clearHour'] ?? -1);
  100. if ($intervalDays <= 0 || $clearHour < 0 || $clearHour > 23) {
  101. $config = $productClass::getLimitBuyClearLoopConfigByProductId($productId);
  102. $intervalDays = intval($config['intervalDays'] ?? 0);
  103. $clearHour = intval($config['clearHour'] ?? -1);
  104. }
  105. if ($intervalDays > 0 && $clearHour >= 0 && $clearHour <= 23) {
  106. $productClass::createRecurringLimitBuyCache($productId, $intervalDays, $clearHour);
  107. } else {
  108. $productClass::clearLimitBuyClearMark($productId);
  109. noticeUtil::push('循环限购清理成功但缺少下一次调度配置: ' . json_encode([
  110. 'ptType' => $ptType,
  111. 'productId' => $productId,
  112. 'clearAt' => $clearAt,
  113. ], JSON_UNESCAPED_UNICODE));
  114. }
  115. } else {
  116. noticeUtil::push('限购字段清理失败: ' . json_encode([
  117. 'ptType' => $ptType,
  118. 'productId' => $productId,
  119. 'clearAt' => $clearAt,
  120. ], JSON_UNESCAPED_UNICODE));
  121. }
  122. return $result;
  123. }
  124. $result = $productClass::clearLimitBuyByProductId($productId);
  125. if ($result) {
  126. $productClass::clearLimitBuyClearMark($productId);
  127. } else {
  128. noticeUtil::push('限购字段清理失败: ' . json_encode([
  129. 'ptType' => $ptType,
  130. 'productId' => $productId,
  131. 'clearAt' => $clearAt,
  132. ], JSON_UNESCAPED_UNICODE));
  133. }
  134. return $result;
  135. }
  136. }