package com.chinaztt.mes.production.state.productout;
|
|
import com.chinaztt.mes.production.entity.OperationTask;
|
import com.chinaztt.mes.production.entity.ProductOutput;
|
import com.chinaztt.mes.production.state.productout.constant.ProductOutEvents;
|
import com.chinaztt.mes.production.state.productout.constant.ProductOutStates;
|
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 = "productOutStateMachineFactory")
|
public class ProductOutStateMachineConfig extends EnumStateMachineConfigurerAdapter<ProductOutStates, ProductOutEvents> {
|
|
public final static String MACHINE_ID = "productOutStateMachine";
|
|
@Override
|
public void configure(StateMachineStateConfigurer<ProductOutStates, ProductOutEvents> states) throws Exception {
|
states.withStates()
|
.initial(ProductOutStates.DRAFT)
|
.states(EnumSet.allOf(ProductOutStates.class));
|
}
|
|
@Override
|
public void configure(StateMachineTransitionConfigurer<ProductOutStates, ProductOutEvents> transitions) throws Exception {
|
transitions
|
.withExternal()
|
.source(ProductOutStates.DRAFT).target(ProductOutStates.SUBMITTED)
|
.event(ProductOutEvents.SUBMIT)
|
.and()
|
.withExternal()
|
.source(ProductOutStates.SUBMITTED).target(ProductOutStates.DRAFT)
|
.event(ProductOutEvents.REVOKE)
|
.and()
|
.withExternal()
|
.source(ProductOutStates.CHANGESHIFT).target(ProductOutStates.DRAFT)
|
.event(ProductOutEvents.CHANGESHIFTTODRAFT)
|
.and()
|
.withExternal()
|
.source(ProductOutStates.DRAFT).target(ProductOutStates.CHANGESHIFT)
|
.event(ProductOutEvents.DRAFTTOCHANGESHIFT);
|
}
|
|
@Override
|
public void configure(StateMachineConfigurationConfigurer<ProductOutStates, ProductOutEvents> config)
|
throws Exception {
|
config.withConfiguration().machineId(MACHINE_ID);
|
}
|
|
/**
|
* 持久化配置
|
*
|
* @return
|
*/
|
@Bean(name = "productOutPersister")
|
public StateMachinePersister<ProductOutStates, ProductOutEvents, ProductOutput> persister() {
|
return new DefaultStateMachinePersister<>(new StateMachinePersist<ProductOutStates, ProductOutEvents, ProductOutput>() {
|
@Override
|
public void write(StateMachineContext<ProductOutStates, ProductOutEvents> stateMachineContext, ProductOutput productOutput) throws Exception {
|
|
}
|
|
@Override
|
public StateMachineContext<ProductOutStates, ProductOutEvents> read(ProductOutput productOutput) throws Exception {
|
StateMachineContext<ProductOutStates, ProductOutEvents> result = new DefaultStateMachineContext<ProductOutStates, ProductOutEvents>(ProductOutStates.getEnum(productOutput.getState()),
|
null, null, null, null, MACHINE_ID);
|
return result;
|
}
|
});
|
}
|
|
}
|