2022-03-01 16:45:57 +08:00
|
|
|
|
package com.engine.salary.util;
|
|
|
|
|
|
|
|
|
|
|
|
import com.engine.salary.enums.BaseEnum;
|
|
|
|
|
|
import com.engine.salary.enums.sicategory.PaymentScopeEnum;
|
2022-07-26 18:01:39 +08:00
|
|
|
|
import com.engine.salary.exception.SalaryRunTimeException;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-03-01 16:45:57 +08:00
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
2022-07-26 18:01:39 +08:00
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
import java.util.*;
|
2022-03-01 16:45:57 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-07-27 16:56:13 +08:00
|
|
|
|
* 枚举工具类
|
2022-07-26 18:01:39 +08:00
|
|
|
|
* <p>Copyright: Copyright (c) 2022</p>
|
|
|
|
|
|
* <p>Company: 泛微软件</p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author qiantao
|
|
|
|
|
|
* @version 1.0
|
|
|
|
|
|
**/
|
|
|
|
|
|
@Slf4j
|
2022-03-01 16:45:57 +08:00
|
|
|
|
public class SalaryEnumUtil {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据枚举的value获取枚举对象
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param value 枚举中的value
|
|
|
|
|
|
* @param list
|
|
|
|
|
|
* @param T
|
|
|
|
|
|
* @param <T>
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
public static <T> T enumMatchByValue(Integer value, BaseEnum<Integer>[] list, Class<? extends BaseEnum<Integer>> T) {
|
|
|
|
|
|
return (T) Arrays.stream(list).filter(item -> Objects.equals(item.getValue(), value)).findFirst().get();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 枚举数组转字符串
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param list
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String enumArrToString(BaseEnum<Integer>[] list) {
|
|
|
|
|
|
List<String> collect = Arrays.stream(list).map(item -> String.valueOf(item.getValue())).collect(Collectors.toList());
|
|
|
|
|
|
return StringUtils.join(collect, ",");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static PaymentScopeEnum[] stringToEnums(String values, String charSequence) {
|
|
|
|
|
|
String[] arr = values.split(charSequence);
|
|
|
|
|
|
PaymentScopeEnum[] enumConstants = PaymentScopeEnum.values();
|
|
|
|
|
|
List<PaymentScopeEnum> collect = Arrays.stream(arr)
|
2022-07-26 18:01:39 +08:00
|
|
|
|
.map(item -> Arrays.stream(enumConstants).filter(s -> Objects.equals(String.valueOf(s.getValue()), item)).findFirst().get()).collect(Collectors.toList());
|
2022-03-01 16:45:57 +08:00
|
|
|
|
return collect.toArray(new PaymentScopeEnum[collect.size()]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-26 18:01:39 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取枚举下拉列表
|
|
|
|
|
|
* @param enumName 枚举路径,列如:com.engine.salary.enums.salaryarchive.SalaryArchiveStatusEnum
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static List<Map<String, Object>> enumSelectList(String enumName) {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
List<Map<String, Object>> keyValueList = new ArrayList<Map<String, Object>>();
|
|
|
|
|
|
|
|
|
|
|
|
Class cls = Class.forName(enumName);
|
|
|
|
|
|
|
|
|
|
|
|
Method method = cls.getMethod("values");
|
|
|
|
|
|
|
|
|
|
|
|
BaseEnum enums[] = (BaseEnum[]) method.invoke(null, null);
|
|
|
|
|
|
|
|
|
|
|
|
for (BaseEnum enumMessage : enums) {
|
|
|
|
|
|
Map<String, Object> hashMap = new HashMap<String, Object>();
|
|
|
|
|
|
hashMap.put("value", enumMessage.getValue());
|
|
|
|
|
|
hashMap.put("defaultLabel", enumMessage.getDefaultLabel());
|
|
|
|
|
|
hashMap.put("labelId", enumMessage.getLabelId());
|
|
|
|
|
|
keyValueList.add(hashMap);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
return keyValueList;
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("获取枚举下拉列表异常", e);
|
|
|
|
|
|
throw new SalaryRunTimeException("指定类型未找到");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-01 16:45:57 +08:00
|
|
|
|
}
|