package com.chinaztt.mes.plan.state.orderstate; import com.chinaztt.mes.plan.entity.CustomerOrder; import com.chinaztt.mes.plan.state.orderstate.constant.CustomerOrderEvents; import com.chinaztt.mes.plan.state.orderstate.constant.CustomerOrderStates; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.statemachine.StateMachineContext; import org.springframework.statemachine.StateMachinePersist; import org.springframework.statemachine.config.EnableStateMachineFactory; import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter; import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer; import org.springframework.statemachine.config.builders.StateMachineStateConfigurer; import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer; import org.springframework.statemachine.persist.DefaultStateMachinePersister; import org.springframework.statemachine.persist.StateMachinePersister; import org.springframework.statemachine.support.DefaultStateMachineContext; import java.util.EnumSet; /** * @Author: cxf * @Date: 2020-09-19 17:21 */ @Configuration @EnableStateMachineFactory(name = "customerOrderStateMachineFactory") public class CustomerOrderStateMachineConfig extends EnumStateMachineConfigurerAdapter { public final static String MACHINE_ID = "customerOrderStateMachine"; @Override public void configure(StateMachineStateConfigurer states) throws Exception { states.withStates() .initial(CustomerOrderStates.PLAN) .states(EnumSet.allOf(CustomerOrderStates.class)); } @Override public void configure(StateMachineTransitionConfigurer transitions) throws Exception { // transitions // .withExternal() // .source(CustomerOrderStates.PLAN).target(CustomerOrderStates.PLANED) // .event(CustomerOrderEvents.PLANED) // .and() // .withExternal() // .source(CustomerOrderStates.PLAN).target(CustomerOrderStates.PLANING) // .event(CustomerOrderEvents.PLANING) // .and() // .withExternal() // .source(CustomerOrderStates.PLAN).target(CustomerOrderStates.COMPLETE) // .event(CustomerOrderEvents.COMPLETE) // .and() // .withExternal() // .source(CustomerOrderStates.PLANED).target(CustomerOrderStates.PLANING) // .event(CustomerOrderEvents.PLANING) // .and() // .withExternal() // .source(CustomerOrderStates.PLANED).target(CustomerOrderStates.COMPLETE) // .event(CustomerOrderEvents.COMPLETE) // .and() // .withExternal() // .source(CustomerOrderStates.PLANING).target(CustomerOrderStates.COMPLETE) // .event(CustomerOrderEvents.COMPLETE) // .and() // .withExternal() // .source(CustomerOrderStates.COMPLETE).target(CustomerOrderStates.PLANING) // .event(CustomerOrderEvents.PLANING) // .and() // .withExternal() // .source(CustomerOrderStates.COMPLETE).target(CustomerOrderStates.PLANED) // .event(CustomerOrderEvents.PLANED) // .and() // .withExternal() // .source(CustomerOrderStates.COMPLETE).target(CustomerOrderStates.PLAN) // .event(CustomerOrderEvents.PLAN) // .and() // .withExternal() // .source(CustomerOrderStates.PLANING).target(CustomerOrderStates.PLANED) // .event(CustomerOrderEvents.PLANED) // .and() // .withExternal() // .source(CustomerOrderStates.PLANING).target(CustomerOrderStates.PLAN) // .event(CustomerOrderEvents.PLAN) // .and() // .withExternal() // .source(CustomerOrderStates.PLANED).target(CustomerOrderStates.PLAN) // .event(CustomerOrderEvents.PLAN); } @Override public void configure(StateMachineConfigurationConfigurer config) throws Exception { config.withConfiguration().machineId(MACHINE_ID); } /** * 持久化配置 * * @return */ @Bean(name = "planPersister") public StateMachinePersister persister() { return new DefaultStateMachinePersister<>(new StateMachinePersist() { @Override public void write(StateMachineContext stateMachineContext, CustomerOrder customerOrder) throws Exception { } @Override public StateMachineContext read(CustomerOrder customerOrder) throws Exception { StateMachineContext result = new DefaultStateMachineContext(CustomerOrderStates.getEnum(customerOrder.getCoState()), null, null, null, null, MACHINE_ID); return result; } }); } }