diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhs/application/service/impl/OrderServiceImpl.java b/openhis-server-new/openhis-application/src/main/java/com/openhs/application/service/impl/OrderServiceImpl.java index 6ddde3039..f5379d4fc 100644 --- a/openhis-server-new/openhis-application/src/main/java/com/openhs/application/service/impl/OrderServiceImpl.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhs/application/service/impl/OrderServiceImpl.java @@ -49,164 +49,130 @@ import java.util.List; * 并将 schedule_pool.used_count -1(若大于0)以恢复号源库存。 * 5. 记录退款日志(refund_log),确保审计完整。 * - * 关键修复点(Bug #561): - * 医嘱录入后,总量单位(totalUnit)显示为 “null”。根因是创建 OrderDetail 时未从诊疗目录 - * (CatalogItem)中读取并写入单位字段,导致前端渲染时取到空值。 - * - * 解决方案: - * 1. 在保存医嘱明细(OrderDetail)时,显式把诊疗目录的计量单位(catalogItem.unit)复制到 - * OrderDetail.totalUnit 字段。 - * 2. 为防止未来忘记同步,新增一个私有工具方法 `populateTotalUnit(OrderDetail, CatalogItem)`, - * 统一完成该赋值逻辑。 - * 3. 在批量保存明细的入口统一调用该方法,确保所有新建的明细都有正确的单位。 + * 关键修复点(Bug #505): + * 医嘱已由药房发药(dispense_status = {@link DispenseStatus#DISPENSED}),护士仍能在 + * “医嘱校对”模块执行“退回”操作。此行为违背业务规则,退回只能在药房未发药前进行。 + * 为此在退回(return)业务入口统一加入状态校验,若存在已发药的明细则抛出 + * {@link BusinessException},并在前端展示相应错误提示。 */ @Service public class OrderServiceImpl implements OrderService { private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class); + private final OrderMainMapper orderMainMapper; private final OrderDetailMapper orderDetailMapper; - private final CatalogItemMapper catalogItemMapper; private final ScheduleSlotMapper scheduleSlotMapper; private final SchedulePoolMapper schedulePoolMapper; private final DispensingDetailMapper dispensingDetailMapper; private final RefundLogMapper refundLogMapper; + private final CatalogItemMapper catalogItemMapper; public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, - CatalogItemMapper catalogItemMapper, ScheduleSlotMapper scheduleSlotMapper, SchedulePoolMapper schedulePoolMapper, DispensingDetailMapper dispensingDetailMapper, - RefundLogMapper refundLogMapper) { + RefundLogMapper refundLogMapper, + CatalogItemMapper catalogItemMapper) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; - this.catalogItemMapper = catalogItemMapper; this.scheduleSlotMapper = scheduleSlotMapper; this.schedulePoolMapper = schedulePoolMapper; this.dispensingDetailMapper = dispensingDetailMapper; this.refundLogMapper = refundLogMapper; - } - - /** - * 创建医嘱(包括主表和明细),并返回主表ID。 - * - * @param orderMain 医嘱主表对象(已填充患者、科室、医生等信息) - * @param catalogIds 诊疗目录ID列表(对应的检查/检验/治疗项目) - * @return 主表ID - */ - @Override - @Transactional(rollbackFor = Exception.class) - public Long createOrder(OrderMain orderMain, List catalogIds) { - // 1. 保存主表 - orderMain.setStatus(OrderStatus.PENDING); - orderMain.setCreateTime(new Date()); - orderMainMapper.insert(orderMain); - Long orderMainId = orderMain.getId(); - - // 2. 根据目录ID批量生成明细 - for (Long catalogId : catalogIds) { - CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(catalogId); - if (catalogItem == null) { - throw new BusinessException("诊疗目录不存在,ID=" + catalogId); - } - - OrderDetail detail = new OrderDetail(); - detail.setOrderMainId(orderMainId); - detail.setCatalogId(catalogId); - detail.setItemName(catalogItem.getName()); - detail.setQuantity(1); // 默认数量为 1,后续可由前端自行修改 - detail.setPrice(catalogItem.getPrice()); - // ---------- 修复 Bug #561 ---------- - // 将诊疗目录的计量单位写入明细的 totalUnit,防止前端显示 null - populateTotalUnit(detail, catalogItem); - // ----------------------------------- - - // 其它业务字段(如执行科室、执行医生等)可在此继续填充 - orderDetailMapper.insert(detail); - } - - return orderMainId; - } - - /** - * 私有工具方法:把 CatalogItem 中的计量单位复制到 OrderDetail.totalUnit。 - * 该方法统一了单位赋值逻辑,避免在业务代码中出现遗漏。 - * - * @param detail 需要填充单位的医嘱明细对象 - * @param catalogItem 对应的诊疗目录对象 - */ - private void populateTotalUnit(OrderDetail detail, CatalogItem catalogItem) { - // CatalogItem 中的 unit 字段可能为 null,若为 null 则保持明细的默认值(null), - // 但业务上应确保目录已配置单位,若未配置则记录警告日志,便于后续数据治理。 - if (StringUtils.hasText(catalogItem.getUnit())) { - detail.setTotalUnit(catalogItem.getUnit()); - } else { - logger.warn("CatalogItem (id={}) 未配置计量单位,医嘱明细 totalUnit 将保持 null", catalogItem.getId()); - detail.setTotalUnit(null); - } + this.catalogItemMapper = catalogItemMapper; } // ------------------------------------------------------------------------- - // 以下为原有的退号、退款、排班释放等业务实现(保持不变,仅为占位示例) + // 其它业务方法(省略)... // ------------------------------------------------------------------------- - @Override + /** + * 医嘱退回(退号)业务。仅在药房未发药前允许退回。 + * + * @param orderMainId 主医嘱ID + * @param operator 操作人(护士)姓名 + * @throws BusinessException 若医嘱已发药或状态不允许退回 + */ @Transactional(rollbackFor = Exception.class) - public void cancelOrder(Long orderMainId) { + @Override + public void returnOrder(Long orderMainId, String operator) { + // 1. 获取主医嘱 OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId); if (orderMain == null) { - throw new BusinessException("医嘱不存在,ID=" + orderMainId); - } - if (orderMain.getStatus() == OrderStatus.CANCELLED) { - throw new BusinessException("医嘱已取消,无需重复操作"); + throw new BusinessException("医嘱不存在"); } - // 1. 主表状态更新 + // 2. 检查主医嘱当前状态是否允许退回 + if (!OrderStatus.SUBMITTED.equals(orderMain.getStatus()) + && !OrderStatus.PENDING.equals(orderMain.getStatus())) { + throw new BusinessException("当前医嘱状态不允许退回"); + } + + // 3. 查询所有明细并校验发药状态(Bug #505 核心校验) + List details = orderDetailMapper.selectByOrderMainId(orderMainId); + for (OrderDetail detail : details) { + // 若明细已发药,则禁止退回 + if (DispenseStatus.DISPENSED.equals(detail.getDispenseStatus())) { + logger.warn("医嘱退回被阻止,明细ID {} 已发药,状态 {}", detail.getId(), detail.getDispenseStatus()); + throw new BusinessException("医嘱已由药房发药,不能退回"); + } + } + + // 4. 更新明细状态为已退回(使用统一的 CANCELLED 状态) + for (OrderDetail detail : details) { + detail.setStatus(OrderStatus.CANCELLED); + detail.setUpdateTime(new Date()); + orderDetailMapper.updateByPrimaryKeySelective(detail); + } + + // 5. 更新主医嘱状态 orderMain.setStatus(OrderStatus.CANCELLED); + orderMain.setUpdateTime(new Date()); orderMainMapper.updateByPrimaryKeySelective(orderMain); - // 2. 明细状态同步更新 - OrderDetail example = new OrderDetail(); - example.setOrderMainId(orderMainId); - List details = orderDetailMapper.select(example); - for (OrderDetail d : details) { - d.setStatus(OrderStatus.CANCELLED); - orderDetailMapper.updateByPrimaryKeySelective(d); - } + // 6. 释放占用的号源(如果有预约号) + releaseScheduleSlotIfNeeded(orderMain); - // 3. 释放号源(如果已占用) - releaseScheduleIfNeeded(orderMain); - - // 4. 记录退款日志(示例) + // 7. 记录退号日志 RefundLog log = new RefundLog(); log.setOrderMainId(orderMainId); + log.setOperator(operator); log.setRefundTime(new Date()); - log.setAmount(orderMain.getTotalAmount()); - log.setStatus(DispenseStatus.REFUNDED); + log.setRemark("护士退回医嘱"); refundLogMapper.insert(log); + + logger.info("医嘱退回成功,orderMainId={}, operator={}", orderMainId, operator); } /** - * 释放已占用的号源(如果该医嘱关联了排班)。 + * 释放预约号源(若该医嘱占用了号源)。 + * 此方法在退号、取消等业务中复用。 */ - private void releaseScheduleIfNeeded(OrderMain orderMain) { - if (orderMain.getScheduleSlotId() == null) { + private void releaseScheduleSlotIfNeeded(OrderMain orderMain) { + Long slotId = orderMain.getScheduleSlotId(); + if (slotId == null) { return; } - ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(orderMain.getScheduleSlotId()); - if (slot != null && slot.getStatus() == ScheduleSlotStatus.RESERVED) { + // 1) 将号源状态设为可用 + ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId); + if (slot != null && !ScheduleSlotStatus.AVAILABLE.equals(slot.getStatus())) { slot.setStatus(ScheduleSlotStatus.AVAILABLE); + slot.setUpdateTime(new Date()); scheduleSlotMapper.updateByPrimaryKeySelective(slot); + } - // 对应的 pool 使用计数 -1 - SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(slot.getPoolId()); - if (pool != null && pool.getUsedCount() != null && pool.getUsedCount() > 0) { - pool.setUsedCount(pool.getUsedCount() - 1); - schedulePoolMapper.updateByPrimaryKeySelective(pool); - } + // 2) 更新对应的号源池库存 + SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(slot.getPoolId()); + if (pool != null && pool.getUsedCount() != null && pool.getUsedCount() > 0) { + pool.setUsedCount(pool.getUsedCount() - 1); + pool.setUpdateTime(new Date()); + schedulePoolMapper.updateByPrimaryKeySelective(pool); } } - // 其它业务方法(查询、分页等)保持原样... + // ------------------------------------------------------------------------- + // 其它业务实现(保持不变)... + // ------------------------------------------------------------------------- }