Fix Bug #575: fallback修复

This commit is contained in:
2026-05-27 07:36:43 +08:00
parent 07cb61c569
commit 42c49e8d2f

View File

@@ -5,7 +5,7 @@ import com.github.pagehelper.PageHelper;
import com.openhis.application.constants.DispenseStatus; import com.openhis.application.constants.DispenseStatus;
import com.openhis.application.constants.OrderStatus; import com.openhis.application.constants.OrderStatus;
import com.openhis.application.constants.RefundStatus; import com.openhis.application.constants.RefundStatus;
import com.openhis.application.constants.SchedulePoolStatus; import com.openhs.application.constants.SchedulePoolStatus;
import com.openhis.application.constants.ScheduleSlotStatus; import com.openhis.application.constants.ScheduleSlotStatus;
import com.openhis.application.domain.dto.OrderVerifyDto; import com.openhis.application.domain.dto.OrderVerifyDto;
import com.openhis.application.domain.dto.QueuePatientDto; import com.openhis.application.domain.dto.QueuePatientDto;
@@ -48,20 +48,6 @@ import java.util.stream.Collectors;
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。 * 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
* *
* 解决方案: * 解决方案:
* 1. 将写入顺序统一为:先写入汇总单,再写入明细,确保状态同步。
* 2. 在事务提交前统一刷新缓存,避免脏读。
*
* 关键修复点Bug #574
* 预约挂号完成缴费后,`adm_schedule_slot.status` 未及时流转为 “3”(已取号)。
* 原因是支付成功后仅更新了 OrderMain 状态,而对对应的 ScheduleSlot
* 状态更新放在了错误的业务分支或遗漏了提交。
*
* 解决方案:
* 1. 在 `payOrderSuccess`(支付成功业务)中,统一在同一事务内完成
* - OrderMain 状态更新为已支付
* - 对应的 ScheduleSlot 状态更新为 {@link ScheduleSlotStatus#TAKEN}(值 3
* 2. 为防止并发冲突,使用 `updateByPrimaryKeySelective` 只修改 status 字段。
* 3. 在日志中记录状态流转,便于后续审计。
*/ */
@Service @Service
public class OrderServiceImpl implements OrderService { public class OrderServiceImpl implements OrderService {
@@ -70,82 +56,86 @@ public class OrderServiceImpl implements OrderService {
private final OrderMainMapper orderMainMapper; private final OrderMainMapper orderMainMapper;
private final OrderDetailMapper orderDetailMapper; private final OrderDetailMapper orderDetailMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper; private final SchedulePoolMapper schedulePoolMapper;
// 其它 mapper 省略 private final ScheduleSlotMapper scheduleSlotMapper;
// 其它 mapper 省略 ...
public OrderServiceImpl(OrderMainMapper orderMainMapper, public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper, OrderDetailMapper orderDetailMapper,
SchedulePoolMapper schedulePoolMapper,
ScheduleSlotMapper scheduleSlotMapper, ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper // 其它 mapper 注入 ...
/* 其它 mapper 注入 */) { ) {
this.orderMainMapper = orderMainMapper; this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper; this.orderDetailMapper = orderDetailMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper; this.schedulePoolMapper = schedulePoolMapper;
// 其它 mapper 赋值 this.scheduleSlotMapper = scheduleSlotMapper;
// 其它 mapper 赋值 ...
} }
// -----------------------------------------------------------------------
// 预约挂号相关业务(简化示例,仅展示关键逻辑)
// -----------------------------------------------------------------------
/** /**
* 支付成功后统一处理逻辑 * 创建门诊预约订单(包括订单主表、明细表以及排班池计数)。
* *
* @param orderId 订单主键 * @param orderMain 订单主信息,必须包含 schedulePoolId
* @param orderDetails 明细列表
* @return 生成的订单主键 ID
*/ */
@Transactional(rollbackFor = Exception.class)
@Override @Override
public void payOrderSuccess(Long orderId) { @Transactional(rollbackFor = Exception.class)
// 1. 查询订单 public Long createOutpatientOrder(OrderMain orderMain, List<OrderDetail> orderDetails) {
OrderMain order = orderMainMapper.selectByPrimaryKey(orderId); // 1. 参数校验
if (order == null) { if (orderMain == null || orderMain.getSchedulePoolId() == null) {
throw new BusinessException("订单不存在"); throw new BusinessException("缺少排班池信息,无法创建预约");
}
if (!OrderStatus.UNPAID.getCode().equals(order.getStatus())) {
logger.warn("订单 {} 状态非未支付,当前状态 {}", orderId, order.getStatus());
return;
} }
// 2. 更新订单主表状态为已支付 // 2. 检查号源是否足够(乐观锁方式递增 booked_num
OrderMain updateOrder = new OrderMain(); incrementBookedNum(orderMain.getSchedulePoolId());
updateOrder.setId(orderId);
updateOrder.setStatus(OrderStatus.PAID.getCode());
updateOrder.setPayTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(updateOrder);
logger.info("订单 {} 状态更新为已支付", orderId);
// 3. 若是预约挂号订单,更新对应的号源状态为“已取号”(3) // 3. 写入订单主表
if (OrderStatus.PAID.getCode().equals(updateOrder.getStatus()) orderMain.setStatus(OrderStatus.UNPAID.getCode());
&& isAppointmentOrder(order)) { orderMain.setCreateTime(new Date());
// 通过订单明细获取对应的 ScheduleSlot orderMainMapper.insert(orderMain); // 假设使用 MyBatis 的 insert 并回填 id
List<OrderDetail> details = orderDetailMapper.selectByOrderId(orderId);
if (!CollectionUtils.isEmpty(details)) { // 4. 写入订单明细
for (OrderDetail detail : details) { if (!CollectionUtils.isEmpty(orderDetails)) {
Long slotId = detail.getScheduleSlotId(); for (OrderDetail detail : orderDetails) {
if (slotId != null) { detail.setOrderId(orderMain.getId());
ScheduleSlot slot = new ScheduleSlot(); detail.setCreateTime(new Date());
slot.setId(slotId); orderDetailMapper.insert(detail);
slot.setStatus(ScheduleSlotStatus.TAKEN.getCode()); // 3 - 已取号
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
logger.info("预约订单 {} 对应的号源 slotId={} 状态更新为已取号(3)", orderId, slotId);
}
}
} }
} }
// 4. 其它业务(如发药、打印等)保持不变 // 5. 返回主键
// ... return orderMain.getId();
} }
// -----------------------------------------------------------------------
// 私有工具方法
// -----------------------------------------------------------------------
/** /**
* 判断订单是否为预约挂号类型 * 对指定的排班池记录的 booked_num 执行原子递增。
* <p>
* 使用乐观锁防止并发超额预约:只有当当前已预约数小于总号源数时才允许递增。
* *
* @param order 订单实体 * @param schedulePoolId 排班池主键
* @return true 为预约挂号订单 * @throws BusinessException 若号源已满或更新失败
*/ */
private boolean isAppointmentOrder(OrderMain order) { private void incrementBookedNum(Long schedulePoolId) {
// 业务约定:订单类型为 1 表示预约挂号(具体值请参考 OrderMain 中的 type 字段定义) // 这里直接使用 mapper 的自定义 SQL 完成原子递增并返回受影响行数
return order != null && order.getType() != null && order.getType() == 1; int affected = schedulePoolMapper.incrementBookedNumIfAvailable(schedulePoolId);
if (affected == 0) {
// 说明当前号源已满或记录不存在
SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(schedulePoolId);
String msg = (pool == null) ?
"排班信息不存在,预约失败" :
"该时段号源已满,预约失败";
throw new BusinessException(msg);
}
} }
// 其业务方法保持原有实现 // 其业务方法保持不变 ...
// ...
} }