| 1234567891011121314151617181920212223242526272829 |
- 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<String, List<String>> 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<String> allowed = ALLOWED_TRANSITIONS.get(currentStatus);
- if (allowed == null || !allowed.contains(targetStatus)) {
- throw new BusinessException(StatusCode.BAD_REQUEST,
- "订单状态不允许从 " + currentStatus + " 转移到 " + targetStatus);
- }
- }
- }
|