Sfoglia il codice sorgente

1. app-hd 新增查询跑腿报价的控制器
2. 跑腿订单状态更新时,补更新xhGhsOrder表与xhGhs表的status字段(指配送完成或取消状态)

shizhongqi 9 mesi fa
parent
commit
2b50a83c3f

+ 1 - 0
app-ghs/controllers/DeliveryController.php

@@ -398,6 +398,7 @@ class DeliveryController extends BaseController
             $ghsOrder->deliveryId = $ret['platform'];
             $ghsOrder->sendType = 2;// 2指跑腿
             $ghsOrder->save();
+            OrderClass::hasSend($ghsOrder);// 更新 status
 
             util::success($ret, "success");
         } else {

+ 155 - 0
app-hd/controllers/DeliveryController.php

@@ -0,0 +1,155 @@
+<?php
+namespace hd\controllers;
+
+use biz\ghs\classes\GhsClass;
+use bizGhs\custom\classes\CustomClass;
+use Yii;
+use biz\shop\classes\ShopClass;
+use common\components\delivery\services\DispatchService;
+use common\components\util;
+use ghs\controllers\BaseController;
+
+class DeliveryController extends BaseController
+{
+    // 获取多个平台报价
+    public function actionAllDeliveryQuotes()
+    {
+        $post = Yii::$app->request->post();
+        $orderTime = date('Y-m-d H:i:s');
+        // --------- $ghsShopId ----------
+        $ghsId = $post['ghsId'] ?? 0;
+        $ghs = GhsClass::getById($ghsId, true);
+        if (empty($ghs)) {
+            util::fail('获取批发商失败');
+        }
+        $ghsShopId = $ghs->shopId ?? 0;
+        //$ghsShopId = $post['shopId'];
+
+        // --------- $customId
+        $customId = $ghs->customId ?? 0;
+        $custom = CustomClass::getById($customId, true);
+        if (empty($custom)) {
+            util::fail('获取custom数据失败');
+        }
+        $weight = $post['weight'] ?? 1;
+
+        //$orderId = intval($post['orderId']);
+        //$order = OrderClass::getById($orderId);
+        //构建出 Order 数据
+        $order = [
+            'orderSn' => '',
+            'customName' => $custom['name'],
+            'customMobile' => $custom['mobile'],
+            'fullAddress' => $custom['fullAddress'],
+            'floor' => $custom['floor'],
+            'dist' => $custom['dist'],
+            'lat' => $custom['lat'],
+            'long' => $custom['long'],
+            'address' => $custom['address'], //'toAddress' => $order['address'],
+            'city' => $custom['city'],
+            'weight' => $weight,
+            'remark' => '',
+            'prePrice' => 10,
+            'actPrice' => 10,
+        ];
+
+        $arr2 = [
+            //'goods_count' => count($goodsItemList), // 这个应该是通过  orderSn 查询出来的,如果  orderSn 是假的怎么办? -- 提供临时的
+        ];
+
+        $ghsShop = ShopClass::getById($ghsShopId);
+
+        $ghsMainId = $ghs->mainId;
+        $ds = new DispatchService($ghsMainId);
+        $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
+
+        $deliveryList = [];
+        foreach ($platformQuotes['quotes'] as $item) {
+            switch ($item['platform']) {
+                case 'shunfeng':
+                    $deliveryList[] = [
+                        'name' => '顺丰',
+                        'en_name' => 'shunfeng',
+                        'price' => $item['real_pay_money'],
+                        'distance' => $item['distance'],
+                        'type' => '',
+                        'isAble' => true
+                    ];
+                    break;
+                case 'shansong':
+                    $deliveryList[] = [
+                        'name' => '闪送',
+                        'en_name' => 'shansong',
+                        'price' => $item['total_amount'],
+                        'distance' => $item['total_distance'],
+                        'type' => '',
+                        'issOrderNo' => $item['order_number'],
+                        'isAble' => true
+                    ];
+                    break;
+                case 'fengniao':
+                    // 循环遍历 goods_infos 数组,提取有效项的信息
+                    $validGoods = [];
+                    if (!empty($item['goods_infos']) && is_array($item['goods_infos'])) {
+                        foreach ($item['goods_infos'] as $good) {
+                            if (!empty($good['is_valid']) && $good['is_valid'] == 1) {
+                                $validGoods[] = [
+                                    'actual_delivery_amount_cent' => $good['actual_delivery_amount_cent'] ?? 0,
+                                    'service_goods_id' => $good['service_goods_id'] ?? ''
+                                ];
+
+                                $deliveryList[] = [
+                                    'name' => '蜂鸟'. ' (' .$good['base_goods_id'] . ')',
+                                    'en_name' => 'fengniao',
+                                    'price' => $good['actual_delivery_amount_cent'],
+                                    'distance' => $item['distance'],
+                                    'valid_goods' => $validGoods,
+                                    'type' => $good['slogan'],
+                                    'isAble' => true
+                                ];
+                            } else {
+                                $deliveryList[] = [
+                                    'name' => '蜂鸟'. ' (' .$good['base_goods_id'] . ')',
+                                    'en_name' => 'fengniao',
+                                    'price' => 0,
+                                    'distance' => $item['distance'],
+                                    'valid_goods' => $good,
+                                    'type' => $good['disable_reason'],
+                                    'isAble' => false
+                                ];
+                            }
+                        }
+                    }
+                    break;
+                case 'huolala':
+                    foreach($item['price_info_list'] as $arr) {
+                        $priceInfo = $arr['calculate_price_info'];
+                        $vehicleType = $arr['vehicle_type'];
+                        $deliveryList[] = [
+                            'name' => '货拉拉' . ' (' . $vehicleType['_meta']['vehicle_type'] . ')',
+                            'en_name' => 'huolala',
+                            'price' => $priceInfo['price_conditions'][0]['price_info']['total_price'],
+                            'distance' => $priceInfo['distance_info']['distance_total'],
+                            'type' => $vehicleType['_meta']['vehicle_type'],
+                            'isAble' => true,
+                            // ---------------------------------------------
+                            'order_vehicle_id' => $vehicleType['order_vehicle_id'],
+                            'city_id' => $item['city_id'],
+                            'city_info_revision' => $vehicleType['city_info_revision'],
+                            // // 下单需要透传的数据
+                            'vehicle_std' => $vehicleType['vehicle_std'],
+                            'spec_req' => $vehicleType['spec_req'],
+                            'price_calculate_id' => $priceInfo['price_calculate_id'],
+                            'price_item_encryption' => $priceInfo['price_conditions'][0]['price_item_encryption'],
+                            'vehicle_attr' => $priceInfo['vehicle_info']['vehicle_attr'],
+                            'commodity_info' => $priceInfo['price_conditions'][0]['commodity_item'],
+                        ];
+                    }
+                    break;
+            }
+        }
+
+        $ret['deliveryList'] = $deliveryList;
+        util::success($ret, "success");
+    }
+}

+ 2 - 2
common/components/delivery/platform/dada/Auth.php

@@ -77,8 +77,8 @@ class Auth
     public function getTicket()
     {
         // 生成随机数
-        // $nonce = $this->generateNonce(15);
-        $nonce = 'VV7JK4BJXAUSYP8'; // TODO 写死,后期修改回去
+        $nonce = $this->generateNonce(15);
+        //$nonce = 'VV7JK4BJXAUSYP8'; // 写死,后期修改回去
         
         // 生成签名
         $sign = $this->generateTicketSign($nonce);

+ 9 - 1
common/components/delivery/platform/fengniao/CallBackNotify.php

@@ -53,10 +53,18 @@ class CallBackNotify
         $deliveryOrder->orderStatus = $sendStatus;
         $deliveryOrder->save();
 
-        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendType,sendStatus,deliveryId');
+        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendType,sendStatus,deliveryId,purchaseId,customId,status');
         $order->sendStatus = $sendStatus;
         $order->save();
 
+        // 确认送达或取消订单,更新主订单状态
+        if ($sendStatus == 5) {
+            OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
+        }
+        if ($sendStatus == -1 || $sendStatus == -2) {
+            OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
+        }
+
         return true;
     }
 

+ 23 - 0
common/components/delivery/platform/fengniao/Fengniao.php

@@ -11,6 +11,7 @@ class Fengniao
     protected $appId;
     protected $appSecret;
     protected $merchantId;
+    protected $shopId;
     protected $accessToken;
     protected $apiVersion = '1.0';
 
@@ -41,6 +42,28 @@ class Fengniao
         return $this;
     }
 
