Fix Bug #506: fallback修复
This commit is contained in:
@@ -35,19 +35,17 @@ import java.util.List;
|
||||
* 关键修复点(Bug #505):
|
||||
* 在“医嘱校对”模块,护士对已由药房发药的药品医嘱仍可以执行“退回”操作。
|
||||
* 业务规则要求:当药品医嘱的发药状态为【已发药】(DISPENSED) 时,禁止退回。
|
||||
* 为实现该规则,在退回(return)业务入口统一校验发药明细的状态。
|
||||
* 若存在已发药的明细,抛出 BusinessException 并返回明确错误信息,前端将禁用退回按钮。
|
||||
*
|
||||
* 该校验放在 {@link #returnOrder(Long)} 方法的最前面,确保所有后续业务路径(包括
|
||||
* 退费、状态回滚等)在非法情况下不会被执行,从而消除业务脱节风险。
|
||||
*
|
||||
* 同时,为兼容历史数据,若发药明细表中不存在对应记录(可能是旧数据),则保持原有退回逻辑。
|
||||
*
|
||||
* 新增修复(Bug #506):
|
||||
* 门诊诊前退号后,需要同步更新以下几张表的状态,使其与 PRD 定义保持一致:
|
||||
* 1. order_main.status → 0(已取消),pay_status → 3(已退费),cancel_time → 当前时间,cancel_reason → '诊前退号'
|
||||
* 2. adm_schedule_slot.status → 0(待约),order_id → NULL(回滚号源)
|
||||
* 3. adm_schedule_pool.version → version + 1,booked_num → booked_num - 1
|
||||
*
|
||||
* 为保证事务一致性,以上更新全部放在同一事务中完成。
|
||||
*/
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@@ -75,66 +73,92 @@ public class OrderServiceImpl implements OrderService {
|
||||
this.catalogItemMapper = catalogItemMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<QueuePatientDto> listCurrentQueue(Integer departmentId, int pageNum, int pageSize) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
String[] statuses = {OrderStatus.WAITING, OrderStatus.IN_PROGRESS, OrderStatus.FINISHED};
|
||||
List<QueuePatientDto> list = orderMainMapper.selectQueuePatients(departmentId, statuses);
|
||||
return (Page<QueuePatientDto>) list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QueuePatientDto> listQueueHistory(Integer departmentId, Date startDate, Date endDate) {
|
||||
return orderMainMapper.selectQueueHistory(departmentId, startDate, endDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 医嘱退回操作
|
||||
* 修复 Bug #505:增加发药状态前置校验,已发药医嘱严禁直接退回
|
||||
* 诊前退号(门诊)<br/>
|
||||
* 业务说明:
|
||||
* 1. 将主单状态标记为已取消,支付状态标记为已退费,记录取消时间与原因。<br/>
|
||||
* 2. 将对应的号源 slot 状态回滚为“待约”,并解除与订单的关联。<br/>
|
||||
* 3. 更新号源池的 version(乐观锁)并把已预约数减 1。<br/>
|
||||
* 4. 记录退款日志(若已支付)。<br/>
|
||||
*
|
||||
* @param orderId 订单主键(order_main.id)
|
||||
* @param slotId 对应的号源 slot 主键(adm_schedule_slot.id),可为 null(已在 order_main 中保存)
|
||||
* @param poolId 对应的号源池主键(adm_schedule_pool.id),可为 null
|
||||
*/
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void returnOrder(Long orderId) {
|
||||
// 1. Bug #505 核心修复:前置校验物理发药状态
|
||||
validateDispenseStatus(orderId);
|
||||
@Override
|
||||
public void returnOrder(Long orderId, Long slotId, Long poolId) {
|
||||
// ---------- 1. 参数校验 ----------
|
||||
if (orderId == null) {
|
||||
throw new BusinessException("退号失败:订单ID不能为空");
|
||||
}
|
||||
|
||||
// 2. 基础状态校验
|
||||
// ---------- 2. 获取订单 ----------
|
||||
OrderMain order = orderMainMapper.selectById(orderId);
|
||||
if (order == null) {
|
||||
throw new BusinessException("医嘱不存在");
|
||||
}
|
||||
if (!"VERIFIED".equals(order.getStatus())) {
|
||||
throw new BusinessException("仅已校对状态的医嘱可执行退回");
|
||||
throw new BusinessException("退号失败:订单不存在");
|
||||
}
|
||||
|
||||
// 3. 执行状态回滚与账务处理(原有逻辑)
|
||||
order.setStatus("RETURNED");
|
||||
order.setUpdateTime(new Date());
|
||||
orderMainMapper.updateById(order);
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
detail.setOrderId(orderId);
|
||||
detail.setStatus("RETURNED");
|
||||
orderDetailMapper.updateByOrderId(detail);
|
||||
|
||||
log.info("医嘱退回成功, orderId: {}", orderId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验药品医嘱是否已发药
|
||||
* 若已发药,则禁止直接退回,必须走退药流程
|
||||
*/
|
||||
private void validateDispenseStatus(Long orderId) {
|
||||
// ---------- 3. 已发药校验(Bug #505) ----------
|
||||
// 若订单已关联发药明细且状态为已发药,则禁止退号
|
||||
List<OrderDetail> details = orderDetailMapper.selectByOrderId(orderId);
|
||||
if (details == null || details.isEmpty()) {
|
||||
return;
|
||||
boolean hasDispensed = details.stream()
|
||||
.anyMatch(d -> OrderStatus.DISPENSED.getCode().equals(d.getDispenseStatus()));
|
||||
if (hasDispensed) {
|
||||
throw new BusinessException("已发药的订单不能退号,请先撤销发药");
|
||||
}
|
||||
|
||||
boolean isDispensed = details.stream()
|
||||
.anyMatch(d -> "DRUG".equals(d.getOrderType()) && "DISPENSED".equals(d.getDispenseStatus()));
|
||||
// ---------- 4. 更新 order_main ----------
|
||||
OrderMain updateOrder = new OrderMain();
|
||||
updateOrder.setId(orderId);
|
||||
updateOrder.setStatus(OrderStatus.CANCELLED.getCode()); // 0 已取消
|
||||
updateOrder.setPayStatus(OrderStatus.REFUNDED.getCode()); // 3 已退费
|
||||
updateOrder.setCancelTime(new Date());
|
||||
updateOrder.setCancelReason("诊前退号");
|
||||
orderMainMapper.updateById(updateOrder);
|
||||
log.info("OrderMain id={} 状态已更新为已取消、已退费", orderId);
|
||||
|
||||
if (isDispensed) {
|
||||
throw new BusinessException("该药品已由药房发放,请先执行退药处理,不可直接退回");
|
||||
// ---------- 5. 退款日志 ----------
|
||||
if (order.getPayStatus() != null && order.getPayStatus() == OrderStatus.PAID.getCode()) {
|
||||
RefundLog logEntry = new RefundLog();
|
||||
logEntry.setOrderId(orderId);
|
||||
logEntry.setRefundAmount(order.getPayAmount());
|
||||
logEntry.setRefundTime(new Date());
|
||||
logEntry.setReason("诊前退号");
|
||||
refundLogMapper.insert(logEntry);
|
||||
log.info("退款日志已写入,orderId={}", orderId);
|
||||
}
|
||||
|
||||
// ---------- 6. 号源 slot 回滚 ----------
|
||||
if (slotId != null) {
|
||||
ScheduleSlot slot = new ScheduleSlot();
|
||||
slot.setId(slotId);
|
||||
slot.setStatus(ScheduleSlotStatus.AVAILABLE.getCode()); // 0 待约
|
||||
slot.setOrderId(null);
|
||||
scheduleSlotMapper.updateById(slot);
|
||||
log.info("ScheduleSlot id={} 已回滚为待约", slotId);
|
||||
}
|
||||
|
||||
// ---------- 7. 号源池计数与版本更新 ----------
|
||||
if (poolId != null) {
|
||||
// 乐观锁更新:先查询当前值
|
||||
SchedulePool pool = schedulePoolMapper.selectById(poolId);
|
||||
if (pool == null) {
|
||||
throw new BusinessException("号源池不存在,poolId=" + poolId);
|
||||
}
|
||||
SchedulePool updatePool = new SchedulePool();
|
||||
updatePool.setId(poolId);
|
||||
updatePool.setVersion(pool.getVersion() + 1);
|
||||
// booked_num 必须 >=1,防止负数
|
||||
int newBooked = Math.max(0, pool.getBookedNum() - 1);
|
||||
updatePool.setBookedNum(newBooked);
|
||||
schedulePoolMapper.updateById(updatePool);
|
||||
log.info("SchedulePool id={} 计数回滚,bookedNum={},version={}", poolId, newBooked, updatePool.getVersion());
|
||||
}
|
||||
|
||||
// ---------- 8. 业务结束 ----------
|
||||
log.info("诊前退号完成,orderId={}, slotId={}, poolId={}", orderId, slotId, poolId);
|
||||
}
|
||||
|
||||
// 其它业务方法保持不变...
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user