CallBackHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace common\components\delivery\platform\huolala;
  3. use bizGhs\express\classes\GhsDeliveryOrderClass;
  4. use bizGhs\order\classes\OrderClass;
  5. use Yii;
  6. class CallBackHandler
  7. {
  8. /**
  9. * 货拉拉状态回调入口
  10. *
  11. * @param array $data
  12. * @return bool
  13. */
  14. public function handler($data)
  15. {
  16. try {
  17. Yii::info('货拉拉回调 - 原始数据: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'huolala-callback');
  18. if (empty($data) || empty($data['data']) || !is_array($data['data'])) {
  19. Yii::error('货拉拉回调参数异常: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'huolala-callback');
  20. return false;
  21. }
  22. $bizData = $data['data'];
  23. $eventType = $bizData['event_type'] ?? '';
  24. $orderDisplayId = $bizData['order_display_id'] ?? '';
  25. if ($eventType === '' || $orderDisplayId === '') {
  26. Yii::error('货拉拉回调缺少必要字段: ' . json_encode($bizData, JSON_UNESCAPED_UNICODE), 'huolala-callback');
  27. return false;
  28. }
  29. $orderStatus = $this->mapEventTypeToOrderStatus($eventType);
  30. if ($orderStatus === null) {
  31. Yii::warning(sprintf('货拉拉事件未配置状态映射: event_type=%s, order_display_id=%s', $eventType, $orderDisplayId), 'huolala-callback');
  32. return true;
  33. }
  34. $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
  35. 'deliveryId' => 'huolala',
  36. 'orderId' => $orderDisplayId,
  37. ], true);
  38. if (!$deliveryOrder) {
  39. Yii::error('货拉拉配送订单不存在: order_display_id=' . $orderDisplayId, 'huolala-callback');
  40. return false;
  41. }
  42. $deliveryOrder->orderStatus = $orderStatus;
  43. if (!$deliveryOrder->save()) {
  44. Yii::error('更新配送订单状态失败: ' . json_encode($deliveryOrder->getErrors(), JSON_UNESCAPED_UNICODE), 'huolala-callback');
  45. return false;
  46. }
  47. $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
  48. if ($order) {
  49. $order->sendStatus = $orderStatus;
  50. if (!$order->save()) {
  51. Yii::warning('更新主订单配送状态失败: ' . json_encode($order->getErrors(), JSON_UNESCAPED_UNICODE), 'huolala-callback');
  52. }
  53. // 确认送达或取消订单,更新主订单状态
  54. if ($orderStatus == 5) {
  55. OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
  56. }
  57. if ($orderStatus == -1 || $orderStatus == -2) {
  58. OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
  59. }
  60. } else {
  61. Yii::warning('未找到主订单: ghsOrderId=' . $deliveryOrder->ghsOrderId, 'huolala-callback');
  62. }
  63. Yii::info(sprintf('货拉拉配送状态更新成功: order_display_id=%s, event_type=%s, orderStatus=%s', $orderDisplayId, $eventType, $orderStatus), 'huolala-callback');
  64. return true;
  65. } catch (\Throwable $e) {
  66. Yii::error('货拉拉回调处理异常: ' . $e->getMessage() . ' | Trace: ' . $e->getTraceAsString(), 'huolala-callback');
  67. return false;
  68. }
  69. }
  70. /**
  71. * 事件到系统配送状态的映射
  72. *
  73. * @param string $eventType
  74. * @return int|null
  75. */
  76. protected function mapEventTypeToOrderStatus($eventType)
  77. {
  78. $map = [
  79. 'order_cancel' => -1,
  80. 'pickup' => 20,
  81. 'driver_reject' => 2,
  82. 'order_complete' => 5,
  83. 'set_order_abnormal' => -2,
  84. 'loaded' => 4,
  85. 'auto_confirm_bill_pay' => 5,
  86. 'no_load_cancel' => -1,
  87. 'no_complete_cancel' => -1,
  88. 'loading' => 3,
  89. 'unloading' => 4,
  90. 'unload_finish' => 4,
  91. 'order_auto_complete' => 5,
  92. 'order_timeout' => -1,
  93. 'prepay_ok' => 2,
  94. 'rear_pay_ok' => 5,
  95. 'cash_received' => 5,
  96. ];
  97. return $map[$eventType] ?? null;
  98. }
  99. }