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<CustomerOrderStates, CustomerOrderEvents> {
|
|
public final static String MACHINE_ID = "customerOrderStateMachine";
|
|
@Override
|
public void configure(StateMachineStateConfigurer<CustomerOrderStates, CustomerOrderEvents> states) throws Exception {
|
states.withStates()
|
.initial(CustomerOrderStates.PLAN)
|
.states(EnumSet.allOf(CustomerOrderStates.class));
|
}
|
|
@Override
|
public void configure(StateMachineTransitionConfigurer<CustomerOrderStates, CustomerOrderEvents> 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<CustomerOrderStates, CustomerOrderEvents> config)
|
throws Exception {
|
config.withConfiguration().machineId(MACHINE_ID);
|
}
|
|
/**
|
* 持久化配置
|
*
|
* @return
|
*/
|
@Bean(name = "planPersister")
|
public StateMachinePersister<CustomerOrderStates, CustomerOrderEvents, CustomerOrder> persister() {
|
return new DefaultStateMachinePersister<>(new StateMachinePersist<CustomerOrderStates, CustomerOrderEvents, CustomerOrder>() {
|
@Override
|
public void write(StateMachineContext<CustomerOrderStates, CustomerOrderEvents> stateMachineContext, CustomerOrder customerOrder) throws Exception {
|
|
}
|
|
@Override
|
public StateMachineContext<CustomerOrderStates, CustomerOrderEvents> read(CustomerOrder customerOrder) throws Exception {
|
StateMachineContext<CustomerOrderStates, CustomerOrderEvents> result = new DefaultStateMachineContext<CustomerOrderStates, CustomerOrderEvents>(CustomerOrderStates.getEnum(customerOrder.getCoState()),
|
null, null, null, null, MACHINE_ID);
|
return result;
|
}
|
});
|
}
|
}
|