liusheng
昨天 b57304d9917beab4671442a0018c1c3a2d681640
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
package com.smartor.common.enums;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
/**
 * 住院状态枚举
 * 
 * @author smartor
 */
public enum InhospStateEnum {
    
    /**
     * 预入院状态
     */
    PRE_ADMISSION("3", "预入院", new String[]{"0"}),
    
    /**
     * 在院状态
     */
    IN_HOSPITAL("0", "在院", new String[]{"1"}),
    
    /**
     * 出院状态
     */
    DISCHARGED("1", "出院", new String[]{});
 
    /**
     * 状态码
     */
    private final String code;
    
    /**
     * 状态描述
     */
    private final String description;
    
    /**
     * 允许转换的下一状态码集合
     */
    private final Set<String> allowedNextStates;
 
    InhospStateEnum(String code, String description, String[] allowedNextStates) {
        this.code = code;
        this.description = description;
        this.allowedNextStates = new HashSet<>(Arrays.asList(allowedNextStates));
    }
 
    public String getCode() {
        return code;
    }
 
    public String getDescription() {
        return description;
    }
 
    /**
     * 判断是否允许转换到目标状态
     * 
     * @param targetStateCode 目标状态码
     * @return 是否允许转换
     */
    public boolean canTransitionTo(String targetStateCode) {
        return allowedNextStates.contains(targetStateCode);
    }
 
    /**
     * 根据状态码获取枚举
     * 
     * @param code 状态码
     * @return 对应的枚举,如果不存在返回null
     */
    public static InhospStateEnum fromCode(String code) {
        for (InhospStateEnum state : values()) {
            if (state.getCode().equals(code)) {
                return state;
            }
        }
        return null;
    }
 
    /**
     * 验证状态转换是否合法
     * 
     * @param currentStateCode 当前状态码
     * @param targetStateCode 目标状态码
     * @return 是否允许转换
     */
    public static boolean isValidTransition(String currentStateCode, String targetStateCode) {
        if (currentStateCode == null) {
            // 初始状态,只允许创建预入院或在院
            return "3".equals(targetStateCode) || "0".equals(targetStateCode);
        }
        
        InhospStateEnum currentState = fromCode(currentStateCode);
        if (currentState == null) {
            return false;
        }
        
        return currentState.canTransitionTo(targetStateCode);
    }
 
    /**
     * 获取所有允许的下一状态
     * 
     * @return 允许的下一状态集合
     */
    public Set<String> getAllowedNextStates() {
        return new HashSet<>(allowedNextStates);
    }
}