Kaynağa Gözat

批量修改分类

shish 3 yıl önce
ebeveyn
işleme
8987a20817
1 değiştirilmiş dosya ile 57 ekleme ve 35 silme
  1. 57 35
      app-ghs/controllers/ProductController.php

+ 57 - 35
app-ghs/controllers/ProductController.php

@@ -1043,46 +1043,68 @@ class ProductController extends BaseController
         if (empty($class) || $class->mainId != $this->mainId) {
             util::fail('请选择您自己的分类');
         }
-        $idsList = $post['ids'] ?? '';
-        $ids = json_decode($idsList, true);
-        if (empty($ids)) {
-            util::fail('请选择花材');
-        }
-        $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
-        if (empty($list)) {
-            util::fail('请选择花材');
-        }
-        foreach ($list as $item) {
-            $name = $item->name ?? '';
-            if ($item->mainId != $this->mainId) {
-                util::fail($name . '不是您的花材');
+
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+
+            $className = $class->name ?? '';
+            $idsList = $post['ids'] ?? '';
+            $ids = json_decode($idsList, true);
+            if (empty($ids)) {
+                util::fail('请选择花材');
             }
-            $item->classId = $classId;
-            $item->save();
-        }
-        $shop = $this->shop;
-        if (isset($shop->default) && $shop->default == 1) {
-            //总店修改同步到所有门店
-            $sjId = $shop->sjId ?? 0;
-            $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
-            foreach ($chainShopList as $chainShop) {
-                $chainShopId = $chainShop->id ?? 0;
-                if (isset($chainShop->join) && $chainShop->join == 1) {
-                    continue;
-                }
-                if ($chainShopId == $shop->id) {
-                    continue;
+            $list = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', null, true);
+            if (empty($list)) {
+                util::fail('请选择花材');
+            }
+            $ptItemList = [];
+            foreach ($list as $item) {
+                $name = $item->name ?? '';
+                if ($item->mainId != $this->mainId) {
+                    util::fail($name . '不是您的花材');
                 }
-                $chainMainId = $chainShop->mainId ?? 0;
-                $chainProduct = ProductClass::getByCondition(['mainId' => $chainMainId, 'itemId' => $ptItemId], true);
-                if (!empty($chainProduct)) {
-                    $chainProduct->delStatus = $delStatus;
-                    $chainProduct->save();
+                $item->classId = $classId;
+                $item->save();
+                $ptItemId = $item->itemId ?? 0;
+                $ptItemList[] = $ptItemId;
+            }
+            $shop = $this->shop;
+            if (isset($shop->default) && $shop->default == 1) {
+                //总店修改同步到所有门店
+                $sjId = $shop->sjId ?? 0;
+                $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
+                foreach ($chainShopList as $chainShop) {
+                    $chainShopId = $chainShop->id ?? 0;
+                    if ($chainShopId == $shop->id) {
+                        continue;
+                    }
+                    $chainManId = $chainShop->mainId ?? 0;
+                    $chainAllClassList = ItemClassClass::getAllByCondition(['mainId' => $chainManId], null, '*', null, true);
+                    if (!empty($chainAllClassList)) {
+                        foreach ($chainAllClassList as $chainClass) {
+                            $chainName = $chainClass->name ?? '';
+                            $chainClassId = $chainClass->id ?? 0;
+                            if ($chainName == $className) {
+                                $chainProductList = Productclass::getAllByCondition(['mainId' => $chainManId, 'itemId' => ['in', $ptItemList]], null, '*', null, true);
+                                if (!empty($chainProductList)) {
+                                    foreach ($chainProductList as $currentProduct) {
+                                        $currentProduct->classId = $chainClassId;
+                                        $currentProduct->save();
+                                    }
+                                }
+                            }
+                        }
+                    }
                 }
             }
+            $transaction->commit();
+            util::complete('修改成功');
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("修改失败原因:" . $e->getMessage());
+            util::fail('修改失败');
         }
-
-        util::complete();
     }
 
 }