瀏覽代碼

待采购数量合并计算方式修改

yzc 1 年之前
父節點
當前提交
6cf01f718e

+ 5 - 7
hx-item/src/main/java/com/fjhx/item/service/product/impl/ProductInfoServiceImpl.java

@@ -346,22 +346,20 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
         if (ObjectUtil.isEmpty(pIds)) {
             return;
         }
-        //获取合同归属归属的物料可用库存
-        Map<Long, ProductStockInfo> availableQuantityMap = productStockInfoService.mapKEntity(ProductStockInfo::getId, q -> q
-                .in(ProductStockInfo::getProductId, pIds)
-                .eq(ProductStockInfo::getCompanyId, companyId)
-        );
 
         List<ProductAvailableRecord> availableRecordList = new ArrayList<>();
         for (InOutBo inOutBo : inOutList) {
-            ProductStockInfo productStockInfo = availableQuantityMap.get(inOutBo.getProductId());
+            //查询可用库存
+            ProductStockInfo productStockInfo = productStockInfoService.getOne(q -> q
+                    .eq(ProductStockInfo::getProductId, inOutBo.getProductId())
+                    .eq(ProductStockInfo::getCompanyId, companyId)
+            );
             if (ObjectUtil.isEmpty(productStockInfo)) {
                 productStockInfo = new ProductStockInfo();
                 productStockInfo.setAvailableQuantity(BigDecimal.ZERO);
                 productStockInfo.setCompanyId(companyId);
                 productStockInfo.setProductId(inOutBo.getProductId());
                 productStockInfoService.save(productStockInfo);
-                availableQuantityMap.put(inOutBo.getProductId(), productStockInfo);
             }
 
             BigDecimal availableQuantityNew;

+ 68 - 73
hx-mes/src/main/java/com/fjhx/mes/service/production/impl/ProduceOrderServiceImpl.java

@@ -57,6 +57,7 @@ import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 
@@ -293,89 +294,83 @@ public class ProduceOrderServiceImpl extends ServiceImpl<ProduceOrderMapper, Pro
         Long companyId = productionOrderDto.getCompanyId();
         Long contractId = productionOrderDto.getContractId();
 
-        //生成物料待采购数据
+        //获取合同产品信息
         List<ContractProduct> contractProductList = contractProductService.list(q -> q.eq(ContractProduct::getContractId, contractId));
+        Map<Long, ContractProduct> productMap = contractProductList.stream().collect(Collectors.toMap(ContractProduct::getId, Function.identity()));
 
         //获取物料列表
         List<Long> cpIds = contractProductList.stream().map(ContractProduct::getId).collect(Collectors.toList());
-        Map<Long, List<ContractProductBom>> cpBMap = contractProductBomService.mapKGroup(ContractProductBom::getContractProductId, q -> q.in(ContractProductBom::getContractProductId, cpIds));
-
-        //待采购列表<物料id,申购明细>相同物料直接合并
-        Map<Long, SubscribeDetail> subscribeDetailMap = new ConcurrentHashMap<>();
-
-        for (ContractProduct contractProduct : contractProductList) {
-            //获取物料列表,以及原材料
-            List<ContractProductBom> contractProductBomList = cpBMap.get(contractProduct.getId());
-
-            if (ObjectUtil.isEmpty(contractProductBomList)) {
-                continue;
+        List<ContractProductBom> cpBList = contractProductBomService.list(q -> q.in(ContractProductBom::getContractProductId, cpIds));
+
+        //合并所有相同物料
+        Map<Long, InOutBo> inOutBoMap = new ConcurrentHashMap<>();
+        for (ContractProductBom bom : cpBList) {
+            Long materialId = bom.getMaterialId();
+            ContractProduct contractProduct = productMap.get(bom.getContractProductId());
+            BigDecimal multiply = bom.getQuantity().multiply(contractProduct.getQuantity());
+
+            //合并相同物料
+            InOutBo inOutBo = inOutBoMap.get(materialId);
+            if (ObjectUtil.isEmpty(inOutBo)) {
+                inOutBo.setProductId(materialId);
+                inOutBo.setQuantity(BigDecimal.ZERO);
             }
-            List<Long> materialIds = contractProductBomList.stream().map(ContractProductBom::getMaterialId).collect(Collectors.toList());
-            Map<Long, ProductInfo> materialMap = productInfoService.mapKEntity(ProductInfo::getId, q -> q.in(ProductInfo::getId, materialIds));
-
-            //获取合同归属归属的物料可用库存
-            Map<Long, BigDecimal> availableQuantityMap = productStockInfoService.mapKV(ProductStockInfo::getId, ProductStockInfo::getAvailableQuantity, q -> q
-                    .in(ProductStockInfo::getProductId, materialIds)
-                    .eq(ProductStockInfo::getCompanyId, companyId)
-            );
-
-
-            //遍历物料列表
-            for (ContractProductBom productBomDetail : contractProductBomList) {
-                Long materialId = productBomDetail.getMaterialId();
-                ProductInfo materialInfo = materialMap.get(materialId);
-
-                BigDecimal multiply = productBomDetail.getQuantity().multiply(contractProduct.getQuantity());
-
-                //获取产品安全库存
-                BigDecimal stockThreshold = materialInfo.getStockThreshold();
-                //获取可用库存
-                BigDecimal availableQuantity = availableQuantityMap.getOrDefault(materialId, BigDecimal.ZERO);
-                //计数需要采购的数量(需采购量 = 安全库存 - (可用库存 - 合同量)若 需采购量<0,则按0算不采购)
-                BigDecimal subtract = availableQuantity.subtract(multiply);
-                BigDecimal requiredQuantity = stockThreshold.subtract(subtract);
-
-                //计算可用库存
-                if (requiredQuantity.compareTo(BigDecimal.ZERO) < 0) {
-                    //需要采购的数量<0 可用库存 = 可用库存 - 合同量
-                    InOutBo inOutBo = new InOutBo();
-                    inOutBo.setProductId(materialId);
-                    inOutBo.setQuantity(multiply);
-                    productInfoService.editAvailableQuantity(Arrays.asList(inOutBo), InOutType.OUT, contractProduct.getId(), ProductAvailableRecordType.SALE_PASS, companyId);
-                } else {
-                    //需要采购的数量>=0 可用库存 = 安全库存
-                    InOutBo inOutBo = new InOutBo();
-                    inOutBo.setProductId(materialId);
-                    inOutBo.setQuantity(stockThreshold);
-                    productInfoService.editAvailableQuantity(Arrays.asList(inOutBo), InOutType.EQ, contractProduct.getId(), ProductAvailableRecordType.SALE_PASS, companyId);
-                }
-
-                //需要采购的数量大于0生成待采购数据
-                if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
-                    SubscribeDetail subscribeDetail = subscribeDetailMap.get(materialId);
-                    if (ObjectUtil.isEmpty(subscribeDetail)) {
-                        subscribeDetail = new SubscribeDetail();
-                        subscribeDetail.setProductId(materialId);
-                        subscribeDetail.setCount(BigDecimal.ZERO);
-                        subscribeDetail.setStatus(15);//待采购
-                        subscribeDetail.setContractId(contractId);
-                        subscribeDetail.setDataType(1);
-                        subscribeDetail.setCompanyId(companyId);
-                    }
-                    //合并待采购数量
-                    BigDecimal count = subscribeDetail.getCount();
-                    subscribeDetail.setCount(count.add(requiredQuantity));
-
-                    //添加到Map里
-                    subscribeDetailMap.put(materialId, subscribeDetail);
-                }
+            inOutBo.setQuantity(inOutBo.getQuantity().add(multiply));
+            inOutBoMap.put(materialId, inOutBo);
+        }
 
+        List<Long> materialIds = cpBList.stream().map(ContractProductBom::getMaterialId).collect(Collectors.toList());
+        Map<Long, ProductInfo> materialMap = productInfoService.mapKEntity(ProductInfo::getId, q -> q.in(ProductInfo::getId, materialIds));
+
+        //计算并生成待出库数据
+        List<SubscribeDetail> subscribeDetailList = new ArrayList<>();
+        for (InOutBo value : inOutBoMap.values()) {
+            Long materialId = value.getProductId();
+            BigDecimal multiply = value.getQuantity();
+
+            ProductInfo materialInfo = materialMap.get(materialId);
+            //获取产品安全库存
+            BigDecimal stockThreshold = materialInfo.getStockThreshold();
+            //获取可用库存
+            BigDecimal availableQuantity = BigDecimal.ZERO;
+            ProductStockInfo productStockInfo = productStockInfoService.getOne(q -> q.eq(ProductStockInfo::getProductId, materialId).eq(ProductStockInfo::getCompanyId, companyId));
+            if (ObjectUtil.isNotEmpty(productStockInfo)) {
+                availableQuantity = productStockInfo.getAvailableQuantity();
+            }
+            //计数需要采购的数量(需采购量 = 安全库存 - (可用库存 - 合同量)若 需采购量<0,则按0算不采购)
+            BigDecimal subtract = availableQuantity.subtract(multiply);
+            BigDecimal requiredQuantity = stockThreshold.subtract(subtract);
+
+            //计算可用库存
+            if (requiredQuantity.compareTo(BigDecimal.ZERO) < 0) {
+                //需要采购的数量<0 可用库存 = 可用库存 - 合同量
+                InOutBo inOutBo = new InOutBo();
+                inOutBo.setProductId(materialId);
+                inOutBo.setQuantity(multiply);
+                productInfoService.editAvailableQuantity(Arrays.asList(inOutBo), InOutType.OUT, contractId, ProductAvailableRecordType.SALE_PASS, companyId);
+            } else {
+                //需要采购的数量>=0 可用库存 = 安全库存
+                InOutBo inOutBo = new InOutBo();
+                inOutBo.setProductId(materialId);
+                inOutBo.setQuantity(stockThreshold);
+                productInfoService.editAvailableQuantity(Arrays.asList(inOutBo), InOutType.EQ, contractId, ProductAvailableRecordType.SALE_PASS, companyId);
             }
 
+            //需要采购的数量大于0生成待采购数据
+            if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                SubscribeDetail subscribeDetail = new SubscribeDetail();
+                subscribeDetail.setProductId(materialId);
+                subscribeDetail.setCount(requiredQuantity);
+                subscribeDetail.setStatus(15);//待采购
+                subscribeDetail.setContractId(contractId);
+                subscribeDetail.setDataType(1);
+                subscribeDetail.setCompanyId(companyId);
+                subscribeDetailList.add(subscribeDetail);
+            }
         }
-        //保存待采购明细
-        subscribeDetailService.saveBatch(subscribeDetailMap.values());
 
+        //保存待采购明细
+        subscribeDetailService.saveBatch(subscribeDetailList);
     }
 
     /**