| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace common\components\delivery\platform\huolala;
- use bizGhs\express\classes\GhsDeliveryOrderClass;
- use bizGhs\order\classes\OrderClass;
- use Yii;
- class CallBackHandler
- {
- /**
- * 货拉拉状态回调入口
- *
- * @param array $data
- * @return bool
- */
- public function handler($data)
- {
- try {
- Yii::info('货拉拉回调 - 原始数据: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'huolala-callback');
- if (empty($data) || empty($data['data']) || !is_array($data['data'])) {
- Yii::error('货拉拉回调参数异常: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'huolala-callback');
- return false;
- }
- $bizData = $data['data'];
- $eventType = $bizData['event_type'] ?? '';
- $orderDisplayId = $bizData['order_display_id'] ?? '';
- if ($eventType === '' || $orderDisplayId === '') {
- Yii::error('货拉拉回调缺少必要字段: ' . json_encode($bizData, JSON_UNESCAPED_UNICODE), 'huolala-callback');
- return false;
- }
- $orderStatus = $this->mapEventTypeToOrderStatus($eventType);
- if ($orderStatus === null) {
- Yii::warning(sprintf('货拉拉事件未配置状态映射: event_type=%s, order_display_id=%s', $eventType, $orderDisplayId), 'huolala-callback');
- return true;
- }
- $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
- 'deliveryId' => 'huolala',
- 'orderId' => $orderDisplayId,
- ], true);
- if (!$deliveryOrder) {
- Yii::error('货拉拉配送订单不存在: order_display_id=' . $orderDisplayId, 'huolala-callback');
- return false;
- }
- $deliveryOrder->orderStatus = $orderStatus;
- if (!$deliveryOrder->save()) {
- Yii::error('更新配送订单状态失败: ' . json_encode($deliveryOrder->getErrors(), JSON_UNESCAPED_UNICODE), 'huolala-callback');
- return false;
- }
- $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
- if ($order) {
- $order->sendStatus = $orderStatus;
- if (!$order->save()) {
- Yii::warning('更新主订单配送状态失败: ' . json_encode($order->getErrors(), JSON_UNESCAPED_UNICODE), 'huolala-callback');
- }
- // 确认送达或取消订单,更新主订单状态
- if ($orderStatus == 5) {
- OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
- }
- if ($orderStatus == -1 || $orderStatus == -2) {
- OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
- }
- } else {
- Yii::warning('未找到主订单: ghsOrderId=' . $deliveryOrder->ghsOrderId, 'huolala-callback');
- }
- Yii::info(sprintf('货拉拉配送状态更新成功: order_display_id=%s, event_type=%s, orderStatus=%s', $orderDisplayId, $eventType, $orderStatus), 'huolala-callback');
- return true;
- } catch (\Throwable $e) {
- Yii::error('货拉拉回调处理异常: ' . $e->getMessage() . ' | Trace: ' . $e->getTraceAsString(), 'huolala-callback');
- return false;
- }
- }
- /**
- * 事件到系统配送状态的映射
- *
- * @param string $eventType
- * @return int|null
- */
- protected function mapEventTypeToOrderStatus($eventType)
- {
- $map = [
- 'order_cancel' => -1,
- 'pickup' => 20,
- 'driver_reject' => 2,
- 'order_complete' => 5,
- 'set_order_abnormal' => -2,
- 'loaded' => 4,
- 'auto_confirm_bill_pay' => 5,
- 'no_load_cancel' => -1,
- 'no_complete_cancel' => -1,
- 'loading' => 3,
- 'unloading' => 4,
- 'unload_finish' => 4,
- 'order_auto_complete' => 5,
- 'order_timeout' => -1,
- 'prepay_ok' => 2,
- 'rear_pay_ok' => 5,
- 'cash_received' => 5,
- ];
- return $map[$eventType] ?? null;
- }
- }
|