diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inhospitalnursestation/appservice/impl/AdviceProcessAppServiceImpl.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inhospitalnursestation/appservice/impl/AdviceProcessAppServiceImpl.java index d146e5b2..95061f5f 100755 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inhospitalnursestation/appservice/impl/AdviceProcessAppServiceImpl.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inhospitalnursestation/appservice/impl/AdviceProcessAppServiceImpl.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.springframework.transaction.annotation.Transactional; import com.core.common.core.domain.R; import com.core.common.enums.TenantOptionDict; import com.core.common.exception.ServiceException; @@ -377,6 +378,7 @@ public class AdviceProcessAppServiceImpl implements IAdviceProcessAppService { * @return 操作结果 */ @Override + @Transactional(rollbackFor = Exception.class) public R adviceExecute(AdviceExecuteParam adviceExecuteParam) { // 实际执行时间 Date exeDate = adviceExecuteParam.getExeDate(); @@ -804,12 +806,6 @@ public class AdviceProcessAppServiceImpl implements IAdviceProcessAppService { chargeItemService.saveOrUpdate(chargeItem); } else { // 批次售卖情况 - // 需要的药品数量(小单位) - BigDecimal minUnitQuantity = medicationRequestUseExe.getMinUnitQuantity(); - if (minUnitQuantity == null || minUnitQuantity.compareTo(BigDecimal.ZERO) <= 0) { - throw new RuntimeException("药品执行数量异常,medicationId: " - + finalLongMedicationRequest.getMedicationId()); - } // 库存集合 List inventoryList = advice.getInventoryList(); if (inventoryList == null || inventoryList.isEmpty()) { @@ -822,9 +818,23 @@ public class AdviceProcessAppServiceImpl implements IAdviceProcessAppService { throw new RuntimeException("未找到药品匹配的定价信息: " + finalLongMedicationRequest.getMedicationId()); } + // 需要的药品数量(小单位) + BigDecimal minUnitQuantity = medicationRequestUseExe.getMinUnitQuantity(); + if (minUnitQuantity == null || minUnitQuantity.compareTo(BigDecimal.ZERO) <= 0) { + throw new RuntimeException("药品执行数量异常,medicationId: " + + finalLongMedicationRequest.getMedicationId()); + } + // 【修复】预先计算总可用库存,确保在写入任何数据前进行校验 + BigDecimal totalAvailableQuantity = inventoryList.stream() + .map(AdviceInventoryDto::getQuantity) + .reduce(BigDecimal.ZERO, BigDecimal::add); + if (totalAvailableQuantity.compareTo(minUnitQuantity) < 0) { + throw new RuntimeException("药品库存不足,medicationId: " + + finalLongMedicationRequest.getMedicationId()); + } // 剩余需要分配的数量 BigDecimal remainingQuantity = minUnitQuantity; - + // 按批次循环分配数量 for (AdviceInventoryDto inventory : inventoryList) { if (remainingQuantity.compareTo(BigDecimal.ZERO) <= 0) { @@ -927,11 +937,6 @@ public class AdviceProcessAppServiceImpl implements IAdviceProcessAppService { chargeItem.setTotalPrice(totalPrice); // 总价 chargeItemService.saveOrUpdate(chargeItem); - - } - if (remainingQuantity.compareTo(BigDecimal.ZERO) > 0) { - throw new RuntimeException("药品库存不足,medicationId: " - + finalLongMedicationRequest.getMedicationId()); } } }