瀏覽代碼

库存的入库出库优化

xiaomi 1 年之前
父節點
當前提交
ae703a77d4

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/in/po/InOutStorageDetails.java

@@ -55,4 +55,9 @@ public class InOutStorageDetails extends BasePo {
      */
     private BigDecimal quantity;
 
+    /**
+     * 逻辑删除
+     */
+    private Integer delFlag;
+
 }

+ 37 - 27
sd-business/src/main/java/com/sd/business/service/inventory/impl/InventoryServiceImpl.java

@@ -117,7 +117,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                     inventoryList.add(inventory);
                     map.put(bomSpecId, inventory);
                 }
-
                 List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
                 if (CollUtil.isEmpty(detailsList)){
                     for (InOutStorageDetails details : detailsList) {
@@ -143,9 +142,12 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
     @Override
     public void out(Long departmentId, Long warehouseId, List<? extends InOutFun> list) {
-        Map<Long, BigDecimal> quantityMap = list.stream().collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
-
-        Map<Long, BigDecimal> map = list.stream().collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
+        //存储这次出库的数量,不可改变
+        Map<Long, BigDecimal> quantityMap = list.stream()
+                .collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
+        //存储此一条数据出库的数量,可以改变
+        Map<Long, BigDecimal> map = list.stream()
+                .collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
 
         List<Long> bomSpecIdList = new ArrayList<>(map.keySet());
         //填装不同bomSpecId的数据
@@ -153,7 +155,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
         synchronized (this) {
             List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
-
             // 查询库存中是否存在bom
             for (Inventory inventory : inventoryList) {
                 Long bomSpecId = inventory.getBomSpecId();
@@ -165,17 +166,21 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                     throw new ServiceException("出库失败,品名为:" + bomSpec.getName() + "的bom库存不足");
                 }
             }
-
             //根据仓库id和规格id获取出入详细表数据
             List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
+
             //将数据为0的记录过滤掉
-            detailsList = detailsList.stream().filter(q -> q.getQuantity().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
+            //detailsList = detailsList.stream().filter(q -> q.getQuantity().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
+            // 根据条件过滤detailsList,得到detailsList中details.getQuantity()不为0的对象列表
+            detailsList = detailsList.stream()
+                    .filter(details -> details.getQuantity().compareTo(BigDecimal.ZERO) > 0)
+                    .collect(Collectors.toList());
 
             if (CollUtil.isEmpty(detailsList)) {
-                throw new ServiceException("数据不存在");
+                throw new ServiceException(departmentId + "和" + warehouseId + "的数据为null");
             }
-
-            detailsHashMap = detailsList.stream().collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
+            detailsHashMap = detailsList.stream()
+                    .collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
 
             //存储需要修改的入库记录
             List<InOutStorageDetails> updateList = new ArrayList<>();
@@ -187,26 +192,24 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                 int j = 0;
                 BigDecimal num;
                 BigDecimal quantity;
-
                 do {
                     //获取这条记录剩余的数量
                     InOutStorageDetails details = outStorageDetails.get(j);
 
+                    // 获取数据库中整体数量与出库数量对比
+                    BigDecimal allQuantity = baseMapper.getQuantityByBomSpecId(details.getBomSpecId(), details.getWarehouseId());
+                    if (allQuantity.compareTo(quantityMap.get(details.getBomSpecId())) < 0) {
+                        throw new ServiceException(details.getBomSpecId() + "这种规格的库存不够,库存数量为" + details.getQuantity());
+                    }
+
                     //如果该记录为最后一条记录
                     if (j == outStorageDetails.size() - 1) {
                         BigDecimal naxtQuantity = outStorageDetails.get(j).getQuantity();
                         //判断库存是否够扣减.不够则抛异常
                         if (naxtQuantity.compareTo(map.get(details.getBomSpecId())) < 0) {
-                            throw new ServiceException("库存不够!!");
+                            throw new ServiceException(details.getBomSpecId() + "这种规格的库存不够,库存数量为" + details.getQuantity());
                         }
                     }
-
-                    // 获取数据库中整体数量与出库数量对比
-                    BigDecimal allQuantity = baseMapper.getQuantityByBomSpecId(details.getBomSpecId(), details.getWarehouseId());
-                    if (allQuantity.compareTo(quantityMap.get(details.getBomSpecId())) < 0) {
-                        throw new ServiceException("库存不够!");
-                    }
-
                     //获取这次一种规格的出库的数量
                     quantity = map.get(details.getBomSpecId());
 
@@ -225,6 +228,9 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                         j++;
                         continue;
                     }
+                    //将这次出库的金额存储到map里面
+                    addOutPriceMap(outPriceMap, map, details);
+
                     //对数量进行减扣
                     //BigDecimal detailsNum = details.getQuantity().subtract(num);
                     details.setQuantity(num.abs());
@@ -233,13 +239,9 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
                     BigDecimal multiply = num.abs().multiply(details.getInUnitPrice());
                     details.setInSumPrice(multiply);
 
-                    //将这次出库的金额存储到map里面
-                    addOutPriceMap(outPriceMap, map, details);
-
                     //将减扣数量后的对象修改回去
                     updateList.add(details);
                     j++;
-
                 } while (num.compareTo(BigDecimal.ZERO) < 0 && j < outStorageDetails.size());
             }
                 inOutStorageDetailsService.updateBatchById(updateList);
@@ -272,8 +274,16 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
     //将出库金额添加到map
     private void addOutPriceMap(Map<Long, BigDecimal> outPriceMap, Map<Long, BigDecimal> map, InOutStorageDetails details) {
+
         BigDecimal mapQuantity = map.get(details.getBomSpecId());
-        BigDecimal multiply = mapQuantity.multiply(details.getInUnitPrice());
+        BigDecimal multiply;
+
+        //如果出库数量大于库存记录数量
+        if (mapQuantity.compareTo(details.getQuantity()) > 0) {
+            multiply = details.getQuantity().multiply(details.getInUnitPrice());
+        } else {
+            multiply = mapQuantity.multiply(details.getInUnitPrice());
+        }
         if (outPriceMap.containsKey(details.getBomSpecId())) {
             BigDecimal naxtPrice = outPriceMap.get(details.getBomSpecId());
             BigDecimal addPrice = naxtPrice.add(multiply);
@@ -294,9 +304,9 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
     private List<InOutStorageDetails> getDetailsList(Long warehouseId, List<Long> bomSpecIdList) {
         return inOutStorageDetailsService.list(q -> q
                 .eq(InOutStorageDetails::getWarehouseId, warehouseId)
-                    .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
-                    .isNotNull(InOutStorageDetails::getQuantity)
-
+                .eq(InOutStorageDetails::getDelFlag, 0)
+                .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
+                .isNotNull(InOutStorageDetails::getQuantity)
             );
         }
 

+ 8 - 5
sd-business/src/main/resources/mapper/inventory/InventoryMapper.xml

@@ -47,20 +47,21 @@
     </select>
 
     <select id="getInOutDetails" resultType="com.sd.business.entity.in.po.InOutStorageDetails">
-        select i.quantity,
+        SELECT i.quantity,
         i.in_unit_price,
         i.in_sum_price
-        from in_out_storage_details i
+        FROM in_out_storage_details i
         <where>
             <if test="bomSpecId != null">
-                and bom_spec_id = #{bomSpecId}
+                AND bom_spec_id = #{bomSpecId}
             </if>
             <if test="warehouseId != null">
-                and warehouse_id = #{warehouseId}
+                AND warehouse_id = #{warehouseId}
             </if>
             <if test="departmentId != null">
-                and department_id = #{departmentId}
+                AND department_id = #{departmentId}
             </if>
+            AND del_flag = 0
         </where>
         order by i.create_time
     </select>
@@ -74,6 +75,8 @@
             warehouse_id = #{warehouseId}
         AND
 	        bom_spec_id = #{bomSpecId}
+	    AND
+	        del_flag = 0
     </select>