Browse Source

商城商品库存添加接口优化

wufeng 2 years ago
parent
commit
4cf9f69c2d

+ 1 - 2
app/admin/config/validate_rules.php

@@ -158,8 +158,7 @@ return [
     //【商城商品库存】
     //添加
     'InventoryShoppingAdd' => [
-        'good_id|商品id' => 'require|number|gt:0',
-        'inventory|库存数' => 'require|number|gt:0|lt:999999999',
+        'list|商城商品库存列表' => 'require|array|max:100',
     ],
 
     //【兑换商品库存】

+ 1 - 1
app/admin/controller/InventoryShopping.php

@@ -23,7 +23,7 @@ class InventoryShopping extends BaseController
     //添加商城商品库存
     public function add()
     {
-        $param = $this->request->only(['good_id', 'inventory'], 'post');
+        $param = $this->request->only(['list'], 'post');
 
         $val = Validate::rule(Config::get('validate_rules.InventoryShoppingAdd'));
 

+ 35 - 28
app/admin/logic/InventoryShoppingLogic.php

@@ -42,39 +42,46 @@ class InventoryShoppingLogic extends BaseLogic
         Db::startTrans();
         try {
 
-            $rs = InventoryShoppingModel::field('id')
-                ->where('good_id', $data['good_id'])
-                ->findOrEmpty()
-                ->isEmpty();
-            if (!$rs) throw new Exception('该账号下的商城商品库存已存在,不能重复添加');
+            $good_list = GoodModel::where(['is_del' => CommonModel::$del_normal, 'type' => GoodModel::$type_shopping])
+                ->whereIn('id', array_column($data['list'], 'good_id'))
+                ->column('id,good_code,good_name', 'id');
 
-            $good = GoodModel::field('id,good_code,good_name')
-                ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
-                ->findOrEmpty();
-            if ($good->isEmpty()) throw new Exception('该商品不存在');
+            if (empty($good_list)) throw new Exception('该商品不存在');
+
+            $rs = InventoryShoppingModel::field('id,good_code')
+                ->whereIn('good_id', array_column($good_list, 'id'))
+                ->findOrEmpty()
+                ->toArray();
+            if (!empty($rs)) throw new Exception($rs['good_code'] . '该商城商品库存已存在,不能重复添加');
 
             $date = date('Y-m-d H:i:s');
+            $inventory = array_column($data['list'], 'inventory', 'good_id');
+
+            foreach ($good_list as $good) {
+                $inventory_shopping_id = Db::name('inventory_shopping')
+                    ->insertGetId([
+                        'good_id' => $good['id'],
+                        'good_code' => $good['good_code'],
+                        'good_name' => $good['good_name'],
+                        'inventory' => $inventory[$good['id']] ?? 0,
+                        'addtime' => $date,
+                        'updatetime' => $date,
+                    ]);
 
-            $inventory_shopping_id = Db::name('inventory_shopping')
-                ->insertGetId(array_merge($data, [
-                    'good_code' => $good->good_code,
-                    'good_name' => $good->good_name,
+                Db::name('inventory_shopping_log')->insert([
+                    'inventory_shopping_id' => $inventory_shopping_id,
+                    'before_inventory' => 0,
+                    'after_inventory' => $inventory[$good['id']] ?? 0,
+                    'good_id' => $good['id'],
+                    'good_code' => $good['good_code'],
+                    'good_name' => $good['good_name'],
+                    'source' => CommonModel::$source_admin,
+                    'createrid' => self::$uid,
+                    'creater' => self::$uname,
                     'addtime' => $date,
-                    'updatetime' => $date,
-                ]));
-
-            Db::name('inventory_shopping_log')->insert([
-                'inventory_shopping_id' => $inventory_shopping_id,
-                'before_inventory' => 0,
-                'after_inventory' => $data['inventory'],
-                'good_id' => $data['good_id'],
-                'good_code' => $good->good_code,
-                'good_name' => $good->good_name,
-                'source' => CommonModel::$source_admin,
-                'createrid' => self::$uid,
-                'creater' => self::$uname,
-                'addtime' => $date,
-            ]);
+                ]);
+
+            }
 
             Db::commit();