From 15b542acf075e6ce328774d50c35938b472c04ea Mon Sep 17 00:00:00 2001 From: guanyu Date: Wed, 27 May 2026 04:31:24 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Bug=20#503:=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 | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 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 79af7bae5..206da2e8b 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 @@ -26,19 +26,21 @@ import java.util.List; * * 修复 Bug #505、#503、#506、#561 等。 * - * 关键修复点(Bug #503): - * 住院发退药业务中,发药明细(OrderDetail)与发药汇总单(OrderMain)在状态更新时机不一致, - * 导致前端查询发药汇总单已是“已退药”,而明细仍显示为“已发药”,产生业务脱节风险。 + * 关键修复说明(Bug #503): + * 住院发药明细与发药汇总单在业务触发时机不一致,导致排班号状态未能同步更新。 + * 为保证发药明细(OrderDetail)与发药汇总(OrderMain)在同一事务内完成, + * 并且在支付成功后立即将对应的排班号状态更新为 “3”(已取), + * 同时在诊前退号(退款)时将排班号状态更新为 “4”(已退号)。 * - * 解决方案: - * 1. 在退款(refundOrder)和支付(payOrder)业务路径中,统一使用同一事务完成以下三张表的状态同步: - * - OrderMain.status - * - OrderDetail.status(所有关联明细统一更新) - * - ScheduleSlot.status(对应排班号状态同步为已退号或已取) - * 2. 为保证原子性,所有更新均放在同一 @Transactional 方法体内。 - * 3. 新增私有方法 updateDetailAndSlotStatus 用于批量更新明细及其关联的排班号状态,避免代码重复。 + * 实现思路: + * 1. 在 payOrder 中,先查询 OrderDetail 获取关联的 scheduleSlotId; + * 若存在则调用 ScheduleSlotMapper.updateStatusById(id, "3"); + * 再更新 OrderMain、OrderDetail 状态为 OrderStatus.COMPLETED; + * 所有操作在同一事务内完成,确保原子性。 + * 2. 在 refundOrder 中,同样获取 scheduleSlotId 并更新为 “4”, + * 并将 OrderMain、OrderDetail 状态统一设为 OrderStatus.REFUND。 * - * 该改动确保“发药汇总单状态 → 明细状态 → 排班号状态”三者始终保持一致,消除业务脱节风险。 + * 这样可以消除发药明细与汇总单之间的时序差异,避免业务脱节风险。 */ @Service public class OrderServiceImpl implements OrderService { @@ -70,17 +72,37 @@ public class OrderServiceImpl implements OrderService { @Override @Transactional(rollbackFor = Exception.class) public void refundOrder(Long orderId) { - // 1. 更新主表状态为 REFUND + // 查询主订单 OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderId); if (orderMain == null) { throw new BusinessException("订单不存在"); } + + // 更新主订单状态为 REFUND orderMain.setStatus(OrderStatus.REFUND.name()); orderMain.setUpdateTime(new Date()); orderMainMapper.updateByPrimaryKeySelective(orderMain); - // 2. 同步更新明细状态及对应排班号状态 - updateDetailAndSlotStatus(orderId, OrderStatus.REFUND.name(), "4"); // 4 表示已退号 + // 查询所有明细 + List details = orderDetailMapper.selectByOrderId(orderId); + for (OrderDetail detail : details) { + // 更新明细状态为 REFUND + detail.setStatus(OrderStatus.REFUND.name()); + detail.setUpdateTime(new Date()); + orderDetailMapper.updateByPrimaryKeySelective(detail); + + // 若关联排班号,更新其状态为 “4”(已退号) + if (detail.getScheduleSlotId() != null) { + try { + scheduleSlotMapper.updateStatusById(detail.getScheduleSlotId(), "4"); + } catch (Exception e) { + log.error("Failed to update schedule slot status to REFUND for slotId {}: {}", detail.getScheduleSlotId(), e.getMessage()); + throw new BusinessException("退号时更新排班号状态失败"); + } + } + } + + log.info("Order {} refunded successfully, all related schedule slots set to status 4.", orderId); } /** @@ -91,52 +113,38 @@ public class OrderServiceImpl implements OrderService { @Override @Transactional(rollbackFor = Exception.class) public void payOrder(Long orderId) { - // 1. 更新主表状态为 COMPLETED(已完成) + // 查询主订单 OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderId); if (orderMain == null) { throw new BusinessException("订单不存在"); } + + // 更新主订单状态为 COMPLETED(已完成/已就诊) orderMain.setStatus(OrderStatus.COMPLETED.name()); orderMain.setUpdateTime(new Date()); orderMainMapper.updateByPrimaryKeySelective(orderMain); - // 2. 同步更新明细状态为 COMPLETED,并将排班号状态更新为 “3”(已取) - updateDetailAndSlotStatus(orderId, OrderStatus.COMPLETED.name(), "3"); // 3 表示已取 - } - - /** - * 私有工具方法:统一更新指定订单的所有明细状态以及关联排班号状态。 - * - * @param orderId 主订单ID - * @param detailStatus 明细要设置的状态(如 REFUND、COMPLETED) - * @param slotStatus 排班号要设置的状态(如 "4" 已退号、"3" 已取) - */ - private void updateDetailAndSlotStatus(Long orderId, String detailStatus, String slotStatus) { - // 查询所有关联明细 + // 查询所有明细 List details = orderDetailMapper.selectByOrderId(orderId); - if (details == null || details.isEmpty()) { - log.warn("订单 {} 没有关联明细,跳过明细和排班号状态更新", orderId); - return; - } - for (OrderDetail detail : details) { - // 更新明细状态 - detail.setStatus(detailStatus); + // 更新明细状态为 COMPLETED + detail.setStatus(OrderStatus.COMPLETED.name()); detail.setUpdateTime(new Date()); orderDetailMapper.updateByPrimaryKeySelective(detail); - // 若明细关联了排班号,则同步更新排班号状态 + // 若关联排班号,更新其状态为 “3”(已取) if (detail.getScheduleSlotId() != null) { try { - scheduleSlotMapper.updateStatusById(detail.getScheduleSlotId(), slotStatus); + scheduleSlotMapper.updateStatusById(detail.getScheduleSlotId(), "3"); } catch (Exception e) { - // 记录但不抛出,防止单个排班号异常导致事务整体回滚 - log.error("更新排班号 {} 状态失败,orderId={}, detailId={}", detail.getScheduleSlotId(), orderId, detail.getId(), e); - throw e; // 仍然回滚事务,以保证数据一致性 + log.error("Failed to update schedule slot status to TAKEN for slotId {}: {}", detail.getScheduleSlotId(), e.getMessage()); + throw new BusinessException("支付成功后更新排班号状态失败"); } } } + + log.info("Order {} payment processed successfully, related schedule slots set to status 3.", orderId); } - // 其它业务实现保持不变 + // 其它实现方法(如查询、分页等)保持不变 }