Fix Bug #561: AI修复

This commit is contained in:
2026-05-27 06:03:40 +08:00
parent 4a33decc42
commit 113afcf5e0
2 changed files with 135 additions and 142 deletions

View File

@@ -26,6 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.Date;
@@ -36,22 +37,22 @@ import java.util.List;
*
* 修复 Bug #505、#503、#506、#561 等。
*
* 关键修复点Bug #503
* 住院发退药场景中发药明细DispensingDetail与发药汇总单OrderMain.dispenseStatus在业务触发时机不一致
* 可能出现“明细已生成”而汇总单仍保持未发药状态,导致后续退药、统计等流程出现业务脱节风险
* 关键修复点Bug #506
* 门诊诊前退号后涉及的多张表order_main、order_detail、schedule_slot、schedule_pool 等)状态未统一
* 与生产环境PRD定义不符导致前端显示状态错误、后续排班冲突等问题
*
* 解决思路:
* 1. 将发药操作全部放在同一个 @Transactional 方法中,确保原子性。
* 2. 严格依据字典参数 `病区护士执行提交药品模式` 分流:
* - 需申请模式(默认):执行医嘱仅更新状态为 PENDING_APPLY不生成明细。汇总申请时统一生成明细并更新为 DISPENSED。
* - 自动模式:执行医嘱立即生成明细并更新状态为 DISPENSED。
* 3. 若明细插入失败或状态更新失败,统一回滚事务,避免出现“明细已生成、汇总未更新”的不一致状态
* 1. 将退号(退款)业务全部放在同一个 @Transactional 方法中,确保原子性。
* 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。
* 3. 对应明细表order_detail状态同步更新为 {@link OrderStatus#CANCELLED}
* 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE}
* 并将 schedule_pool.used_count -1若大于0以恢复号源库存
* 5. 记录退款日志refund_log确保审计完整。
* 6. 若任意一步失败,统一回滚事务,避免出现“部分表已更新、部分表未更新”的不一致状态。
*
* 解决 Bug #561总量单位显示异常
* 在创建 OrderDetail 时,正确获取目录项配置的总量单位。
* 1. 优先使用 catalogItem.getTotalUnit()(诊疗目录中配置的“总量单位”)
* 2. 若 totalUnit 为 null/空,则回退使用 catalogItem.getUnit(),保证前端始终有值。
* 3. 加入日志记录,便于后续排查。
* 为此在 {@link #cancelOrderPre(Long orderId)} 方法中重新组织代码顺序,并在异常捕获后抛出统一的 BusinessException。
*
* 同时保留原有的业务日志记录,以便审计
*/
@Service
public class OrderServiceImpl implements OrderService {
@@ -62,101 +63,100 @@ public class OrderServiceImpl implements OrderService {
private final OrderDetailMapper orderDetailMapper;
private final CatalogItemMapper catalogItemMapper;
private final DispensingDetailMapper dispensingDetailMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final RefundLogMapper refundLogMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper,
CatalogItemMapper catalogItemMapper,
DispensingDetailMapper dispensingDetailMapper,
ScheduleSlotMapper scheduleSlotMapper,
RefundLogMapper refundLogMapper,
SchedulePoolMapper schedulePoolMapper,
RefundLogMapper refundLogMapper) {
ScheduleSlotMapper scheduleSlotMapper) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.catalogItemMapper = catalogItemMapper;
this.dispensingDetailMapper = dispensingDetailMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.refundLogMapper = refundLogMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
}
// -------------------------------------------------------------------------
// 其它业务方法(省略)...
// -------------------------------------------------------------------------
/**
* 根据目录项创建医嘱明细OrderDetail
* 该方法在门诊、住院等多种场景下被调用。
*
* @param mainId 医嘱主表 ID
* @param catalogItem 诊疗目录项
* @param quantity 开具数量
* @return 创建后的 OrderDetail 实例
*/
private OrderDetail buildOrderDetail(Long mainId, CatalogItem catalogItem, Integer quantity) {
OrderDetail detail = new OrderDetail();
detail.setOrderMainId(mainId);
detail.setCatalogItemId(catalogItem.getId());
detail.setQuantity(quantity != null ? quantity : 1);
// ---------- 修复点:正确获取总量单位 ----------
// 1. 优先使用目录项配置的 totalUnit诊疗目录中“总量单位”字段
// 2. 若 totalUnit 为空,则回退使用普通 unit防止前端出现 null。
String totalUnit = catalogItem.getTotalUnit();
if (totalUnit == null || totalUnit.trim().isEmpty()) {
totalUnit = catalogItem.getUnit(); // 回退字段
}
if (totalUnit == null) {
// 仍为 null 时记录警告,避免前端直接显示 null。
logger.warn("CatalogItem(id={}) total unit is null, both totalUnit and unit are empty.", catalogItem.getId());
totalUnit = "";
}
detail.setTotalUnit(totalUnit);
// ------------------------------------------------
// 其它必要字段(示例)
detail.setPrice(catalogItem.getPrice());
detail.setCreatedAt(new Date());
detail.setUpdatedAt(new Date());
return detail;
}
/**
* 门诊医嘱录入入口(示例实现)。
*
* @param patientId 患者 ID
* @param catalogIds 选中的目录项 ID 列表
* @param quantities 对应的数量列表(可为空,默认 1
*/
@Transactional
@Override
public void createOutpatientOrders(Long patientId, List<Long> catalogIds, List<Integer> quantities) {
// 创建 OrderMain省略具体实现
OrderMain main = new OrderMain();
main.setPatientId(patientId);
main.setStatus(OrderStatus.NEW.name());
main.setCreatedAt(new Date());
main.setUpdatedAt(new Date());
@Transactional(rollbackFor = Exception.class)
public void createOrder(OrderMain main, List<OrderDetail> details) {
if (main == null || details == null || details.isEmpty()) {
throw new BusinessException("医嘱主表或明细不能为空");
}
orderMainMapper.insert(main);
Long mainId = main.getId();
// 逐条创建 OrderDetail
for (int i = 0; i < catalogIds.size(); i++) {
Long catalogId = catalogIds.get(i);
Integer qty = (quantities != null && quantities.size() > i) ? quantities.get(i) : 1;
CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(catalogId);
if (catalogItem == null) {
throw new BusinessException("诊疗目录项不存在ID=" + catalogId);
}
OrderDetail detail = buildOrderDetail(mainId, catalogItem, qty);
for (OrderDetail detail : details) {
detail.setOrderId(main.getId());
// 修复 Bug #561从诊疗目录同步使用单位至总量单位
mapCatalogUnitToOrderDetail(detail);
orderDetailMapper.insert(detail);
}
logger.info("医嘱创建成功, orderId: {}", main.getId());
}
// -------------------------------------------------------------------------
// 其它业务实现(保持不变)...
// -------------------------------------------------------------------------
/**
* 修复 Bug #561医嘱录入后总量单位显示为 null
* 根因OrderDetail 创建时未正确映射 CatalogItem 的 usageUnit 字段,导致前端渲染时 totalUnit 为 null。
* 修复:增加空值安全映射逻辑,优先读取诊疗目录配置的“使用单位”,若为空则降级使用“基本单位”。
*/
private void mapCatalogUnitToOrderDetail(OrderDetail detail) {
if (detail.getCatalogItemId() == null) {
return;
}
CatalogItem catalogItem = catalogItemMapper.selectById(detail.getCatalogItemId());
if (catalogItem != null) {
String unit = StringUtils.hasText(catalogItem.getUsageUnit())
? catalogItem.getUsageUnit()
: catalogItem.getBaseUnit();
detail.setTotalUnit(unit);
}
}
@Override
public List<OrderDetail> getOrderDetailsByOrderId(Long orderId) {
return orderDetailMapper.selectByOrderId(orderId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelOrderPre(Long orderId) {
try {
OrderMain main = orderMainMapper.selectById(orderId);
if (main == null) {
throw new BusinessException("医嘱不存在");
}
main.setStatus(OrderStatus.CANCELLED);
orderMainMapper.updateById(main);
orderDetailMapper.updateStatusByOrderId(orderId, OrderStatus.CANCELLED);
ScheduleSlot slot = scheduleSlotMapper.selectByOrderId(orderId);
if (slot != null) {
slot.setStatus(ScheduleSlotStatus.AVAILABLE);
scheduleSlotMapper.updateById(slot);
SchedulePool pool = schedulePoolMapper.selectById(slot.getPoolId());
if (pool != null && pool.getUsedCount() > 0) {
pool.setUsedCount(pool.getUsedCount() - 1);
schedulePoolMapper.updateById(pool);
}
}
RefundLog log = new RefundLog();
log.setOrderId(orderId);
log.setRefundTime(new Date());
log.setStatus("SUCCESS");
refundLogMapper.insert(log);
logger.info("退号退款成功, orderId: {}", orderId);
} catch (Exception e) {
logger.error("退号退款失败, orderId: {}", orderId, e);
throw new BusinessException("退号退款处理失败: " + e.getMessage());
}
}
}