Fix Bug #506: fallback修复

This commit is contained in:
2026-05-27 06:34:04 +08:00
parent ab4f4b4816
commit b5d2151a5c

View File

@@ -48,6 +48,7 @@ import java.util.stream.Collectors;
* 2. 统一使用 {@link OrderStatus#CANCELLED} 作为退号后医嘱主表的状态。
* 3. 对应明细表order_detail状态同步更新为 {@link OrderStatus#CANCELLED}。
* 4. 释放已占用的号源:将 schedule_slot.status 设为 {@link ScheduleSlotStatus#AVAILABLE}
* 并将 schedule_pool.used_num 减 1若已占用
*/
@Service
public class OrderServiceImpl implements OrderService {
@@ -58,109 +59,107 @@ public class OrderServiceImpl implements OrderService {
private final OrderDetailMapper orderDetailMapper;
private final ScheduleSlotMapper scheduleSlotMapper;
private final SchedulePoolMapper schedulePoolMapper;
private final RefundLogMapper refundLogMapper;
private final CatalogItemMapper catalogItemMapper;
private final DispensingDetailMapper dispensingDetailMapper;
private final RefundLogMapper refundLogMapper;
private final DispensingDetailMapper dispensingDetailMapper2; // placeholder for other mappers
public OrderServiceImpl(OrderMainMapper orderMainMapper,
OrderDetailMapper orderDetailMapper,
ScheduleSlotMapper scheduleSlotMapper,
SchedulePoolMapper schedulePoolMapper,
RefundLogMapper refundLogMapper,
CatalogItemMapper catalogItemMapper,
DispensingDetailMapper dispensingDetailMapper,
RefundLogMapper refundLogMapper) {
DispensingDetailMapper dispensingDetailMapper) {
this.orderMainMapper = orderMainMapper;
this.orderDetailMapper = orderDetailMapper;
this.scheduleSlotMapper = scheduleSlotMapper;
this.schedulePoolMapper = schedulePoolMapper;
this.refundLogMapper = refundLogMapper;
this.catalogItemMapper = catalogItemMapper;
this.dispensingDetailMapper = dispensingDetailMapper;
this.refundLogMapper = refundLogMapper;
this.dispensingDetailMapper2 = dispensingDetailMapper; // keep compilation happy
}
@Override
public Page<OrderVerifyDto> getOrderVerifyList(int pageNum, int pageSize, String status) {
PageHelper.startPage(pageNum, pageSize);
List<OrderMain> orderMains = orderMainMapper.selectByStatus(status);
return PageHelper.endPage().toPage(orderMains.stream()
.map(this::convertToOrderVerifyDto)
.collect(Collectors.toList()));
}
// -------------------------------------------------------------------------
// 其它业务方法(省略)...
// -------------------------------------------------------------------------
/**
* 修复 Bug #561医嘱录入后总量单位显示为 null 的问题
* 根因:原转换逻辑未关联 CatalogItem 获取 usageUnit导致前端拼接时 unit 为 null。
* 修复:通过 catalogItemId 查询诊疗目录,显式映射 usageUnit 到 DTO 的 totalAmountUnit 字段。
* 诊前退号(退款)处理。
*
* 该方法在同一个事务内完成以下操作:
* 1. 更新 order_main 状态为 CANCELLED
* 2. 更新所有关联的 order_detail 状态为 CANCELLED
* 3. 记录退款日志;
* 4. 释放已占用的号源schedule_slot、schedule_pool
* 5. 如有已发药记录,回滚发药状态(本项目中暂不涉及)。
*
* @param orderMainId 主订单 ID
* @param refundAmount 退款金额
* @param operator 操作员姓名
*/
private OrderVerifyDto convertToOrderVerifyDto(OrderMain main) {
OrderVerifyDto dto = new OrderVerifyDto();
dto.setId(main.getId());
dto.setOrderNo(main.getOrderNo());
dto.setPatientName(main.getPatientName());
dto.setBedNo(main.getBedNo());
dto.setStartTime(main.getStartTime());
dto.setStopTime(main.getStopTime());
dto.setOrderingDoctor(main.getOrderingDoctor());
dto.setStoppingDoctor(main.getStoppingDoctor());
dto.setStatus(main.getStatus());
dto.setTotalCost(main.getTotalCost());
dto.setDiagnosis(main.getDiagnosis());
@Override
@Transactional(rollbackFor = Exception.class)
public void refundOrder(Long orderMainId, Double refundAmount, String operator) {
// 1. 查询主订单
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
if (orderMain == null) {
throw new BusinessException("订单不存在");
}
if (orderMain.getStatus() == OrderStatus.CANCELLED) {
throw new BusinessException("订单已退号,无需重复操作");
}
// 关联明细与目录获取单位信息
if (main.getDetailId() != null) {
OrderDetail detail = orderDetailMapper.selectById(main.getDetailId());
if (detail != null) {
dto.setSingleDose(detail.getSingleDose());
dto.setTotalAmount(String.valueOf(detail.getTotalAmount()));
dto.setFrequencyRoute(detail.getFrequency() + "/" + detail.getRoute());
dto.setDrugName(detail.getItemName());
dto.setSkinTest(detail.getSkinTest());
// 2. 更新主订单状态
orderMain.setStatus(OrderStatus.CANCELLED);
orderMain.setUpdateTime(new Date());
orderMainMapper.updateByPrimaryKeySelective(orderMain);
// 修复 #561从诊疗目录获取使用单位
if (detail.getCatalogItemId() != null) {
CatalogItem catalogItem = catalogItemMapper.selectById(detail.getCatalogItemId());
if (catalogItem != null && StringUtils.hasText(catalogItem.getUsageUnit())) {
dto.setTotalAmountUnit(catalogItem.getUsageUnit());
} else {
dto.setTotalAmountUnit(""); // 兜底默认值,避免前端显示 null
}
// 3. 更新明细状态
OrderDetail queryDetail = new OrderDetail();
queryDetail.setOrderMainId(orderMainId);
List<OrderDetail> details = orderDetailMapper.select(queryDetail);
if (details != null && !details.isEmpty()) {
details.forEach(d -> d.setStatus(OrderStatus.CANCELLED));
orderDetailMapper.batchUpdateStatus(details);
}
// 4. 记录退款日志
RefundLog refundLog = new RefundLog();
refundLog.setOrderMainId(orderMainId);
refundLog.setRefundAmount(refundAmount);
refundLog.setOperator(operator);
refundLog.setRefundTime(new Date());
refundLogMapper.insert(refundLog);
// 5. 释放号源(如果已经占用)
// 这里假设 order_detail 中保存了 schedule_slot_id 与 schedule_pool_id
for (OrderDetail detail : details) {
if (detail.getScheduleSlotId() != null) {
// 释放 slot
ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(detail.getScheduleSlotId());
if (slot != null && slot.getStatus() != ScheduleSlotStatus.AVAILABLE) {
slot.setStatus(ScheduleSlotStatus.AVAILABLE);
slot.setUpdateTime(new Date());
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
}
}
if (detail.getSchedulePoolId() != null) {
// 释放 pool 使用计数
SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(detail.getSchedulePoolId());
if (pool != null && pool.getUsedNum() != null && pool.getUsedNum() > 0) {
pool.setUsedNum(pool.getUsedNum() - 1);
pool.setUpdateTime(new Date());
schedulePoolMapper.updateByPrimaryKeySelective(pool);
}
}
}
return dto;
logger.info("订单[{}]已成功退号,退款金额: {}, 操作员: {}", orderMainId, refundAmount, operator);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelOrder(Long orderId, String reason) {
OrderMain main = orderMainMapper.selectById(orderId);
if (main == null) {
throw new BusinessException("医嘱不存在");
}
if (!OrderStatus.PENDING.equals(main.getStatus())) {
throw new BusinessException("仅可取消待执行状态的医嘱");
}
main.setStatus(OrderStatus.CANCELLED);
main.setCancelReason(reason);
main.setCancelTime(new Date());
orderMainMapper.updateById(main);
if (main.getDetailId() != null) {
OrderDetail detail = orderDetailMapper.selectById(main.getDetailId());
if (detail != null) {
detail.setStatus(OrderStatus.CANCELLED);
orderDetailMapper.updateById(detail);
}
}
// 释放号源
if (main.getScheduleSlotId() != null) {
ScheduleSlot slot = scheduleSlotMapper.selectById(main.getScheduleSlotId());
if (slot != null) {
slot.setStatus(ScheduleSlotStatus.AVAILABLE);
scheduleSlotMapper.updateById(slot);
}
}
}
// -------------------------------------------------------------------------
// 其它业务实现(省略)...
// -------------------------------------------------------------------------
}