package com.chinaztt.mes.technology.state.bom;
|
|
import com.chinaztt.mes.technology.entity.Bom;
|
import com.chinaztt.mes.technology.entity.Routing;
|
import com.chinaztt.mes.technology.state.bom.constant.BomEvents;
|
import com.chinaztt.mes.technology.state.bom.constant.BomStates;
|
import com.chinaztt.mes.technology.state.routing.constant.RoutingEvents;
|
import com.chinaztt.mes.technology.state.routing.constant.RoutingStates;
|
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 = "bomStateMachineFactory")
|
public class BomStateMachineConfig extends EnumStateMachineConfigurerAdapter<BomStates, BomEvents> {
|
|
public final static String MACHINE_ID = "bomStateMachine";
|
|
@Override
|
public void configure(StateMachineStateConfigurer<BomStates, BomEvents> states) throws Exception {
|
states.withStates()
|
.initial(BomStates.DRAFT)
|
.states(EnumSet.allOf(BomStates.class));
|
}
|
|
@Override
|
public void configure(StateMachineTransitionConfigurer<BomStates, BomEvents> transitions) throws Exception {
|
transitions
|
.withExternal()
|
.source(BomStates.DRAFT).target(BomStates.ACCEPTED)
|
.event(BomEvents.ACCEPT)
|
.and()
|
.withExternal()
|
.source(BomStates.ACCEPTED).target(BomStates.DRAFT)
|
.event(BomEvents.REVOKE)
|
.and()
|
.withExternal()
|
.source(BomStates.ACCEPTED).target(BomStates.CANCELLED)
|
.event(BomEvents.CANCEL);
|
}
|
|
@Override
|
public void configure(StateMachineConfigurationConfigurer<BomStates, BomEvents> config)
|
throws Exception {
|
config.withConfiguration().machineId(MACHINE_ID);
|
}
|
|
/**
|
* 持久化配置
|
*
|
* @return
|
*/
|
@Bean(name ="bomPersister")
|
public StateMachinePersister<BomStates, BomEvents, Bom> persister() {
|
return new DefaultStateMachinePersister<>(new StateMachinePersist<BomStates, BomEvents, Bom>() {
|
@Override
|
public void write(StateMachineContext<BomStates, BomEvents> stateMachineContext, Bom bom) throws Exception {
|
|
}
|
|
@Override
|
public StateMachineContext<BomStates, BomEvents> read(Bom bom) throws Exception {
|
StateMachineContext<BomStates, BomEvents> result = new DefaultStateMachineContext<BomStates, BomEvents>(BomStates.getEnum(bom.getState()),
|
null, null, null, null, MACHINE_ID);
|
return result;
|
}
|
});
|
}
|
|
}
|