From dc391936246ea4156fae38ad9802eac981bbc4b3 Mon Sep 17 00:00:00 2001 From: syruan <15555146157@163.com> Date: Mon, 28 Jul 2025 09:51:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=A2=86=E6=96=99=E5=BA=93?= =?UTF-8?q?=E5=AD=98=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/MaterialLeaseInfoServiceImpl.java | 74 ++++++++++++++----- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/clz/service/impl/MaterialLeaseInfoServiceImpl.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/clz/service/impl/MaterialLeaseInfoServiceImpl.java index 16eaa8a5..87bbde81 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/clz/service/impl/MaterialLeaseInfoServiceImpl.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/clz/service/impl/MaterialLeaseInfoServiceImpl.java @@ -1839,8 +1839,8 @@ public class MaterialLeaseInfoServiceImpl implements MaterialLeaseInfoService { } /** - * 预计算库存数据,替代复杂的类型树计算 - * 这个方法将原来每个detail都要执行的复杂计算优化为一次性预计算 + * 预计算库存数据,重现原始的类型树计算逻辑 + * 业务逻辑:总领料数量 - 已在用数量 = 剩余可用库存 */ private Map preCalculateStorageNums(MaterialLeaseApplyInfo info, List agreementList, @@ -1848,27 +1848,67 @@ public class MaterialLeaseInfoServiceImpl implements MaterialLeaseInfoService { Map storageNumMap = new HashMap<>(); try { - // 简化的库存计算:直接查询ma_type表的storage_num字段 - for (Long typeId : typeIds) { - try { - // 查询单个类型的信息,获取storage_num - Type typeInfo = typeMapper.selectTypeByTypeId(typeId); - if (typeInfo != null && typeInfo.getStorageNum() != null) { - storageNumMap.put(typeId, typeInfo.getStorageNum()); - } else { - storageNumMap.put(typeId, BigDecimal.ZERO); + log.info("开始预计算库存,原始类型树逻辑,处理{}个类型", typeIds.size()); + + // 第1步:从协议中获取总领料数量(原listL5逻辑) + Map totalLeaseNums = new HashMap<>(); + if (!CollectionUtils.isEmpty(agreementList)) { + for (AgreementVo agreementVo : agreementList) { + BackApplyInfo backApplyInfo = new BackApplyInfo(); + backApplyInfo.setAgreementId(Long.valueOf(agreementVo.getAgreementId())); + List listL4 = mapper.getUseTypeTreeL4(backApplyInfo); + + if (!CollectionUtils.isEmpty(listL4)) { + for (TypeTreeNode node : listL4) { + if (node.getTypeId() != 0L && node.getNum() != null) { + totalLeaseNums.merge(node.getTypeId(), node.getNum(), BigDecimal::add); + } + } } - } catch (Exception e) { - log.warn("查询typeId {}的库存失败: {}", typeId, e.getMessage()); - storageNumMap.put(typeId, BigDecimal.ZERO); } } - log.info("预计算库存完成,为{}个类型查询了库存值", typeIds.size()); + // 第2步:通过工程获取已在用数量(原list7逻辑) + Map usedNums = new HashMap<>(); + MaterialLeaseApplyInfo bean = new MaterialLeaseApplyInfo(); + bean.setProId(info.getProId()); + List listAgreement = materialLeaseInfoMapper.getAgreementIdByProId(bean); + if (!CollectionUtils.isEmpty(listAgreement)) { + for (BmAgreementInfo agreementInfo : listAgreement) { + bean.setAgreementId(agreementInfo.getAgreementId()); + List list6 = materialLeaseInfoMapper.getUseTypeTree(bean); + + if (!CollectionUtils.isEmpty(list6)) { + for (TypeTreeNode node : list6) { + if (node.getTypeId() != 0L && node.getNum() != null) { + usedNums.merge(node.getTypeId(), node.getNum(), BigDecimal::add); + } + } + } + } + } + + // 第3步:计算剩余库存 = 总领料数量 - 已在用数量 + for (Long typeId : typeIds) { + BigDecimal totalNum = totalLeaseNums.getOrDefault(typeId, BigDecimal.ZERO); + BigDecimal usedNum = usedNums.getOrDefault(typeId, BigDecimal.ZERO); + BigDecimal storageNum = totalNum.subtract(usedNum); + + // 确保不为负数 + if (storageNum.compareTo(BigDecimal.ZERO) < 0) { + storageNum = BigDecimal.ZERO; + } + + storageNumMap.put(typeId, storageNum); + } + + log.info("库存计算完成 - 总领料:{}项,已在用:{}项,计算库存:{}项", + totalLeaseNums.size(), usedNums.size(), storageNumMap.size()); + } catch (Exception e) { - log.error("预计算库存数据失败: {}", e.getMessage()); - // 发生错误时返回默认值 + log.error("预计算库存失败: {}", e.getMessage(), e); + // 失败时为所有类型设置默认值 for (Long typeId : typeIds) { storageNumMap.put(typeId, BigDecimal.ZERO); }