/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.sxkj.gd.workorder.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
/**
* 事件状态枚举类
*
* 定义事件状态的编码和名称,以及合法的状态流转矩阵
*
*
* @author system
* @since 2026-07-29
*/
@Getter
@AllArgsConstructor
public enum EventStatusEnum {
/**
* 待确认
*/
PENDING_CONFIRM(0, "待确认"),
/**
* 处置中
*/
DISPOSING(1, "处置中"),
/**
* 已退回
*/
RETURNED(2, "已退回"),
/**
* 已处置
*/
DISPOSED(3, "已处置"),
/**
* 已完成
*/
COMPLETED(4, "已完成");
/**
* 状态编码
*/
private final Integer code;
/**
* 状态名称
*/
private final String name;
/**
* 合法状态流转矩阵
* Key: 当前状态编码
* Value: 允许流转到的目标状态编码列表
*/
private static final Map> STATUS_FLOW_MATRIX = new HashMap<>();
static {
// 初始化状态流转矩阵
// 0-待确认 -> 1-处置中, 2-已退回
STATUS_FLOW_MATRIX.put(0, Arrays.asList(1, 2));
// 1-处置中 -> 2-已退回, 3-已处置
STATUS_FLOW_MATRIX.put(1, Arrays.asList(2, 3));
// 2-已退回 -> 0-待确认, 1-处置中
STATUS_FLOW_MATRIX.put(2, Arrays.asList(0, 1));
// 3-已处置 -> 2-已退回, 4-已完成
STATUS_FLOW_MATRIX.put(3, Arrays.asList(1, 4));
// 4-已完成 -> 终态,不允许流转
STATUS_FLOW_MATRIX.put(4, Arrays.asList());
}
/**
* 根据状态编码获取枚举对象
*
* @param code 状态编码
* @return 枚举对象,如果不存在则返回null
*/
public static EventStatusEnum getByCode(Integer code) {
if (code == null) {
return null;
}
for (EventStatusEnum status : values()) {
if (status.getCode().equals(code)) {
return status;
}
}
return null;
}
/**
* 根据状态编码获取状态名称
*
* @param code 状态编码
* @return 状态名称,如果不存在则返回"未知状态"
*/
public static String getNameByCode(Integer code) {
EventStatusEnum status = getByCode(code);
return status != null ? status.getName() : "未知状态";
}
/**
* 校验状态流转是否合法
*
* @param currentStatus 当前状态编码
* @param targetStatus 目标状态编码
* @return true-合法,false-非法
*/
public static boolean isValidTransition(Integer currentStatus, Integer targetStatus) {
if (currentStatus == null || targetStatus == null) {
return false;
}
List allowedStatuses = STATUS_FLOW_MATRIX.get(currentStatus);
return allowedStatuses != null && allowedStatuses.contains(targetStatus);
}
/**
* 获取指定状态允许流转的所有目标状态编码列表
*
* @param currentStatus 当前状态编码
* @return 允许流转的目标状态编码列表
*/
public static List getAllowedTransitions(Integer currentStatus) {
if (currentStatus == null) {
return Arrays.asList();
}
List allowed = STATUS_FLOW_MATRIX.get(currentStatus);
return allowed != null ? allowed : Arrays.asList();
}
/**
* 校验状态编码是否有效
*
* @param code 状态编码
* @return true-有效,false-无效
*/
public static boolean isValidCode(Integer code) {
return getByCode(code) != null;
}
/**
* 获取所有状态编码列表(按编码升序)
*
* @return 所有状态编码列表
*/
public static List getAllStatusCodes() {
return Arrays.asList(0, 1, 2, 3, 4);
}
}