shish 4 лет назад
Родитель
Сommit
e19e6e3185
1 измененных файлов с 62 добавлено и 46 удалено
  1. 62 46
      app-hd/controllers/MtController.php

+ 62 - 46
app-hd/controllers/MtController.php

@@ -6,8 +6,10 @@ use bizHd\custom\classes\CustomClass;
 use bizHd\goods\classes\CategoryClass;
 use bizHd\goods\classes\GoodsClass;
 use bizHd\meituan\classes\MtOrderClass;
+use bizHd\order\classes\OrderClass;
 use bizHd\order\services\OrderService;
 use bizHd\shop\classes\ShopClass;
+use bizHd\work\classes\WorkClass;
 use common\components\arrayUtil;
 use common\components\dict;
 use common\components\httpUtil;
@@ -22,39 +24,17 @@ class MtController extends PublicController
 
     public $guestAccess = [];
 
-    //部分退款
-    public function actionPartRefund()
-    {
-        $post = Yii::$app->request->post();
-        if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
-            return Json::encode(['data' => 'ok']);
-        }
-        $data = arrayUtil::humpUnderlineConversion($post, 1);
-        Yii::warning('partRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
-    }
-
-    //全部退款
-    public function actionAllRefund()
-    {
-        $post = Yii::$app->request->post();
-        if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
-            return Json::encode(['data' => 'ok']);
-        }
-        $data = arrayUtil::humpUnderlineConversion($post, 1);
-        Yii::warning('allRefund: ' . urldecode(json_encode($data))); // 想要知道美团请求中带过来哪些参数,查看此日志输出
-    }
-
     //已确认订单通知
     public function actionConfirmOrder()
     {
         $post = Yii::$app->request->post();
-        $orderId = $post['order_id'] ?? 0;
+        $mtOrderId = $post['order_id'] ?? 0;
 
         //4秒内不允许第三方重复通知
-        $cacheKey = 'mt_confirm_order_' . $orderId;
+        $cacheKey = 'mt_confirm_order_' . $mtOrderId;
         $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
         if (!empty($has)) {
-            Yii::info("美团已确认订单通知,出现重复通知,流水类型:orderId:{$orderId}");
+            Yii::info("美团已确认订单通知,出现重复通知,orderId:{$mtOrderId}");
             return Json::encode(['data' => 'ok']);
         }
         Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
@@ -72,19 +52,19 @@ class MtController extends PublicController
             $shopId = $post['app_poi_code'] ?? '';
             $shop = ShopClass::getById($shopId, true);
             if (empty($shop)) {
-                noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,没有找到门店,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
             $mainId = $shop->mainId ?? 0;
             $sjId = $shop->sjId ?? 0;
             $customId = $shop->defaultCustomId ?? 0;
             if (empty($customId)) {
-                noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,没有找到客户哦,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
             $custom = CustomClass::getById($customId);
             if (empty($custom)) {
-                noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,没有找到客户,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
 
@@ -95,12 +75,12 @@ class MtController extends PublicController
 
             $detail = $post['detail'] ?? '';
             if (empty($detail)) {
-                noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,没有找到商品,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
             $mtGoodsData = json_decode($detail, true);
             if (empty($mtGoodsData)) {
-                noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,没有找到商品哦,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
 
@@ -114,7 +94,7 @@ class MtController extends PublicController
             }
             $catId = $mtCategory->id ?? 0;
             if (empty($catId)) {
-                noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,美团默认分类没有找到,美团订单id:{$mtOrderId}", '15280215347');
                 return Json::encode(['data' => 'ok']);
             }
 
@@ -137,7 +117,7 @@ class MtController extends PublicController
                 $mgGoodsDetail = $app->retail->getDetail($params);
                 $mgGoodsDetail = json_decode($mgGoodsDetail, true);
                 if (isset($mgGoodsDetail['error'])) {
-                    noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$orderId}", '15280215347');
+                    noticeUtil::push("美团确认订单报错,没有找到商品图片,美团订单id:{$mtOrderId}", '15280215347');
                     util::stop();
                 }
                 $imgString = $mgGoodsDetail['data']['picture'];
@@ -197,17 +177,17 @@ class MtController extends PublicController
             }
             $poi_receive_detail_yuan = $post['poi_receive_detail_yuan'] ?? '';
             if (empty($poi_receive_detail_yuan)) {
-                noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,订单金额有问题,美团订单id:{$mtOrderId}", '15280215347');
                 util::stop();
             }
             $poi_receive_detail_yuan_data = json_decode($poi_receive_detail_yuan, true);
             if (is_array($poi_receive_detail_yuan_data) == false) {
-                noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,订单金额有问题哦,美团订单id:{$mtOrderId}", '15280215347');
                 util::stop();
             }
             $onlinePayment = $poi_receive_detail_yuan_data['onlinePayment'] ?? 0;
             if ($onlinePayment == 0) {
-                noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$orderId}", '15280215347');
+                noticeUtil::push("美团确认订单报错,订单金额有问题!美团订单id:{$mtOrderId}", '15280215347');
                 util::stop();
             }
             $modifyPrice = $onlinePayment;
@@ -254,7 +234,7 @@ class MtController extends PublicController
                 'receiveMobile' => $receiveMobile,
                 'receiveUserName' => $receiveUserName,
                 'fullAddress' => $fullAddress,
-                'thirdOrderId' => $orderId,
+                'thirdOrderId' => $mtOrderId,
             ];
             $hasPay = dict::getDict('hasPay', 'payed');
             OrderService::createFinishOrder($orderData, $custom, $hasPay);
@@ -263,27 +243,63 @@ class MtController extends PublicController
         } catch (\Exception $e) {
             $transaction->rollBack();
             $msg = $e->getMessage();
-            noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$orderId}", '15280215347');
+            noticeUtil::push("确认订单报错了,错误信息:{$msg} 美团订单id:{$mtOrderId}", '15280215347');
         }
     }
 
