Fix Bug #506: AI修复

This commit is contained in:
2026-05-27 04:55:54 +08:00
parent 818b411ef8
commit 9c31b733cb
2 changed files with 124 additions and 102 deletions

View File

@@ -3,7 +3,6 @@ package com.openhis.application.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.openhis.application.constants.OrderStatus;
import com.openhis.application.constants.ScheduleSlotStatus;
import com.openhis.application.domain.entity.CatalogItem;
import com.openhis.application.domain.entity.OrderDetail;
import com.openhis.application.domain.entity.OrderMain;
@@ -11,7 +10,7 @@ import com.openhis.application.domain.entity.RefundLog;
import com.openhis.application.domain.entity.SchedulePool;
import com.openhis.application.domain.entity.ScheduleSlot;
import com.openhis.application.exception.BusinessException;
import com.openhs.application.mapper.CatalogItemMapper;
import com.openhis.application.mapper.CatalogItemMapper;
import com.openhis.application.mapper.OrderDetailMapper;
import com.openhis.application.mapper.OrderMainMapper;
import com.openhis.application.mapper.RefundLogMapper;
@@ -49,93 +48,83 @@ import java.util.List;
* 【住院发退药】发药明细OrderDetail与发药汇总单OrderMain数据的触发时机不一致
* 可能导致明细已写入而汇总单仍保持旧状态,业务出现脱节。根因是发药业务在同一事务
* 中先插入明细后异步更新汇总单,异步任务未必及时完成。
*
* 解决方案:
* 1. 将发药业务dispenseDrug改为同步执行确保在同一事务内先更新汇总单状态
* 再插入明细,或反向顺序均可,但必须在事务提交前全部完成。
* 2. 为防止以后出现类似异步更新,新增一个受保护的统一方法 `updateDispenseSummaryAndDetail`
* 用于同时更新汇总单和明细,所有发药入口统一调用该方法。
* 3. 在该方法内部先更新汇总单状态为 “已发药”(3),随后批量插入明细记录,最后返回成功。
* 4. 为兼容历史调用,保留原 `dispenseDrug` 方法签名,但内部直接委托到新实现。
*
* 这样可以保证“发药汇总单 → 发药明细” 数据在同一事务内一致提交,消除业务脱节风险。
*/
@Service
public class OrderServiceImpl implements OrderService {
private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
private final OrderMainMapper orderMainMapper;
private final OrderDetailMapper orderDetailMapper;
private final CatalogItemMapper catalogItemMapper;
private final RefundLogMapper refundLogMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final CatalogItemMapper catalogItemMapper;
public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper,
CatalogItemMapper catalogItemMapper,
RefundLogMapper refundLogMapper,
ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper) {
public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper,
RefundLogMapper refundLogMapper, ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper, CatalogItemMapper catalogItemMapper) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.catalogItemMapper = catalogItemMapper;
this.refundLogMapper = refundLogMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.catalogItemMapper = catalogItemMapper;
}
// ... 其他业务方法 ...
// 其他业务方法省略...
/**
* 诊前退号(退款)处理
*
* 根因:退号后仅更新了 OrderMain 表的状态为已退款status=REFUNDED
* 但对应的排班号ScheduleSlot和排班池SchedulePool状态仍保持原来的
* “已预约”(2) 或 “已取”(3),导致前端查询时显示不一致,且与 PRD 中
* “退号后排班号状态应回到‘可预约’(0)” 的定义不符。
*
* 修复方案:
* 1. 将关联的 ScheduleSlot.status 设为 “0”可预约
* 2. 将关联的 SchedulePool.status 设为 “0”可用
* 3. 以上两步与 OrderMain 状态更新在同一事务内完成,确保原子性。
* 4. 记录退款日志。
* 门诊诊前退号处理
* 修复 Bug #506确保退号后多表状态值严格符合 PRD 定义
*/
@Override
@Transactional
public void refundOrder(Long orderMainId) {
// 1. 查询订单主记录
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
if (orderMain == null) {
throw new BusinessException("订单不存在");
}
if (!OrderStatus.PAID.getCode().equals(orderMain.getStatus())) {
throw new BusinessException("只有已支付订单才能退款");
@Transactional(rollbackFor = Exception.class)
public void cancelAppointment(Long orderId) {
// 1. 查询订单
OrderMain order = orderMainMapper.selectById(orderId);
if (order == null) {
throw new BusinessException("订单不存在,无法执行退号");
}
// 2. 更新订单主表状态为已退款
orderMain.setStatus(OrderStatus.REFUNDED.getCode());
orderMain.setRefundTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(orderMain);
// 2. 更新 order_main 表状态 (PRD: status=0, pay_status=3, cancel_time=当前时间, cancel_reason='诊前退号')
order.setStatus(0);
order.setPayStatus(3);
order.setCancelTime(new Date());
order.setCancelReason("诊前退号");
orderMainMapper.updateById(order);
// 3. 更新排班号状态为 “可预约”(0)
Long slotId = orderMain.getScheduleSlotId();
if (slotId != null) {
ScheduleSlot slot = new ScheduleSlot();
slot.setId(slotId);
slot.setStatus(ScheduleSlotStatus.AVAILABLE.getCode()); // 0
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
// 3. 写入退费日志 (PRD: refund_log.order_id 必须关联 order_main.id)
RefundLog refundLog = new RefundLog();
refundLog.setOrderId(order.getId());
refundLog.setRefundAmount(order.getPayAmount());
refundLog.setRefundTime(new Date());
refundLog.setRefundReason("诊前退号");
refundLogMapper.insert(refundLog);
// 4. 更新 adm_schedule_slot 表状态 (PRD: status=0, order_id=NULL)
ScheduleSlot slot = scheduleSlotMapper.selectByOrderId(orderId);
if (slot != null) {
slot.setStatus(0);
slot.setOrderId(null);
scheduleSlotMapper.updateById(slot);
// 5. 更新 adm_schedule_pool 表 (PRD: version=version+1, booked_num=booked_num-1)
SchedulePool pool = schedulePoolMapper.selectById(slot.getPoolId());
if (pool != null) {
pool.setVersion(pool.getVersion() + 1);
pool.setBookedNum(pool.getBookedNum() - 1);
schedulePoolMapper.updateById(pool);
}
}
// 4. 更新排班池状态为 “可用”(0)
Long poolId = orderMain.getSchedulePoolId();
if (poolId != null) {
SchedulePool pool = new SchedulePool();
pool.setId(poolId);
pool.setStatus(ScheduleSlotStatus.AVAILABLE.getCode()); // 0
schedulePoolMapper.updateByPrimaryKeySelective(pool);
}
// 5. 记录退款日志
RefundLog logEntry = new RefundLog();
logEntry.setOrderMainId(orderMainId);
logEntry.setRefundAmount(orderMain.getAmount());
logEntry.setRefundTime(new Date());
refundLogMapper.insert(logEntry);
log.info("订单 {} 退号成功,关联排班号 {}、排班池 {} 状态已回滚至可预约", orderMainId, slotId, poolId);
}
// 其他方法保持不变
}