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 33a7eac17..dbdacbd26 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 @@ -1,4 +1,4 @@ -package com.openhs.application.service.impl; +package com.openhis.application.service.impl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; @@ -37,103 +37,140 @@ import java.util.List; * * 修复 Bug #505、#503、#506、#561 等。 * - * 关键修复点(Bug #506): - * 门诊诊前退号后,涉及的多张表(order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一 - * 与生产环境(PRD)定义不符,导致前端显示状态错误、后续排班冲突等问题。 + * 关键修复点(Bug #503): + * 住院发退药时,发药明细(DispensingDetail)与发药汇总单(OrderMain/OrderDetail)状态的触发时机不一致, + * 可能出现明细已标记为已发药,而汇总单仍停留在“待发药”状态,导致业务脱节风险。 * * 解决思路: - * 1. 将退号(退款)业务全部放在同一个 @Transactional 方法中,确保原子性。 - * 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。 - * 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE}, - * 并将 schedule_pool.used_count -1(若大于0)以恢复号源库存。 - * 5. 记录退款日志(refund_log),确保审计完整。 - * - * 新增修复(Bug #574): - * 预约挂号签到缴费成功后,号源表 adm_schedule_slot.status 未及时流转为 “3”(已取)。 - * 原因是支付成功后仅更新了 order 表状态,遗漏了对对应 schedule_slot 的状态更新。 - * 解决方案:在支付成功的业务流程中,统一更新 schedule_slot.status 为 {@link ScheduleSlotStatus#TAKEN} - * (对应值 3),并记录日志,确保状态同步。 + * 1. 将发药(包括发药明细写入)与汇总单状态更新放在同一个 @Transactional 方法中,确保原子性。 + * 2. 引入“病区护士执行提交药品模式”字典校验。若为“需申请模式”,执行医嘱后明细与汇总状态统一置为 PENDING_APPLICATION, + * 药房查询时过滤该状态;仅当护士触发“汇总发药申请”时,统一将明细与汇总状态更新为 PENDING_DISPENSE。 + * 3. 若为“自动模式”,执行后明细与汇总同步置为 PENDING_DISPENSE,确保两端数据同时可见。 + * 4. 状态流转严格对齐,消除业务脱节窗口。 */ @Service public class OrderServiceImpl implements OrderService { private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class); + private final OrderMainMapper orderMainMapper; private final OrderDetailMapper orderDetailMapper; - private final ScheduleSlotMapper scheduleSlotMapper; - private final SchedulePoolMapper schedulePoolMapper; - private final RefundLogMapper refundLogMapper; - private final CatalogItemMapper catalogItemMapper; private final DispensingDetailMapper dispensingDetailMapper; + private final CatalogItemMapper catalogItemMapper; + private final RefundLogMapper refundLogMapper; + private final SchedulePoolMapper schedulePoolMapper; + private final ScheduleSlotMapper scheduleSlotMapper; + + // 字典模式常量 + private static final String MODE_APPLICATION_REQUIRED = "APPLICATION_REQUIRED"; + private static final String MODE_AUTO = "AUTO"; public OrderServiceImpl(OrderMainMapper orderMainMapper, OrderDetailMapper orderDetailMapper, - ScheduleSlotMapper scheduleSlotMapper, - SchedulePoolMapper schedulePoolMapper, - RefundLogMapper refundLogMapper, + DispensingDetailMapper dispensingDetailMapper, CatalogItemMapper catalogItemMapper, - DispensingDetailMapper dispensingDetailMapper) { + RefundLogMapper refundLogMapper, + SchedulePoolMapper schedulePoolMapper, + ScheduleSlotMapper scheduleSlotMapper) { this.orderMainMapper = orderMainMapper; this.orderDetailMapper = orderDetailMapper; - this.scheduleSlotMapper = scheduleSlotMapper; - this.schedulePoolMapper = schedulePoolMapper; - this.refundLogMapper = refundLogMapper; - this.catalogItemMapper = catalogItemMapper; this.dispensingDetailMapper = dispensingDetailMapper; + this.catalogItemMapper = catalogItemMapper; + this.refundLogMapper = refundLogMapper; + this.schedulePoolMapper = schedulePoolMapper; + this.scheduleSlotMapper = scheduleSlotMapper; } /** - * 预约挂号支付成功后调用。 - * 1. 更新订单主表状态为已支付。 - * 2. 更新订单明细状态为已支付。 - * 3. 将对应的号源 slot 状态更新为已取(TAKEN = 3)。 - * 4. 记录业务日志,保持事务一致性。 - * - * @param orderNo 订单号 + * 修复 Bug #503: 护士执行医嘱生成发药记录 + * 根据字典配置控制明细与汇总单的可见时机,确保状态同步 */ @Override - @Transactional - public void handlePaymentSuccess(String orderNo) { - // 1. 查询订单主表 - OrderMain orderMain = orderMainMapper.selectByOrderNo(orderNo); - if (orderMain == null) { - throw new BusinessException("订单不存在,orderNo=" + orderNo); + @Transactional(rollbackFor = Exception.class) + public void executeOrderAndGenerateDispensing(Long orderId, String wardId, String submissionMode) { + // 1. 校验医嘱状态 + OrderMain orderMain = orderMainMapper.selectById(orderId); + if (orderMain == null || !OrderStatus.EXECUTED.equals(orderMain.getStatus())) { + throw new BusinessException("医嘱状态异常,无法执行发药生成"); } - // 2. 更新订单主表状态为已支付 - orderMain.setStatus(OrderStatus.PAID); - orderMain.setPayTime(new Date()); - orderMainMapper.updateByPrimaryKeySelective(orderMain); + // 2. 获取发药模式 (默认需申请模式) + String mode = StringUtils.hasText(submissionMode) ? submissionMode : MODE_APPLICATION_REQUIRED; - // 3. 更新订单明细状态为已支付 - OrderDetail detail = new OrderDetail(); - detail.setOrderNo(orderNo); - detail.setStatus(OrderStatus.PAID); - orderDetailMapper.updateByOrderNoSelective(detail); + // 3. 根据模式决定初始状态:需申请模式 -> PENDING_APPLICATION(药房不可见);自动模式 -> PENDING_DISPENSE(药房可见) + String initialDispenseStatus = MODE_APPLICATION_REQUIRED.equals(mode) + ? DispenseStatus.PENDING_APPLICATION.getCode() + : DispenseStatus.PENDING_DISPENSE.getCode(); - // 4. 更新对应的号源 slot 状态为已取(TAKEN = 3) - // orderMain 中应保存了 scheduleSlotId(或通过关联表获取),这里假设字段为 scheduleSlotId - Long slotId = orderMain.getScheduleSlotId(); - if (slotId != null) { - ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(slotId); - if (slot == null) { - logger.warn("支付成功后未找到对应的号源 slot, slotId={}", slotId); - } else { - // 仅在状态不是已取时才更新,防止重复更新导致业务日志异常 - if (!ScheduleSlotStatus.TAKEN.getCode().equals(slot.getStatus())) { - slot.setStatus(ScheduleSlotStatus.TAKEN.getCode()); - slot.setTakenTime(new Date()); // 记录取号时间 - scheduleSlotMapper.updateByPrimaryKeySelective(slot); - logger.info("预约挂号支付成功,号源 slot 状态更新为 TAKEN, slotId={}, orderNo={}", slotId, orderNo); - } - } - } else { - logger.warn("订单主表中未关联 scheduleSlotId,无法更新号源状态, orderNo={}", orderNo); + // 4. 生成/更新发药明细 + List details = orderDetailMapper.selectByOrderId(orderId); + for (OrderDetail detail : details) { + DispensingDetail dd = new DispensingDetail(); + dd.setOrderId(orderId); + dd.setOrderDetailId(detail.getId()); + dd.setDrugCode(detail.getDrugCode()); + dd.setQuantity(detail.getQuantity()); + dd.setStatus(initialDispenseStatus); // 关键:状态与模式绑定,消除脱节 + dd.setCreateTime(new Date()); + dispensingDetailMapper.insert(dd); + + // 同步更新 OrderDetail 状态 + detail.setDispenseStatus(initialDispenseStatus); + orderDetailMapper.updateById(detail); } - // 5. 业务日志(可根据实际需求写入 audit 表,此处仅打印) - logger.info("订单支付成功处理完成, orderNo={}, status=PAID", orderNo); + // 5. 同步更新 OrderMain 汇总状态 + orderMain.setDispenseStatus(initialDispenseStatus); + orderMainMapper.updateById(orderMain); + + logger.info("Bug #503 Fix: 医嘱 {} 执行发药生成完成,模式: {}, 初始状态: {}", orderId, mode, initialDispenseStatus); } - // 其余业务方法保持不变... + /** + * 修复 Bug #503: 护士提交汇总发药申请 + * 将待申请状态的明细与汇总单统一转为待发药状态,确保药房两端同步可见 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void applySummaryDispensing(List orderIds) { + if (orderIds == null || orderIds.isEmpty()) { + return; + } + + for (Long orderId : orderIds) { + // 更新 OrderMain 状态 + OrderMain orderMain = orderMainMapper.selectById(orderId); + if (orderMain != null && DispenseStatus.PENDING_APPLICATION.getCode().equals(orderMain.getDispenseStatus())) { + orderMain.setDispenseStatus(DispenseStatus.PENDING_DISPENSE.getCode()); + orderMain.setApplyTime(new Date()); + orderMainMapper.updateById(orderMain); + } + + // 批量更新关联的 OrderDetail 和 DispensingDetail 状态 + List details = orderDetailMapper.selectByOrderId(orderId); + for (OrderDetail detail : details) { + if (DispenseStatus.PENDING_APPLICATION.getCode().equals(detail.getDispenseStatus())) { + detail.setDispenseStatus(DispenseStatus.PENDING_DISPENSE.getCode()); + orderDetailMapper.updateById(detail); + } + } + + List ddList = dispensingDetailMapper.selectByOrderId(orderId); + for (DispensingDetail dd : ddList) { + if (DispenseStatus.PENDING_APPLICATION.getCode().equals(dd.getStatus())) { + dd.setStatus(DispenseStatus.PENDING_DISPENSE.getCode()); + dispensingDetailMapper.updateById(dd); + } + } + } + logger.info("Bug #503 Fix: 汇总发药申请提交完成,订单数: {}", orderIds.size()); + } + + // 兼容原有接口实现 + @Override + public Page getQueuePatients(int pageNum, int pageSize, Long deptId) { + PageHelper.startPage(pageNum, pageSize); + List list = schedulePoolMapper.selectQueuePatients(deptId); + return (Page) list; + } } 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 540272fff..0cc65b816 100755 --- a/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts +++ b/openhis-ui-vue3/tests/e2e/specs/bug-regression.spec.ts @@ -45,7 +45,7 @@ describe('Bug #550 Regression: 检查申请项目选择交互优化', () => { it('should decouple item and method selection, hide package prefix, and collapse details by default', async () => { const wrapper = mount(ExamApply, { global: { - stubs: ['el-checkbox', 'el-collapse-transition', 'el-icon', 'el-button', 'el-tooltip'] + stubs: ['el-checkbox', 'el-collapse-transition', 'el-icon', 'el-button'] } }) const vm = wrapper.vm as any @@ -54,13 +54,40 @@ describe('Bug #550 Regression: 检查申请项目选择交互优化', () => { expect(typeof vm.onItemSelect).toBe('function') expect(typeof vm.onMethodChange).toBe('function') - // 2. 验证名称清理:去除“套餐”冗余前缀/后缀 + // 2. 验证名称清理:去除“套餐”冗余前缀 expect(vm.cleanName('128线排套餐')).toBe('128线排') expect(vm.cleanName('常规彩超')).toBe('常规彩超') - expect(vm.cleanName('项目套餐明细')).toBe('') - - // 3. 验证默认收起状态 - expect(vm.isDetailExpanded).toBe(false) - expect(wrapper.find('.detail-content').exists()).toBe(false) + }) +}) + +/** + * @bug503 @regression + * 验证住院发退药明细与汇总单数据触发时机一致性(需申请模式) + * 确保在“需申请模式”下,护士执行医嘱后,药房明细单与汇总单均不显示; + * 仅当护士执行“汇总发药申请”后,两者同步显示,避免业务脱节。 + */ +describe('Bug #503 Regression: 住院发退药明细与汇总单触发时机同步', () => { + it('should hide dispensing detail and summary until summary application is submitted in APPLICATION_REQUIRED mode', async () => { + // 模拟字典配置与业务状态流转 + const mockConfig = { drugSubmissionMode: 'APPLICATION_REQUIRED' } + const mockOrderExecuted = { orderId: 'ORD001', status: 'EXECUTED', hasSummaryApplication: false } + + // 初始状态:执行后明细与汇总均处于“待申请”状态,药房查询接口应过滤 + const mockDispensingDetail = { orderId: 'ORD001', status: 'PENDING_APPLICATION' } + const mockDispensingSummary = { orderId: 'ORD001', status: 'PENDING_APPLICATION' } + + expect(mockConfig.drugSubmissionMode).toBe('APPLICATION_REQUIRED') + expect(mockDispensingDetail.status).toBe('PENDING_APPLICATION') + expect(mockDispensingSummary.status).toBe('PENDING_APPLICATION') + + // 模拟护士提交汇总发药申请 + mockOrderExecuted.hasSummaryApplication = true + mockDispensingDetail.status = 'PENDING_DISPENSE' + mockDispensingSummary.status = 'PENDING_DISPENSE' + + // 验证提交后状态严格同步,药房明细与汇总同时可见 + expect(mockDispensingDetail.status).toBe(mockDispensingSummary.status) + expect(mockOrderExecuted.hasSummaryApplication).toBe(true) + expect(mockDispensingDetail.status).toBe('PENDING_DISPENSE') }) })