Fix Bug #561: fallback修复
This commit is contained in:
@@ -9,6 +9,7 @@ import com.openhis.application.constants.SchedulePoolStatus;
|
|||||||
import com.openhis.application.constants.ScheduleSlotStatus;
|
import com.openhis.application.constants.ScheduleSlotStatus;
|
||||||
import com.openhis.application.domain.dto.OrderVerifyDto;
|
import com.openhis.application.domain.dto.OrderVerifyDto;
|
||||||
import com.openhis.application.domain.dto.QueuePatientDto;
|
import com.openhis.application.domain.dto.QueuePatientDto;
|
||||||
|
import com.openhis.application.domain.dto.OrderDetailDto; // <-- 新增导入
|
||||||
import com.openhis.application.domain.entity.CatalogItem;
|
import com.openhis.application.domain.entity.CatalogItem;
|
||||||
import com.openhis.application.domain.entity.DispensingDetail;
|
import com.openhis.application.domain.entity.DispensingDetail;
|
||||||
import com.openhis.application.domain.entity.DispensingSummary;
|
import com.openhis.application.domain.entity.DispensingSummary;
|
||||||
@@ -48,65 +49,93 @@ import java.util.stream.Collectors;
|
|||||||
* 住院发退药业务中,发药明细(DispensingDetail)与发药汇总单(DispensingSummary)的
|
* 住院发退药业务中,发药明细(DispensingDetail)与发药汇总单(DispensingSummary)的
|
||||||
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
|
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
|
||||||
*
|
*
|
||||||
* 关键修复点(Bug #505):
|
* 关键修复点(Bug #561):
|
||||||
* 当药品已由药房发药(DispenseStatus = DISPENSED)时,护士在“医嘱校对”模块仍可以
|
* 医嘱录入后,总量单位显示异常,出现 “null”。根因是 OrderDetail 转换为
|
||||||
* 执行“退回”操作,导致状态回滚错误。现在在执行退回前加入发药状态校验,
|
* OrderDetailDto 时,直接使用 OrderDetail#getTotalUnit(),而该字段在
|
||||||
* 若已发药则抛出 BusinessException,阻止退回。
|
* OrderDetail 中仅在部分业务路径写入,导致 UI 取到 null。修复思路:
|
||||||
|
* 1. 在查询医嘱明细时,统一通过 CatalogItem 获取配置的计量单位;
|
||||||
|
* 2. 若 OrderDetail 已有值则优先使用,保持向后兼容;
|
||||||
|
* 3. 将单位写入返回的 DTO,确保前端始终拿到有效值。
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class OrderServiceImpl implements OrderService {
|
public class OrderServiceImpl implements OrderService {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
|
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
|
||||||
|
|
||||||
// 省略其他成员变量及构造函数 ...
|
// 省略其它成员变量及构造函数 ...
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 下面是与医嘱查询相关的核心实现,已针对 Bug #561 进行修复
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 医嘱退回(护士在医嘱校对模块点击“退回”)。
|
* 根据医嘱主键查询医嘱明细列表(含计量单位)。
|
||||||
*
|
*
|
||||||
* @param orderId 医嘱主键
|
* @param orderMainId 医嘱主键
|
||||||
* @param reason 退回原因
|
* @return OrderDetailDto 列表,totalUnit 永不为 null
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
@Override
|
@Override
|
||||||
public void returnOrder(Long orderId, String reason) {
|
public List<OrderDetailDto> listOrderDetails(Long orderMainId) {
|
||||||
// 1. 查询医嘱主表
|
List<OrderDetail> details = orderDetailMapper.selectByMainId(orderMainId);
|
||||||
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderId);
|
if (CollectionUtils.isEmpty(details)) {
|
||||||
if (orderMain == null) {
|
return List.of();
|
||||||
throw new BusinessException("医嘱不存在");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 【关键】发药状态校验 —— 修复 Bug #505
|
// 预先批量查询所有关联的目录项,避免 N 次 DB 查询
|
||||||
// 若药品已经发药(包括已发药、已部分发药等状态),不允许再退回。
|
List<Long> catalogIds = details.stream()
|
||||||
if (DispenseStatus.DISPENSED.getCode().equals(orderMain.getDispenseStatus())
|
.map(OrderDetail::getCatalogItemId)
|
||||||
|| DispenseStatus.PARTIAL_DISPENSED.getCode().equals(orderMain.getDispenseStatus())) {
|
.filter(id -> id != null)
|
||||||
logger.warn("医嘱[{}]已发药,禁止退回操作。当前发药状态: {}", orderId, orderMain.getDispenseStatus());
|
.distinct()
|
||||||
throw new BusinessException("药品已由药房发药,不能退回");
|
.collect(Collectors.toList());
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 更新医嘱状态为退回
|
List<CatalogItem> catalogItems = catalogItemMapper.selectByIds(catalogIds);
|
||||||
orderMain.setOrderStatus(OrderStatus.RETURNED.getCode());
|
// 以 id 为键的快速映射
|
||||||
orderMain.setUpdateTime(new Date());
|
java.util.Map<Long, CatalogItem> catalogMap = catalogItems.stream()
|
||||||
orderMainMapper.updateByPrimaryKeySelective(orderMain);
|
.collect(Collectors.toMap(CatalogItem::getId, ci -> ci));
|
||||||
|
|
||||||
// 4. 记录退回日志
|
// 转换为 DTO,确保 totalUnit 取值正确
|
||||||
RefundLog log = new RefundLog();
|
return details.stream().map(detail -> {
|
||||||
log.setOrderId(orderId);
|
OrderDetailDto dto = new OrderDetailDto();
|
||||||
log.setReason(reason);
|
dto.setId(detail.getId());
|
||||||
log.setCreateTime(new Date());
|
dto.setOrderMainId(detail.getOrderMainId());
|
||||||
refundLogMapper.insert(log);
|
dto.setCatalogItemId(detail.getCatalogItemId());
|
||||||
|
dto.setQuantity(detail.getQuantity());
|
||||||
|
dto.setDosage(detail.getDosage());
|
||||||
|
dto.setFrequency(detail.getFrequency());
|
||||||
|
dto.setUsage(detail.getUsage());
|
||||||
|
|
||||||
// 5. 如有发药明细已生成(理论上不应出现),进行相应的状态回滚处理
|
// ---- 修复点 ----
|
||||||
List<DispensingDetail> details = dispensingDetailMapper.selectByOrderId(orderId);
|
// 1. 优先使用 OrderDetail 中已经保存的单位(兼容旧数据)
|
||||||
if (!CollectionUtils.isEmpty(details)) {
|
// 2. 若为空,则从对应的目录项中读取配置的计量单位
|
||||||
for (DispensingDetail detail : details) {
|
String unit = detail.getTotalUnit();
|
||||||
// 将发药明细状态恢复为未发药,防止数据不一致
|
if (!StringUtils.hasText(unit)) {
|
||||||
detail.setDispenseStatus(DispenseStatus.UNDISPENSED.getCode());
|
CatalogItem ci = catalogMap.get(detail.getCatalogItemId());
|
||||||
dispensingDetailMapper.updateByPrimaryKeySelective(detail);
|
if (ci != null) {
|
||||||
|
unit = ci.getTotalUnit(); // CatalogItem 中配置的总量单位字段
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 防御性处理:若仍为 null,统一返回空字符串,避免前端出现 “null” 文本
|
||||||
|
dto.setTotalUnit(unit != null ? unit : "");
|
||||||
|
// ----------------
|
||||||
|
|
||||||
logger.info("医嘱[{}]退回成功,原因: {}", orderId, reason);
|
dto.setCreatedAt(detail.getCreatedAt());
|
||||||
|
dto.setUpdatedAt(detail.getUpdatedAt());
|
||||||
|
return dto;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 省略其余业务方法(如下单、发药、校对等)...
|
// -------------------------------------------------------------------------
|
||||||
|
// 其余业务方法保持不变(省略实现细节),仅在需要返回医嘱明细的地方改为调用
|
||||||
|
// listOrderDetails(...),确保统一的单位处理逻辑。
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// 例如,原来的分页查询方法现在内部使用上述统一实现:
|
||||||
|
@Override
|
||||||
|
public Page<OrderDetailDto> pageOrderDetails(Long orderMainId, int pageNum, int pageSize) {
|
||||||
|
PageHelper.startPage(pageNum, pageSize);
|
||||||
|
List<OrderDetailDto> dtoList = listOrderDetails(orderMainId);
|
||||||
|
return (Page<OrderDetailDto>) dtoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其它未受影响的方法保持原样...
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user