shish 2 лет назад
Родитель
Сommit
cb36a002b5
1 измененных файлов с 32 добавлено и 18 удалено
  1. 32 18
      app-ghs/controllers/OrderItemController.php

+ 32 - 18
app-ghs/controllers/OrderItemController.php

@@ -80,6 +80,8 @@ class OrderItemController extends BaseController
         $post = Yii::$app->request->post();
         $id = $post['id'] ?? 0;
         $data = $post['data'] ?? [];
+        $packCost = $post['packCost'] ?? 0;
+        $sendCost = $post['sendCost'] ?? 0;
         if (empty($data)) {
             util::fail('没有花材数据');
         }
@@ -96,25 +98,37 @@ class OrderItemController extends BaseController
         if ($order->status != 2) {
             util::fail('待发货订单才能修改');
         }
-        foreach ($data as $item) {
-            $id = $item['id'] ?? 0;
-            $xhNum = $item['xhNum'] ?? 0;
-            $xhUnitPrice = $item['xhUnitPrice'] ?? 0;
-            $info = OrderItemClass::getById($id, true);
-            if (empty($info)) {
-                continue;
-            }
-            if ($info->orderSn != $order->orderSn) {
-                continue;
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            foreach ($data as $item) {
+                $id = $item['id'] ?? 0;
+                $xhNum = $item['xhNum'] ?? 0;
+                $xhUnitPrice = $item['xhUnitPrice'] ?? 0;
+                $info = OrderItemClass::getById($id, true);
+                if (empty($info)) {
+                    continue;
+                }
+                if ($info->orderSn != $order->orderSn) {
+                    continue;
+                }
+                $info->xhNum = $xhNum;
+                $info->xhPreNum = $xhNum;
+                $info->xhUnitPrice = $xhUnitPrice;
+                $info->xhPreUnitPrice = $xhUnitPrice;
+                $info->save();
             }
-            $info->xhNum = $xhNum;
-            $info->xhPreNum = $xhNum;
-            $info->xhUnitPrice = $xhUnitPrice;
-            $info->xhPreUnitPrice = $xhUnitPrice;
-            $info->save();
-        }
-        $order->save();
-        util::complete('保存成功');
+            $order->packCost = $packCost;
+            $order->sendCost = $sendCost;
+            $order->save();
+            $transaction->commit();
+            util::complete('保存成功');
+
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("保存出错了:" . $e->getMessage());
+            util::fail('保存出错了');
+        }
     }
 
     //删除花材 ssh 20240123