zhanghl
2025-05-15 f16ec51787ed0cb094b71ff8146c740d944f729d
aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsPlateStandardRequireServiceImpl.java
@@ -64,7 +64,7 @@
    ApsPlateStandardRequireBomOrderDetailMapper plateBomOrderDetailMapper;
    @Autowired
    private ApsPlatePlanMapper apsPlatePlanMapper;
    private IApsPlatePlanService apsPlatePlanService;
    @Resource
    IApsPlateStandardRequireBomOrderDetailService bomOrderDetailService;
@@ -175,20 +175,24 @@
        String batchNum= requireBatchService.getNewBatchNumber();
        /*获取钣金主单信息*/
        List<ApsPlatePlan> mainPlans = platePlanMapper.selectPlatePlanByPlantMajor(plantCode,mainOrderType);
        Hashtable<String, BigDecimal> usedStorage = new Hashtable<>();
        Hashtable<String, ApsMaterialStorageManagement> usedStorage = new Hashtable<>();
        /*内存中存储子件工单*/
        Hashtable<String, List<ApsPlatePlan>> subPlans = new Hashtable<>();
        log.info("开始生成需求:");
        for (ApsPlatePlan mainPlan : mainPlans) {
            String itemNumber = mainPlan.getItemNumber();
            /*根据料号 获取BOM Header */
            /*当前Bom节点处理完成后,处理下级BOM*/
            List<ApsPlateStandardRequire> requiresList=new ArrayList<>();
            List<ApsPlateStandardRequireBomStockDetail> stockDetailsList=new ArrayList<>();
            List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList=new ArrayList<>();
            log.info("开始生成需求:工单号:"+mainPlan.getDocumentNumber());
            /*当前Bom节点处理完成后,处理下级BOM*/
            getBomRequires(plantCode, "0","0",itemNumber,BigDecimal.ONE, batchNum, null
                    , mainPlan, requiresList, 0L,stockDetailsList,orderDetailsList,usedStorage
                    , mainPlan, requiresList, 0L,stockDetailsList,orderDetailsList,usedStorage,subPlans
            );
            // 批量插入以提高性能
@@ -200,38 +204,40 @@
                    apsPlateStandardRequireMapper.batchInsert(batch);
                }
            }
            log.info("生成完成");
//            if(!stockDetailsList.isEmpty()){
//                int batchSize = 1000;
//                stockDetailsList.forEach(x->x.setId(IdUtil.getSnowflakeNextId()));
//                for (int i = 0; i < stockDetailsList.size(); i += batchSize) {
//                    int end = Math.min(i + batchSize, stockDetailsList.size());
//                    List<ApsPlateStandardRequireBomStockDetail> batch = stockDetailsList.subList(i, end);
//                    plateBomStockDetailMapper.batchInsert(batch);
//                }
//            }
//
//            if(!orderDetailsList.isEmpty()){
//                int batchSize = 1000;
//                orderDetailsList.forEach(x->x.setId(IdUtil.getSnowflakeNextId()));
//                for (int i = 0; i < orderDetailsList.size(); i += batchSize) {
//                    int end = Math.min(i + batchSize, orderDetailsList.size());
//                    List<ApsPlateStandardRequireBomOrderDetail> batch = orderDetailsList.subList(i, end);
//                    plateBomOrderDetailMapper.batchInsert(batch);
//                }
//            }
            /*集中保存库存信息*/
           /* if(!usedStorage.isEmpty()){
                usedStorage.forEach((key, sm) -> {
                    storageManagementService.updateRemainderStock(sm.getId(),sm.getRemainderStock(),sm.getVersion());
                });
            }*/
            if(!stockDetailsList.isEmpty()){
                int batchSize = 1000;
                stockDetailsList.forEach(x->x.setId(IdUtil.getSnowflakeNextId()));
                for (int i = 0; i < stockDetailsList.size(); i += batchSize) {
                    int end = Math.min(i + batchSize, stockDetailsList.size());
                    List<ApsPlateStandardRequireBomStockDetail> batch = stockDetailsList.subList(i, end);
                    plateBomStockDetailMapper.batchInsert(batch);
                }
            }
            if(!orderDetailsList.isEmpty()){
                int batchSize = 1000;
                orderDetailsList.forEach(x->x.setId(IdUtil.getSnowflakeNextId()));
                for (int i = 0; i < orderDetailsList.size(); i += batchSize) {
                    int end = Math.min(i + batchSize, orderDetailsList.size());
                    List<ApsPlateStandardRequireBomOrderDetail> batch = orderDetailsList.subList(i, end);
                    plateBomOrderDetailMapper.batchInsert(batch);
                }
            }
        }
        usedStorage.forEach((key,value)->{
            log.info("料号:"+key+",库存:"+value);
        });
        /*集中保存库存信息*/
        if(!usedStorage.isEmpty()){
            usedStorage.forEach((key, sm) -> {
                storageManagementService.updateRemainderStock(sm.getId(),sm.getRemainderStock(),sm.getVersion());
            });
        }
        if(!subPlans.isEmpty()){
            subPlans.forEach((key, subPlansList) -> {
                subPlansList.forEach(x->{
                    if (!x.getUnmatchedQuantity().equals(x.getProductionQuantity())){
                        platePlanMapper.updatePlanUnMatchQtyAndVersion(x);
                    }
                });
            });
        }
    }
    /**
@@ -255,7 +261,8 @@
                                List<ApsPlateStandardRequire> allRequires,
                                Long level,List<ApsPlateStandardRequireBomStockDetail> stockDetailsList,
                                List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList,
                                 Hashtable<String, BigDecimal> usedStorage
                                Hashtable<String, ApsMaterialStorageManagement> usedStorage,
                                Hashtable<String, List<ApsPlatePlan>> subPlans
    ) {
        /*构建需求信息*/
        ApsPlateStandardRequire require = new ApsPlateStandardRequire();
