吉安感知网项目-后端
linwei
5 days ago e28a746eb7cbb23a3d7bbb78dba0ae3023e8215d
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *      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;
 
/**
 * 事件状态枚举类
 * <p>
 * 定义事件状态的编码和名称,以及合法的状态流转矩阵
 * </p>
 *
 * @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<Integer, List<Integer>> 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<Integer> allowedStatuses = STATUS_FLOW_MATRIX.get(currentStatus);
        return allowedStatuses != null && allowedStatuses.contains(targetStatus);
    }
 
    /**
     * 获取指定状态允许流转的所有目标状态编码列表
     *
     * @param currentStatus 当前状态编码
     * @return 允许流转的目标状态编码列表
     */
    public static List<Integer> getAllowedTransitions(Integer currentStatus) {
        if (currentStatus == null) {
            return Arrays.asList();
        }
        List<Integer> 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;
    }
}