From 1ef72d1f92a005c1fc71b3640722f570955af9e4 Mon Sep 17 00:00:00 2001 From: xunyu Date: Wed, 27 May 2026 06:25:25 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Bug=20#575:=20fallback=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/OrderServiceImpl.java | 172 +++++++----------- 1 file changed, 61 insertions(+), 111 deletions(-) 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 4da04ba39..b1ccd3730 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 @@ -5,6 +5,7 @@ import com.github.pagehelper.PageHelper; import com.openhis.application.constants.OrderStatus; import com.openhis.application.constants.ScheduleSlotStatus; import com.openhis.application.constants.DispenseStatus; +import com.openhis.application.domain.dto.OrderVerifyDto; import com.openhis.application.domain.dto.QueuePatientDto; import com.openhis.application.domain.entity.CatalogItem; import com.openhis.application.domain.entity.DispensingDetail; @@ -20,7 +21,7 @@ import com.openhis.application.mapper.OrderDetailMapper; import com.openhis.application.mapper.OrderMainMapper; import com.openhis.application.mapper.RefundLogMapper; import com.openhis.application.mapper.SchedulePoolMapper; -import com.openhs.application.mapper.ScheduleSlotMapper; +import com.openhis.application.mapper.ScheduleSlotMapper; import com.openhis.application.service.OrderService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,27 +39,15 @@ import java.util.stream.Collectors; * * 修复 Bug #505、#503、#506、#561 等。 * - * 关键修复点(Bug #561): - * 医嘱录入后,总量单位显示异常,显示为“null”。根因是 OrderDetail 在保存时 - * 未正确从诊疗目录(CatalogItem)读取并写入 totalUnit 字段,导致前端取到 null。 - * - * 解决方案: - * 1. 在创建 OrderDetail 前,确保通过 catalogItemId 查询到对应的 CatalogItem。 - * 2. 将 CatalogItem 中的 totalUnit(或 unit)写入 OrderDetail.totalUnit。 - * 3. 若 CatalogItem 为 null 或 totalUnit 为 null,抛出业务异常,防止脏数据写入。 - * 4. 为兼容历史数据,若 CatalogItem 中的 totalUnit 为空,仍使用旧字段 unit 作为回退。 - * * 关键修复点(Bug #506): - * 门诊诊前退号后,系统需要同步更新以下几张表的状态,使其与 PRD 定义保持一致: - * 1. OrderMain.status → OrderStatus.CANCELLED(已退号) - * 2. OrderDetail.status → OrderStatus.CANCELLED - * 3. ScheduleSlot.status → ScheduleSlotStatus.AVAILABLE(号源恢复为可预约) - * 4. SchedulePool.status → ScheduleSlotStatus.AVAILABLE(对应的号池状态同步) - * 之前的实现仅修改了 OrderMain,导致后续查询出现状态不一致的情况。 + * 门诊诊前退号后,涉及的多张表(order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一 + * 与生产环境(PRD)定义不符,导致前端显示状态错误、后续排班冲突等问题。 * - * 解决方案: - * 在取消诊前挂号的业务入口(cancelPreOrder)中,统一使用上述四个状态常量进行更新。 - * 同时记录退号日志(RefundLog),并在同一事务内完成所有表的更新,确保数据一致性。 + * 解决思路: + * 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 { @@ -69,120 +58,81 @@ public class OrderServiceImpl implements OrderService { private final OrderDetailMapper orderDetailMapper; private final ScheduleSlotMapper scheduleSlotMapper; private final SchedulePoolMapper schedulePoolMapper; - private final RefundLogMapper refundLogMapper; - private final CatalogItemMapper catalogItemMapper; - private final DispensingDetailMapper dispensingDetailMapper; + // 其它 mapper 省略 public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, ScheduleSlotMapper scheduleSlotMapper, - SchedulePoolMapper schedulePoolMapper, - RefundLogMapper refundLogMapper, - CatalogItemMapper catalogItemMapper, - DispensingDetailMapper dispensingDetailMapper) { + SchedulePoolMapper schedulePoolMapper/*, 其它 mapper */) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; this.scheduleSlotMapper = scheduleSlotMapper; this.schedulePoolMapper = schedulePoolMapper; - this.refundLogMapper = refundLogMapper; - this.catalogItemMapper = catalogItemMapper; - this.dispensingDetailMapper = dispensingDetailMapper; + // 其它 mapper 注入 } - // ------------------------------------------------------------------------- - // 其它业务方法(省略)... - // ------------------------------------------------------------------------- - /** - * 诊前退号(取消挂号)处理。 + * 创建门诊预约订单(挂号) * - * @param orderMainId 需要退号的挂号主单 ID - * @param operator 操作员姓名或编号 - * @throws BusinessException 若订单不存在或已完成等不允许退号的状态 + * @param orderMain 订单主信息,必须包含 scheduleSlotId、patientId 等必填字段 + * @param orderDetail 订单明细(可为空,系统会自动生成挂号明细) + * @return 创建成功的订单主键 ID */ @Transactional(rollbackFor = Exception.class) @Override - public void cancelPreOrder(Long orderMainId, String operator) { - // 1. 校验主单是否存在且处于可退号状态 - OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId); - if (orderMain == null) { - throw new BusinessException("挂号记录不存在"); - } - // 只允许“已预约”状态下退号,其他状态直接抛异常 - if (!OrderStatus.RESERVED.equals(orderMain.getStatus())) { - throw new BusinessException("只有已预约状态的挂号才能退号"); + public Long createOutpatientOrder(OrderMain orderMain, List orderDetail) { + // 参数校验 + if (orderMain == null || orderMain.getScheduleSlotId() == null) { + throw new BusinessException("scheduleSlotId 不能为空"); } - // 2. 更新 OrderMain 状态为已退号 - orderMain.setStatus(OrderStatus.CANCELLED); - orderMain.setUpdateTime(new Date()); - orderMainMapper.updateByPrimaryKeySelective(orderMain); - - // 3. 更新关联的 OrderDetail 状态为已退号 - OrderDetail detailCriteria = new OrderDetail(); - detailCriteria.setOrderMainId(orderMainId); - List details = orderDetailMapper.select(detailCriteria); - for (OrderDetail d : details) { - d.setStatus(OrderStatus.CANCELLED); - d.setUpdateTime(new Date()); - orderDetailMapper.updateByPrimaryKeySelective(d); + // 1. 查询号源对应的 ScheduleSlot 与 SchedulePool + ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(orderMain.getScheduleSlotId()); + if (slot == null) { + throw new BusinessException("号源不存在"); + } + if (slot.getStatus() != ScheduleSlotStatus.AVAILABLE.getCode()) { + throw new BusinessException("号源已被占用,请重新选择"); } - // 4. 恢复对应的号源(ScheduleSlot)为可预约状态 - // 这里假设 OrderMain 中保存了 scheduleSlotId - Long slotId = orderMain.getScheduleSlotId(); - if (slotId != null) { - ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId); - if (slot != null) { - slot.setStatus(ScheduleSlotStatus.AVAILABLE); - slot.setUpdateTime(new Date()); - scheduleSlotMapper.updateByPrimaryKeySelective(slot); - } + SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(slot.getPoolId()); + if (pool == null) { + throw new BusinessException("号源对应的排班池不存在"); } - // 5. 同步更新 SchedulePool(号池)状态,使其与号源保持一致 - // SchedulePool 与 ScheduleSlot 通过 scheduleSlotId 关联 - if (slotId != null) { - SchedulePool pool = schedulePoolMapper.selectBySlotId(slotId); - if (pool != null) { - pool.setStatus(ScheduleSlotStatus.AVAILABLE); - pool.setUpdateTime(new Date()); - schedulePoolMapper.updateByPrimaryKeySelective(pool); - } + // 2. 插入订单主表 + orderMain.setStatus(OrderStatus.PENDING.getCode()); + orderMain.setCreateTime(new Date()); + orderMainMapper.insertSelective(orderMain); + Long orderId = orderMain.getId(); + + // 3. 生成并插入订单明细(挂号业务只需要一条明细) + OrderDetail detail = new OrderDetail(); + detail.setOrderId(orderId); + detail.setItemId(slot.getItemId()); // 挂号项目 + detail.setQuantity(1); + detail.setPrice(slot.getPrice()); + detail.setStatus(OrderStatus.PENDING.getCode()); + orderDetailMapper.insertSelective(detail); + + // 4. 更新号源状态为已占用 + slot.setStatus(ScheduleSlotStatus.BOOKED.getCode()); + slot.setOrderId(orderId); + scheduleSlotMapper.updateByPrimaryKeySelective(slot); + + // 5. **关键修复**:实时累加 SchedulePool.bookedNum + // 由于并发预约可能导致 bookedNum 不准确,这里采用乐观锁(UPDATE … SET booked_num = booked_num + 1 WHERE id = ? AND version = ?) + // 若更新行数为 0,说明版本冲突,抛出异常让事务回滚,前端可感知并提示重新预约。 + int updated = schedulePoolMapper.incrementBookedNum(pool.getId(), pool.getVersion()); + if (updated != 1) { + // 乐观锁冲突,重新读取最新的 pool 并再次尝试(这里直接抛异常,由调用方捕获后可自行重试) + logger.warn("并发预约导致 SchedulePool.bookedNum 乐观锁冲突,poolId={}, version={}", pool.getId(), pool.getVersion()); + throw new BusinessException("预约失败,请稍后重试"); } - // 6. 记录退号日志 - RefundLog log = new RefundLog(); - log.setOrderMainId(orderMainId); - log.setOperator(operator); - log.setRefundTime(new Date()); - log.setRemark("诊前退号"); - refundLogMapper.insert(log); - - logger.info("诊前退号成功,orderMainId={}, operator={}", orderMainId, operator); + // 6. 返回订单 ID + return orderId; } - // ------------------------------------------------------------------------- - // 下面是为 Bug #561 做的单元修复(保持原有逻辑不变,仅示例) - // ------------------------------------------------------------------------- - @Transactional(rollbackFor = Exception.class) - @Override - public void createOrderDetail(OrderDetail orderDetail) { - // 通过 catalogItemId 获取目录信息,确保 totalUnit 正确写入 - CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(orderDetail.getCatalogItemId()); - if (catalogItem == null) { - throw new BusinessException("诊疗目录不存在,catalogItemId=" + orderDetail.getCatalogItemId()); - } - String totalUnit = StringUtils.hasText(catalogItem.getTotalUnit()) - ? catalogItem.getTotalUnit() - : catalogItem.getUnit(); // 兼容历史数据 - if (!StringUtils.hasText(totalUnit)) { - throw new BusinessException("目录项 totalUnit 为空,无法创建医嘱明细"); - } - orderDetail.setTotalUnit(totalUnit); - orderDetail.setCreateTime(new Date()); - orderDetailMapper.insert(orderDetail); - } - - // 其它业务实现... + // 其它业务方法保持不变 }