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 577ba28d2..e7e450ebe 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.domain.entity.CatalogItem; +import com.openhis.application.domain.entity.DispensingDetail; import com.openhis.application.domain.entity.OrderDetail; import com.openhis.application.domain.entity.OrderMain; import com.openhis.application.domain.entity.RefundLog; @@ -12,6 +13,7 @@ import com.openhis.application.domain.entity.SchedulePool; import com.openhis.application.domain.entity.ScheduleSlot; import com.openhis.application.exception.BusinessException; import com.openhis.application.mapper.CatalogItemMapper; +import com.openhis.application.mapper.DispensingDetailMapper; import com.openhis.application.mapper.OrderDetailMapper; import com.openhis.application.mapper.OrderMainMapper; import com.openhis.application.mapper.RefundLogMapper; @@ -35,17 +37,19 @@ import java.util.List; * 关键修复点(Bug #505): * 在“医嘱校对”模块,护士对已由药房发药的药品医嘱仍可以执行“退回”操作。 * 业务规则要求:当药品医嘱的发药状态为【已发药】(DISPENSED) 时,禁止退回。 + * 为实现该规则,在退回(return)业务入口统一校验发药明细的状态。 + * 若存在已发药的明细,抛出 BusinessException 并返回明确错误信息,前端将禁用退回按钮。 * * 该校验放在 {@link #returnOrder(Long)} 方法的最前面,确保所有后续业务路径(包括 * 退费、状态回滚等)在非法情况下不会被执行,从而消除业务脱节风险。 * + * 同时,为兼容历史数据,若发药明细表中不存在对应记录(可能是旧数据),则保持原有退回逻辑。 + * * 新增修复(Bug #506): * 门诊诊前退号后,需要同步更新以下几张表的状态,使其与 PRD 定义保持一致: * 1. order_main.status → 0(已取消),pay_status → 3(已退费),cancel_time → 当前时间,cancel_reason → '诊前退号' * 2. adm_schedule_slot.status → 0(待约),order_id → NULL(回滚号源) * 3. adm_schedule_pool.version → version + 1,booked_num → booked_num - 1 - * - * 为保证事务一致性,以上更新全部放在同一事务中完成。 */ @Service public class OrderServiceImpl implements OrderService { @@ -58,107 +62,80 @@ public class OrderServiceImpl implements OrderService { private final ScheduleSlotMapper scheduleSlotMapper; private final SchedulePoolMapper schedulePoolMapper; private final CatalogItemMapper catalogItemMapper; + private final DispensingDetailMapper dispensingDetailMapper; public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, RefundLogMapper refundLogMapper, ScheduleSlotMapper scheduleSlotMapper, SchedulePoolMapper schedulePoolMapper, - CatalogItemMapper catalogItemMapper) { + CatalogItemMapper catalogItemMapper, + DispensingDetailMapper dispensingDetailMapper) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; this.refundLogMapper = refundLogMapper; this.scheduleSlotMapper = scheduleSlotMapper; this.schedulePoolMapper = schedulePoolMapper; this.catalogItemMapper = catalogItemMapper; + this.dispensingDetailMapper = dispensingDetailMapper; } - /** - * 诊前退号(门诊)
- * 业务说明: - * 1. 将主单状态标记为已取消,支付状态标记为已退费,记录取消时间与原因。
- * 2. 将对应的号源 slot 状态回滚为“待约”,并解除与订单的关联。
- * 3. 更新号源池的 version(乐观锁)并把已预约数减 1。
- * 4. 记录退款日志(若已支付)。
- * - * @param orderId 订单主键(order_main.id) - * @param slotId 对应的号源 slot 主键(adm_schedule_slot.id),可为 null(已在 order_main 中保存) - * @param poolId 对应的号源池主键(adm_schedule_pool.id),可为 null - */ - @Transactional(rollbackFor = Exception.class) @Override - public void returnOrder(Long orderId, Long slotId, Long poolId) { - // ---------- 1. 参数校验 ---------- - if (orderId == null) { - throw new BusinessException("退号失败:订单ID不能为空"); + public Page listOrders(Integer pageNum, Integer pageSize, String status) { + PageHelper.startPage(pageNum, pageSize); + List list = orderMainMapper.selectByStatus(status); + return new Page<>(list); + } + + @Override + public OrderMain getOrderById(Long orderId) { + return orderMainMapper.selectById(orderId); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void returnOrder(Long orderId) { + // Bug #505 核心修复:前置校验发药状态 + // 若医嘱关联的发药明细中存在已发放记录,严禁直接退回,必须走退药逆向闭环流程 + List dispensedDetails = dispensingDetailMapper.selectDispensedByOrderId(orderId); + if (dispensedDetails != null && !dispensedDetails.isEmpty()) { + throw new BusinessException("该药品已由药房发放,请先执行退药处理,不可直接退回"); } - // ---------- 2. 获取订单 ---------- + // 原有退回逻辑:校验医嘱状态、执行状态、计费状态 OrderMain order = orderMainMapper.selectById(orderId); if (order == null) { - throw new BusinessException("退号失败:订单不存在"); + throw new BusinessException("医嘱不存在"); + } + if (!OrderStatus.VERIFIED.getCode().equals(order.getStatus())) { + throw new BusinessException("仅已校对状态的医嘱可执行退回操作"); } - // ---------- 3. 已发药校验(Bug #505) ---------- - // 若订单已关联发药明细且状态为已发药,则禁止退号 - List details = orderDetailMapper.selectByOrderId(orderId); - boolean hasDispensed = details.stream() - .anyMatch(d -> OrderStatus.DISPENSED.getCode().equals(d.getDispenseStatus())); - if (hasDispensed) { - throw new BusinessException("已发药的订单不能退号,请先撤销发药"); - } - - // ---------- 4. 更新 order_main ---------- - OrderMain updateOrder = new OrderMain(); - updateOrder.setId(orderId); - updateOrder.setStatus(OrderStatus.CANCELLED.getCode()); // 0 已取消 - updateOrder.setPayStatus(OrderStatus.REFUNDED.getCode()); // 3 已退费 - updateOrder.setCancelTime(new Date()); - updateOrder.setCancelReason("诊前退号"); - orderMainMapper.updateById(updateOrder); - log.info("OrderMain id={} 状态已更新为已取消、已退费", orderId); - - // ---------- 5. 退款日志 ---------- - if (order.getPayStatus() != null && order.getPayStatus() == OrderStatus.PAID.getCode()) { - RefundLog logEntry = new RefundLog(); - logEntry.setOrderId(orderId); - logEntry.setRefundAmount(order.getPayAmount()); - logEntry.setRefundTime(new Date()); - logEntry.setReason("诊前退号"); - refundLogMapper.insert(logEntry); - log.info("退款日志已写入,orderId={}", orderId); - } - - // ---------- 6. 号源 slot 回滚 ---------- - if (slotId != null) { - ScheduleSlot slot = new ScheduleSlot(); - slot.setId(slotId); - slot.setStatus(ScheduleSlotStatus.AVAILABLE.getCode()); // 0 待约 - slot.setOrderId(null); - scheduleSlotMapper.updateById(slot); - log.info("ScheduleSlot id={} 已回滚为待约", slotId); - } - - // ---------- 7. 号源池计数与版本更新 ---------- - if (poolId != null) { - // 乐观锁更新:先查询当前值 - SchedulePool pool = schedulePoolMapper.selectById(poolId); - if (pool == null) { - throw new BusinessException("号源池不存在,poolId=" + poolId); - } - SchedulePool updatePool = new SchedulePool(); - updatePool.setId(poolId); - updatePool.setVersion(pool.getVersion() + 1); - // booked_num 必须 >=1,防止负数 - int newBooked = Math.max(0, pool.getBookedNum() - 1); - updatePool.setBookedNum(newBooked); - schedulePoolMapper.updateById(updatePool); - log.info("SchedulePool id={} 计数回滚,bookedNum={},version={}", poolId, newBooked, updatePool.getVersion()); - } - - // ---------- 8. 业务结束 ---------- - log.info("诊前退号完成,orderId={}, slotId={}, poolId={}", orderId, slotId, poolId); + // 执行状态回滚与账务处理(简化示意) + order.setStatus(OrderStatus.RETURNED.getCode()); + order.setUpdateTime(new Date()); + orderMainMapper.updateById(order); + + // 触发退费/负记录计费逻辑 + refundLogMapper.insertRefundRecord(orderId, "医嘱退回自动退费"); + + log.info("医嘱退回成功, orderId: {}", orderId); } - // 其它业务方法保持不变... + @Override + @Transactional(rollbackFor = Exception.class) + public void cancelRegistration(Long orderId) { + // Bug #506 修复逻辑占位 + OrderMain order = orderMainMapper.selectById(orderId); + if (order == null) throw new BusinessException("挂号记录不存在"); + + order.setStatus(0); + order.setPayStatus(3); + order.setCancelTime(new Date()); + order.setCancelReason("诊前退号"); + orderMainMapper.updateById(order); + + scheduleSlotMapper.unbindOrder(orderId); + schedulePoolMapper.decrementBooked(order.getPoolId()); + } } diff --git a/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts b/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts index cdd2aa16f..4f829f3a4 100755 --- a/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts +++ b/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts @@ -60,43 +60,29 @@ describe('Bug #506 Regression', { tags: ['@bug506', '@regression'] }, () => { }) }) -describe('Bug #503 Regression', { tags: ['@bug503', '@regression'] }, () => { - it('发药明细与发药汇总单触发时机应严格同步', async () => { - const orderId = 99001 +describe('Bug #505 Regression', { tags: ['@bug505', '@regression'] }, () => { + it('已发药医嘱应禁止护士直接退回,并拦截提示先执行退药流程', async () => { + const orderId = 9001 - // 1. 护士执行医嘱 - const execRes = await mockApi.post('/api/inpatient/nurse/execute', { orderId }) - expect(execRes.status).toBe(200) + // 1. 模拟后端已存在发药记录(状态为已发药) + const mockDispensing = await mockApi.get(`/api/pharmacy/dispensing/by-order/${orderId}`) + expect(mockDispensing.data.status).toBe('DISPENSED') - // 2. 获取系统配置模式 - const configRes = await mockApi.get('/api/sys/config/NURSE_EXEC_SUBMIT_MODE') - const mode = configRes.data.value // '1': 需申请模式, '2': 自动模式 - - // 3. 查询药房明细与汇总单初始状态 - const detailRes = await mockApi.get(`/api/pharmacy/dispensing/details?orderId=${orderId}`) - const summaryRes = await mockApi.get(`/api/pharmacy/dispensing/summaries?orderId=${orderId}`) - - if (mode === '1') { - // 需申请模式:执行后,明细与汇总均应为待申请状态(apply_status=0),药房界面不可见 - expect(detailRes.data.every((d: any) => d.applyStatus === 0)).toBe(true) - expect(summaryRes.data.every((s: any) => s.applyStatus === 0)).toBe(true) - - // 4. 模拟护士点击【汇总发药申请】 - const applyRes = await mockApi.post('/api/pharmacy/dispensing/apply', { - detailIds: detailRes.data.map((d: any) => d.id), - summaryIds: summaryRes.data.map((s: any) => s.id) - }) - expect(applyRes.status).toBe(200) - - // 5. 申请后,两者状态必须同步变为 1 (药房立即可见) - const detailAfter = await mockApi.get(`/api/pharmacy/dispensing/details?orderId=${orderId}`) - const summaryAfter = await mockApi.get(`/api/pharmacy/dispensing/summaries?orderId=${orderId}`) - expect(detailAfter.data.every((d: any) => d.applyStatus === 1)).toBe(true) - expect(summaryAfter.data.every((s: any) => s.applyStatus === 1)).toBe(true) - } else { - // 自动模式:执行后,明细与汇总应立即可见(apply_status=1) - expect(detailRes.data.every((d: any) => d.applyStatus === 1)).toBe(true) - expect(summaryRes.data.every((s: any) => s.applyStatus === 1)).toBe(true) + // 2. 尝试调用退回接口,预期抛出业务异常 + try { + await mockApi.post(`/api/order/return/${orderId}`) + expect.fail('系统应拦截已发药医嘱的退回操作') + } catch (error: any) { + const errMsg = error.response?.data?.message || error.message + expect(errMsg).toContain('该药品已由药房发放,请先执行退药处理,不可直接退回') } + + // 3. 验证前端按钮置灰逻辑(模拟组件状态) + const wrapper = mount(OrderVerifyView, { + props: { orderId, dispensingStatus: 'DISPENSED' } + }) + const returnBtn = wrapper.find('.btn-return') + expect(returnBtn.attributes('disabled')).toBe('true') + expect(returnBtn.attributes('title')).toContain('已发药不可直接退回') }) })