notifyConsumer.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 消息通知消费者
  4. * 处理各种消息通知:新订单、新客户、充值、销账、配送状态变更等
  5. */
  6. namespace common\components\rabbitmq;
  7. use biz\wx\classes\WxMessageClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizHd\device\classes\HdDeviceClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\shop\classes\ShopClass;
  12. use common\components\noticeUtil;
  13. use common\components\push;
  14. use mikemadisonweb\rabbitmq\components\ConsumerInterface;
  15. use PhpAmqpLib\Message\AMQPMessage;
  16. class notifyConsumer extends baseConsumer
  17. {
  18. /**
  19. * 执行消费者逻辑
  20. *
  21. * @param AMQPMessage $msg 消息对象
  22. * @return string 消息处理结果
  23. *
  24. * ConsumerInterface::MSG_ACK - 确认消息(标记为已处理)并从队列中删除
  25. * ConsumerInterface::MSG_REJECT - 拒绝并从队列中删除消息
  26. * ConsumerInterface::MSG_REQUEUE - 拒绝并重新入队消息
  27. */
  28. public function execute(AMQPMessage $msg)
  29. {
  30. try {
  31. $this->ensureDbConnection();
  32. // 反序列化消息体
  33. $data = unserialize($msg->body);
  34. if (!is_array($data)) {
  35. noticeUtil::push("通知的消费者报错:Invalid notify message format: {$msg->body}", '15280215347');
  36. return ConsumerInterface::MSG_REJECT;
  37. }
  38. print_r($data);
  39. // 根据通知类型分发处理
  40. $type = $data['type'] ?? null;
  41. $result = $this->runWithDbReconnect(function () use ($type, $data) {
  42. switch ($type) {
  43. case 'ghs_new_order_notify':
  44. //供货商的新订单通知
  45. return $this->ghsNewOrderNotify($data);
  46. case 'hd_new_order_notify':
  47. //花店的新订单通知
  48. return $this->hdNewOrderNotify($data);
  49. case 'hd_new_cg_notify':
  50. //花店的新采购单通知
  51. return $this->hdNewCgNotify($data);
  52. case 'ghs_pt_error':
  53. //供货商跑腿异常通知
  54. return $this->ghsPtErrorAction($data);
  55. case 'hd_pt_error':
  56. //花店跑腿异常通知
  57. return $this->hdPtErrorAction($data);
  58. default:
  59. noticeUtil::push("通知的消费者提示:Unknown notify type: {$type}", '15280215347');
  60. return false;
  61. }
  62. });
  63. if ($result) {
  64. return ConsumerInterface::MSG_ACK;
  65. } else {
  66. noticeUtil::push("通知的消费者提示:Notify message processing failed", '15280215347');
  67. return ConsumerInterface::MSG_REQUEUE;
  68. }
  69. } catch (\Exception $e) {
  70. noticeUtil::push("Notify consumer exception: " . $e->getMessage(), '15280215347');
  71. return ConsumerInterface::MSG_REQUEUE;
  72. }
  73. }
  74. private function ghsPtErrorAction($data)
  75. {
  76. $orderId = $data['orderId'] ?? 0;
  77. $event = $data['event'] ?? '';
  78. $order = OrderClass::getById($orderId, true);
  79. if (!empty($order)) {
  80. $sendNum = $order->sendNum ?? '';
  81. $result = $event . ',取货号 ' . $sendNum;
  82. $customName = $order->customName ?? '';
  83. $shopId = $order->shopId ?? '';
  84. $shop = ShopClass::getById($shopId, true);
  85. if (!empty($shop)) {
  86. WxMessageClass::expressErrorInform($shop, $customName, $orderId, $result);
  87. }
  88. }
  89. return true;
  90. }
  91. private function hdPtErrorAction($data)
  92. {
  93. $orderId = $data['orderId'] ?? 0;
  94. $event = $data['event'] ?? '';
  95. $order = \bizHd\order\classes\OrderClass::getById($orderId, true);
  96. if (!empty($order)) {
  97. $sendNum = $order->sendNum ?? '';
  98. $result = $event . ',取货号 ' . $sendNum;
  99. $customName = $order->customName ?? '';
  100. $shopId = $order->shopId ?? '';
  101. $shop = ShopClass::getById($shopId, true);
  102. if (!empty($shop)) {
  103. WxMessageClass::expressErrorInform($shop, $customName, $orderId, $result);
  104. }
  105. }
  106. return true;
  107. }
  108. private function ghsNewOrderNotify($data)
  109. {
  110. $orderId = $data['orderId'] ?? 0;
  111. if (empty($orderId)) {
  112. return false;
  113. }
  114. $order = OrderClass::getById($orderId, true);
  115. if (empty($order)) {
  116. return false;
  117. }
  118. $shopId = $order->shopId ?? 0;
  119. $shop = ShopClass::getById($shopId, true);
  120. if (empty($shop)) {
  121. return false;
  122. }
  123. if ($shop->actTime != strtotime(date('Y-m-d'))) {
  124. // actTime 活跃时间,只记录以天为单位的时候戳,用于花店注册时推荐批发店
  125. $shop->actTime = strtotime(date('Y-m-d'));
  126. $shop->save();
  127. }
  128. WxMessageClass::ghsHasNewOrderInform($shop, $order);
  129. // 向供货商App发通知
  130. $cgId = $order->purchaseId ?? 0;
  131. $cg = PurchaseClass::getById($cgId, true);
  132. $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
  133. $push = new push('ghs', push::MSG_TYPE_ORDER);
  134. $push->ghsOrderMessage($shop, $cgShop, $order);
  135. return true;
  136. }
  137. private function hdNewOrderNotify($data)
  138. {
  139. return true;
  140. }
  141. private function hdNewCgNotify($data)
  142. {
  143. $cgId = $data['cgId'] ?? 0;
  144. $cg = PurchaseClass::getById($cgId, true);
  145. $shopId = $cg->shopId ?? 0;
  146. $shop = ShopClass::getById($shopId, true);
  147. $allDevices = HdDeviceClass::getAllByCondition(['shopId' => $shopId, 'status' => 1]);
  148. $cids = array_column($allDevices, 'clientId');
  149. $title = '买花成功';
  150. $shopName = $shop->merchantName . ($shop->shopName != '首店' ? '(' . $shop->shopName . ')' : '');
  151. $content = $shopName . ' ¥' . $cg->actPrice;
  152. $payload = [
  153. "page" => "pagesPurchase/purDetails",
  154. "params" => ["id" => $cgId]
  155. ];
  156. $push = new push('hd', push::MSG_TYPE_ORDER);
  157. $push->pushByCloud($cids, $title, $content, $payload);
  158. //$str = implode(',', $cids);
  159. //noticeUtil::push("需要通知:" . $content . ' ' . $str, '15280215347');
  160. return true;
  161. }
  162. }