|
@@ -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();
|
|
|
|