cqpCow 1 year ago
parent
commit
19778307af
2 changed files with 41 additions and 22 deletions
  1. 3 2
      .idea/workspace.xml
  2. 38 20
      app/Service/InventoryService.php

+ 3 - 2
.idea/workspace.xml

@@ -3,7 +3,7 @@
   <component name="ChangeListManager">
     <list default="true" id="b5852db3-28ab-419d-82cf-b6c0f6b99397" name="变更" comment="">
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/app/Service/MaterialService.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Service/MaterialService.php" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/app/Service/InventoryService.php" beforeDir="false" afterPath="$PROJECT_DIR$/app/Service/InventoryService.php" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -152,7 +152,8 @@
       <workItem from="1685595177391" duration="2533000" />
       <workItem from="1685669123669" duration="2439000" />
       <workItem from="1685952381761" duration="726000" />
-      <workItem from="1686021203384" duration="13363000" />
+      <workItem from="1686021203384" duration="14595000" />
+      <workItem from="1686099374239" duration="2870000" />
     </task>
     <servers />
   </component>

+ 38 - 20
app/Service/InventoryService.php

@@ -38,36 +38,54 @@ class InventoryService extends Service
     /**
      * 业务单据审核后 更新库存
      * order_number 订单编号
-     * opt_case 有些订单一次创建会同时有出入库单据
      */
     public function changeInventory($data){
-        $model = InOutRecord::where('del_time',0)
+        $result = InOutRecord::where('del_time',0)
             ->where('from_order_number',$data['order_number'])
             ->select('storehouse_id',DB::raw("sum(number) as number"),'goods_id')
-            ->groupby('goods_id','storehouse_id');
+            ->groupby('goods_id','storehouse_id')
+            ->get()->toArray();
 
-        $result = $model->get()->toArray();
         if (empty($result)) return false;
 
-        foreach ($result as $key => $value){
-            $m = Inventory::where('goods_id',$value['goods_id'])
-                ->where('storehouse_id',$value['storehouse_id'])
-                ->select('goods_id','number','storehouse_id')
-                ->first();
+        //查询参数拼接
+        $search_args = '';
+        foreach ($result as $value){
+            $search_args .= "(goods_id = '{$value["goods_id"]} and storehouse_id = '{$value["storehouse_id"]}') OR ";
+        }
+        $search_args = rtrim($search_args,'OR ');
 
-            if(empty($m)){
-                Inventory::insert($result[$key]);
-            }else{
-                $array['number'] = $value['number'] + $m->number;
-                $array['lock_number'] = $value['lock_number'] - $m->number;
+        DB::beginTransaction();
+        try {
+            $inventory = Inventory::whereRaw($search_args)
+                ->select('id','goods_id','number','storehouse_id')
+                ->sharedLock()
+                ->get()
+                ->toArray();
+            $inventory_map = [];
+            if(! empty($inventory)){
+                foreach ($inventory as $value){
+                    $inventory_map[$value['goods_id'] . $value['storehouse_id']]['id'] = $value['id'];
+                    $inventory_map[$value['goods_id'] . $value['storehouse_id']]['number'] = $value['number'];
+                }
+            }
 
-                Inventory::where('goods_id',$m->roll_film_id)
-                    ->where('storehouse_id',$m->storehouse_id)
-                    ->lockForUpdate()
-                    ->update($array);
+            foreach ($result as $key => $value){
+                $keys = $value['goods_id'] . $value['storehouse_id'];
+                if(! isset($inventory_map[$keys])){
+                    Inventory::insert($result[$key]);
+                }else{
+                    $array['number'] = $value['number'] + $inventory_map[$keys]['number'];
+                    $array['lock_number'] = $value['lock_number'] - $inventory_map[$keys]['number'];
+                    Inventory::where('id', $inventory_map[$keys]['id'])->update($array);
+                }
             }
-        }
 
-        return true;
+            DB::commit();
+            return true;
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return false;
+        }
     }
 }