1、创建对应的枚举类
package cs.gov.bigdata.ca.ability.portal.enums;
import com.lgx.common.util.KeyValue;
import lombok.Getter;
import java.util.*;
/**
* 发布状态类枚举
*
*/
@Getter
public enum NoticeStatusEnum {
/**
* 未发布
*/
NO(0, "未发布"),
/**
* 已同步
*/
YES(1, "已发布");
private Integer value;
private String text;
private static final Map<Integer, String> ENUM_MAP;
private static final List<KeyValue<Integer, String>> KEY_VALUES;
static {
ENUM_MAP = new HashMap<>(NoticeStatusEnum.values().length);
List<KeyValue<Integer, String>> list = new ArrayList<>(NoticeStatusEnum.values().length);
for (NoticeStatusEnum g : NoticeStatusEnum.values()) {
ENUM_MAP.put(g.getValue(), g.getText());
list.add(new KeyValue<>(g.getValue(), g.getText()));
}
KEY_VALUES = Collections.unmodifiableList(list);
}
NoticeStatusEnum(Integer value, String text) {
this.value = value;
this.text = text;
}
public static List<KeyValue<Integer, String>> keyValues() {
return KEY_VALUES;
}
public static String getValueText(Integer value) {
return ENUM_MAP.get(value);
}
public static Integer getValue(String text) {
for (NoticeStatusEnum h : NoticeStatusEnum.values()) {
if (h.text.equals(text)) {
return h.value;
}
}
return null;
}
}
package com.lgx.common.util;
/**
* 双属性类型枚举封装类
*/
public class KeyValue<K, V> {
private static final KeyValue<Object, Object> EMPTY = new KeyValue.EmptyKeyValue();
private K key;
private V value;
public KeyValue() {
}
public KeyValue(K key) {
this.key = key;
}
public KeyValue(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return this.key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return this.value;
}
public void setValue(V value) {
this.value = value;
}
public String toString() {
return "KeyValue{key=" + this.key + ", value=" + this.value + "}";
}
public static <K, V> KeyValue<K, V> emptyKeyValue() {
return EMPTY;
}
private static class EmptyKeyValue<K, V> extends KeyValue<K, V> {
private EmptyKeyValue() {
}
public void setKey(K key) {
}
public void setValue(V value) {
}
}
}
package cs.gov.bigdata.ca.ability.common.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Description 多属性类型枚举类型,需要多少属性可自己定义
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonVO {
private Object key;
private Object value;
private Object value2;
}
2、枚举类使用
/**
* 罗列枚举中的所有数据 -- 一般是用户给前端做数据回显或者下拉框选择时用
* @return Result
*/
@PreAuth
@Log(value = "服务事项管理相关下拉选", exception = "服务事项管理相关下拉选请求异常")
@GetMapping("/enums")
@Operation(summary = "服务事项管理相关下拉选", description = "相关下拉选")
public Result<?> enums() {
Map<String,Object> result = new HashMap<>();
//服务状态 -- 直接获取枚举类型数据
result.put("status", NoticeStatusEnum.keyValues());
//服务机构人员 -- 从数据库获取数据集合后,封装成双属性枚举
result.put("affiliatedPerson",servePersonService.list().stream().map(s -> {
return new KeyValue<>(s.getId(), s);
}).collect(Collectors.toList()));
//所需材料 -- 从数据库获取数据集合后,封装成多属性枚举
List<FileInfo> requiredInformationList = fileInfoService.list();
result.put("requiredInformation",requiredInformationList.stream().map(s -> {
return new CommonVO(s.getId(), s.getFileName(), s.getFilePath());
}).collect(Collectors.toList()));
return Result.data(result);
}