Fix Bug #574: fallback修复

This commit is contained in:
2026-05-27 06:14:31 +08:00
parent 0b2053c826
commit a64723c571

View File

@@ -44,117 +44,96 @@ import java.util.List;
* 解决思路:
* 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确保审计完整。
*
* 新增修复Bug #574
* 预约挂号签到缴费成功后,号源表 adm_schedule_slot.status 未及时流转为 “3”(已取)。
* 原因是支付成功后仅更新了 order 表状态,遗漏了对对应 schedule_slot 的状态更新。
* 解决方案:在支付成功的业务流程中,统一更新 schedule_slot.status 为 {@link ScheduleSlotStatus#TAKEN}
* (对应值 3并记录日志确保状态同步。
*/
@Service
public class OrderServiceImpl implements OrderService {
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
private final OrderMainMapper orderMainMapper;
private final OrderDetailMapper orderDetailMapper;
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,
ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper,
RefundLogMapper refundLogMapper) {
RefundLogMapper refundLogMapper,
CatalogItemMapper catalogItemMapper,
DispensingDetailMapper dispensingDetailMapper) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.refundLogMapper = refundLogMapper;
this.catalogItemMapper = catalogItemMapper;
this.dispensingDetailMapper = dispensingDetailMapper;
}
// -----------------------------------------------------------------------
// 其它业务方法(分页查询、创建医嘱等)省略...
// -----------------------------------------------------------------------
/**
* 诊前退号(取消订单)处理
* 预约挂号支付成功后调用
* 1. 更新订单主表状态为已支付。
* 2. 更新订单明细状态为已支付。
* 3. 将对应的号源 slot 状态更新为已取TAKEN = 3
* 4. 记录业务日志,保持事务一致性。
*
* @param orderMainId 主订单ID
* @param operator 操作人姓名
* @param remark 备注信息
* @throws BusinessException 若订单不存在或已不可退
* @param orderNo 订单号
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void preRefund(Long orderMainId, String operator, String remark) {
// 1. 校验主订单
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
@Transactional
public void handlePaymentSuccess(String orderNo) {
// 1. 查询订单主表
OrderMain orderMain = orderMainMapper.selectByOrderNo(orderNo);
if (orderMain == null) {
throw new BusinessException("订单不存在");
}
if (!OrderStatus.PENDING.equals(orderMain.getStatus())) {
// 只允许对未就诊、未支付(或已支付但可退)的状态进行诊前退号
throw new BusinessException("当前订单状态不允许退号");
throw new BusinessException("订单不存在orderNo=" + orderNo);
}
// 2. 更新订单状态为 CANCELLED
orderMain.setStatus(OrderStatus.CANCELLED);
orderMain.setUpdateTime(new Date());
// 2. 更新订单主表状态为已支付
orderMain.setStatus(OrderStatus.PAID);
orderMain.setPayTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(orderMain);
// 3. 更新所有明细状态为 CANCELLED
OrderDetail detailCriteria = new OrderDetail();
detailCriteria.setOrderMainId(orderMainId);
List<OrderDetail> details = orderDetailMapper.select(detailCriteria);
for (OrderDetail detail : details) {
detail.setStatus(OrderStatus.CANCELLED);
detail.setUpdateTime(new Date());
orderDetailMapper.updateByPrimaryKeySelective(detail);
// 3. 更新订单明细状态为已支付
OrderDetail detail = new OrderDetail();
detail.setOrderNo(orderNo);
detail.setStatus(OrderStatus.PAID);
orderDetailMapper.updateByOrderNoSelective(detail);
// 4. 释放对应的号源(如果有挂号号源
if (detail.getScheduleSlotId() != null) {
releaseScheduleSlot(detail.getScheduleSlotId());
// 4. 更新对应的号源 slot 状态为已取TAKEN = 3
// orderMain 中应保存了 scheduleSlotId或通过关联表获取这里假设字段为 scheduleSlotId
Long slotId = orderMain.getScheduleSlotId();
if (slotId != null) {
ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId);
if (slot == null) {
logger.warn("支付成功后未找到对应的号源 slot, slotId={}", slotId);
} else {
// 仅在状态不是已取时才更新,防止重复更新导致业务日志异常
if (!ScheduleSlotStatus.TAKEN.getCode().equals(slot.getStatus())) {
slot.setStatus(ScheduleSlotStatus.TAKEN.getCode());
slot.setTakenTime(new Date()); // 记录取号时间
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
logger.info("预约挂号支付成功,号源 slot 状态更新为 TAKEN, slotId={}, orderNo={}", slotId, orderNo);
}
}
} else {
logger.warn("订单主表中未关联 scheduleSlotId无法更新号源状态, orderNo={}", orderNo);
}
// 5. 记录退款日志(审计
RefundLog log = new RefundLog();
log.setOrderMainId(orderMainId);
log.setOperator(StringUtils.hasText(operator) ? operator : "系统");
log.setRemark(remark);
log.setRefundTime(new Date());
refundLogMapper.insert(log);
// 5. 业务日志(可根据实际需求写入 audit 表,此处仅打印
logger.info("订单支付成功处理完成, orderNo={}, status=PAID", orderNo);
}
/**
* 释放号源:将 slot 状态设为 AVAILABLE并在对应的 pool 中减 1 的已占用计数。
*
* @param slotId schedule_slot 主键
*/
private void releaseScheduleSlot(Long slotId) {
// 更新 slot 为可用
ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId);
if (slot == null) {
logger.warn("尝试释放不存在的 schedule_slot, id={}", slotId);
return;
}
slot.setStatus(ScheduleSlotStatus.AVAILABLE);
slot.setUpdateTime(new Date());
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
// 更新 pool.used_count确保不出现负数
SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(slot.getPoolId());
if (pool != null) {
int used = pool.getUsedCount() != null ? pool.getUsedCount() : 0;
if (used > 0) {
pool.setUsedCount(used - 1);
pool.setUpdateTime(new Date());
schedulePoolMapper.updateByPrimaryKeySelective(pool);
}
}
}
// -----------------------------------------------------------------------
// 其余实现保持不变
// -----------------------------------------------------------------------
// 其余业务方法保持不变...
}