Java-Until 如何将枚举类返回给前端
Posted 赵晓东-Nastu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Until 如何将枚举类返回给前端相关的知识,希望对你有一定的参考价值。
一、业务场景
现在要展示下图中的客户成熟度,客户成熟度根据新签和续签的值是不一样的,新签是0-6类,续签是0-8类,我们后端是定义为了两个枚举,那么如何将这两个枚举展示到前端呢?
二、解决方案
新签枚举
package com.hc360.crm.common.enums;
import com.hc360.crm.utils.BaseEnum;
import lombok.Getter;
/**
* @Classname NewSignatureEnum
* @Description TODO
* @Date 2021/10/14 13:55
* @Author zhaoxiaodong
*/
@Getter
public enum NewSignatureEnum implements BaseEnum {
ZERO_CUSTOMERMATURITY(0, "0类"),
ONE_CUSTOMERMATURITY(1, "1类"),
TWO_CUSTOMERMATURITY(2, "2类"),
THREE_CUSTOMERMATURITY(3, "3类"),
FOUR_CUSTOMERMATURITY(4, "4类"),
FIVE_CUSTOMERMATURITY(5, "5类"),
SIX_CUSTOMERMATURITY(6, "6类"),
;
//成员变量
private Integer code;
private String desc;
//构造方法
NewSignatureEnum(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public static String getName(int index){
for (NewSignatureEnum newSignatureEnum : NewSignatureEnum.values()) {
if (newSignatureEnum.getCode()==index) {
return newSignatureEnum.getDesc();
}
}
return null;
}
}
续签枚举
package com.hc360.crm.common.enums;
import com.hc360.crm.utils.BaseEnum;
import lombok.Getter;
/**
* @Classname CustomerMaturityEnum
* @Description TODO
* @Date 2021/10/14 10:57
* @Author zhaoxiaodong
*/
@Getter
public enum OldSignatureEnum implements BaseEnum {
ZERO_CUSTOMERMATURITY(0, "0类"),
ONE_CUSTOMERMATURITY(1, "1类"),
TWO_CUSTOMERMATURITY(2, "2类"),
THREE_CUSTOMERMATURITY(3, "3类"),
FOUR_CUSTOMERMATURITY(4, "4类"),
FIVE_CUSTOMERMATURITY(5, "5类"),
SIX_CUSTOMERMATURITY(6, "6类"),
SEVEN_CUSTOMERMATURITY(7, "7类"),
EIGHT_CUSTOMERMATURITY(8, "8类");
//成员变量
private int code;
private String desc;
//构造方法
OldSignatureEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
//获取Name
public static String getName(int index){
for (OldSignatureEnum customerMaturityEnum : OldSignatureEnum.values()) {
if (customerMaturityEnum.getCode() == index){
return customerMaturityEnum.getDesc();
}
}
return null;
}
}
前端通过传入我们分身ID,通过分身ID我们可以判断这个分身到底是新签还是续签
public Integer isNewOrOldSigNature(String userId) {
return crmCustomerSubtotalMapper.getSignFlag(userId);
}
我们定义一个实体接受
@Data
@ApiModel("客户成熟度")
public class CustomerMaturityVo {
@ApiModelProperty("是否为新签, 0-新签 1-续签")
private Integer isNewSignature;
@ApiModelProperty("客户成熟度")
private List list;
}
再通过将枚举转换为list
package com.hc360.crm.utils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
/**
* @Classname EnumListUtil
* @Description TODO
* @Date 2021/10/14 15:24
* @Author zhaoxiaodong
*/
public class EnumListUtil {
private static String ENUM_CLASSPATH="java.lang.Enum";
public static List<Map<String, Object>> enumToListMap(Class<?> enumClass) {
List<Map<String, Object>> resultList= new ArrayList<>();
if (!ENUM_CLASSPATH.equals(enumClass.getSuperclass().getCanonicalName())) {
return resultList;
}
// 获取所有public方法
Method[] methods = enumClass.getMethods();
List<Field> fieldList = new ArrayList<>();
//1.通过get方法提取字段,避免get作为自定义方法的开头,建议使用 ‘find’或其余命名
Arrays.stream(methods)
.map(Method::getName)
.filter(
methodName -> methodName.startsWith("get") && !"getDeclaringClass".equals(methodName) && !"getClass".equals(methodName)
).forEachOrdered(methodName -> {
try {
Field field = enumClass.getDeclaredField(StringUtils.uncapitalize(methodName.substring(3)));
if (null != field) {
fieldList.add(field);
}
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
});
//2.将字段作为key,逐一把枚举值作为value 存入list
if (CollectionUtils.isEmpty(fieldList)) {
return resultList;
}
Enum<?>[] enums = (Enum[]) enumClass.getEnumConstants();
for (Enum<?> anEnum : enums) {
Map<String, Object> map = new HashMap<>(fieldList.size());
for (Field field : fieldList) {
field.setAccessible(true);
try {
// 向map集合添加字段名称 和 字段值
map.put(field.getName(), field.get(anEnum));
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
// 将Map添加到集合中
resultList.add(map);
}
return resultList;
}
// public static void main(String[] args) {
//
//
// // 枚举类
// System.out.println(enumToListMap(EducateStatusEnum.class));
//
//
// }
}
运用到我们项目中
public CustomerMaturityVo selectCustomerMaturity(String userId) {
CustomerMaturityVo customerMaturityVo = new CustomerMaturityVo();
//根据新签客户还是续签客户返回不同的客户的成熟度
Integer newOrOldSigNature = isNewOrOldSigNature(userId);
// 0-新签、1-续签
if (newOrOldSigNature == 0){
customerMaturityVo.setIsNewSignature(0);
List<Map<String, Object>> maps = EnumListUtil.enumToListMap(NewSignatureEnum.class);
customerMaturityVo.setList(maps);
}else{
customerMaturityVo.setIsNewSignature(1);
List<Map<String, Object>> maps = EnumListUtil.enumToListMap(OldSignatureEnum.class);
customerMaturityVo.setList(maps);
}
return customerMaturityVo;
}
测试
这就大功告成了,大家还有什么好的思路可以互相交流哦。
以上是关于Java-Until 如何将枚举类返回给前端的主要内容,如果未能解决你的问题,请参考以下文章
C#如何将枚举类(enum)型转换成字符(string)类型