@@ -285,17 +292,14 @@
        /*默认净需求为BOM用量*/
        require.setNetRequirement(require.getRequireAmount());
        /*读取库存信息的优先级-> 内存、redis、db*/
       /* ApsMaterialStorageManagement storage = usedStorage.get(itemCode);
        if(storage==null){
            storage = storageManagementService.getRdsStorage(plant, itemCode);
            if(storage!=null){
                *//*剩余库存*//*
                remainderStock = storage.getRemainderStock();
            }
        }*/
        Optional<ApsMaterialStorageManagement> itemStorageOpt = storageManagementService.getItemStorage(plant, itemCode);
        Optional<ApsMaterialStorageManagement> itemStorageOpt = Optional.ofNullable(usedStorage.get(itemCode)) ;
        if(itemStorageOpt.isEmpty()){
            itemStorageOpt = storageManagementService.getRdsStorage(plant, itemCode);
        }
        if (itemStorageOpt.isPresent()) {
            ApsMaterialStorageManagement     itemStorage=itemStorageOpt.get();
            BigDecimal remainderStock =itemStorage.getRemainderStock();
            /*计算净需求 默认=需求数量*/
@@ -320,7 +324,7 @@
                        , afterStockAmount, require.getId(), stockDetailsList
                );
                /*更新内存中的库存使用信息*/
               usedStorage.put(itemCode, afterStockAmount);
               usedStorage.put(itemCode, itemStorage);
            }
        }
        /*未匹配数量,默认为净需求*/
@@ -377,7 +381,7 @@
        if (require.getNetRequirement().compareTo(BigDecimal.ZERO) > 0) {
            require.setMatchMode("工单匹配");
            /*使用子件工单进行需求匹配*/
            matchRequireAndSubPlan(require,orderDetailsList);
            matchRequireAndSubPlan(require,orderDetailsList,subPlans);
        }
        allRequires.add(require);
        log.info("已生成需求:"+plan.getDocumentNumber()+"bomHeaderCode:"+bomHeaderCode+"bomLineCode:"+itemCode+"bomLevel:"+level);
@@ -389,7 +393,7 @@
                bomLineList.forEach(line -> {
                    getBomRequires(plant, itemCode, line.getBomLineId(),line.getItemCode() ,line.getNum() , batchNum
                            , require.getStartDate(), plan, allRequires, nextLevel,stockDetailsList
                            ,orderDetailsList,usedStorage
                            ,orderDetailsList,usedStorage,subPlans
                    );
                });
            }
@@ -413,12 +417,12 @@
    * 使用子件工单匹配需求中的净需求
    * */
    private void matchRequireAndSubPlan(ApsPlateStandardRequire require, List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList) {
    private void matchRequireAndSubPlan(ApsPlateStandardRequire require, List<ApsPlateStandardRequireBomOrderDetail> orderDetailsList,Hashtable<String, List<ApsPlatePlan>> subPlans) {
        BigDecimal netRequirement = require.getNetRequirement();
        require.setMatchMode("工单匹配");
        require.setMatchState("已匹配");
        if (netRequirement.compareTo(BigDecimal.ZERO) > 0) {
            ApsPlatePlan platePlan = apsPlatePlanMapper.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode());
            ApsPlatePlan platePlan = apsPlatePlanService.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode(),subPlans);
            /*子件工单的未匹配数量 作为当前的库存*/
            while (platePlan != null && netRequirement.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal stock = platePlan.getUnmatchedQuantity();
@@ -426,6 +430,8 @@
                    /* 库存数量 大于 净需求数量*/
                    /* 净需求数量=0 ,子件工单未匹配数量= 库存-净需求*/
                    BigDecimal subtract = stock.subtract(netRequirement);
                    platePlan.setUnmatchedQuantity(subtract);
                    platePlan.setVersion(platePlan.getVersion() + 1);
                    bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, subtract, stock, netRequirement,orderDetailsList);
                    netRequirement = BigDecimal.ZERO;
@@ -436,6 +442,8 @@
                    /*净需求数量 == 库存数量*/
                    BigDecimal subtract = BigDecimal.ZERO;
                    platePlan.setUnmatchedQuantity(subtract);
                    platePlan.setVersion(platePlan.getVersion() + 1);
                    bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, subtract, stock, netRequirement,orderDetailsList);
                    netRequirement = BigDecimal.ZERO;
@@ -446,11 +454,12 @@
                    /*需求大于库存*/
                    /*净需求 被部分满足 */
                    BigDecimal rest = netRequirement.subtract(stock);
                    platePlan.setUnmatchedQuantity(BigDecimal.ZERO);
                    platePlan.setVersion(platePlan.getVersion() + 1);
                    /*工单 未匹配数量为0 全部用于匹配需求*/
                    bomOrderDetailService.savePlastPlanAndBomOrderDetail(require, platePlan, BigDecimal.ZERO, stock, netRequirement,orderDetailsList);
                    /*净需求未被满足,需要继续匹配*/
                    platePlan = apsPlatePlanMapper.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode());
                    platePlan = apsPlatePlanService.selectUnMatchPlateSubPlan(require.getOrgCode(),  require.getBomLineCode(),subPlans);
                    /*剩余净需求*/
                    netRequirement = rest;
                    require.setUnmatchedDemandAmount(rest);