@Component
public class GenericsUtils {
private final static Logger logger = LoggerFactory.getLogger(GenericsUtils.class);
@Autowired
private OperationPlatformServiceClient operationPlatformServiceClient;
private static GenericsUtils genericsUtils;
@PostConstruct
public void init() {
genericsUtils = this;
genericsUtils.operationPlatformServiceClient = this.operationPlatformServiceClient;
}
/**
* 单独设置数据字典信息
*
* @param t
* @param tCls
* @param enumCode secode父级码表标示
* @param fieldName 要对比的字段 class_model-->0001
* @param setFildName 要替换的子弹
* @param <T>
*/
public static <T> void setEnum(T t, Class tCls, String enumCode, String fieldName, String setFildName) {
if (t != null) {
//获取属性方法
Method getMethod = getGetMethod(tCls, fieldName);
//设置属性方法 setMethodName 方法名称 String.class:参数类型
Method setNameMethod = getSetMethod(tCls, setFildName, String.class);
//获取id
String oldCode = invokeMethod(getMethod,t);
if (StringUtils.isNotBlank(oldCode)) {
//查询指定数据
List<EnumCode> enumByCodes = genericsUtils.operationPlatformServiceClient.queryByCodes(enumCode);
if (CollectionUtils.isNotEmpty(enumByCodes)) {
for (EnumCode enumByCode : enumByCodes) {
String seiValue = enumByCode.getSeiValue();
String seiName = enumByCode.getSeiName();
if (oldCode.equals(seiValue)) {
if (StringUtils.isNotBlank(setFildName)) {
invokeSetMethod(setNameMethod,t,seiName);
}
}
}
}
}
}
}
/**
* 批量设置数据字典信息
*
* @param data
* @param tCls
* @param enumCode
* @param fieldName
* @param setNameFild
* @param <T>
*/
public static <T> void setEnumToList(List<T> data, Class tCls, String enumCode, String fieldName, String setNameFild) {
if (CollectionUtils.isNotEmpty(data)) {
//获取属性方法
Method getMethod = getGetMethod(tCls, fieldName);
//设置属性方法 setMethodName 方法名称 String.class:参数类型
Method setNameMethod = getSetMethod(tCls, setNameFild, String.class);
if (StringUtils.isNotBlank(enumCode)) {
//查询指定数据
List<EnumCode> enumByCodes = genericsUtils.operationPlatformServiceClient.queryByCodes(enumCode+",''");
if (CollectionUtils.isNotEmpty(enumByCodes)) {
//设置数据
for (int i = 0; i < data.size(); i++) {
T t = data.get(i);
String oldCode = invokeMethod(getMethod,t);
for (EnumCode item : enumByCodes) {
String seiName = item.getSeiName();
String seiValue = item.getSeiValue();
if (!StringUtils.isEmpty(oldCode)&&oldCode.equals(seiValue)) {
if (StringUtils.isNotBlank(setNameFild)) {
invokeSetMethod(setNameMethod,t,seiName);
}
}
}
}
}
}
}
}
/**
* 获取ids集合
*
* @param data
* @param getMethod
* @param <T>
* @return
* @throws Exception
*/
private static <T> List getIds(List<T> data, Method getMethod) {
List<String> ids = new ArrayList<>();
Set<String> set = new HashSet<String>();
for (int i = 0; i < data.size(); i++) {
T t = data.get(i);
String itemId =invokeMethod(getMethod,t);
if (itemId != null) {
set.add(itemId);
}
}
ids=new ArrayList<>(set);
return ids;
}
/**
* 获取get方法
*
* @param tCls
* @param fieldName
* @return
* @throws Exception
*/
public static Method getGetMethod(Class tCls, String fieldName) {
try {
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method getMethod = tCls.getMethod(getMethodName);
return getMethod;
}catch (Exception e){
logger.error("获取get方法报错:{}",e);
return null ;
}
}
/**
* 获取set方法
*
* @param tCls
* @param courseNameFild
* @return
* @throws Exception
*/
public static Method getSetMethod(Class tCls, String courseNameFild, Class argClass) {
try {
String setNameMethodName = "set" + courseNameFild.substring(0, 1).toUpperCase() + courseNameFild.substring(1);
Method setNameMethod = tCls.getMethod(setNameMethodName, argClass);
return setNameMethod;
}catch (Exception e){
logger.error("获取set方法报错:{}",e);
return null ;
}
}
/**
* 获取结果数据
* @param getMethod
* @param t
* @return
*/
public static <T> String invokeMethod(Method getMethod,T t) {
try {
String invoke = (String) getMethod.invoke(t);
return invoke;
}catch (Exception e){
logger.error("执行get方法报错:{}",e);
return null ;
}
}
/**
* 设置数据解雇
* @param setMethod
* @param t
* @param fieldData
*/
public static <T> void invokeSetMethod(Method setMethod,T t,T fieldData) {
try {
setMethod.invoke(t, fieldData);
}catch (Exception e){
logger.error("执行set方法报错:{}",e);
}
}