Fix Bug #503: fallback修复

This commit is contained in:
2026-05-27 04:31:24 +08:00
parent e0614b1a6e
commit 15b542acf0

View File

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