修正领料库存逻辑

This commit is contained in:
syruan 2025-07-28 09:51:48 +08:00
parent 21bee91a10
commit dc39193624
1 changed files with 57 additions and 17 deletions

View File

@ -1839,8 +1839,8 @@ public class MaterialLeaseInfoServiceImpl implements MaterialLeaseInfoService {
}
/**
* 预计算库存数据替代复杂的类型树计算
* 这个方法将原来每个detail都要执行的复杂计算优化为一次性预计算
* 预计算库存数据重现原始的类型树计算逻辑
* 业务逻辑总领料数量 - 已在用数量 = 剩余可用库存
*/
private Map<Long, BigDecimal> preCalculateStorageNums(MaterialLeaseApplyInfo info,
List<AgreementVo> agreementList,
@ -1848,27 +1848,67 @@ public class MaterialLeaseInfoServiceImpl implements MaterialLeaseInfoService {
Map<Long, BigDecimal> storageNumMap = new HashMap<>();
try {
// 简化的库存计算直接查询ma_type表的storage_num字段
log.info("开始预计算库存,原始类型树逻辑,处理{}个类型", typeIds.size());
// 第1步从协议中获取总领料数量原listL5逻辑
Map<Long, BigDecimal> totalLeaseNums = new HashMap<>();
if (!CollectionUtils.isEmpty(agreementList)) {
for (AgreementVo agreementVo : agreementList) {
BackApplyInfo backApplyInfo = new BackApplyInfo();
backApplyInfo.setAgreementId(Long.valueOf(agreementVo.getAgreementId()));
List<TypeTreeNode> 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);
}
}
}
}
}
// 第2步通过工程获取已在用数量原list7逻辑
Map<Long, BigDecimal> usedNums = new HashMap<>();
MaterialLeaseApplyInfo bean = new MaterialLeaseApplyInfo();
bean.setProId(info.getProId());
List<BmAgreementInfo> listAgreement = materialLeaseInfoMapper.getAgreementIdByProId(bean);
if (!CollectionUtils.isEmpty(listAgreement)) {
for (BmAgreementInfo agreementInfo : listAgreement) {
bean.setAgreementId(agreementInfo.getAgreementId());
List<TypeTreeNode> 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) {
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);
}
} catch (Exception e) {
log.warn("查询typeId {}的库存失败: {}", typeId, e.getMessage());
storageNumMap.put(typeId, BigDecimal.ZERO);
}
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;
}
log.info("预计算库存完成,为{}个类型查询了库存值", typeIds.size());
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);
}