fix:订单状态模块

上级 903b2f1c
...@@ -8,24 +8,34 @@ import org.springframework.statemachine.annotation.OnTransition; ...@@ -8,24 +8,34 @@ import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine; import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
// 监听器是监听到 action 后进行状态的一个变更。
/**
* 监听器是监听到 action 后进行状态的一个变更。
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/29 19:18
*/
@Component("orderStateListener") @Component("orderStateListener")
@WithStateMachine(name="orderStateMachine") @WithStateMachine(name = "orderStateMachine")
public class OrderStateListener { public class OrderStateListener {
@OnTransition(source = "ORDER_WAIT_PAY", target = "ORDER_WAIT_SEND") @OnTransition(source = "ORDER_WAIT_PAY", target = "ORDER_WAIT_SEND")
public boolean payToSend(Message<OrderStateChangeAction> message){ public boolean payToSend(Message<OrderStateChangeAction> message) {
Order order = (Order) message.getHeaders().get("order"); Order order = (Order) message.getHeaders().get("order");
order.setOrderState(OrderState.ORDER_WAIT_SEND); order.setOrderState(OrderState.ORDER_WAIT_SEND);
return true; return true;
} }
@OnTransition(source = "ORDER_WAIT_SEND", target = "ORDER_WAIT_RECEIVE") @OnTransition(source = "ORDER_WAIT_SEND", target = "ORDER_WAIT_RECEIVE")
public boolean sendToReceive(Message<OrderStateChangeAction> message){ public boolean sendToReceive(Message<OrderStateChangeAction> message) {
Order order = (Order) message.getHeaders().get("order"); Order order = (Order) message.getHeaders().get("order");
order.setOrderState(OrderState.ORDER_WAIT_RECEIVE); order.setOrderState(OrderState.ORDER_WAIT_RECEIVE);
return true; return true;
} }
@OnTransition(source = "ORDER_WAIT_RECEIVE", target = "ORDER_FINISH") @OnTransition(source = "ORDER_WAIT_RECEIVE", target = "ORDER_FINISH")
public boolean receiveToFinish(Message<OrderStateChangeAction> message){ public boolean receiveToFinish(Message<OrderStateChangeAction> message) {
Order order = (Order) message.getHeaders().get("order"); Order order = (Order) message.getHeaders().get("order");
order.setOrderState(OrderState.ORDER_FINISH); order.setOrderState(OrderState.ORDER_FINISH);
return true; return true;
......
...@@ -16,14 +16,24 @@ import org.springframework.statemachine.support.DefaultStateMachineContext; ...@@ -16,14 +16,24 @@ import org.springframework.statemachine.support.DefaultStateMachineContext;
import java.util.EnumSet; import java.util.EnumSet;
/**
* 订单状态配置类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/29 19:22
*/
@Configuration @Configuration
@EnableStateMachine(name="orderStateMachine") @EnableStateMachine(name = "orderStateMachine")
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderState, OrderStateChangeAction> { public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<OrderState, OrderStateChangeAction> {
@Override
public void configure(StateMachineStateConfigurer<OrderState, OrderStateChangeAction> states) throws Exception { public void configure(StateMachineStateConfigurer<OrderState, OrderStateChangeAction> states) throws Exception {
states.withStates().initial(OrderState.ORDER_WAIT_PAY) states.withStates().initial(OrderState.ORDER_WAIT_PAY)
.states(EnumSet.allOf(OrderState.class)); .states(EnumSet.allOf(OrderState.class));
} }
@Override
public void configure(StateMachineTransitionConfigurer<OrderState, OrderStateChangeAction> transitions) throws Exception { public void configure(StateMachineTransitionConfigurer<OrderState, OrderStateChangeAction> transitions) throws Exception {
transitions.withExternal().source(OrderState.ORDER_WAIT_PAY) transitions.withExternal().source(OrderState.ORDER_WAIT_PAY)
.target(OrderState.ORDER_WAIT_SEND) .target(OrderState.ORDER_WAIT_SEND)
...@@ -38,7 +48,11 @@ public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<Order ...@@ -38,7 +48,11 @@ public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<Order
.event(OrderStateChangeAction.RECEIVE_ORDER); .event(OrderStateChangeAction.RECEIVE_ORDER);
} }
// 配置状态机持久化 /**
* 配置状态机持久化
*
* @return
*/
@Bean @Bean
public DefaultStateMachinePersister machinePersister() { public DefaultStateMachinePersister machinePersister() {
return new DefaultStateMachinePersister<>(new StateMachinePersist<Object, Object, Order>() { return new DefaultStateMachinePersister<>(new StateMachinePersist<Object, Object, Order>() {
......
package com.study.design.order.pojo; package com.study.design.order.pojo;
//状态转化的一个控制机。状态机:初始化状态;配置我们的所有状态之间的转化关系;
//一些持久化的工作(redis)。 /**
* 状态转化的一个控制机。状态机:初始化状态;配置我们的所有状态之间的转化关系;
* 一些持久化的工作(redis)。
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/29 19:18
*/
public class Order { public class Order {
/**
* 订单id
*/
private Integer orderId; private Integer orderId;
private OrderState orderState;//订单状态 /**
* 订单状态
*/
private OrderState orderState;
public Integer getOrderId() { public Integer getOrderId() {
return orderId; return orderId;
......
package com.study.design.order.pojo; package com.study.design.order.pojo;
/**
* 订单状态枚举类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/29 19:19
*/
public enum OrderState { public enum OrderState {
ORDER_WAIT_PAY, // 待支付 /**
ORDER_WAIT_SEND, // 待发货 * 待支付
ORDER_WAIT_RECEIVE, // 待收货 */
ORDER_FINISH; // 完成订单 ORDER_WAIT_PAY,
/**
* 待发货
*/
ORDER_WAIT_SEND,
/**
* 待收货
*/
ORDER_WAIT_RECEIVE,
/**
* 完成订单
*/
ORDER_FINISH;
} }
package com.study.design.order.pojo; package com.study.design.order.pojo;
/**
* 操作动作枚举
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/29 19:20
*/
public enum OrderStateChangeAction { public enum OrderStateChangeAction {
PAY_ORDER, // 支付操作 /**
SEND_ORDER, // 发货操作 * 支付操作
RECEIVE_ORDER; // 收货操作 */
PAY_ORDER,
/**
* 发货操作
*/
SEND_ORDER,
/**
* 收货操作
*/
RECEIVE_ORDER;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册