Selaa lähdekoodia

in方法完成
out方法完成

xiaomi 1 vuosi sitten
vanhempi
commit
0de399ba04

+ 1 - 0
sd-business/src/main/java/com/sd/business/mapper/in/InOutStorageDetailsMapper.java

@@ -8,6 +8,7 @@ import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.sd.business.entity.inventory.po.BalancePriceNum;
 import org.apache.ibatis.annotations.Param;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 

+ 5 - 0
sd-business/src/main/java/com/sd/business/mapper/inventory/InventoryMapper.java

@@ -12,6 +12,7 @@ import com.sd.business.entity.inventory.vo.QuantityByDepartmentVo;
 import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
 import org.apache.ibatis.annotations.Param;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 
@@ -49,4 +50,8 @@ public interface InventoryMapper extends BaseMapper<Inventory> {
                                               @Param("departmentId") Long departmentId,
                                               @Param("bomSpecId") Long bomSpecId);
 
+    /**
+     * 根据bomSpecId获取结存数量
+     */
+    BigDecimal getQuantityByBomSpecId(@Param("bomSpecId") Long bomSpecId, @Param("warehouseId") Long warehouseId);
 }

+ 105 - 49
sd-business/src/main/java/com/sd/business/service/inventory/impl/InventoryServiceImpl.java

@@ -2,6 +2,7 @@ package com.sd.business.service.inventory.impl;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollectionUtil;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.exception.ServiceException;
@@ -18,11 +19,13 @@ import com.sd.business.entity.inventory.po.InventoryBackup;
 import com.sd.business.entity.inventory.vo.InventoryVo;
 import com.sd.business.entity.inventory.vo.QuantityByDepartmentVo;
 import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
+import com.sd.business.mapper.in.InOutStorageDetailsMapper;
 import com.sd.business.mapper.inventory.InventoryMapper;
 import com.sd.business.service.bom.BomSpecService;
 import com.sd.business.service.in.InOutStorageDetailsService;
 import com.sd.business.service.in.impl.InOutStorageServiceImpl;
 import com.sd.business.service.inventory.InventoryService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -44,6 +47,7 @@ import java.util.stream.Collectors;
  * @author
  * @since 2023-07-03
  */
+@Slf4j
 @Service
 public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {
 
@@ -135,13 +139,17 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
             saveOrUpdateBatch(inventoryList);
         }
-
     }
 
     @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));
+
         List<Long> bomSpecIdList = new ArrayList<>(map.keySet());
+        //填装不同bomSpecId的数据
+        Map<Long, List<InOutStorageDetails>> detailsHashMap = new HashMap<>();
 
         synchronized (this) {
             List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
@@ -160,84 +168,132 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
 
             //根据仓库id和规格id获取出入详细表数据
             List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
-            if (CollUtil.isEmpty(detailsList)){
+            //将数据为0的记录过滤掉
+            detailsList = detailsList.stream().filter(q -> q.getQuantity().compareTo(BigDecimal.ZERO) > 0).collect(Collectors.toList());
+
+            if (CollUtil.isEmpty(detailsList)) {
                 throw new ServiceException("数据不存在");
             }
+
+            detailsHashMap = detailsList.stream().collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
+
             //存储需要修改的入库记录
             List<InOutStorageDetails> updateList = new ArrayList<>();
             //存储这次出库的金额
             Map<Long, BigDecimal> outPriceMap = new HashMap<>();
 
-            int j = 0;
-            BigDecimal num;
-            BigDecimal quantity;
-            do {
-                //获取这条记录剩余的数量
-                InOutStorageDetails details = detailsList.get(j);
+            for (Long bomSpecId : bomSpecIdList) {
+                List<InOutStorageDetails> outStorageDetails = detailsHashMap.get(bomSpecId);
+                int j = 0;
+                BigDecimal num;
+                BigDecimal quantity;
+
+                do {
+                    //获取这条记录剩余的数量
+                    InOutStorageDetails details = outStorageDetails.get(j);
+
+                    //如果该记录为最后一条记录
+                    if (j == outStorageDetails.size() - 1) {
+                        BigDecimal naxtQuantity = outStorageDetails.get(j).getQuantity();
+                        //判断库存是否够扣减.不够则抛异常
+                        if (naxtQuantity.compareTo(map.get(details.getBomSpecId())) < 0) {
+                            throw new ServiceException("库存不够!!");
+                        }
+                    }
 
-                //获取这次一种规格的出库的数量
-                quantity = map.get(details.getBomSpecId());
+                    // 获取数据库中整体数量与出库数量对比
+                    BigDecimal allQuantity = baseMapper.getQuantityByBomSpecId(details.getBomSpecId(), details.getWarehouseId());
+                    if (allQuantity.compareTo(quantityMap.get(details.getBomSpecId())) < 0) {
+                        throw new ServiceException("库存不够!");
+                    }
 
-                //获取两次数量直接的差价
-                num = details.getQuantity().subtract(quantity);
+                    //获取这次一种规格的出库的数量
+                    quantity = map.get(details.getBomSpecId());
 
-                //如果这条记录库存不足,则计算下一条记录
-                if (num.compareTo(BigDecimal.ZERO) < 0){
-                    details.setQuantity(BigDecimal.ZERO);
-                    j++;
-                    continue;
-                }
-                //对数量进行减扣
-                //BigDecimal detailsNum = details.getQuantity().subtract(num);
-                details.setQuantity(num.abs());
+                    //获取两次数量直接的差价
+                    num = details.getQuantity().subtract(quantity);
 
-                //数量减少了,那金额也得减少
-                details.setInSumPrice(num.abs().multiply(details.getInUnitPrice()));
+                    //如果这条记录库存不足,则计算下一条记录
+                    if (num.compareTo(BigDecimal.ZERO) < 0) {
+                        addOutPriceMap(outPriceMap, map, details);
 
-                //将这次出库的金额存储到map里面
-                BigDecimal outPrice = details.getInUnitPrice().multiply(num);
-                outPriceMap.put(details.getBomSpecId(), outPrice);
-                //将减扣数量后的对象修改回去
-                updateList.add(details);
-                j++;
+                        details.setQuantity(BigDecimal.ZERO);
+                        details.setInSumPrice(BigDecimal.ZERO);
 
-            }while (num.compareTo(BigDecimal.ZERO) < 0 && j < detailsList.size() );
+                        updateList.add(details);
+                        map.put(details.getBomSpecId(), num.abs());
+                        j++;
+                        continue;
+                    }
+                    //对数量进行减扣
+                    //BigDecimal detailsNum = details.getQuantity().subtract(num);
+                    details.setQuantity(num.abs());
 
-                inOutStorageDetailsService.updateBatchById(updateList);
+                    //数量减少了,那金额也得减少
+                    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);
 
                 for (Inventory inventory : inventoryList) {
                     // 获取之前的结存金额
                     BigDecimal balancePrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice());
                     // 减去出库金额后的结存金额
-                    BigDecimal newBalancePrice;
-                    if (outPriceMap.get(inventory.getBomSpecId()) == null){
+                    BigDecimal newBalancePrice = BigDecimal.ZERO;
+                    if (outPriceMap.get(inventory.getBomSpecId()) == null) {
                         newBalancePrice = balancePrice.subtract(BigDecimal.ZERO);
-                    }else {
-                        newBalancePrice = balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId()));
+                    } else {
+                        newBalancePrice = newBalancePrice.add(balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId())));
                     }
                     // 减去出库数量后的结存数量
