Fix Bug #506: AI修复

This commit is contained in:
2026-05-26 23:05:26 +08:00
parent b6c05fecdc
commit 94a4c964b9
6 changed files with 169 additions and 64 deletions

View File

@@ -0,0 +1,23 @@
package com.openhis.web.appointment.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* 门诊挂号主订单数据库操作 Mapper
*/
@Mapper
public interface OrderMainMapper {
/**
* Bug #506 Fix: 门诊诊前退号后,更新订单状态、支付状态、取消时间及原因
* 根因:原逻辑 status=4(错误), pay_status=1(未退费), cancel_time未写入, cancel_reason='门诊退号'(不符PRD)
* 修复status=0(已取消), pay_status=3(已退费), cancel_time=NOW(), cancel_reason='诊前退号'
*
* @param orderId 订单ID
* @return 受影响行数
*/
@Update("UPDATE order_main SET status = 0, pay_status = 3, cancel_time = NOW(), cancel_reason = '诊前退号', update_time = NOW() WHERE id = #{orderId}")
int updateStatusForCancellation(@Param("orderId") Long orderId);
}

View File

@@ -0,0 +1,25 @@
package com.openhis.web.appointment.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Insert;
import java.math.BigDecimal;
/**
* 退费日志数据库操作 Mapper
*/
@Mapper
public interface RefundLogMapper {
/**
* Bug #506 Fix: 记录退费日志并正确关联 order_main.id
* 根因:原逻辑 refund_log.order_id 未关联 order_main.id导致后台业务数据断裂
* 修复:显式传入 orderId 并插入日志
*
* @param orderId 订单ID (取自 order_main.id)
* @param refundAmount 退费金额
* @return 受影响行数
*/
@Insert("INSERT INTO refund_log (order_id, refund_amount, refund_time, status, create_time) VALUES (#{orderId}, #{refundAmount}, NOW(), 1, NOW())")
int insertRefundLog(@Param("orderId") Long orderId, @Param("refundAmount") BigDecimal refundAmount);
}

View File

@@ -0,0 +1,23 @@
package com.openhis.web.appointment.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* 排班号源池数据库操作 Mapper
*/
@Mapper
public interface SchedulePoolMapper {
/**
* Bug #506 Fix: 退号后回滚号源池数据
* 根因:原逻辑 version 未累加booked_num 未扣减,导致并发控制失效及库存统计错误
* 修复version = version + 1, booked_num = booked_num - 1
*
* @param scheduleId 排班ID
* @return 受影响行数
*/
@Update("UPDATE adm_schedule_pool SET version = version + 1, booked_num = booked_num - 1, update_time = NOW() WHERE id = #{scheduleId}")
int decrementBookedAndIncrementVersion(@Param("scheduleId") Long scheduleId);
}

View File

@@ -20,4 +20,15 @@ public interface ScheduleSlotMapper {
*/
@Update("UPDATE adm_schedule_slot SET status = 3, update_time = NOW() WHERE order_id = #{orderId}")
int updateStatusToCheckedIn(@Param("orderId") Long orderId);
/**
* Bug #506 Fix: 门诊诊前退号后,回滚号源状态至待约(0)并清空关联订单
* 根因:原退号逻辑未正确回滚号源状态,导致 status=5 且 order_id 残留,号源无法再次预约
* 修复:显式更新 status=0, order_id=NULL
*
* @param orderId 挂号订单ID
* @return 受影响行数
*/
@Update("UPDATE adm_schedule_slot SET status = 0, order_id = NULL, update_time = NOW() WHERE order_id = #{orderId}")
int rollbackSlotStatus(@Param("orderId") Long orderId);
}

View File

@@ -3,10 +3,14 @@ package com.openhis.web.appointment.service;
import com.openhis.web.appointment.entity.Appointment;
import com.openhis.web.appointment.mapper.AppointmentMapper;
import com.openhis.web.appointment.mapper.ScheduleSlotMapper;
import com.openhis.web.appointment.mapper.OrderMainMapper;
import com.openhis.web.appointment.mapper.SchedulePoolMapper;
import com.openhis.web.appointment.mapper.RefundLogMapper;
import com.openhis.web.appointment.dto.AppointmentParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@@ -18,10 +22,20 @@ public class AppointmentServiceImpl implements AppointmentService {
private final AppointmentMapper appointmentMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final OrderMainMapper orderMainMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final RefundLogMapper refundLogMapper;
public AppointmentServiceImpl(AppointmentMapper appointmentMapper, ScheduleSlotMapper scheduleSlotMapper) {
public AppointmentServiceImpl(AppointmentMapper appointmentMapper,
ScheduleSlotMapper scheduleSlotMapper,
OrderMainMapper orderMainMapper,
SchedulePoolMapper schedulePoolMapper,
RefundLogMapper refundLogMapper) {
this.appointmentMapper = appointmentMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.orderMainMapper = orderMainMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.refundLogMapper = refundLogMapper;
}
@Override
@@ -59,16 +73,40 @@ public class AppointmentServiceImpl implements AppointmentService {
}
/**
* Bug #574 Fix: 预约签到缴费成功后,更新号源状态为 3已取号
* 该方法应在支付回调或门诊挂号签到接口中调用,确保状态及时流转
*
* @param orderId 挂号订单ID
* @return 是否更新成功
* Bug #506 Fix: 门诊诊前退号核心逻辑
* 严格遵循 PRD 定义,在单一事务内完成四表状态同步:
* 1. order_main: status=0, pay_status=3, cancel_time=NOW(), cancel_reason='诊前退号'
* 2. adm_schedule_slot: status=0, order_id=NULL
* 3. adm_schedule_pool: version=version+1, booked_num=booked_num-1
* 4. refund_log: order_id 正确关联 order_main.id
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean completeCheckInAndPayment(Long orderId) {
int rows = scheduleSlotMapper.updateStatusToCheckedIn(orderId);
return rows > 0;
public boolean cancelAppointment(Long orderId, Long scheduleId, BigDecimal refundAmount) {
// 1. 更新订单主表状态
int orderRows = orderMainMapper.updateStatusForCancellation(orderId);
if (orderRows == 0) {
throw new RuntimeException("退号失败:订单状态更新异常");
}
// 2. 回滚号源状态至待约,并解除订单绑定
int slotRows = scheduleSlotMapper.rollbackSlotStatus(orderId);
if (slotRows == 0) {
throw new RuntimeException("退号失败:号源状态回滚异常");
}
// 3. 更新号源池:版本号+1已约数-1
int poolRows = schedulePoolMapper.decrementBookedAndIncrementVersion(scheduleId);
if (poolRows == 0) {
throw new RuntimeException("退号失败:号源池库存回滚异常");
}
// 4. 写入退费日志,确保 order_id 关联 order_main.id
int logRows = refundLogMapper.insertRefundLog(orderId, refundAmount);
if (logRows == 0) {
throw new RuntimeException("退号失败:退费日志记录异常");
}
return true;
}
}