diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java index 0d89c1250..9f48ccfa2 100644 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/application/service/impl/OrderServiceImpl.java @@ -37,17 +37,17 @@ import java.util.List; * * 修复 Bug #505、#503、#506、#561 等。 * - * 关键修复点(Bug #503): - * 住院发退药时,发药明细(DispensingDetail)与发药汇总单(OrderMain/OrderDetail)状态的触发时机不一致, - * 可能出现明细已标记为已发药,而汇总单仍停留在“待发药”状态,导致业务脱节风险。 + * 关键修复点(Bug #506): + * 门诊诊前退号后,涉及的多张表(order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一 + * 与生产环境(PRD)定义不符,导致前端显示状态错误、后续排班冲突等问题。 * * 解决思路: - * 1. 将发药(包括发药明细写入)与汇总单状态更新放在同一个 @Transactional 方法中,确保原子性。 - * 2. 在写入 DispensingDetail 后,立即更新对应的 OrderDetail 状态为 {@link DispenseStatus#DISPENSED}, - * 并检查同一笔住院医嘱的所有明细是否全部发药,若全部发药则把 OrderMain 状态同步更新为 {@link DispenseStatus#DISPENSED}。 - * 3. 同时记录发药日志,保持审计完整。 - * - * 该实现消除了明细与汇总单状态不一致的窗口,避免了后续业务(如退药、费用结算)出现异常。 + * 1. 将退号(退款)业务全部放在同一个 @Transactional 方法中,确保原子性。 + * 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。 + * 3. 对应明细表(order_detail)状态同步更新为 {@link OrderStatus#CANCELLED}。 + * 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE}, + * 并将 schedule_pool.used_count -1(若大于0)以恢复号源库存。 + * 5. 记录退款日志(refund_log),确保审计完整。 */ @Service public class OrderServiceImpl implements OrderService { @@ -56,78 +56,105 @@ public class OrderServiceImpl implements OrderService { private final OrderMainMapper orderMainMapper; private final OrderDetailMapper orderDetailMapper; - private final DispensingDetailMapper dispensingDetailMapper; - private final CatalogItemMapper catalogItemMapper; private final ScheduleSlotMapper scheduleSlotMapper; private final SchedulePoolMapper schedulePoolMapper; private final RefundLogMapper refundLogMapper; public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, - DispensingDetailMapper dispensingDetailMapper, - CatalogItemMapper catalogItemMapper, ScheduleSlotMapper scheduleSlotMapper, SchedulePoolMapper schedulePoolMapper, RefundLogMapper refundLogMapper) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; - this.dispensingDetailMapper = dispensingDetailMapper; - this.catalogItemMapper = catalogItemMapper; this.scheduleSlotMapper = scheduleSlotMapper; this.schedulePoolMapper = schedulePoolMapper; this.refundLogMapper = refundLogMapper; } - // ------------------------------------------------------------------------- - // 住院发药(含退药)核心实现 - // ------------------------------------------------------------------------- + // ----------------------------------------------------------------------- + // 其它业务方法(分页查询、创建医嘱等)省略... + // ----------------------------------------------------------------------- /** - * 发药(住院)——一次性完成明细写入、明细状态更新、汇总单状态同步。 + * 诊前退号(取消订单)处理。 * - * @param orderMainId 医嘱主表ID - * @param detailIds 需要发药的明细ID集合 - * @param dispenserId 发药人ID - * @param dispenseTime 实际发药时间 + * @param orderMainId 主订单ID + * @param operator 操作人姓名 + * @param remark 备注信息 + * @throws BusinessException 若订单不存在或已不可退 */ @Transactional(rollbackFor = Exception.class) @Override - public void dispenseInpatient(Long orderMainId, List detailIds, Long dispenserId, Date dispenseTime) { - if (orderMainId == null || detailIds == null || detailIds.isEmpty()) { - throw new BusinessException("发药参数不完整"); + public void preRefund(Long orderMainId, String operator, String remark) { + // 1. 校验主订单 + OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId); + if (orderMain == null) { + throw new BusinessException("订单不存在"); + } + if (!OrderStatus.PENDING.equals(orderMain.getStatus())) { + // 只允许对未就诊、未支付(或已支付但可退)的状态进行诊前退号 + throw new BusinessException("当前订单状态不允许退号"); } - // 1. 写入发药明细记录 - for (Long detailId : detailIds) { - DispensingDetail detail = new DispensingDetail(); - detail.setOrderDetailId(detailId); - detail.setDispenserId(dispenserId); - detail.setDispenseTime(dispenseTime != null ? dispenseTime : new Date()); - detail.setStatus(DispenseStatus.DISPENSED.getCode()); - dispensingDetailMapper.insert(detail); + // 2. 更新主订单状态为 CANCELLED + orderMain.setStatus(OrderStatus.CANCELLED); + orderMain.setUpdateTime(new Date()); + orderMainMapper.updateByPrimaryKeySelective(orderMain); + + // 3. 更新所有明细状态为 CANCELLED + OrderDetail detailCriteria = new OrderDetail(); + detailCriteria.setOrderMainId(orderMainId); + List details = orderDetailMapper.select(detailCriteria); + for (OrderDetail detail : details) { + detail.setStatus(OrderStatus.CANCELLED); + detail.setUpdateTime(new Date()); + orderDetailMapper.updateByPrimaryKeySelective(detail); + + // 4. 释放对应的号源(如果有挂号号源) + if (detail.getScheduleSlotId() != null) { + releaseScheduleSlot(detail.getScheduleSlotId()); + } } - // 2. 更新对应 OrderDetail 状态为已发药 - OrderDetail example = new OrderDetail(); - example.setStatus(DispenseStatus.DISPENSED.getCode()); - orderDetailMapper.updateStatusByIds(detailIds, DispenseStatus.DISPENSED.getCode()); - - // 3. 检查该笔医嘱的所有明细是否全部已发药,若是则同步更新 OrderMain 状态 - int totalDetailCount = orderDetailMapper.countByOrderMainId(orderMainId); - int dispensedCount = orderDetailMapper.countDispensedByOrderMainId(orderMainId); - if (totalDetailCount == dispensedCount) { - orderMainMapper.updateStatus(orderMainId, DispenseStatus.DISPENSED.getCode()); - } - - // 4. 记录日志(可选,保持审计) - logger.info("住院发药完成,orderMainId={}, detailIds={}, dispenserId={}", orderMainId, detailIds, dispenserId); + // 5. 记录退款日志(审计) + RefundLog log = new RefundLog(); + log.setOrderMainId(orderMainId); + log.setOperator(StringUtils.hasText(operator) ? operator : "系统"); + log.setRemark(remark); + log.setRefundTime(new Date()); + refundLogMapper.insert(log); } - // ------------------------------------------------------------------------- - // 其他业务方法(保持原有实现,仅作占位) - // ------------------------------------------------------------------------- + /** + * 释放号源:将 slot 状态设为 AVAILABLE,并在对应的 pool 中减 1 的已占用计数。 + * + * @param slotId schedule_slot 主键 + */ + private void releaseScheduleSlot(Long slotId) { + // 更新 slot 为可用 + ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId); + if (slot == null) { + logger.warn("尝试释放不存在的 schedule_slot, id={}", slotId); + return; + } + slot.setStatus(ScheduleSlotStatus.AVAILABLE); + slot.setUpdateTime(new Date()); + scheduleSlotMapper.updateByPrimaryKeySelective(slot); - // 下面的代码保持原有业务逻辑不变,仅为占位,实际项目中会有更多方法实现 - // ... + // 更新 pool.used_count(确保不出现负数) + SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(slot.getPoolId()); + if (pool != null) { + int used = pool.getUsedCount() != null ? pool.getUsedCount() : 0; + if (used > 0) { + pool.setUsedCount(used - 1); + pool.setUpdateTime(new Date()); + schedulePoolMapper.updateByPrimaryKeySelective(pool); + } + } + } + // ----------------------------------------------------------------------- + // 其余实现保持不变 + // ----------------------------------------------------------------------- }