-                    inventory.setQuantity(inventory.getQuantity().subtract(map.get(inventory.getBomSpecId())));
+                    inventory.setQuantity(inventory.getQuantity().subtract(quantityMap.get(inventory.getBomSpecId())));
                     // 获取最后的结存单价
-                    BigDecimal balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
+                    BigDecimal balanceUnitPrice;
+                    if (inventory.getQuantity().compareTo(BigDecimal.ZERO) == 0) {
+                        balanceUnitPrice = BigDecimal.ZERO;
+                    } else {
+                        balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
+                    }
                     //将数据存入对象
                     inventory.setBalanceUnitPrice(balanceUnitPrice);
                 }
-                updateBatchById(inventoryList);
-            }
-
+            updateBatchById(inventoryList);
+        }
     }
-        private List<Inventory> getInventoryList (Long departmentId, Long warehouseId, List < Long > bomSpecIdList){
-            return list(q -> q
-                    .eq(Inventory::getDepartmentId, departmentId)
-                    .eq(Inventory::getWarehouseId, warehouseId)
-                    .in(Inventory::getBomSpecId, bomSpecIdList)
-            );
+
+    //将出库金额添加到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());
+        if (outPriceMap.containsKey(details.getBomSpecId())) {
+            BigDecimal naxtPrice = outPriceMap.get(details.getBomSpecId());
+            BigDecimal addPrice = naxtPrice.add(multiply);
+            outPriceMap.put(details.getBomSpecId(), addPrice);
+        } else {
+            outPriceMap.put(details.getBomSpecId(), multiply);
         }
+    }
+
+    private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
+        return list(q -> q
+                .eq(Inventory::getDepartmentId, departmentId)
+                .eq(Inventory::getWarehouseId, warehouseId)
+                .in(Inventory::getBomSpecId, bomSpecIdList)
+        );
+    }
 
-        private List<InOutStorageDetails> getDetailsList (Long warehouseId, List < Long > bomSpecIdList){
-            return inOutStorageDetailsService.list(q -> q
-                    .eq(InOutStorageDetails::getWarehouseId, warehouseId)
+    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)
 

+ 1 - 0
sd-business/src/main/resources/mapper/in/InOutStorageBomMapper.xml

@@ -26,4 +26,5 @@
             ${ew.customSqlSegment}
     </select>
 
+
 </mapper>

+ 11 - 0
sd-business/src/main/resources/mapper/inventory/InventoryMapper.xml

@@ -65,5 +65,16 @@
         order by i.create_time
     </select>
 
+    <select id="getQuantityByBomSpecId" resultType="java.math.BigDecimal" parameterType="java.lang.Long">
+        SELECT
+	        sum(quantity)
+        FROM
+	        in_out_storage_details
+        WHERE
+            warehouse_id = #{warehouseId}
+        AND
+	        bom_spec_id = #{bomSpecId}
+    </select>
+
 
 </mapper>