Fixiaobai
2023-10-13 0545c459dfb24c503bdf280794c2129f1a2aafe7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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;
            }
        });
    }
}