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;
}
}
package com.honghan.common.core.enums;
/**
* @Description 平台通用订单枚举
* @author °DJ°
* @date 2023/3/17 18:22
* @Motto ^V^你们都要好好的^V^
*/
public enum OrderStatus {
CREATE(1, "待确认"), //提货单、3发货单,4退货单
CONFIRMED(2, "待付款"),
SENDING(3, "待收货"),
RECEIVE(4, "已收货"),
WAIT_TALK(50, "待询价"),
OFFLINE_CONFIRMED(60, "付款待确认"),//线下打款待确认-确认生成对账单
BILL(70, "已结算"),//
PAYED(80, "待发货"),//
PART_PAYED(81,"部分付款"),
BILL_APPLY(82,"可提货"),
//PICK_UP(81, "备货中"),
COUNT(90, "已完成"),//
FINISH_ORDER(91, "已对账"),//已对账 目前用这个
REFUSE(98, "已驳回"),
CANCEL(99, "已取消"),
CANCELLED(130,"已关闭"),
PARTIAL_SHIPMENTS(140,"分批发货中"),
/*售后的状态*/
REFUNDING(7, "退款中"),
REFUND(120, "退款完成"); //已退款统一用这个值
private Integer value;
private String name;
OrderStatus(int value, String name) {
this.value = value;
this.name = name;
}
public static boolean contains(int value) {
for (OrderStatus anEnum : OrderStatus.values()) {
if (anEnum.value == value) {
return true;
}
}
return false;
}
public static String getDesc(int value) {
for (OrderStatus anEnum : OrderStatus.values()) {
if (anEnum.value == value) {
return anEnum.getName();
}
}
return "";
}
public static String getNameByValue(Integer value){
for(OrderStatus k : OrderStatus.values()){
if(value.equals(k.getValue())){
return k.name;
}
}
return null;
}
public static OrderStatus getOrderStatusEnum(Integer value) {
for (OrderStatus orderStatusEnum: OrderStatus.values()) {
if (orderStatusEnum.value.equals(value)) {
return orderStatusEnum;
}
}
return null;
}
public Integer getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.honghan.trade.enums;
public enum PaymentMethod {
PREPAY(1, "先款后货"),
DEPOSIT(2, "先付订金后付尾款"),
POSTPAY(3, "先货后款"),
MULTI_PAY(4, "多次付款");
private final int value;
private final String description;
PaymentMethod(int value, String description) {
this.value = value;
this.description = description;
}
public int getValue() {
return value;
}
public String getDescription() {
return description;
}
public static PaymentMethod fromValue(int value) {
for (PaymentMethod method : PaymentMethod.values()) {
if (method.value == value) {
return method;
}
}
return null;
}
}
package com.honghan.trade.enums;
public enum PaymentMethod {
PREPAY(1, "先款后货"),
DEPOSIT(2, "先付订金后付尾款"),
POSTPAY(3, "先货后款"),
MULTI_PAY(4, "多次付款");
private final int value;
private final String description;
PaymentMethod(int value, String description) {
this.value = value;
this.description = description;
}
public int getValue() {
return value;
}
public String getDescription() {
return description;
}
public static PaymentMethod fromValue(int value) {
for (PaymentMethod method : PaymentMethod.values()) {
if (method.value == value) {
return method;
}
}
return null;
}
}
package com.honghan.trade.util;
import com.honghan.common.core.enums.OrderStatus;
import com.honghan.trade.api.domain.TradeOrder;
import com.honghan.trade.enums.PaymentMethod;
import com.honghan.trade.statemachine.OrderStateMachine;
/**
* 订单状态机工具类
*/
public class OrderStateMachineUtil {
private static OrderStateMachine orderStateMachine = new OrderStateMachine();
public static void updateOrderStatus(TradeOrder tradeOrder) {
OrderStatus currentStatus = OrderStatus.getOrderStatusEnum(tradeOrder.getStatus());
PaymentMethod paymentMethod = PaymentMethod.fromValue(tradeOrder.getSettlePayType());
OrderStatus nextStatus = orderStateMachine.getNextStatus(currentStatus, paymentMethod, false);
tradeOrder.setStatus(nextStatus.getValue());
}
public static void updateOrderStatus(TradeOrder tradeOrder, OrderStatus currentStatus) {
tradeOrder.setStatus(currentStatus.getValue());
}
public static void updateOrderNextStatus(TradeOrder tradeOrder, OrderStatus currentStatus) {
PaymentMethod paymentMethod = PaymentMethod.fromValue(tradeOrder.getSettlePayType());
OrderStatus nextStatus = orderStateMachine.getNextStatus(currentStatus, paymentMethod, false);
tradeOrder.setStatus(currentStatus.getValue());
}
}