Browse Source

批量添加

shish 5 năm trước cách đây
mục cha
commit
34f5ba1c92
2 tập tin đã thay đổi với 54 bổ sung38 xóa
  1. 37 8
      app-ghs/controllers/ItemController.php
  2. 17 30
      biz-ghs/item/services/ItemService.php

+ 37 - 8
app-ghs/controllers/ItemController.php

@@ -6,7 +6,7 @@ use biz\item\classes\PtItemClass;
 use bizGhs\item\classes\ItemClass;
 use bizGhs\item\services\ItemService;
 use common\components\util;
-use common\services\xhItemService;
+use bizGhs\product\classes\ProductClass;
 use Yii;
 
 class ItemController extends BaseController
@@ -28,8 +28,18 @@ class ItemController extends BaseController
         $post = Yii::$app->request->post();
         $post['adminId'] = $this->adminId;
         $post['sjId'] = $this->sjId;
-        ItemService::addGhsItem($post);
-        util::complete('添加成功');
+
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            ItemService::addGhsItem($post);
+            $transaction->commit();
+            util::complete('添加成功');
+        } catch (Exception $e) {
+            $transaction->rollBack();
+            util::fail('添加失败');
+        }
+
     }
 
     //批量添加花材
@@ -51,12 +61,31 @@ class ItemController extends BaseController
         if (!is_array($data) || empty($data)) {
             util::fail("请选花材");
         }
-        foreach ($data as $v) {
-            $v['adminId'] = $this->adminId;
-            $v['sjId'] = $this->sjId;
-            ItemService::addGhsItem($v);
+
+        $sjId = $this->sjId;
+        $ids = array_column($data,'itemId');
+		$ids = array_unique(array_filter($ids));
+
+        $has = ProductClass::getByCondition(['sjId' => $sjId, 'itemId'=>['in',$ids]]);
+        if(!empty($has)){
+            util::fail("有花材已经添加过了");
+        }
+
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            foreach ($data as $v) {
+                $v['adminId'] = $this->adminId;
+                $v['sjId'] = $sjId;
+                ItemService::addGhsItem($v);
+            }
+            $transaction->commit();
+            util::complete('添加成功');
+        } catch (Exception $e) {
+            $transaction->rollBack();
+            util::fail();
         }
-        util::complete('添加成功');
+
     }
 
     public function actionUpdate()

+ 17 - 30
biz-ghs/item/services/ItemService.php

@@ -60,19 +60,16 @@ class ItemService extends BaseService
 
         $stockWarning = isset($data['stockWarning']) && preg_match("/^[1-9][0-9]*$/", $data['stockWarning']) ? $data['stockWarning'] : 5;
 
-        //判断花材是否存在 调用平台花材的方法
+        //判断平台花材是否存在
         $checkItem = PtItemClass::getById($itemId);
         if (empty($checkItem)) {
-            util::fail("花材不存在");
+            util::fail("平台花材不存在");
         }
         $existItemClass = ItemClassClass::exists(['id' => $classId, 'sjId' => $sjId]);
         if (empty($existItemClass)) {
             util::fail("分类不存在");
         }
-        $currentProduct = ProductClass::getByCondition(['sjId' => $sjId, 'itemId' => $itemId]);
-        if (!empty($currentProduct)) {
-            util::fail("有花材已添加过了,请刷新");
-        }
+
         //补全花材信息
         $data['name'] = $name;
         $data['alias'] = $checkItem['alias'];
@@ -84,32 +81,22 @@ class ItemService extends BaseService
         $data['smallUnit'] = $checkItem['smallUnit'];
         $data['stockWarning'] = $stockWarning;
 
-        $connection = Yii::$app->db;
-        $transaction = $connection->beginTransaction();
-        try {
-            //写入一条item,同时写入一条itemInfo(初始化库存)
-            ItemClass::add($data);
-            //根据sjId ,获取所有的shopId, 循环写入product
-            $shopIds = ShopService::getShopIds($sjId);
-            if (empty($shopIds)) {
-                $transaction->rollBack();
-                util::fail("没有找到门店");
-            }
-            foreach ($shopIds as $shopId) {
-                $data['shopId'] = $shopId;
-                //判断product 是否存在,若存在,则不再处理
-                $existItem = ProductClass::exists(['itemId' => $itemId, 'sjId' => $sjId, 'shopId' => $shopId]);
-                if ($existItem) {
-                    continue;
-                }
-                ProductClass::add($data);
+        //写入一条item,同时写入一条itemInfo(初始化库存)
+        ItemClass::add($data);
+        //根据sjId ,获取所有的shopId, 循环写入product
+        $shopIds = ShopService::getShopIds($sjId);
+        if (empty($shopIds)) {
+            util::fail("没有找到门店");
+        }
+        foreach ($shopIds as $shopId) {
+            $data['shopId'] = $shopId;
+            //判断product 是否存在,若存在,则不再处理
+            $existItem = ProductClass::exists(['itemId' => $itemId, 'sjId' => $sjId, 'shopId' => $shopId]);
+            if ($existItem) {
+                continue;
             }
-            $transaction->commit();
-        } catch (Exception $exception) {
-            $transaction->rollBack();
-            util::fail("新增失败");
+            ProductClass::add($data);
         }
-
     }
 
     //初始化花材