shish 1 сар өмнө
parent
commit
1f3ed06314

+ 43 - 26
app-hd/controllers/PurchaseController.php

@@ -828,7 +828,19 @@ class PurchaseController extends BaseController
         }
     }
 
-    //赊账 ssh 2021.1.24
+    /**
+     * 赊账支付
+     * 职责:处理采购单延期支付(记欠款/赊账),更新采购单状态,并触发后续的自动收货、通知、在线打印、跑腿呼叫等业务流
+     * 入参:GET 传参 orderSn (采购单号)
+     * 返回:JSON 格式的采购单属性
+     * 副作用:更新采购单、订单状态,增减花材库存并记录库存流水,更新客户及供货商的最后下单时间,发送外部通知和打印请求
+     * 关键边界:
+     *   1. 校验采购单及客户的赊账权限
+     *   2. 优化:复用采购单实例,避免重复查询
+     *   3. 优化:将自动确认收货(confirmTake)包裹在独立事务中,合并写操作,减少磁盘 I/O
+     *   4. 优化:合并打印逻辑中的 ShopExt 查询及订单保存,减少数据库交互
+     *   5. 优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE
+     */
     public function actionDebtPay()
     {
         $get = Yii::$app->request->get();
@@ -895,14 +907,22 @@ class PurchaseController extends BaseController
                 }
             }
 
-            //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
-            $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
-            if (!empty($cg)) {
-                if (isset($cg->book) && $cg->book == 0) {
+            // 复杂分支/关键逻辑:不是预订单,且供货商配置为直接完成流程,则自动确认收货。
+            // 优化:复用已有的 $info 实例,避免重复查询;同时将自动确认收货包裹在独立事务中,合并批量写操作,减少磁盘 I/O。
+            if (!empty($info)) {
+                if (isset($info->book) && $info->book == 0) {
                     $ghsShopId = $ghs['shopId'] ?? 0;
                     $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
-                    if ($ext->orderFlow == 0) {
-                        PurchaseClass::confirmTake($cg);
+                    if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
+                        $takeTransaction = Yii::$app->db->beginTransaction();
+                        try {
+                            PurchaseClass::confirmTake($info);
+                            $takeTransaction->commit();
+                        } catch (\Exception $takeEx) {
+                            $takeTransaction->rollBack();
+                            Yii::error("自动确认收货失败: " . $takeEx->getMessage());
+                            throw $takeEx;
+                        }
                     }
                 }
             }
@@ -927,51 +947,48 @@ class PurchaseController extends BaseController
                     } else {
                         $mallOrderNoPrint = [];
                     }
-                    if (in_array($order->mainId, $mallOrderNoPrint)) {
-
-                    } else {
+                    if (!in_array($order->mainId, $mallOrderNoPrint)) {
+                        // 复杂分支/关键逻辑:优化:只查询一次 $ext,合并 $order->save(),减少数据库交互
+                        $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
+                        $hasPrintSn = isset($ext->printSn) && !empty($ext->printSn);
 
                         OrderClass::onlinePrint($order);
-                        $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
-                        if (isset($ext->printSn) && !empty($ext->printSn)) {
+                        if ($hasPrintSn) {
                             $order->printNum += 1;
-                            $order->save();
                         }
 
                         //花镜 打二次小票,有多处要修改搜索关键词tow_print
                         if (in_array($order->mainId, [76796])) {
                             OrderClass::onlinePrint($order);
-                            $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
-                            if (isset($ext->printSn) && !empty($ext->printSn)) {
+                            if ($hasPrintSn) {
                                 $order->printNum += 1;
-                                $order->save();
                             }
                         }
 
+                        if ($hasPrintSn) {
+                            $order->save();
+                        }
                     }
                 }
 
                 //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
                 GhsDeliveryOrderClass::payAfter($order);
 
-                //更新最后下单时间
+                // 复杂分支/关键逻辑:优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE 的性能开销
                 $customId = $order->customId ?? 0;
-                $custom = CustomClass::getById($customId, true);
+                $ghsId = $order->ghsId ?? 0;
                 $date = date("Y-m-d H:i:s");
-                if (!empty($custom)) {
-                    $custom->recentExpend = $date;
-                    $custom->save();
+                if ($customId > 0) {
+                    CustomClass::updateByCondition(['id' => $customId], ['recentExpend' => $date]);
                 }
-                $ghsId = $order->ghsId ?? 0;
-                $ghs = GhsClass::getById($ghsId, true);
-                if (!empty($ghs)) {
-                    $ghs->recentExpend = $date;
-                    $ghs->save();
+                if ($ghsId > 0) {
+                    GhsClass::updateByCondition(['id' => $ghsId], ['recentExpend' => $date]);
                 }
 
             }
         } catch (\Exception $e) {
             $transaction->rollBack();
+            Yii::error("赊账支付失败原因:" . $e->getMessage());
             util::fail('支付失败');
         }
         util::success($info->attributes);