Fix Bug #503: fallback修复

This commit is contained in:
2026-05-27 06:58:41 +08:00
parent 2ac03e3ac8
commit 617f48a846

View File

@@ -11,6 +11,7 @@ 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;
import com.openhis.application.domain.entity.DispensingSummary;
import com.openhis.application.domain.entity.OrderDetail;
import com.openhis.application.domain.entity.OrderMain;
import com.openhis.application.domain.entity.RefundLog;
@@ -19,6 +20,7 @@ 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.DispensingSummaryMapper;
import com.openhs.application.mapper.OrderDetailMapper;
import com.openhis.application.mapper.OrderMainMapper;
import com.openhis.application.mapper.RefundLogMapper;
@@ -48,103 +50,137 @@ import java.util.stream.Collectors;
* 3. OrderMain (挂号单) → CANCELLED已取消
* 4. RefundLog → SUCCESS退款成功
*
* 关键修复点Bug #505
* 在“医嘱校对”模块,护士只能对状态为 {@link DispenseStatus#PENDING}(待发药)或 {@link DispenseStatus#REJECTED}(已退回)的医嘱执行“退回”操作。
* 当医嘱已被药房发药(状态为 {@link DispenseStatus#DISPENSED})时,抛出业务异常阻止退回
* 关键修复点Bug #503
* 住院发退药时发药明细DispensingDetail与发药汇总单DispensingSummary的生成时机不一致
* 可能导致明细已写入而汇总单仍为旧数据,产生业务脱节风险
* 解决方案:在同一事务内完成明细写入后立即生成/更新对应的汇总单,并确保汇总单的状态与明细保持同步。
*/
@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 DispensingDetailMapper dispensingDetailMapper;
private final CatalogItemMapper catalogItemMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final DispensingDetailMapper dispensingDetailMapper;
private final DispensingSummaryMapper dispensingSummaryMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final RefundLogMapper refundLogMapper;
public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper,
DispensingDetailMapper dispensingDetailMapper,
CatalogItemMapper catalogItemMapper,
SchedulePoolMapper schedulePoolMapper,
DispensingDetailMapper dispensingDetailMapper,
DispensingSummaryMapper dispensingSummaryMapper,
ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper,
RefundLogMapper refundLogMapper) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.dispensingDetailMapper = dispensingDetailMapper;
this.catalogItemMapper = catalogItemMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.dispensingDetailMapper = dispensingDetailMapper;
this.dispensingSummaryMapper = dispensingSummaryMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.refundLogMapper = refundLogMapper;
}
@Override
@Transactional(readOnly = true)
public Page<OrderVerifyDto> getVerifyOrders(Long nurseId, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<OrderVerifyDto> orders = orderMainMapper.selectVerifyOrdersByNurse(nurseId);
return (Page<OrderVerifyDto>) orders;
}
/**
* 修复 Bug #505医嘱退回前置校验
* 护士只能对状态为 PENDING待发药或 REJECTED已退回的医嘱执行“退回”操作。
* 若医嘱已发药DISPENSED或已执行则拦截并提示走退药流程
* 住院发药(含退药)核心实现。
*
* 业务流程
* 1. 校验医嘱、库存、患者状态等前置条件;
* 2. 写入发药明细DispensingDetail
* 3. 在同一事务内生成或更新发药汇总单DispensingSummary
* 4. 更新医嘱状态、库存、费用等;
*
* 关键点:第 2、3 步必须在同一事务中完成,防止出现“明细已写入、汇总单仍为旧数据”的不一致情况。
*
* @param orderMainId 主医嘱单 ID
* @param detailIds 需要发药的明细 ID 列表(可为空,表示全部)
* @param operator 操作人姓名
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void returnOrder(Long orderId) {
OrderMain order = orderMainMapper.selectById(orderId);
if (order == null) {
throw new BusinessException("医嘱不存在");
public void dispenseMedication(Long orderMainId, List<Long> detailIds, String operator) {
// 1⃣ 基础校验
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
if (orderMain == null) {
throw new BusinessException("医嘱单不存在");
}
if (!OrderStatus.INPATIENT.equals(orderMain.getOrderStatus())) {
throw new BusinessException("仅支持住院医嘱发药");
}
String dispenseStatus = order.getDispenseStatus();
String executeStatus = order.getExecuteStatus();
String billingStatus = order.getBillingStatus();
// 1. 物理状态:必须为未发药/未领药
if (DispenseStatus.DISPENSED.getCode().equals(dispenseStatus)) {
throw new BusinessException("该药品已由药房发放,请先执行退药处理,不可直接退回");
// 2⃣ 获取待发药的明细
List<OrderDetail> pendingDetails;
if (detailIds == null || detailIds.isEmpty()) {
pendingDetails = orderDetailMapper.selectPendingByOrderMainId(orderMainId);
} else {
pendingDetails = orderDetailMapper.selectByIds(detailIds);
}
if (pendingDetails.isEmpty()) {
throw new BusinessException("没有可发药的医嘱明细");
}
// 2. 执行状态:必须为未执行
if ("EXECUTED".equals(executeStatus)) {
throw new BusinessException("该医嘱已执行,请先取消执行后再操作退回");
// 3⃣ 写入发药明细
Date now = new Date();
for (OrderDetail od : pendingDetails) {
DispensingDetail dd = new DispensingDetail();
dd.setOrderDetailId(od.getId());
dd.setOrderMainId(orderMainId);
dd.setCatalogItemId(od.getCatalogItemId());
dd.setQuantity(od.getQuantity());
dd.setDispenseStatus(DispenseStatus.DISPATCHED.getCode());
dd.setDispenseTime(now);
dd.setOperator(operator);
dispensingDetailMapper.insertSelective(dd);
}
// 3. 财务状态:若已计费,需拦截(需先走退费流程
if ("BILLED".equals(billingStatus)) {
throw new BusinessException("该医嘱已产生费用,请先完成退费流程");
// 4⃣ 生成/更新发药汇总单(关键修复点
// 汇总单以 orderMainId 为唯一键,若不存在则插入;若已存在则重新计算数量、费用等并更新状态。
DispensingSummary summary = dispensingSummaryMapper.selectByOrderMainId(orderMainId);
if (summary == null) {
summary = new DispensingSummary();
summary.setOrderMainId(orderMainId);
summary.setPatientId(orderMain.getPatientId());
summary.setCreateTime(now);
summary.setOperator(operator);
}
// 重新统计汇总信息
List<DispensingDetail> allDetails = dispensingDetailMapper.selectByOrderMainId(orderMainId);
int totalQty = allDetails.stream().mapToInt(DispensingDetail::getQuantity).sum();
summary.setTotalQuantity(totalQty);
summary.setDispenseStatus(DispenseStatus.DISPATCHED.getCode());
summary.setUpdateTime(now);
summary.setOperator(operator);
if (summary.getId() == null) {
dispensingSummaryMapper.insertSelective(summary);
} else {
dispensingSummaryMapper.updateByPrimaryKeySelective(summary);
}
// 校验通过,执行退回逻辑
order.setOrderStatus(OrderStatus.RETURNED.getCode());
order.setUpdateTime(new Date());
orderMainMapper.updateById(order);
logger.info("医嘱退回成功, orderId: {}", orderId);
// 5⃣ 更新医嘱明细状态
for (OrderDetail od : pendingDetails) {
od.setOrderStatus(OrderStatus.DISPATCHED.getCode());
od.setUpdateTime(now);
orderDetailMapper.updateByPrimaryKeySelective(od);
}
// 6⃣ 更新主医嘱状态(若全部明细已发药,则整体标记为已发药)
long undispatchedCount = orderDetailMapper.countByOrderMainIdAndStatus(orderMainId, OrderStatus.PENDING.getCode());
if (undispatchedCount == 0) {
orderMain.setOrderStatus(OrderStatus.DISPATCHED.getCode());
orderMain.setUpdateTime(now);
orderMainMapper.updateByPrimaryKeySelective(orderMain);
}
logger.info("住院发药完成orderMainId={}, detailCount={}, operator={}",
orderMainId, pendingDetails.size(), operator);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelRegistration(Long orderId) {
// Bug #506 修复逻辑占位
OrderMain order = orderMainMapper.selectById(orderId);
if (order == null) throw new BusinessException("挂号单不存在");
order.setOrderStatus(OrderStatus.CANCELLED.getCode());
orderMainMapper.updateById(order);
// 同步更新排班池与号源状态
scheduleSlotMapper.updateStatusByOrderId(orderId, ScheduleSlotStatus.AVAILABLE.getCode());
schedulePoolMapper.updateStatusByOrderId(orderId, SchedulePoolStatus.FREE.getCode());
RefundLog refundLog = new RefundLog();
refundLog.setOrderId(orderId);
refundLog.setStatus(RefundStatus.SUCCESS.getCode());
refundLog.setCreateTime(new Date());
refundLogMapper.insert(refundLog);
}
// 其它业务方法保持不变...
}