|
|
@@ -0,0 +1,197 @@
|
|
|
+<?php
|
|
|
+namespace common\components\delivery\platform\shanSong;
|
|
|
+
|
|
|
+use bizGhs\express\classes\GhsDeliveryOrderClass;
|
|
|
+use bizGhs\order\classes\OrderClass;
|
|
|
+use Yii;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 闪送订单状态回调处理类
|
|
|
+ * Class OrderStatusCallBack
|
|
|
+ * @package common\components\delivery\platform\shanSong
|
|
|
+ */
|
|
|
+class OrderStatusCallBack
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 处理闪送订单状态回调
|
|
|
+ *
|
|
|
+ * @param array $data 回调数据
|
|
|
+ * @return bool 处理结果
|
|
|
+ */
|
|
|
+ public function handle($data)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 记录回调原始数据
|
|
|
+ Yii::info('闪送订单状态回调 - 原始数据: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+
|
|
|
+ // 验证必要参数
|
|
|
+ if (empty($data['issOrderNo']) || !isset($data['status'])) {
|
|
|
+ Yii::error('闪送回调参数不完整: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找配送订单
|
|
|
+ $deliveryOrder = GhsDeliveryOrderClass::getByCondition([
|
|
|
+ 'deliveryId' => 'shansong',
|
|
|
+ 'orderId' => $data['issOrderNo']
|
|
|
+ ], true);
|
|
|
+
|
|
|
+ if (!$deliveryOrder) {
|
|
|
+ Yii::error('闪送订单不存在: issOrderNo=' . $data['issOrderNo'], 'shansong-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将闪送平台的订单状态转换为系统内的 orderStatus
|
|
|
+ $subStatus = isset($data['subStatus']) ? $data['subStatus'] : null;
|
|
|
+ $sendStatus = OrderStatusUtil::convertOrderStatus($data['status'], $subStatus);
|
|
|
+
|
|
|
+
|
|
|
+ // 处理骑手信息
|
|
|
+ $this->handleCourierInfo($data, $deliveryOrder);
|
|
|
+ // 处理取消信息
|
|
|
+ $this->handleCancelInfo($data, $deliveryOrder);
|
|
|
+ // 处理费用信息
|
|
|
+ $this->handleFeeInfo($data, $deliveryOrder);
|
|
|
+ // 处理密码信息
|
|
|
+ $this->handlePasswordInfo($data, $deliveryOrder);
|
|
|
+
|
|
|
+ // 更新配送订单状态
|
|
|
+ $deliveryOrder->orderStatus = $sendStatus;
|
|
|
+ $deliveryOrder->save();
|
|
|
+
|
|
|
+ // 更新主订单状态
|
|
|
+ $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendType,sendStatus,deliveryId');
|
|
|
+ if ($order) {
|
|
|
+ $order->sendStatus = $sendStatus;
|
|
|
+ $order->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ Yii::info('闪送订单状态更新成功: issOrderNo=' . $data['issOrderNo'] .
|
|
|
+ ', status=' . $data['status'] .
|
|
|
+ ', sendStatus=' . $sendStatus, 'shansong-callback');
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Yii::error('闪送回调处理异常: ' . $e->getMessage() .
|
|
|
+ ' | Trace: ' . $e->getTraceAsString(), 'shansong-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理骑手信息
|
|
|
+ *
|
|
|
+ * @param array $data 回调数据
|
|
|
+ * @param object $deliveryOrder 配送订单对象
|
|
|
+ */
|
|
|
+ private function handleCourierInfo($data, $deliveryOrder)
|
|
|
+ {
|
|
|
+ if (isset($data['courier']) && is_array($data['courier'])) {
|
|
|
+ $courier = $data['courier'];
|
|
|
+
|
|
|
+ Yii::info($data['issOrderNo'] . '-闪送骑手信息: ' . json_encode([
|
|
|
+ 'name' => $courier['name'] ?? '',
|
|
|
+ 'mobile' => $courier['mobile'] ?? '',
|
|
|
+ 'id' => $courier['id'] ?? '',
|
|
|
+ 'headIcon' => $courier['headIcon'] ?? '',
|
|
|
+ 'latitude' => $courier['latitude'] ?? '',
|
|
|
+ 'longitude' => $courier['longitude'] ?? '',
|
|
|
+ 'time' => $courier['time'] ?? '',
|
|
|
+ 'orderCount' => $courier['orderCount'] ?? 0,
|
|
|
+ 'estimateDeliveryTimeTip' => $courier['estimateDeliveryTimeTip'] ?? '',
|
|
|
+ ], JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+
|
|
|
+ // TODO: 保存到数据库字段
|
|
|
+ // $deliveryOrder->courierName = $courier['name'] ?? '';
|
|
|
+ // $deliveryOrder->courierPhone = $courier['mobile'] ?? '';
|
|
|
+ // $deliveryOrder->courierId = $courier['id'] ?? '';
|
|
|
+ // $deliveryOrder->courierHeadIcon = $courier['headIcon'] ?? '';
|
|
|
+ // $deliveryOrder->courierLatitude = $courier['latitude'] ?? '';
|
|
|
+ // $deliveryOrder->courierLongitude = $courier['longitude'] ?? '';
|
|
|
+ // $deliveryOrder->courierTime = $courier['time'] ?? '';
|
|
|
+ // $deliveryOrder->estimateDeliveryTimeTip = $courier['estimateDeliveryTimeTip'] ?? '';
|
|
|
+
|
|
|
+ // 处理配送轨迹(如需要)
|
|
|
+ if (isset($courier['deliveryProcessTrail']) && is_array($courier['deliveryProcessTrail'])) {
|
|
|
+ Yii::info('闪送配送轨迹: ' . json_encode($courier['deliveryProcessTrail'], JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+ // TODO: 如需要可以保存到单独的轨迹表或JSON字段
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理取消信息
|
|
|
+ *
|
|
|
+ * @param array $data 回调数据
|
|
|
+ * @param object $deliveryOrder 配送订单对象
|
|
|
+ */
|
|
|
+ private function handleCancelInfo($data, $deliveryOrder)
|
|
|
+ {
|
|
|
+ // 订单取消时(status=60)会返回取消相关信息
|
|
|
+ if (isset($data['status']) && $data['status'] == 60) {
|
|
|
+ $abortType = $data['abortType'] ?? null;
|
|
|
+ $punishType = $data['punishType'] ?? null;
|
|
|
+ $abortReason = $data['abortReason'] ?? '';
|
|
|
+
|
|
|
+ Yii::info($data['issOrderNo'] . '-闪送取消信息: ' . json_encode([
|
|
|
+ 'abortType' => $abortType,
|
|
|
+ 'abortTypeDesc' => $abortType ? OrderStatusUtil::getAbortTypeDesc($abortType) : '',
|
|
|
+ 'punishType' => $punishType,
|
|
|
+ 'punishTypeDesc' => $punishType ? OrderStatusUtil::getPunishTypeDesc($punishType) : '',
|
|
|
+ 'abortReason' => $abortReason,
|
|
|
+ ], JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+
|
|
|
+ // TODO: 保存到数据库字段
|
|
|
+ // $deliveryOrder->abortType = $abortType;
|
|
|
+ // $deliveryOrder->punishType = $punishType;
|
|
|
+ // $deliveryOrder->abortReason = $abortReason;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理费用信息
|
|
|
+ *
|
|
|
+ * @param array $data 回调数据
|
|
|
+ * @param object $deliveryOrder 配送订单对象
|
|
|
+ */
|
|
|
+ private function handleFeeInfo($data, $deliveryOrder)
|
|
|
+ {
|
|
|
+ $deductAmount = $data['deductAmount'] ?? 0;
|
|
|
+ $drawback = $data['drawback'] ?? 0;
|
|
|
+ $sendBackFee = $data['sendBackFee'] ?? 0;
|
|
|
+
|
|
|
+ // 记录费用信息(金额单位:分)
|
|
|
+ if ($deductAmount > 0 || $drawback > 0 || $sendBackFee > 0) {
|
|
|
+ Yii::info($data['issOrderNo'] . '-闪送费用信息: ' . json_encode([
|
|
|
+ 'deductAmount' => $deductAmount, // 扣款金额
|
|
|
+ 'drawback' => $drawback, // 退款金额
|
|
|
+ 'sendBackFee' => $sendBackFee, // 送回费
|
|
|
+ ], JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+
|
|
|
+ // 保存到数据库字段 | 扣款金额 + 送回费
|
|
|
+ $deliveryOrder->cancelCost = $deductAmount + $sendBackFee;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理密码信息
|
|
|
+ *
|
|
|
+ * @param array $data 回调数据
|
|
|
+ * @param object $deliveryOrder 配送订单对象
|
|
|
+ */
|
|
|
+ private function handlePasswordInfo($data, $deliveryOrder)
|
|
|
+ {
|
|
|
+ $pickupPassword = $data['pickupPassword'] ?? '';
|
|
|
+ $deliveryPassword = $data['deliveryPassword'] ?? '';
|
|
|
+
|
|
|
+ if (!empty($pickupPassword) || !empty($deliveryPassword)) {
|
|
|
+ Yii::info($data['issOrderNo'] . '-闪送密码信息: ' . json_encode([
|
|
|
+ 'pickupPassword' => $pickupPassword,
|
|
|
+ 'deliveryPassword' => $deliveryPassword,
|
|
|
+ ], JSON_UNESCAPED_UNICODE), 'shansong-callback');
|
|
|
+
|
|
|
+ // TODO: 保存到数据库字段
|
|
|
+ // $deliveryOrder->pickupPassword = $pickupPassword;
|
|
|
+ // $deliveryOrder->deliveryPassword = $deliveryPassword;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|