-    public function actionNewOrder()
+    //取消订单
+    public function actionCancelOrder()
     {
         $post = Yii::$app->request->post();
-        if (empty($post)) { // 无数据过来时,是美团对接口进行存活试探,请保留此段代码,好让请求在此被中断返回
+        $mtOrderId = $post['order_id'] ?? 0;
+
+        if (empty($post)) {
             return Json::encode(['data' => 'ok']);
         }
-        return Json::encode(['data' => 'ok']);
-    }
 
-    public function actionCancelOrder()
-    {
-        $post = Yii::$app->request->post();
-        if (empty($post)) {
+        Yii::info('cancelOrderData: ' . json_encode($post));
+
+        //4秒内不允许第三方重复通知
+        $cacheKey = 'mt_cancel_order_' . $mtOrderId;
+        $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (!empty($has)) {
+            Yii::info("美团取消订单通知,出现重复通知,orderId:{$mtOrderId}");
+            return Json::encode(['data' => 'ok']);
+        }
+        Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
+
+        $mt = dict::getDict('fromType', 'mt');
+        $order = OrderClass::getByCondition(['fromType' => $mt, 'thirdOrderId' => $mtOrderId], true);
+        if (empty($order)) {
+            //没有接单时取消订单
             return Json::encode(['data' => 'ok']);
         }
 
-        Yii::warning('cancelOrder: ' . urldecode(json_encode($post)));
+        //只要制作单没有完成,则走取消订单流程。如果有制作单完成,则订单状态和制作单暂时先不管,后面要兼容。
+        $orderId = $order->id ?? 0;
+        $workList = WorkClass::getAllByCondition(['orderId' => $orderId], null, '*', null, true);
+        $hasCompleteWork = false;
+        if (!empty($workList)) {
+            foreach ($workList as $work) {
+                if ($work->status == 1) {
+                    $hasCompleteWork = true;
+                }
+            }
+        }
+
+        //取消订单和制作单
+        if ($hasCompleteWork == false) {
+            $order->status = 5;
+            $order->save();
+            if (!empty($workList)) {
+                foreach ($workList as $work) {
+                    $work->status == 2;
+                    $work->save();
+                }
+            }
+        }
+
+        //如果制作单完成了还会出现取消订单情况,这个等于全部退款,这个后面再考虑兼容!!!!
 
         return Json::encode(['data' => 'ok']);
     }