+    /**
+     * 设置门店ID
+     *
+     * @param string $shopId 门店ID
+     * @return $this
+     */
+    public function setShopId($shopId)
+    {
+        $this->shopId = $shopId;
+        return $this;
+    }
+
+    /**
+     * 获取门店ID
+     *
+     * @return int $shopId
+     */
+    public function getShopId()
+    {
+        return $this->shopId;
+    }
+
     /**
      * 构建API请求参数
      *

+ 9 - 1
common/components/delivery/platform/huolala/CallBackHandler.php

@@ -54,12 +54,20 @@ class CallBackHandler
                 return false;
             }
 
-            $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus');
+            $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');
             }

+ 10 - 2
common/components/delivery/platform/shansong/OrderStatusCallBack.php

@@ -60,10 +60,18 @@ class OrderStatusCallBack
             $deliveryOrder->save();
             
             // 更新主订单状态
-            $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendType,sendStatus,deliveryId');
+            $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendType,sendStatus,deliveryId,purchaseId,customId,status');
             if ($order) {
                 $order->sendStatus = $sendStatus;
                 $order->save();
+
+                // 确认送达或取消订单,更新主订单状态
+                if ($sendStatus == 5) {
+                    OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
+                }
+                if ($sendStatus == -1 || $sendStatus == -2) {
+                    OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
+                }
             }
             
             Yii::info('闪送订单状态更新成功: issOrderNo=' . $data['issOrderNo'] . 
@@ -100,7 +108,7 @@ class OrderStatusCallBack
                 'estimateDeliveryTimeTip' => $courier['estimateDeliveryTimeTip'] ?? '',
             ], JSON_UNESCAPED_UNICODE), 'shansong-callback');
             
-            // TODO: 保存到数据库字段
+            // 保存到数据库字段
             // $deliveryOrder->courierName = $courier['name'] ?? '';
             // $deliveryOrder->courierPhone = $courier['mobile'] ?? '';
             // $deliveryOrder->courierId = $courier['id'] ?? '';

+ 18 - 3
common/components/delivery/platform/shunfeng/CallBackHandler.php

@@ -177,10 +177,15 @@ class CallBackHandler
         $deliveryOrder->save();
         
         // 更新主订单状态为已送达
-        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true);
+        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
         if ($order) {
             $order->sendStatus = $sendStatus;
             $order->save();
+
+            // 确认送达,更新主订单状态
+            if ($sendStatus == 5) {
+                OrderClass::confirmReach($order, ['staffId' => 0, 'staffName' => '']);
+            }
         }
         
         return true;
@@ -225,10 +230,15 @@ class CallBackHandler
         $deliveryOrder->save();
         
         // 更新主订单状态为取消
-        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true);
+        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
         if ($order) {
             $order->sendStatus = $sendStatus;
             $order->save();
+
+            // 更新主订单状态为取消
+            if ($sendStatus == -1) {
+                OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
+            }
         }
         
         return true;
@@ -273,10 +283,15 @@ class CallBackHandler
         $deliveryOrder->save();
 
         // 更新主订单状态为异常
-        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true);
+        $order = OrderClass::getById($deliveryOrder->ghsOrderId, true, 'id,sendStatus,deliveryId,purchaseId,customId,status');
         if ($order) {
             $order->sendStatus = $sendStatus;
             $order->save();
+
+            // 更新主订单状态为异常
+            if ($sendStatus == -2) {
+                OrderClass::cancelOrderSend($order, ['staffId' => 0, 'staffName' => '']);
+            }
         }
         
         return true;

+ 15 - 10
common/components/delivery/services/DispatchService.php

@@ -33,7 +33,9 @@ class DispatchService
                         break;
                     case 'fengniao':
                         $adapter = new FengniaoAdapter($pt['accessToken']);
-                        $adapter->setMerchantId($pt['merchantId']); // TODO: 确认是否需要使用动态的商户ID
+                        // 动态设置商户ID与门店ID
+                        $adapter->setMerchantId($pt['merchantId']);
+                        $adapter->setShopId($pt['shopId']);
                         $this->adapters['fengniao'] = $adapter;
                         break;
                     case 'shunfeng':
@@ -60,7 +62,9 @@ class DispatchService
                 case 'fengniao':
                     //$this->adapters['fengniao'] = new FengniaoAdapter($authPlatform['accessToken']);
                     $adapter = new FengniaoAdapter($authPlatform['accessToken']);
-                    $adapter->setMerchantId($authPlatform['merchantId']); // TODO: 确认是否需要使用动态的商户ID
+                    // 动态设置商户ID与门店ID
+                    $adapter->setMerchantId($authPlatform['merchantId']);
+                    $adapter->setShopId($authPlatform['shopId']);
                     $this->adapters['fengniao'] = $adapter;
                     break;
                 case 'shunfeng':
@@ -142,7 +146,7 @@ class DispatchService
     public function getAllPlatformPrice($order, $shop, $orderTime)
     {
         // 第一步:准备前置数据(同步进行,因为某些平台需要这些数据)
-        $preparedData = $this->preparePlatformData($order, $shop, $orderTime);
+        $preparedData = $this->preparePlatformData($order, $shop);
 
         if (empty($preparedData)) {
             return ['error' => '所有平台数据准备失败'];
@@ -185,7 +189,10 @@ class DispatchService
         // 准备 Fengniao 数据
         if (isset($this->adapters['fengniao'])) {
             try {
-                $preparedData['fengniao'] = $this->prepareFengniaoData($order, $shop);
+                /** @var FengniaoAdapter $fengniaoAdapter */
+                $fengniaoAdapter = $this->adapters['fengniao'];
+                $platformShopId = $fengniaoAdapter->getShopId();
+                $preparedData['fengniao'] = $this->prepareFengniaoData($order, $shop, $platformShopId);
             } catch (\Exception $e) {
                 Yii::warning("Fengniao 数据准备失败: {$e->getMessage()}");
             }
