package com.openhis.web.outpatient.service.impl; import com.openhis.web.outpatient.mapper.RegistrationCancelMapper; import com.openhis.web.outpatient.service.RegistrationCancelService; import com.openhis.web.inpatient.mapper.OrderMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Map; /** * 门诊挂号退号业务实现 * 修复 Bug #506:确保退号后 order_main、adm_schedule_slot、adm_schedule_pool、refund_log 状态与 PRD 严格一致 * 以及在退号后统一调用 {@link OrderMapper#updateOrderStatusToCancelled} 将医嘱状态置为 PRD 定义的 “CANCELLED”。 */ @Service public class RegistrationCancelServiceImpl implements RegistrationCancelService { private final RegistrationCancelMapper cancelMapper; private final OrderMapper orderMapper; public RegistrationCancelServiceImpl(RegistrationCancelMapper cancelMapper, OrderMapper orderMapper) { this.cancelMapper = cancelMapper; this.orderMapper = orderMapper; } @Override @Transactional(rollbackFor = Exception.class) public void cancelRegistration(Long orderId, BigDecimal refundAmount) { if (orderId == null) { throw new IllegalArgumentException("订单ID不能为空"); } // 1. 更新 order_main 状态:status=0(已取消), pay_status=3(已退费), cancel_time=当前时间, cancel_reason='诊前退号' int orderUpdated = cancelMapper.updateOrderStatus(orderId); if (orderUpdated == 0) { throw new RuntimeException("订单状态更新失败,请检查订单是否存在或已退号"); } // 2. 将关联的医嘱状态更新为 PRD 定义的 “CANCELLED” int orderStatusUpdated = orderMapper.updateOrderStatusToCancelled(orderId, OrderMapper.ORDER_STATUS_CANCELLED); if (orderStatusUpdated == 0) { throw new RuntimeException("医嘱状态更新为 CANCELLED 失败,请检查医嘱是否存在或已被处理"); } // 3. 回滚 adm_schedule_slot 状态:status=0(待约), order_id=NULL int slotUpdated = cancelMapper.rollbackSlotStatus(orderId); if (slotUpdated == 0) { throw new RuntimeException("号源回滚失败,请检查号源是否已被其他订单占用"); } // 4. 更新对应的排班池(adm_schedule_pool)版本号和已约数 Map slotInfo = cancelMapper.selectSlotByOrderId(orderId); if (slotInfo != null && slotInfo.get("pool_id") != null) { Long poolId = ((Number) slotInfo.get("pool_id")).longValue(); int poolUpdated = cancelMapper.updatePoolAfterCancel(poolId); if (poolUpdated == 0) { throw new RuntimeException("排班池信息更新失败,请检查 pool_id 是否正确"); } } // 5. 记录退费日志 int logInserted = cancelMapper.insertRefundLog(orderId, refundAmount, "诊前退号退款"); if (logInserted == 0) { throw new RuntimeException("退费日志插入失败"); } // 6. 如有需要,可在此处加入对支付成功后号源状态流转为“已取”(status=3)的处理(已在 Mapper 中预留方法)。 } }