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);
|
}
|
}
|