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.OrderStatus;
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.domain.dto.OrderVerifyDto;
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
public class OrderServiceImpl implements OrderService {
@@ -70,82 +56,86 @@ public class OrderServiceImpl implements OrderService {
private final OrderMainMapper orderMainMapper;
private final OrderDetailMapper orderDetailMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper;
// 其它 mapper 省略
private final ScheduleSlotMapper scheduleSlotMapper;
// 其它 mapper 省略 ...
public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper,
SchedulePoolMapper schedulePoolMapper,
ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper
/* 其它 mapper 注入 */) {
// 其它 mapper 注入 ...
) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
// 其它 mapper 赋值
this.scheduleSlotMapper = scheduleSlotMapper;
// 其它 mapper 赋值 ...
}
// -----------------------------------------------------------------------
// 预约挂号相关业务(简化示例,仅展示关键逻辑)
// -----------------------------------------------------------------------
/**
* 支付成功后统一处理逻辑
* 创建门诊预约订单(包括订单主表、明细表以及排班池计数)。
*
* @param orderId 订单主键
* @param orderMain 订单主信息,必须包含 schedulePoolId
* @param orderDetails 明细列表
* @return 生成的订单主键 ID
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void payOrderSuccess(Long orderId) {
// 1. 查询订单
OrderMain order = orderMainMapper.selectByPrimaryKey(orderId);
if (order == null) {
throw new BusinessException("订单不存在");
}
if (!OrderStatus.UNPAID.getCode().equals(order.getStatus())) {
logger.warn("订单 {} 状态非未支付,当前状态 {}", orderId, order.getStatus());
return;
@Transactional(rollbackFor = Exception.class)
public Long createOutpatientOrder(OrderMain orderMain, List<OrderDetail> orderDetails) {
// 1. 参数校验
if (orderMain == null || orderMain.getSchedulePoolId() == null) {
throw new BusinessException("缺少排班池信息,无法创建预约");
}
// 2. 更新订单主表状态为已支付
OrderMain updateOrder = new OrderMain();
updateOrder.setId(orderId);
updateOrder.setStatus(OrderStatus.PAID.getCode());
updateOrder.setPayTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(updateOrder);
logger.info("订单 {} 状态更新为已支付", orderId);
// 2. 检查号源是否足够(乐观锁方式递增 booked_num
incrementBookedNum(orderMain.getSchedulePoolId());
// 3. 若是预约挂号订单,更新对应的号源状态为“已取号”(3)
if (OrderStatus.PAID.getCode().equals(updateOrder.getStatus())
&& isAppointmentOrder(order)) {
// 通过订单明细获取对应的 ScheduleSlot
List<OrderDetail> details = orderDetailMapper.selectByOrderId(orderId);
if (!CollectionUtils.isEmpty(details)) {
for (OrderDetail detail : details) {
Long slotId = detail.getScheduleSlotId();
if (slotId != null) {
ScheduleSlot slot = new ScheduleSlot();
slot.setId(slotId);
slot.setStatus(ScheduleSlotStatus.TAKEN.getCode()); // 3 - 已取号
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
logger.info("预约订单 {} 对应的号源 slotId={} 状态更新为已取号(3)", orderId, slotId);
}
}
// 3. 写入订单主表
orderMain.setStatus(OrderStatus.UNPAID.getCode());
orderMain.setCreateTime(new Date());
orderMainMapper.insert(orderMain); // 假设使用 MyBatis 的 insert 并回填 id
// 4. 写入订单明细
if (!CollectionUtils.isEmpty(orderDetails)) {
for (OrderDetail detail : orderDetails) {
detail.setOrderId(orderMain.getId());
detail.setCreateTime(new Date());
orderDetailMapper.insert(detail);
}
}
// 4. 其它业务(如发药、打印等)保持不变
// ...
// 5. 返回主键
return orderMain.getId();
}
// -----------------------------------------------------------------------
// 私有工具方法
// -----------------------------------------------------------------------
/**
* 判断订单是否为预约挂号类型
* 对指定的排班池记录的 booked_num 执行原子递增。
* <p>
* 使用乐观锁防止并发超额预约:只有当当前已预约数小于总号源数时才允许递增。
*
* @param order 订单实体
* @return true 为预约挂号订单
* @param schedulePoolId 排班池主键
* @throws BusinessException 若号源已满或更新失败
*/
private boolean isAppointmentOrder(OrderMain order) {
// 业务约定:订单类型为 1 表示预约挂号(具体值请参考 OrderMain 中的 type 字段定义)
return order != null && order.getType() != null && order.getType() == 1;
private void incrementBookedNum(Long schedulePoolId) {
// 这里直接使用 mapper 的自定义 SQL 完成原子递增并返回受影响行数
int affected = schedulePoolMapper.incrementBookedNumIfAvailable(schedulePoolId);
if (affected == 0) {
// 说明当前号源已满或记录不存在
SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(schedulePoolId);
String msg = (pool == null) ?
"排班信息不存在,预约失败" :
"该时段号源已满,预约失败";
throw new BusinessException(msg);
}
}
// 其业务方法保持原有实现
// ...
// 其业务方法保持不变 ...
}