@@ -300,13 +307,11 @@ class DispatchService
     /**
      * 准备蜂鸟订单数据
      */
-    private function prepareFengniaoData($order, $shop)
+    private function prepareFengniaoData($order, $shop, $platformShopId)
     {
-        $this->adapters['fengniao']->setMerchantId(14594092); // TODO: 确认是否需要使用动态的商户ID
-
-        $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num');
+        // $this->adapters['fengniao']->setMerchantId(14594092); // adapter 创建时已经设置了
         $goodsItemList = [];
-
+        $itemInfos = OrderItemClass::getAllByCondition(['orderSn' => $order['orderSn']], null, 'id, name, unitPrice, num');
         foreach ($itemInfos as $item) {
             $goodsItemList[] = [
                 'item_actual_amount_cent' => (int)($item['unitPrice'] * 100 * $item['num']),
@@ -332,7 +337,7 @@ class DispatchService
             'goods_actual_amount_cent' => (int)($order['actPrice'] * 100),
             'goods_item_list' => $goodsItemList,
             'order_type' => 1,
-            'chain_store_id' => 467788524,
+            'chain_store_id' => $platformShopId, // 蜂鸟平台 shopId
             'order_remark' => $order['remark'] ?? '',
         ];
     }

+ 4 - 2
common/components/delivery/services/ShopService.php

@@ -32,7 +32,8 @@ class ShopService
                         break;
                     case 'fengniao':
                         $adapter = new FengniaoShop($pt['accessToken']);
-                        $adapter->setMerchantId($authPlatforms['merchantId']); // TODO: 确认是否需要使用动态的商户ID
+                        $adapter->setMerchantId($authPlatforms['merchantId']);
+                        $adapter->setShopId($authPlatforms['shopId']);
                         $this->adapters['fengniao'] = $adapter;
                         break;
                     case 'shunfeng':
@@ -52,7 +53,8 @@ class ShopService
                 case 'fengniao':
                     //$this->adapters['fengniao'] = new FengniaoAdapter($authPlatform['accessToken']);
                     $adapter = new FengniaoShop($authPlatform['accessToken']);
-                    $adapter->setMerchantId($authPlatform['merchantId']); // TODO: 确认是否需要使用动态的商户ID
+                    $adapter->setMerchantId($authPlatform['merchantId']);
+                    $adapter->setShopId($authPlatform['shopId']);
                     $this->adapters['fengniao'] = $adapter;
                     break;
                 case 'shunfeng':