package com.campus.trade.common; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Component public class OrderStatusMachine { private static final Map> ALLOWED_TRANSITIONS = Map.of( "PENDING", List.of("CONFIRMED", "CANCELLED"), "CONFIRMED", List.of("DELIVERED", "CANCELLED"), "DELIVERED", List.of("COMPLETED", "CANCELLED"), "COMPLETED", List.of(), "CANCELLED", List.of() ); public void validateTransition(String currentStatus, String targetStatus) { if (currentStatus == null || targetStatus == null) { throw new BusinessException(StatusCode.BAD_REQUEST, "订单状态参数不能为空"); } List allowed = ALLOWED_TRANSITIONS.get(currentStatus); if (allowed == null || !allowed.contains(targetStatus)) { throw new BusinessException(StatusCode.BAD_REQUEST, "订单状态不允许从 " + currentStatus + " 转移到 " + targetStatus); } } }