diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/doctorstation/utils/AdviceUtils.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/doctorstation/utils/AdviceUtils.java index 37b1a3cc9..a9e923666 100755 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/doctorstation/utils/AdviceUtils.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/doctorstation/utils/AdviceUtils.java @@ -109,45 +109,74 @@ public class AdviceUtils { continue; } boolean matched = false; + boolean insufficientInventory = false; + + // 第一步:严格匹配(adviceDefinitionId + adviceTableName + locationId + lotNumber) for (AdviceInventoryDto inventoryDto : adviceInventory) { - // 匹配条件:adviceDefinitionId, adviceTableName, locationId, lotNumber 同时相等 - // 如果选择了具体的批次号,校验库存时需要加上批次号的匹配条件 - // 🔧 Bug #177 修复:添加容错处理,如果 adviceTableName 为空则跳过该项匹配 boolean lotNumberMatch = StringUtils.isEmpty(saveDto.getLotNumber()) || saveDto.getLotNumber().equals(inventoryDto.getLotNumber()); boolean tableNameMatch = StringUtils.isEmpty(saveDto.getAdviceTableName()) || inventoryDto.getItemTable().equals(saveDto.getAdviceTableName()); - // 🔧 Bug #504 修复:退回医嘱可能locationId为空,跳过location匹配 boolean locationMatch = saveDto.getLocationId() == null || inventoryDto.getLocationId().equals(saveDto.getLocationId()); if (inventoryDto.getItemId().equals(saveDto.getAdviceDefinitionId()) && tableNameMatch && locationMatch && lotNumberMatch) { matched = true; - // 检查库存是否充足 BigDecimal minUnitQuantity = saveDto.getMinUnitQuantity(); - // 🔧 Bug Fix: 对于耗材类型,如果没有设置minUnitQuantity,则使用quantity作为默认值 if (minUnitQuantity == null) { if (CommonConstants.TableName.ADM_DEVICE_DEFINITION.equals(inventoryDto.getItemTable())) { - // 耗材只有一个单位,minUnitQuantity等于quantity minUnitQuantity = saveDto.getQuantity(); } else { return saveDto.getAdviceName() + "的小单位数量不能为空"; } } - BigDecimal chineseHerbsDoseQuantity = saveDto.getChineseHerbsDoseQuantity(); // 中药付数 - // 中草药医嘱的情况 + BigDecimal chineseHerbsDoseQuantity = saveDto.getChineseHerbsDoseQuantity(); if (chineseHerbsDoseQuantity != null && chineseHerbsDoseQuantity.compareTo(BigDecimal.ZERO) > 0) { minUnitQuantity = minUnitQuantity.multiply(chineseHerbsDoseQuantity); } if (minUnitQuantity.compareTo(inventoryDto.getQuantity()) > 0) { - return saveDto.getAdviceName() + "在" + inventoryDto.getLocationName() + "库存不足"; + insufficientInventory = true; } break; } } + + // 第二步:如果严格匹配未找到,放宽location条件匹配所有药房库存 + if (!matched && saveDto.getLocationId() != null) { + for (AdviceInventoryDto inventoryDto : adviceInventory) { + boolean lotNumberMatch = StringUtils.isEmpty(saveDto.getLotNumber()) + || saveDto.getLotNumber().equals(inventoryDto.getLotNumber()); + boolean tableNameMatch = StringUtils.isEmpty(saveDto.getAdviceTableName()) + || inventoryDto.getItemTable().equals(saveDto.getAdviceTableName()); + if (inventoryDto.getItemId().equals(saveDto.getAdviceDefinitionId()) + && tableNameMatch && lotNumberMatch) { + matched = true; + BigDecimal minUnitQuantity = saveDto.getMinUnitQuantity(); + if (minUnitQuantity == null) { + if (CommonConstants.TableName.ADM_DEVICE_DEFINITION.equals(inventoryDto.getItemTable())) { + minUnitQuantity = saveDto.getQuantity(); + } else { + return saveDto.getAdviceName() + "的小单位数量不能为空"; + } + } + BigDecimal chineseHerbsDoseQuantity = saveDto.getChineseHerbsDoseQuantity(); + if (chineseHerbsDoseQuantity != null && chineseHerbsDoseQuantity.compareTo(BigDecimal.ZERO) > 0) { + minUnitQuantity = minUnitQuantity.multiply(chineseHerbsDoseQuantity); + } + if (minUnitQuantity.compareTo(inventoryDto.getQuantity()) > 0) { + insufficientInventory = true; + } + break; + } + } + } + // 如果没有匹配到库存 if (!matched) { + if (insufficientInventory) { + return saveDto.getAdviceName() + "库存不足"; + } return saveDto.getAdviceName() + "未匹配到库存信息"; } }