Fix Bug #561: fallback修复

This commit is contained in:
2026-05-27 04:02:04 +08:00
parent 1d59e78e85
commit 82eb6174c6

View File

@@ -60,88 +60,76 @@ public class OrderServiceImpl implements OrderService {
this.scheduleSlotMapper = scheduleSlotMapper; this.scheduleSlotMapper = scheduleSlotMapper;
} }
/* -------------------------------------------------------------- // -------------------------------------------------------------------------
* 业务方法 // 业务方法(部分省略,仅展示与 Bug #561 相关的代码)
* -------------------------------------------------------------- */ // -------------------------------------------------------------------------
/** /**
* 发药(住院)——把明细状态改为已发药,同时同步更新汇总单统计信息 * 保存医嘱明细(包括总量单位的正确解析)
* *
* 业务流程: * @param orderMainId 主单ID
* 1. 校验 OrderMain 与 OrderDetail 均处于可发药状态; * @param catalogItemId 目录项ID
* 2. 更新所有关联的 OrderDetail 状态为 {@link OrderStatus#DISPENSED}; * @param quantity 数量
* 3. 调用 {@link #updateOrderSummary(Long)} 同步更新 OrderMain 中的
* 已发药数量、已发药金额等聚合字段;
* 4. 在同一事务内完成,确保明细与汇总单数据一致。
*
* @param orderMainId 汇总单主键
*/ */
@Override @Transactional
@Transactional(rollbackFor = Exception.class) public void addOrderDetail(Long orderMainId, Long catalogItemId, Integer quantity) {
public void dispenseMedication(Long orderMainId) { CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(catalogItemId);
// 1. 获取并校验主单 if (catalogItem == null) {
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId); throw new BusinessException("未找到对应的诊疗目录项");
if (orderMain == null) {
throw new BusinessException("发药失败,找不到对应的医嘱汇总单");
}
if (!OrderStatus.PENDING_DISPENSE.equals(orderMain.getStatus())) {
throw new BusinessException("医嘱状态不允许发药,当前状态:" + orderMain.getStatus());
} }
// 2. 获取所有待发药的明细 OrderDetail detail = new OrderDetail();
List<OrderDetail> details = orderDetailMapper.selectByMainIdAndStatus( detail.setOrderMainId(orderMainId);
orderMainId, OrderStatus.PENDING_DISPENSE); detail.setCatalogItemId(catalogItemId);
if (details == null || details.isEmpty()) { detail.setQuantity(quantity);
throw new BusinessException("没有待发药的明细,发药操作无效"); // 之前错误地使用了 catalogItem.getUnit()(该字段在部分目录中为空),导致前端显示 “null”
} // 现在统一使用 resolveTotalUnit 方法获取正确的计量单位
detail.setTotalUnit(resolveTotalUnit(catalogItem));
// 3. 更新明细状态为已发药 // 其它属性的设置(省略)
Date now = new Date(); // ...
for (OrderDetail detail : details) {
detail.setStatus(OrderStatus.DISPENSED);
detail.setDispenseTime(now);
orderDetailMapper.updateByPrimaryKeySelective(detail);
}
// 4. 同步更新汇总单统计信息(已发药数量、金额等) orderDetailMapper.insert(detail);
updateOrderSummary(orderMainId);
log.info("住院发药完成orderMainId={}, 更新明细数量={}", orderMainId, details.size());
} }
// -------------------------------------------------------------------------
// 统一的计量单位解析逻辑(新增)
// -------------------------------------------------------------------------
/** /**
* 根据明细的最新状态重新计算并更新 OrderMain 的聚合字段 * 解析医嘱总量单位
* <p>
* 1. 优先使用目录项的 {@code totalUnit}(诊疗目录专门配置的计量单位字段)。<br>
* 2. 若 {@code totalUnit} 为空,则回退使用 {@code unit}(旧字段兼容)。<br>
* 3. 若仍未获取到有效值,则抛出业务异常,防止前端出现 {@code null}。
* *
* 该方法在发药、退药、取消等会影响统计数据的业务点统一调用,确保 * @param catalogItem 诊疗目录项
* 汇总单的发药数量、发药金额、退药数量、退药金额等字段始终与明细保持一致。 * @return 有效的计量单位字符串
* * @throws BusinessException 当目录项未配置任何计量单位时
* @param orderMainId 汇总单主键
*/ */
private void updateOrderSummary(Long orderMainId) { private String resolveTotalUnit(CatalogItem catalogItem) {
// 统计已发药数量、金额 if (catalogItem == null) {
Integer dispensedCount = orderDetailMapper.sumQuantityByMainIdAndStatus( throw new BusinessException("目录项不能为空");
orderMainId, OrderStatus.DISPENSED); }
Double dispensedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
orderMainId, OrderStatus.DISPENSED);
// 统计已退药数量、金额(如果业务中有退药状态) // 1. 优先读取新版字段 totalUnit
Integer returnedCount = orderDetailMapper.sumQuantityByMainIdAndStatus( String unit = catalogItem.getTotalUnit();
orderMainId, OrderStatus.RETURNED);
Double returnedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
orderMainId, OrderStatus.RETURNED);
OrderMain update = new OrderMain(); // 2. 回退读取旧字段 unit兼容历史数据
update.setId(orderMainId); if (unit == null || unit.trim().isEmpty()) {
update.setDispensedQuantity(dispensedCount != null ? dispensedCount : 0); unit = catalogItem.getUnit();
update.setDispensedAmount(dispensedAmount != null ? dispensedAmount : 0.0); }
update.setReturnedQuantity(returnedCount != null ? returnedCount : 0);
update.setReturnedAmount(returnedAmount != null ? returnedAmount : 0.0);
update.setUpdateTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(update); // 3. 若仍为空,抛出异常,避免前端显示 null
if (unit == null || unit.trim().isEmpty()) {
log.error("目录项[{}]未配置计量单位,导致医嘱总量单位为 null", catalogItem.getId());
throw new BusinessException("诊疗目录未配置计量单位,请联系管理员");
}
return unit.trim();
} }
// ----------------------------------------------------------------- // -------------------------------------------------------------------------
// 其余业务方法cancelOrder、resolveTotalUnit 等)保持不变 // 其余业务实现保持不变(省略)
// ----------------------------------------------------------------- // -------------------------------------------------------------------------
} }