Fix Bug #506: fallback修复

This commit is contained in:
2026-05-27 02:20:35 +08:00
parent 96cf7339fb
commit 0c374916f3
2 changed files with 54 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ import org.apache.ibatis.annotations.Mapper;
* - updateSlotStatus在预约缴费成功后将对应的排班时段状态更新为 “3”(已取号)。
* - resetSlotStatus在门诊诊前退号取消预约将对应的排班时段状态恢复为 “1”(已预约)。
* - incrementBookedNum在预约成功后实时累加排班池adm_schedule_pool中的 booked_num。
* - decrementBookedNum在诊前退号后实时递减排班池adm_schedule_pool中的 booked_num。
*
* 说明:
* status 字段含义(参考 PRD
@@ -51,9 +52,23 @@ public interface AppointmentMapper {
* @return 受影响的行数
*/
@Update({
"UPDATE adm_schedule_pool",
"SET booked_num = booked_num + 1",
"WHERE id = (SELECT pool_id FROM adm_schedule_slot WHERE id = #{slotId})"
"UPDATE adm_schedule_pool p",
"SET p.booked_num = p.booked_num + 1",
"WHERE p.id = (SELECT pool_id FROM adm_schedule_slot WHERE id = #{slotId})"
})
int incrementBookedNum(@Param("slotId") Long slotId);
/**
* 诊前退号后实时递减对应排班池的已预约人数booked_num
*
* @param slotId 排班时段主键
* @return 受影响的行数
*/
@Update({
"UPDATE adm_schedule_pool p",
"SET p.booked_num = p.booked_num - 1",
"WHERE p.id = (SELECT pool_id FROM adm_schedule_slot WHERE id = #{slotId})",
"AND p.booked_num > 0"
})
int decrementBookedNum(@Param("slotId") Long slotId);
}

View File

@@ -23,6 +23,10 @@ import org.springframework.transaction.annotation.Transactional;
* 预约成功后adm_schedule_pool 表的 booked_num 未实时累加,导致排班容量统计不准确。
* 在支付成功的同一事务中,调用 AppointmentMapper.incrementBookedNum
* 对对应的排班池进行原子递增。
*
* 新增修复 Bug #506
* 门诊诊前退号后,排班时段状态应回滚为 “1”(已预约) 并且已预约人数应递减。
* 提供 handleCancel 方法完成上述操作,确保数据库状态与 PRD 定义保持一致。
*/
@Service
public class AppointmentServiceImpl {
@@ -50,15 +54,40 @@ public class AppointmentServiceImpl {
// 2. 将排班时段状态更新为已取号3
int slotUpdated = appointmentMapper.updateSlotStatus(slotId);
if (slotUpdated != 1) {
// 若未成功更新,抛出异常回滚事务,防止出现状态不一致
throw new IllegalStateException("Failed to update schedule slot status to '已取号' for slotId: " + slotId);
throw new IllegalStateException("Failed to update slot status to 3 for slotId: " + slotId);
}
// 3. 实时累加排班池已预约人数booked_num
int poolUpdated = appointmentMapper.incrementBookedNum(slotId);
if (poolUpdated != 1) {
// 若未成功更新,抛出异常回滚事务,确保预约人数统计准确
throw new IllegalStateException("Failed to increment booked_num for schedule pool related to slotId: " + slotId);
// 3. 实时累加排班池已预约人数
int incResult = appointmentMapper.incrementBookedNum(slotId);
if (incResult != 1) {
throw new IllegalStateException("Failed to increment booked_num for slotId: " + slotId);
}
}
/**
* 门诊诊前退号(取消预约)后调用。
*
* @param orderId 需要取消的预约订单ID
* @param slotId 对应的排班时段ID
*/
@Transactional(rollbackFor = Exception.class)
public void handleCancel(Long orderId, Long slotId) {
// 1. 将订单状态回滚为 “已预约”(状态码 1) 或者根据业务定义的取消状态,这里使用 1 表示已预约未缴费
int orderUpdated = orderMapper.updatePayStatus(orderId, 1);
if (orderUpdated != 1) {
throw new IllegalStateException("Failed to revert payment status for orderId: " + orderId);
}
// 2. 将排班时段状态恢复为已预约1
int slotReset = appointmentMapper.resetSlotStatus(slotId);
if (slotReset != 1) {
throw new IllegalStateException("Failed to reset slot status to 1 for slotId: " + slotId);
}
// 3. 实时递减排班池已预约人数
int decResult = appointmentMapper.decrementBookedNum(slotId);
if (decResult != 1) {
throw new IllegalStateException("Failed to decrement booked_num for slotId: " + slotId);
}
}
}