stockConsumer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * 库存管理消费者
  4. * 处理花材库存补充、扣减等库存操作
  5. */
  6. namespace common\components\rabbitmq;
  7. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  8. use PhpAmqpLib\Message\AMQPMessage;
  9. use Yii;
  10. class stockConsumer implements ConsumerInterface
  11. {
  12. /**
  13. * 执行消费者逻辑
  14. *
  15. * @param AMQPMessage $msg 消息对象
  16. * @return string 消息处理结果
  17. *
  18. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  19. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  20. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  21. */
  22. public function execute(AMQPMessage $msg)
  23. {
  24. try {
  25. // 反序列化消息体
  26. $data = unserialize($msg->body);
  27. if (!is_array($data)) {
  28. Yii::error("Invalid stock message format: {$msg->body}", 'rabbitmq.stock');
  29. return ConsumerInterface::MSG_REJECT;
  30. }
  31. Yii::info("Processing stock message: " . json_encode($data), 'rabbitmq.stock');
  32. // 根据操作类型分发处理
  33. $action = $data['action'] ?? null;
  34. switch ($action) {
  35. case 'add':
  36. $result = $this->handleAddStock($data);
  37. break;
  38. case 'reduce':
  39. $result = $this->handleReduceStock($data);
  40. break;
  41. case 'adjust':
  42. $result = $this->handleAdjustStock($data);
  43. break;
  44. default:
  45. Yii::warning("Unknown stock action: {$action}", 'rabbitmq.stock');
  46. $result = false;
  47. }
  48. if ($result) {
  49. Yii::info("Stock message processed successfully", 'rabbitmq.stock');
  50. return ConsumerInterface::MSG_ACK;
  51. } else {
  52. Yii::error("Stock message processing failed", 'rabbitmq.stock');
  53. return ConsumerInterface::MSG_REQUEUE;
  54. }
  55. } catch (\Exception $e) {
  56. Yii::error("Stock consumer exception: " . $e->getMessage(), 'rabbitmq.stock');
  57. return ConsumerInterface::MSG_REQUEUE;
  58. }
  59. }
  60. /**
  61. * 处理库存补充(增加)
  62. *
  63. * @param array $data 消息数据,包含 material_id, quantity 等
  64. * @return bool 处理结果
  65. */
  66. private function handleAddStock($data)
  67. {
  68. // TODO: 实现库存增加业务逻辑
  69. // 调用对应的 Service 或 Class 层处理
  70. // 例如: StockClass::addStock($data);
  71. return true;
  72. }
  73. /**
  74. * 处理库存扣减
  75. *
  76. * @param array $data 消息数据,包含 material_id, quantity 等
  77. * @return bool 处理结果
  78. */
  79. private function handleReduceStock($data)
  80. {
  81. // TODO: 实现库存扣减业务逻辑
  82. // 调用对应的 Service 或 Class 层处理
  83. // 例如: StockClass::reduceStock($data);
  84. return true;
  85. }
  86. /**
  87. * 处理库存调整
  88. *
  89. * @param array $data 消息数据,包含 material_id, quantity, type 等
  90. * @return bool 处理结果
  91. */
  92. private function handleAdjustStock($data)
  93. {
  94. // TODO: 实现库存调整业务逻辑
  95. // 调用对应的 Service 或 Class 层处理
  96. // 例如: StockClass::adjustStock($data);
  97. return true;
  98. }
  99. }