订单状态实现状态机机制

package com.honghan.trade.statemachine;

import com.honghan.common.core.enums.OrderStatus;
import com.honghan.trade.enums.PaymentMethod;

import java.util.HashMap;
import java.util.Map;

public class OrderStateMachine {
    private final Map<PaymentMethod, Map<OrderStatus, OrderStatus>> transitions;
    private OrderStatus currentStatus;

    public OrderStateMachine() {
        this.transitions = new HashMap<>();
        initializeTransitions();
    }

//这里规定好订单的每步状态流向
    private void initializeTransitions() {
        // 先款后货状态转换
        Map<OrderStatus, OrderStatus> prepayTransitions = new HashMap<>();
        prepayTransitions.put(OrderStatus.WAIT_TALK, OrderStatus.CONFIRMED);
        prepayTransitions.put(OrderStatus.CONFIRMED, OrderStatus.OFFLINE_CONFIRMED);
        prepayTransitions.put(OrderStatus.OFFLINE_CONFIRMED, OrderStatus.PAYED);
        prepayTransitions.put(OrderStatus.PAYED, OrderStatus.SENDING);
        prepayTransitions.put(OrderStatus.SENDING, OrderStatus.RECEIVE);
        prepayTransitions.put(OrderStatus.RECEIVE, OrderStatus.COUNT);
        transitions.put(PaymentMethod.PREPAY, prepayTransitions);

        // 先付订金后付尾款状态转换 / 多次付款状态转换
        Map<OrderStatus, OrderStatus> depositTransitions = new HashMap<>();
        depositTransitions.put(OrderStatus.WAIT_TALK, OrderStatus.CONFIRMED);
        depositTransitions.put(OrderStatus.CONFIRMED, OrderStatus.OFFLINE_CONFIRMED);
        depositTransitions.put(OrderStatus.OFFLINE_CONFIRMED, OrderStatus.PAYED);
        depositTransitions.put(OrderStatus.PAYED, OrderStatus.SENDING);
        depositTransitions.put(OrderStatus.SENDING, OrderStatus.RECEIVE);
        depositTransitions.put(OrderStatus.RECEIVE, OrderStatus.COUNT);
        transitions.put(PaymentMethod.DEPOSIT, depositTransitions);
        transitions.put(PaymentMethod.MULTI_PAY, depositTransitions);

        // 先货后款状态转换
        Map<OrderStatus, OrderStatus> postpayTransitions = new HashMap<>();
        postpayTransitions.put(OrderStatus.WAIT_TALK, OrderStatus.PARTIAL_SHIPMENTS);
        postpayTransitions.put(OrderStatus.PARTIAL_SHIPMENTS, OrderStatus.COUNT);
        transitions.put(PaymentMethod.POSTPAY, postpayTransitions);
    }

    public OrderStatus getNextStatus(OrderStatus currentStatus, PaymentMethod method) {
        return getNextStatus(currentStatus, method, false); // 如果没有找到对应的状态转换
    }

    public OrderStatus getNextStatus(OrderStatus currentStatus, PaymentMethod method, boolean userWantsToCompleteOrder) {
        if (currentStatus == OrderStatus.RECEIVE) {
            return userWantsToCompleteOrder ? OrderStatus.COUNT : OrderStatus.REFUNDING;
        }

        Map<OrderStatus, OrderStatus> methodTransitions = transitions.get(method);
        if (methodTransitions != null) {
            return methodTransitions.get(currentStatus);
        }
        return null; // 如果没有找到对应的状态转换
    }

    public OrderStatus getCurrentStatus() {
        return currentStatus;
    }

    public void setCurrentStatus(OrderStatus newStatus) {
        this.currentStatus = newStatus;
    }
}

展开阅读剩余部分