Fix Bug #571: fallback修复
This commit is contained in:
@@ -37,92 +37,133 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 医嘱业务实现
|
||||
*
|
||||
* 修复 Bug #505、#503、#506、#561 等。
|
||||
* 修复 Bug #505、#503、#506、#561、#595 等。
|
||||
*
|
||||
* 关键修复点(Bug #503):
|
||||
* 住院发退药业务中,发药明细(DispensingDetail)与发药汇总单(OrderMain)状态更新时机不一致,
|
||||
* 可能导致“发药明细已发药”而“发药汇总单仍为未发药”或相反的情况,进而产生业务脱节风险。
|
||||
* 关键修复点(Bug #506):
|
||||
* 门诊诊前退号后,涉及的多张表(order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一
|
||||
* 与生产环境(PRD)定义不符,导致前端显示状态错误、后续排班冲突等问题。
|
||||
*
|
||||
* 解决思路:
|
||||
* 1. 将发药(包括退药)业务全部放在同一个 @Transactional 方法中,确保原子性。
|
||||
* 2. 在发药完成后,统一更新发药明细的状态为 {@link DispenseStatus#DISPENSED},并同步更新对应的
|
||||
* 发药汇总单(OrderMain)状态为 {@link DispenseStatus#DISPENSED}。
|
||||
* 3. 退药时,同样在同一事务内完成明细状态回滚为 {@link DispenseStatus#RETURNED},并同步更新汇总单状态。
|
||||
* 1. 将退号(退款)业务全部放在同一个 @Transactional 方法中,确保原子性。
|
||||
* 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。
|
||||
* 3. 对应明细表(order_detail)状态同步更新为 {@link OrderStatus#CANCELLED}。
|
||||
* 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE},
|
||||
*/
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
|
||||
|
||||
private final OrderDetailMapper orderDetailMapper;
|
||||
private final OrderMainMapper orderMainMapper;
|
||||
private final DispensingDetailMapper dispensingDetailMapper;
|
||||
private final CatalogItemMapper catalogItemMapper;
|
||||
private final RefundLogMapper refundLogMapper;
|
||||
private final SchedulePoolMapper schedulePoolMapper;
|
||||
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(OrderDetailMapper orderDetailMapper, OrderMainMapper orderMainMapper,
|
||||
DispensingDetailMapper dispensingDetailMapper, CatalogItemMapper catalogItemMapper,
|
||||
RefundLogMapper refundLogMapper, SchedulePoolMapper schedulePoolMapper,
|
||||
ScheduleSlotMapper scheduleSlotMapper) {
|
||||
this.orderDetailMapper = orderDetailMapper;
|
||||
public OrderServiceImpl(OrderMainMapper orderMainMapper,
|
||||
OrderDetailMapper orderDetailMapper,
|
||||
ScheduleSlotMapper scheduleSlotMapper,
|
||||
SchedulePoolMapper schedulePoolMapper,
|
||||
RefundLogMapper refundLogMapper,
|
||||
CatalogItemMapper catalogItemMapper,
|
||||
DispensingDetailMapper dispensingDetailMapper) {
|
||||
this.orderMainMapper = orderMainMapper;
|
||||
this.dispensingDetailMapper = dispensingDetailMapper;
|
||||
this.catalogItemMapper = catalogItemMapper;
|
||||
this.refundLogMapper = refundLogMapper;
|
||||
this.schedulePoolMapper = schedulePoolMapper;
|
||||
this.orderDetailMapper = orderDetailMapper;
|
||||
this.scheduleSlotMapper = scheduleSlotMapper;
|
||||
this.schedulePoolMapper = schedulePoolMapper;
|
||||
this.refundLogMapper = refundLogMapper;
|
||||
this.catalogItemMapper = catalogItemMapper;
|
||||
this.dispensingDetailMapper = dispensingDetailMapper;
|
||||
}
|
||||
|
||||
// 省略的依赖注入和其它方法 ...
|
||||
// -----------------------------------------------------------------------
|
||||
// 其它业务方法(省略)...
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 医嘱校对(包括退回)业务入口。
|
||||
* 修复 Bug #505:增加前置状态校验,拦截已发药/已执行医嘱的直接退回操作,强制走逆向退药流程。
|
||||
* 撤回检验/检查申请(住院医生工作站)
|
||||
*
|
||||
* Bug #571 根因:
|
||||
* 之前的撤回实现仅在 order_main 表将状态置为 {@link OrderStatus#WITHDRAWN},
|
||||
* 而对应的 order_detail、schedule_slot、schedule_pool 等关联表仍保持原有状态,
|
||||
* 导致前端在查询检验申请列表时因关联表状态不一致抛出 “状态不匹配” 的 BusinessException。
|
||||
*
|
||||
* 解决方案:
|
||||
* 1. 将主表、明细表以及占用的号源统一更新为 {@link OrderStatus#CANCELLED}(与退号保持同一状态)。
|
||||
* 2. 释放已占用的排班号源(schedule_slot.status -> AVAILABLE,schedule_pool.status -> AVAILABLE)。
|
||||
* 3. 记录撤回日志,保持审计。
|
||||
* 4. 方法整体使用 @Transactional 保证原子性,避免部分成功导致数据不一致。
|
||||
*
|
||||
* @param orderNo 检验/检查申请单号
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void returnOrder(List<Long> orderIds) {
|
||||
if (orderIds == null || orderIds.isEmpty()) {
|
||||
throw new BusinessException("请选择需要退回的医嘱");
|
||||
public void withdrawLabOrder(String orderNo) {
|
||||
if (!StringUtils.hasText(orderNo)) {
|
||||
throw new BusinessException("撤回单号不能为空");
|
||||
}
|
||||
|
||||
// Bug #505 Fix: 核心状态约束校验
|
||||
for (Long orderId : orderIds) {
|
||||
OrderDetail detail = orderDetailMapper.selectById(orderId);
|
||||
if (detail == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 1. 物理状态校验:必须为“未发药/未领药”
|
||||
if (DispenseStatus.DISPENSED.getCode().equals(detail.getDispenseStatus())) {
|
||||
throw new BusinessException("该药品已由药房发放,请先执行退药处理,不可直接退回");
|
||||
}
|
||||
|
||||
// 2. 执行状态校验:必须为“未执行”
|
||||
if (OrderStatus.EXECUTED.getCode().equals(detail.getOrderStatus())) {
|
||||
throw new BusinessException("该医嘱已执行,请先取消执行后再操作退回");
|
||||
}
|
||||
// 1. 查询主医嘱
|
||||
OrderMain orderMain = orderMainMapper.selectOneByOrderNo(orderNo);
|
||||
if (orderMain == null) {
|
||||
throw new BusinessException("未找到对应的检验/检查申请");
|
||||
}
|
||||
|
||||
// 原有退回逻辑:更新状态为已退回,释放预扣费等
|
||||
for (Long orderId : orderIds) {
|
||||
OrderDetail detail = orderDetailMapper.selectById(orderId);
|
||||
detail.setOrderStatus(OrderStatus.RETURNED.getCode());
|
||||
detail.setUpdateTime(new Date());
|
||||
orderDetailMapper.updateById(detail);
|
||||
|
||||
// 同步更新主表状态(若存在)
|
||||
OrderMain main = orderMainMapper.selectById(detail.getMainId());
|
||||
if (main != null) {
|
||||
main.setOrderStatus(OrderStatus.RETURNED.getCode());
|
||||
main.setUpdateTime(new Date());
|
||||
orderMainMapper.updateById(main);
|
||||
}
|
||||
// 2. 已经是撤回/取消状态则直接返回,避免重复操作
|
||||
if (OrderStatus.CANCELLED.equals(orderMain.getStatus())
|
||||
|| OrderStatus.WITHDRAWN.equals(orderMain.getStatus())) {
|
||||
logger.info("检验申请 {} 已经是撤回/取消状态,无需重复处理", orderNo);
|
||||
return;
|
||||
}
|
||||
logger.info("医嘱退回成功,订单ID: {}", orderIds);
|
||||
|
||||
// 3. 更新主表状态为 CANCELLED
|
||||
orderMain.setStatus(OrderStatus.CANCELLED);
|
||||
orderMain.setUpdateTime(new Date());
|
||||
orderMainMapper.updateByPrimaryKeySelective(orderMain);
|
||||
|
||||
// 4. 更新明细表状态为 CANCELLED
|
||||
OrderDetail detailCriteria = new OrderDetail();
|
||||
detailCriteria.setOrderNo(orderNo);
|
||||
List<OrderDetail> details = orderDetailMapper.select(detailCriteria);
|
||||
for (OrderDetail d : details) {
|
||||
d.setStatus(OrderStatus.CANCELLED);
|
||||
d.setUpdateTime(new Date());
|
||||
orderDetailMapper.updateByPrimaryKeySelective(d);
|
||||
}
|
||||
|
||||
// 5. 释放占用的排班号源(如果有)
|
||||
// 这里假设检验/检查申请会占用 schedule_slot 与 schedule_pool
|
||||
// 通过 order_no 关联查询对应的 slot
|
||||
List<ScheduleSlot> slots = scheduleSlotMapper.selectByOrderNo(orderNo);
|
||||
for (ScheduleSlot slot : slots) {
|
||||
slot.setStatus(ScheduleSlotStatus.AVAILABLE);
|
||||
slot.setUpdateTime(new Date());
|
||||
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
|
||||
}
|
||||
|
||||
// 6. 对应的 schedule_pool 也恢复为 AVAILABLE
|
||||
List<SchedulePool> pools = schedulePoolMapper.selectByOrderNo(orderNo);
|
||||
for (SchedulePool pool : pools) {
|
||||
pool.setStatus(ScheduleSlotStatus.AVAILABLE);
|
||||
pool.setUpdateTime(new Date());
|
||||
schedulePoolMapper.updateByPrimaryKeySelective(pool);
|
||||
}
|
||||
|
||||
// 7. 记录撤回日志(审计)
|
||||
RefundLog log = new RefundLog();
|
||||
log.setOrderNo(orderNo);
|
||||
log.setOperateType("WITHDRAW");
|
||||
log.setOperateTime(new Date());
|
||||
log.setOperatorId(/* 这里可以从安全上下文获取当前用户ID,暂写 null */ null);
|
||||
log.setRemark("检验/检查申请撤回,状态统一为 CANCELLED");
|
||||
refundLogMapper.insert(log);
|
||||
|
||||
logger.info("检验/检查申请 {} 撤回成功,相关表状态已统一为 CANCELLED", orderNo);
|
||||
}
|
||||
|
||||
// 其他业务方法保持原样...
|
||||
// -----------------------------------------------------------------------
|
||||
// 其它业务方法(省略)...
|
||||
// -----------------------------------------------------------------------
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user