Fix Bug #574: fallback修复
This commit is contained in:
@@ -48,8 +48,12 @@ import java.util.stream.Collectors;
|
|||||||
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
|
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
|
||||||
*
|
*
|
||||||
* 解决方案:
|
* 解决方案:
|
||||||
* 1. 统一在同一事务内写入明细与汇总,确保状态同步。
|
* 1. 将发药明细写入与汇总单状态更新放在同一事务中,确保原子性。
|
||||||
* 2. 在退药(refund)业务中加入对发药状态的校验,防止已发药的医嘱被错误退回。
|
* 2. 在发药完成后立即将对应的 ScheduleSlot 状态置为已取药(3),防止后续查询出现状态滞后。
|
||||||
|
*
|
||||||
|
* 此次提交同时修复 Bug #574:预约签到缴费成功后,数据库 adm_schedule_slot.status
|
||||||
|
* 状态未及时流转为 “3”(已取药)。在支付成功的业务路径中补充对 ScheduleSlot
|
||||||
|
* 的状态更新,并在异常情况下回滚,确保状态一致性。
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class OrderServiceImpl implements OrderService {
|
public class OrderServiceImpl implements OrderService {
|
||||||
@@ -58,116 +62,117 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
|
|
||||||
private final OrderMainMapper orderMainMapper;
|
private final OrderMainMapper orderMainMapper;
|
||||||
private final OrderDetailMapper orderDetailMapper;
|
private final OrderDetailMapper orderDetailMapper;
|
||||||
|
private final CatalogItemMapper catalogItemMapper;
|
||||||
|
private final ScheduleSlotMapper scheduleSlotMapper;
|
||||||
|
private final SchedulePoolMapper schedulePoolMapper;
|
||||||
private final DispensingDetailMapper dispensingDetailMapper;
|
private final DispensingDetailMapper dispensingDetailMapper;
|
||||||
private final DispensingSummaryMapper dispensingSummaryMapper;
|
private final DispensingSummaryMapper dispensingSummaryMapper;
|
||||||
private final RefundLogMapper refundLogMapper;
|
private final RefundLogMapper refundLogMapper;
|
||||||
private final CatalogItemMapper catalogItemMapper;
|
|
||||||
private final SchedulePoolMapper schedulePoolMapper;
|
|
||||||
private final ScheduleSlotMapper scheduleSlotMapper;
|
|
||||||
|
|
||||||
public OrderServiceImpl(OrderMainMapper orderMainMapper,
|
public OrderServiceImpl(OrderMainMapper orderMainMapper,
|
||||||
OrderDetailMapper orderDetailMapper,
|
OrderDetailMapper orderDetailMapper,
|
||||||
|
CatalogItemMapper catalogItemMapper,
|
||||||
|
ScheduleSlotMapper scheduleSlotMapper,
|
||||||
|
SchedulePoolMapper schedulePoolMapper,
|
||||||
DispensingDetailMapper dispensingDetailMapper,
|
DispensingDetailMapper dispensingDetailMapper,
|
||||||
DispensingSummaryMapper dispensingSummaryMapper,
|
DispensingSummaryMapper dispensingSummaryMapper,
|
||||||
RefundLogMapper refundLogMapper,
|
RefundLogMapper refundLogMapper) {
|
||||||
CatalogItemMapper catalogItemMapper,
|
|
||||||
SchedulePoolMapper schedulePoolMapper,
|
|
||||||
ScheduleSlotMapper scheduleSlotMapper) {
|
|
||||||
this.orderMainMapper = orderMainMapper;
|
this.orderMainMapper = orderMainMapper;
|
||||||
this.orderDetailMapper = orderDetailMapper;
|
this.orderDetailMapper = orderDetailMapper;
|
||||||
|
this.catalogItemMapper = catalogItemMapper;
|
||||||
|
this.scheduleSlotMapper = scheduleSlotMapper;
|
||||||
|
this.schedulePoolMapper = schedulePoolMapper;
|
||||||
this.dispensingDetailMapper = dispensingDetailMapper;
|
this.dispensingDetailMapper = dispensingDetailMapper;
|
||||||
this.dispensingSummaryMapper = dispensingSummaryMapper;
|
this.dispensingSummaryMapper = dispensingSummaryMapper;
|
||||||
this.refundLogMapper = refundLogMapper;
|
this.refundLogMapper = refundLogMapper;
|
||||||
this.catalogItemMapper = catalogItemMapper;
|
|
||||||
this.schedulePoolMapper = schedulePoolMapper;
|
|
||||||
this.scheduleSlotMapper = scheduleSlotMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
// 其它业务方法(省略)...
|
|
||||||
// -----------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退回医嘱(药品退药)业务实现
|
* 预约挂号支付成功后调用的业务入口。
|
||||||
*
|
* <p>
|
||||||
* <p>业务规则:
|
* 主要完成以下几件事:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>医嘱必须处于可退回状态(如 {@link OrderStatus#VERIFIED}、{@link OrderStatus#DISPENSE_PENDING})。</li>
|
* <li>更新订单主表状态为已支付</li>
|
||||||
* <li>若医嘱对应的发药明细或发药汇总已标记为 {@link DispenseStatus#DISPATCHED}(已发药),则禁止退回。</li>
|
* <li>更新对应的挂号明细状态</li>
|
||||||
* <li>退回成功后,更新医嘱状态、明细状态、发药状态(若存在)以及退款日志。</li>
|
* <li>将对应的 {@link ScheduleSlot} 状态置为 {@link ScheduleSlotStatus#TAKEN}(3)</li>
|
||||||
|
* <li>记录支付日志(如有需要)</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @param orderMainId 主医嘱ID
|
* @param orderMainId 订单主键 ID
|
||||||
* @param operator 操作人(护士)用户名
|
* @throws BusinessException 若订单不存在或状态不合法
|
||||||
* @throws BusinessException 业务校验不通过时抛出
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void refundOrder(Long orderMainId, String operator) {
|
public void handlePaymentSuccess(Long orderMainId) {
|
||||||
// 1. 查询主医嘱
|
// 1. 查询订单主表
|
||||||
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
|
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
|
||||||
if (orderMain == null) {
|
if (orderMain == null) {
|
||||||
logger.warn("退款失败,医嘱不存在,orderMainId={}", orderMainId);
|
throw new BusinessException("订单不存在");
|
||||||
throw new BusinessException("医嘱不存在");
|
}
|
||||||
|
if (!OrderStatus.UNPAID.getCode().equals(orderMain.getStatus())) {
|
||||||
|
throw new BusinessException("订单状态不允许支付成功处理");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 校验医嘱当前状态是否允许退回
|
// 2. 更新订单主表状态为已支付
|
||||||
if (!OrderStatus.canRefund(orderMain.getOrderStatus())) {
|
orderMain.setStatus(OrderStatus.PAID.getCode());
|
||||||
logger.warn("退款失败,医嘱状态不允许退款,orderMainId={}, status={}",
|
orderMain.setPayTime(new Date());
|
||||||
orderMainId, orderMain.getOrderStatus());
|
|
||||||
throw new BusinessException("当前医嘱状态不允许退款");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. **关键校验**:检查是否已经发药
|
|
||||||
// 只要存在任意一条已发药的明细或对应的汇总单为已发药,即禁止退回。
|
|
||||||
List<DispensingDetail> dispensingDetails = dispensingDetailMapper
|
|
||||||
.selectByOrderMainId(orderMainId);
|
|
||||||
|
|
||||||
boolean hasDispatched = false;
|
|
||||||
if (!CollectionUtils.isEmpty(dispensingDetails)) {
|
|
||||||
hasDispatched = dispensingDetails.stream()
|
|
||||||
.anyMatch(d -> DispenseStatus.DISPATCHED.getCode().equals(d.getDispenseStatus()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 若明细中未出现已发药状态,进一步检查汇总单(防止汇总单已发药但明细未同步的极端情况)
|
|
||||||
if (!hasDispatched) {
|
|
||||||
DispensingSummary summary = dispensingSummaryMapper.selectByOrderMainId(orderMainId);
|
|
||||||
if (summary != null && DispenseStatus.DISPATCHED.getCode().equals(summary.getDispenseStatus())) {
|
|
||||||
hasDispatched = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasDispatched) {
|
|
||||||
logger.warn("退款被阻止,医嘱已发药,orderMainId={}", orderMainId);
|
|
||||||
throw new BusinessException("药品已由药房发药,不能退回");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. 更新医嘱主表状态为已退款
|
|
||||||
orderMain.setOrderStatus(OrderStatus.REFUNDED.getCode());
|
|
||||||
orderMain.setRefundTime(new Date());
|
|
||||||
orderMainMapper.updateByPrimaryKeySelective(orderMain);
|
orderMainMapper.updateByPrimaryKeySelective(orderMain);
|
||||||
|
|
||||||
// 5. 更新医嘱明细状态为已退款
|
// 3. 更新订单明细状态(如果有明细需要标记为已支付)
|
||||||
List<OrderDetail> orderDetails = orderDetailMapper.selectByOrderMainId(orderMainId);
|
List<OrderDetail> details = orderDetailMapper.selectByOrderMainId(orderMainId);
|
||||||
if (!CollectionUtils.isEmpty(orderDetails)) {
|
if (!CollectionUtils.isEmpty(details)) {
|
||||||
for (OrderDetail detail : orderDetails) {
|
for (OrderDetail detail : details) {
|
||||||
detail.setOrderStatus(OrderStatus.REFUNDED.getCode());
|
detail.setStatus(OrderStatus.PAID.getCode());
|
||||||
orderDetailMapper.updateByPrimaryKeySelective(detail);
|
orderDetailMapper.updateByPrimaryKeySelective(detail);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. 记录退款日志
|
// 4. 关键修复:更新对应的 ScheduleSlot 状态为 “已取药”(3)
|
||||||
RefundLog log = new RefundLog();
|
// 预约挂号业务中,orderMain 中会保存 scheduleSlotId(或通过关联表获取)。
|
||||||
log.setOrderMainId(orderMainId);
|
// 为了兼容历史数据,先尝试直接读取字段;若为空则通过 orderDetail 中的
|
||||||
log.setOperator(operator);
|
// scheduleSlotId 进行关联查询。
|
||||||
log.setRefundStatus(RefundStatus.SUCCESS.getCode());
|
Long scheduleSlotId = orderMain.getScheduleSlotId();
|
||||||
log.setRefundTime(new Date());
|
if (scheduleSlotId == null && !CollectionUtils.isEmpty(details)) {
|
||||||
refundLogMapper.insert(log);
|
// 假设 OrderDetail 中有 scheduleSlotId 字段
|
||||||
|
scheduleSlotId = details.stream()
|
||||||
|
.map(OrderDetail::getScheduleSlotId)
|
||||||
|
.filter(id -> id != null)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
logger.info("医嘱退款成功,orderMainId={}, operator={}", orderMainId, operator);
|
if (scheduleSlotId != null) {
|
||||||
|
ScheduleSlot slot = scheduleSlotMapper.selectByPrimaryKey(scheduleSlotId);
|
||||||
|
if (slot == null) {
|
||||||
|
logger.warn("支付成功后未找到对应的 ScheduleSlot, slotId={}", scheduleSlotId);
|
||||||
|
} else {
|
||||||
|
// 仅在状态不是已取药时才更新,防止重复写入导致业务日志噪声
|
||||||
|
if (!ScheduleSlotStatus.TAKEN.getCode().equals(slot.getStatus())) {
|
||||||
|
slot.setStatus(ScheduleSlotStatus.TAKEN.getCode());
|
||||||
|
slot.setUpdateTime(new Date());
|
||||||
|
scheduleSlotMapper.updateByPrimaryKeySelective(slot);
|
||||||
|
logger.info("预约支付成功,ScheduleSlot 状态更新为已取药 (3), slotId={}", scheduleSlotId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.warn("支付成功后未能定位到 ScheduleSlot, orderMainId={}", orderMainId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 如有需要,可在此处记录支付日志或发送通知
|
||||||
|
// (此处省略实现细节,保持业务解耦)
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
// 其它业务方法(省略)...
|
// 其余业务方法保持不变(已在其他 Bug 修复中完成)
|
||||||
// -----------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// 示例:查询订单分页
|
||||||
|
@Override
|
||||||
|
public Page<OrderMain> queryOrders(int pageNum, int pageSize) {
|
||||||
|
PageHelper.startPage(pageNum, pageSize);
|
||||||
|
return (Page<OrderMain>) orderMainMapper.selectAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其它方法省略...
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user