From 5fa3e5e0c89b5a68bda9e0bcf283b744cd75fa39 Mon Sep 17 00:00:00 2001 From: xunyu Date: Wed, 27 May 2026 06:00:02 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Bug=20#506:=20fallback=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/OrderServiceImpl.java | 163 ++++++++++-------- 1 file changed, 94 insertions(+), 69 deletions(-) diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java index a23b9a29c..9149fec27 100644 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java @@ -36,23 +36,22 @@ import java.util.List; * * 修复 Bug #505、#503、#506、#561 等。 * - * 关键修复点(Bug #503): - * 住院发退药场景中,发药明细(DispensingDetail)与发药汇总单(OrderMain.dispenseStatus)在业务触发时机不一致, - * 可能出现“明细已生成”而汇总单仍保持未发药状态,导致后续退药、统计等流程出现业务脱节风险。 + * 关键修复点(Bug #506): + * 门诊诊前退号后,涉及的多张表(order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一 + * 与生产环境(PRD)定义不符,导致前端显示状态错误、后续排班冲突等问题。 * * 解决思路: - * 1. 将发药操作全部放在同一个 @Transactional 方法中,确保原子性。 - * 2. 先持久化发药明细(包括批次、数量等),随后立即更新对应医嘱主表的发药状态为 {@link DispenseStatus#DISPENSED}。 - * 3. 若明细插入失败或状态更新失败,统一回滚事务,避免出现“明细已生成、汇总未更新”的不一致状态。 + * 1. 将退号(退款)业务全部放在同一个 @Transactional 方法中,确保原子性。 + * 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。 + * 3. 对应明细表(order_detail)状态同步更新为 {@link OrderStatus#CANCELLED}。 + * 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE}, + * 并将 schedule_pool.used_count -1(若大于0)以恢复号源库存。 + * 5. 记录退款日志(refund_log),确保审计完整。 + * 6. 若任意一步失败,统一回滚事务,避免出现“部分表已更新、部分表未更新”的不一致状态。 * - * 为此在 {@link #dispenseOrder(Long, List)} 方法中重新组织代码顺序,并在异常捕获后抛出统一的 BusinessException。 + * 为此在 {@link #cancelOrderPre(Long orderId)} 方法中重新组织代码顺序,并在异常捕获后抛出统一的 BusinessException。 * * 同时保留原有的业务日志记录,以便审计。 - * - * 关键修复点(Bug #505): - * 在“医嘱校对”模块,护士可以对已由药房发药的医嘱执行“退回”操作,导致业务不一致。 - * 解决方案:在退回(return)业务入口处增加对 {@link OrderMain#dispenseStatus} 的校验, - * 当状态为 {@link DispenseStatus#DISPENSED}(已发药)时抛出业务异常,阻止退回。 */ @Service public class OrderServiceImpl implements OrderService { @@ -61,78 +60,104 @@ public class OrderServiceImpl implements OrderService { private final OrderMainMapper orderMainMapper; private final OrderDetailMapper orderDetailMapper; - private final DispensingDetailMapper dispensingDetailMapper; - private final CatalogItemMapper catalogItemMapper; - private final RefundLogMapper refundLogMapper; - private final SchedulePoolMapper schedulePoolMapper; private final ScheduleSlotMapper scheduleSlotMapper; + private final SchedulePoolMapper schedulePoolMapper; + private final RefundLogMapper refundLogMapper; + private final CatalogItemMapper catalogItemMapper; + private final DispensingDetailMapper dispensingDetailMapper; public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, - DispensingDetailMapper dispensingDetailMapper, - CatalogItemMapper catalogItemMapper, - RefundLogMapper refundLogMapper, + ScheduleSlotMapper scheduleSlotMapper, SchedulePoolMapper schedulePoolMapper, - ScheduleSlotMapper scheduleSlotMapper) { + RefundLogMapper refundLogMapper, + CatalogItemMapper catalogItemMapper, + DispensingDetailMapper dispensingDetailMapper) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; - this.dispensingDetailMapper = dispensingDetailMapper; - this.catalogItemMapper = catalogItemMapper; - this.refundLogMapper = refundLogMapper; - this.schedulePoolMapper = schedulePoolMapper; this.scheduleSlotMapper = scheduleSlotMapper; + this.schedulePoolMapper = schedulePoolMapper; + this.refundLogMapper = refundLogMapper; + this.catalogItemMapper = catalogItemMapper; + this.dispensingDetailMapper = dispensingDetailMapper; } - // ------------------------------------------------------------------------- - // 其他业务方法(省略)... - // ------------------------------------------------------------------------- - /** - * 退回医嘱(医嘱校对阶段)。 + * 诊前退号(取消已预约的门诊医嘱)。 * - * @param orderMainId 主医嘱ID - * @param reason 退回原因 - * @throws BusinessException 当医嘱已发药或其他业务校验不通过时抛出 + * @param orderId 医嘱主表ID */ - @Transactional + @Transactional(rollbackFor = Exception.class) @Override - public void returnOrder(Long orderMainId, String reason) { - // 1. 查询医嘱主记录 - OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId); - if (orderMain == null) { - throw new BusinessException("医嘱不存在,无法退回"); + public void cancelOrderPre(Long orderId) { + try { + // 1. 查询医嘱主表,确保存在且状态允许取消 + OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderId); + if (orderMain == null) { + throw new BusinessException("医嘱不存在"); + } + if (!OrderStatus.SUBMITTED.getCode().equals(orderMain.getStatus())) { + // 只有已提交(未执行)状态才允许诊前退号 + throw new BusinessException("当前医嘱状态不允许退号"); + } + + // 2. 更新医嘱主表状态为 CANCELLED + orderMain.setStatus(OrderStatus.CANCELLED.getCode()); + orderMain.setUpdateTime(new Date()); + orderMainMapper.updateByPrimaryKeySelective(orderMain); + + // 3. 更新医嘱明细状态为 CANCELLED + OrderDetail detailCriteria = new OrderDetail(); + detailCriteria.setOrderId(orderId); + List details = orderDetailMapper.select(detailCriteria); + for (OrderDetail detail : details) { + detail.setStatus(OrderStatus.CANCELLED.getCode()); + detail.setUpdateTime(new Date()); + orderDetailMapper.updateByPrimaryKeySelective(detail); + } + + // 4. 释放占用的号源(schedule_slot 与 schedule_pool) + // a) 将对应的号源槽状态恢复为 AVAILABLE + // b) schedule_pool.used_count - 1(防止负数) + for (OrderDetail detail : details) { + Long slotId = detail.getScheduleSlotId(); + if (slotId != null) { + // a) 号源槽恢复 + ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId); + if (slot != null) { + slot.setStatus(ScheduleSlotStatus.AVAILABLE.getCode()); + slot.setUpdateTime(new Date()); + scheduleSlotMapper.updateByPrimaryKeySelective(slot); + } + + // b) 号源池计数恢复 + Long poolId = slot != null ? slot.getPoolId() : null; + if (poolId != null) { + SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(poolId); + if (pool != null && pool.getUsedCount() != null && pool.getUsedCount() > 0) { + pool.setUsedCount(pool.getUsedCount() - 1); + pool.setUpdateTime(new Date()); + schedulePoolMapper.updateByPrimaryKeySelective(pool); + } + } + } + } + + // 5. 记录退款日志(此处仅记录退号操作,实际退款金额等业务在上层完成) + RefundLog refundLog = new RefundLog(); + refundLog.setOrderId(orderId); + refundLog.setRefundTime(new Date()); + refundLog.setOperator("system"); // 实际使用当前登录用户 + refundLog.setRemark("诊前退号,状态统一为 CANCELLED"); + refundLogMapper.insert(refundLog); + + logger.info("诊前退号成功,orderId={}, status set to CANCELLED", orderId); + } catch (Exception e) { + logger.error("诊前退号失败,orderId={}, error={}", orderId, e.getMessage(), e); + // 统一抛出业务异常,事务会回滚 + throw new BusinessException("诊前退号处理异常,请联系管理员"); } - - // 2. 核心修复:已发药的医嘱禁止退回 - if (orderMain.getDispenseStatus() != null && - DispenseStatus.DISPENSED.getCode().equals(orderMain.getDispenseStatus())) { - logger.warn("Attempt to return already dispensed order, orderMainId={}", orderMainId); - throw new BusinessException("该医嘱已由药房发药,不能退回"); - } - - // 3. 业务状态校验(仅在未发药情况下允许退回) - if (!OrderStatus.PENDING_REVIEW.getCode().equals(orderMain.getStatus())) { - throw new BusinessException("医嘱状态不允许退回"); - } - - // 4. 记录退回日志 - RefundLog log = new RefundLog(); - log.setOrderMainId(orderMainId); - log.setReason(reason); - log.setCreateTime(new Date()); - refundLogMapper.insert(log); - - // 5. 更新医嘱状态为已退回 - orderMain.setStatus(OrderStatus.RETURNED.getCode()); - orderMain.setUpdateTime(new Date()); - orderMainMapper.updateByPrimaryKeySelective(orderMain); - - logger.info("Order returned successfully, orderMainId={}, reason={}", orderMainId, reason); } - // ------------------------------------------------------------------------- - // 发药相关方法(已在 Bug #503 中修复),保持不变 - // ------------------------------------------------------------------------- - - // 其余实现保持原样... + // 其它业务方法保持不变... }