package com.chinaztt.mes.production.state.productmain;
|
|
import com.chinaztt.mes.production.entity.ProductMain;
|
import com.chinaztt.mes.production.state.productmain.constant.ProductMainEvents;
|
import com.chinaztt.mes.production.state.productmain.constant.ProductMainStates;
|
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: zhangxy
|
* @Date: 2020-08-24 17:21
|
*/
|
@Configuration
|
@EnableStateMachineFactory(name = "productMainStateMachineFactory")
|
public class ProductMainStateMachineConfig extends EnumStateMachineConfigurerAdapter<ProductMainStates, ProductMainEvents> {
|
|
public final static String MACHINE_ID = "productMainStateMachine";
|
|
@Override
|
public void configure(StateMachineStateConfigurer<ProductMainStates, ProductMainEvents> states) throws Exception {
|
states.withStates()
|
.initial(ProductMainStates.DRAFT)
|
.states(EnumSet.allOf(ProductMainStates.class));
|
}
|
|
@Override
|
public void configure(StateMachineTransitionConfigurer<ProductMainStates, ProductMainEvents> transitions) throws Exception {
|
transitions
|
.withExternal()
|
.source(ProductMainStates.PROCESSING).target(ProductMainStates.SUBMITTED)
|
.event(ProductMainEvents.SUBMIT)
|
.and()
|
.withExternal()
|
.source(ProductMainStates.SUBMITTED).target(ProductMainStates.DRAFT)
|
.event(ProductMainEvents.REVOKE)
|
.and()
|
.withExternal()
|
.source(ProductMainStates.CHANGESHIFT).target(ProductMainStates.DRAFT)
|
.event(ProductMainEvents.CHANGESHIFTTODRAFT)
|
.and()
|
.withExternal()
|
.source(ProductMainStates.DRAFT).target(ProductMainStates.CHANGESHIFT)
|
.event(ProductMainEvents.DRAFTTOCHANGESHIFT)
|
.and()
|
.withExternal()
|
.source(ProductMainStates.DRAFT).target(ProductMainStates.SUBMITTED)
|
.event(ProductMainEvents.SUBMIT);
|
}
|
|
@Override
|
public void configure(StateMachineConfigurationConfigurer<ProductMainStates, ProductMainEvents> config)
|
throws Exception {
|
config.withConfiguration().machineId(MACHINE_ID);
|
}
|
|
/**
|
* 持久化配置
|
*
|
* @return
|
*/
|
@Bean(name = "productMainPersister")
|
public StateMachinePersister<ProductMainStates, ProductMainEvents, ProductMain> persister() {
|
return new DefaultStateMachinePersister<>(new StateMachinePersist<ProductMainStates, ProductMainEvents, ProductMain>() {
|
@Override
|
public void write(StateMachineContext<ProductMainStates, ProductMainEvents> stateMachineContext, ProductMain operationTask) throws Exception {
|
|
}
|
|
@Override
|
public StateMachineContext<ProductMainStates, ProductMainEvents> read(ProductMain operationTask) throws Exception {
|
StateMachineContext<ProductMainStates, ProductMainEvents> result = new DefaultStateMachineContext<ProductMainStates, ProductMainEvents>(ProductMainStates.getEnum(operationTask.getState()),
|
null, null, null, null, MACHINE_ID);
|
return result;
|
}
|
});
|
}
|
|
}
|