diff --git a/src/main/java/com/study/design/order/OrderStateListener.java b/src/main/java/com/study/design/order/OrderStateListener.java index 2aea114fa8be74b9338d5be542bd2f355786d6ca..a4f4152c954c2452e9dab9779d7f5ea9960dcf6e 100644 --- a/src/main/java/com/study/design/order/OrderStateListener.java +++ b/src/main/java/com/study/design/order/OrderStateListener.java @@ -8,24 +8,34 @@ import org.springframework.statemachine.annotation.OnTransition; import org.springframework.statemachine.annotation.WithStateMachine; import org.springframework.stereotype.Component; -// 监听器是监听到 action 后进行状态的一个变更。 + +/** + * 监听器是监听到 action 后进行状态的一个变更。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/29 19:18 + */ @Component("orderStateListener") -@WithStateMachine(name="orderStateMachine") +@WithStateMachine(name = "orderStateMachine") public class OrderStateListener { + @OnTransition(source = "ORDER_WAIT_PAY", target = "ORDER_WAIT_SEND") - public boolean payToSend(Message message){ + public boolean payToSend(Message message) { Order order = (Order) message.getHeaders().get("order"); order.setOrderState(OrderState.ORDER_WAIT_SEND); return true; } + @OnTransition(source = "ORDER_WAIT_SEND", target = "ORDER_WAIT_RECEIVE") - public boolean sendToReceive(Message message){ + public boolean sendToReceive(Message message) { Order order = (Order) message.getHeaders().get("order"); order.setOrderState(OrderState.ORDER_WAIT_RECEIVE); return true; } + @OnTransition(source = "ORDER_WAIT_RECEIVE", target = "ORDER_FINISH") - public boolean receiveToFinish(Message message){ + public boolean receiveToFinish(Message message) { Order order = (Order) message.getHeaders().get("order"); order.setOrderState(OrderState.ORDER_FINISH); return true; diff --git a/src/main/java/com/study/design/order/config/OrderStateMachineConfig.java b/src/main/java/com/study/design/order/config/OrderStateMachineConfig.java index 5f4575f11715efe0b1364593ce03171e737eb7fc..e80aa5c2003a9e2d91e302149fb0f712a0ad74c8 100644 --- a/src/main/java/com/study/design/order/config/OrderStateMachineConfig.java +++ b/src/main/java/com/study/design/order/config/OrderStateMachineConfig.java @@ -16,14 +16,24 @@ import org.springframework.statemachine.support.DefaultStateMachineContext; import java.util.EnumSet; + +/** + * 订单状态配置类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/29 19:22 + */ @Configuration -@EnableStateMachine(name="orderStateMachine") +@EnableStateMachine(name = "orderStateMachine") public class OrderStateMachineConfig extends StateMachineConfigurerAdapter { + @Override public void configure(StateMachineStateConfigurer states) throws Exception { states.withStates().initial(OrderState.ORDER_WAIT_PAY) .states(EnumSet.allOf(OrderState.class)); } + @Override public void configure(StateMachineTransitionConfigurer transitions) throws Exception { transitions.withExternal().source(OrderState.ORDER_WAIT_PAY) .target(OrderState.ORDER_WAIT_SEND) @@ -38,7 +48,11 @@ public class OrderStateMachineConfig extends StateMachineConfigurerAdapter(new StateMachinePersist() { diff --git a/src/main/java/com/study/design/order/pojo/Order.java b/src/main/java/com/study/design/order/pojo/Order.java index 46e0184790dbc863db08fd8d5d7738cf8f178a4c..1eb4085f41a15f60b600dc66d077e881d51477e2 100644 --- a/src/main/java/com/study/design/order/pojo/Order.java +++ b/src/main/java/com/study/design/order/pojo/Order.java @@ -1,10 +1,23 @@ package com.study.design.order.pojo; -//状态转化的一个控制机。状态机:初始化状态;配置我们的所有状态之间的转化关系; -//一些持久化的工作(redis)。 + +/** + * 状态转化的一个控制机。状态机:初始化状态;配置我们的所有状态之间的转化关系; + * 一些持久化的工作(redis)。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/29 19:18 + */ public class Order { + /** + * 订单id + */ private Integer orderId; - private OrderState orderState;//订单状态 + /** + * 订单状态 + */ + private OrderState orderState; public Integer getOrderId() { return orderId; diff --git a/src/main/java/com/study/design/order/pojo/OrderState.java b/src/main/java/com/study/design/order/pojo/OrderState.java index 0b959453e497d57264f4c44b17ccc6b46d400d06..cb75fa8f9c065244ec7f7e4ec6ff931f84f7afd6 100644 --- a/src/main/java/com/study/design/order/pojo/OrderState.java +++ b/src/main/java/com/study/design/order/pojo/OrderState.java @@ -1,8 +1,28 @@ package com.study.design.order.pojo; -public enum OrderState { - ORDER_WAIT_PAY, // 待支付 - ORDER_WAIT_SEND, // 待发货 - ORDER_WAIT_RECEIVE, // 待收货 - ORDER_FINISH; // 完成订单 + +/** + * 订单状态枚举类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/29 19:19 + */ +public enum OrderState { + /** + * 待支付 + */ + ORDER_WAIT_PAY, + /** + * 待发货 + */ + ORDER_WAIT_SEND, + /** + * 待收货 + */ + ORDER_WAIT_RECEIVE, + /** + * 完成订单 + */ + ORDER_FINISH; } diff --git a/src/main/java/com/study/design/order/pojo/OrderStateChangeAction.java b/src/main/java/com/study/design/order/pojo/OrderStateChangeAction.java index dd7f65aec7e1cdd5f7476dc5bd8d692f794d0d81..aec085cea991dbdd9ed9ea6826e141abf367d287 100644 --- a/src/main/java/com/study/design/order/pojo/OrderStateChangeAction.java +++ b/src/main/java/com/study/design/order/pojo/OrderStateChangeAction.java @@ -1,7 +1,24 @@ package com.study.design.order.pojo; + +/** + * 操作动作枚举 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/29 19:20 + */ public enum OrderStateChangeAction { - PAY_ORDER, // 支付操作 - SEND_ORDER, // 发货操作 - RECEIVE_ORDER; // 收货操作 + /** + * 支付操作 + */ + PAY_ORDER, + /** + * 发货操作 + */ + SEND_ORDER, + /** + * 收货操作 + */ + RECEIVE_ORDER; }