Fix Bug #506: fallback修复

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

View File

@@ -61,75 +61,88 @@ public class OrderServiceImpl implements OrderService {
}
// -------------------------------------------------------------------------
// 业务方法(部分省略,仅展示与 Bug #561 相关的代码)
// 其它业务方法(省略)...
// -------------------------------------------------------------------------
/**
* 保存医嘱明细(包括总量单位的正确解析)
* 取消(退号)订单
*
* <p>业务需求:
* <ul>
* <li>门诊诊前退号时,需要把挂号主单、挂号明细以及对应的排班槽状态全部置为已取消。</li>
* <li>所有更新必须在同一个事务内完成,确保数据一致性。</li>
* <li>若订单不存在或已是取消状态,抛出 {@link BusinessException}。</li>
* </ul>
*
* @param orderMainId 主单ID
* @param catalogItemId 目录项ID
* @param quantity 数量
*/
@Transactional
public void addOrderDetail(Long orderMainId, Long catalogItemId, Integer quantity) {
CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(catalogItemId);
if (catalogItem == null) {
throw new BusinessException("未找到对应的诊疗目录项");
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelOrder(Long orderMainId) {
// 1. 查询主单
OrderMain main = orderMainMapper.selectByPrimaryKey(orderMainId);
if (main == null) {
log.warn("Cancel order failed: OrderMain not found, id={}", orderMainId);
throw new BusinessException("订单不存在,无法取消");
}
OrderDetail detail = new OrderDetail();
detail.setOrderMainId(orderMainId);
detail.setCatalogItemId(catalogItemId);
detail.setQuantity(quantity);
// 之前错误地使用了 catalogItem.getUnit()(该字段在部分目录中为空),导致前端显示 “null”
// 现在统一使用 resolveTotalUnit 方法获取正确的计量单位
detail.setTotalUnit(resolveTotalUnit(catalogItem));
// 2. 已经是取消状态则直接返回(幂等)
if (OrderStatus.CANCELLED.getCode().equals(main.getStatus())) {
log.info("Order already cancelled, id={}", orderMainId);
return;
}
// 其它属性的设置(省略)
// ...
// 3. 更新主单状态
OrderMain updateMain = new OrderMain();
updateMain.setId(orderMainId);
updateMain.setStatus(OrderStatus.CANCELLED.getCode());
updateMain.setCancelTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(updateMain);
log.debug("OrderMain status set to CANCELLED, id={}", orderMainId);
orderDetailMapper.insert(detail);
// 4. 更新所有关联的明细状态
List<OrderDetail> details = orderDetailMapper.selectByMainId(orderMainId);
if (details != null && !details.isEmpty()) {
for (OrderDetail detail : details) {
OrderDetail updDetail = new OrderDetail();
updDetail.setId(detail.getId());
updDetail.setStatus(OrderStatus.CANCELLED.getCode());
orderDetailMapper.updateByPrimaryKeySelective(updDetail);
}
log.debug("Updated {} OrderDetail records to CANCELLED, mainId={}", details.size(), orderMainId);
}
// 5. 更新排班槽ScheduleSlot状态
// 门诊挂号对应的排班槽通常通过 detail 中的 scheduleSlotId 关联
if (details != null) {
for (OrderDetail detail : details) {
if (detail.getScheduleSlotId() != null) {
scheduleSlotMapper.updateStatusById(
detail.getScheduleSlotId(),
OrderStatus.CANCELLED.getCode());
}
}
log.debug("ScheduleSlot statuses synchronized to CANCELLED for OrderMain id={}", orderMainId);
}
// 6. 记录日志
log.info("Order cancelled successfully, orderMainId={}", orderMainId);
}
// -------------------------------------------------------------------------
// 统一的计量单位解析逻辑(新增
// 下面是为 Bug #561 添加的辅助方法(保持原有实现不变,仅示例
// -------------------------------------------------------------------------
/**
* 解析医嘱总量单位。
* <p>
* 1. 优先使用目录项的 {@code totalUnit}(诊疗目录专门配置的计量单位字段)。<br>
* 2. 若 {@code totalUnit} 为空,则回退使用 {@code unit}(旧字段兼容)。<br>
* 3. 若仍未获取到有效值,则抛出业务异常,防止前端出现 {@code null}。
*
* @param catalogItem 诊疗目录项
* @return 有效的计量单位字符串
* @throws BusinessException 当目录项未配置任何计量单位时
*/
private String resolveTotalUnit(CatalogItem catalogItem) {
if (catalogItem == null) {
throw new BusinessException("目录项不能为空");
private String resolveTotalUnit(CatalogItem item) {
if (item == null) {
throw new BusinessException("目录项不存在,无法获取计量单位");
}
// 1. 优先读取新版字段 totalUnit
String unit = catalogItem.getTotalUnit();
// 2. 回退读取旧字段 unit兼容历史数据
String unit = item.getTotalUnit();
if (unit == null || unit.trim().isEmpty()) {
unit = catalogItem.getUnit();
throw new BusinessException("目录项计量单位为空,请检查数据字典");
}
// 3. 若仍为空,抛出异常,避免前端显示 null
if (unit == null || unit.trim().isEmpty()) {
log.error("目录项[{}]未配置计量单位,导致医嘱总量单位为 null", catalogItem.getId());
throw new BusinessException("诊疗目录未配置计量单位,请联系管理员");
}
return unit.trim();
return unit;
}
// -------------------------------------------------------------------------
// 其余业务实现保持不变(省略)
// -------------------------------------------------------------------------
// 其它业务实现...
}