package com.vci.rmip.code.client.codeapply.Apply410.utils;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
|
|
import org.apache.commons.beanutils.ConvertUtils;
|
import org.apache.commons.beanutils.PropertyUtils;
|
import org.apache.commons.lang.StringUtils;
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Type;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Locale;
|
import java.util.Map;
|
|
public class ListUtil {
|
/**
|
* targetList 需要处理的list
|
* targetClass 目标class
|
* @return List 处理好的list
|
*/
|
public static List listMapParseListObj(List targetList, Class targetClass){
|
// 先获取该类的代理对象
|
Object obj = null;
|
try {
|
obj = targetClass.newInstance();
|
} catch (InstantiationException e) {
|
e.printStackTrace();
|
} catch (IllegalAccessException e) {
|
e.printStackTrace();
|
}
|
Field[] fields = targetClass.getDeclaredFields();
|
// 将list转为list对象
|
for(int i = 0; i< targetList.size(); i++){
|
Map map = (Map)targetList.get(i); // 获取每个list里面每个map
|
for (Field field : fields) {
|
String fieldName = field.getName();
|
if(fieldName.equals("data")){
|
try {
|
PropertyUtils.setProperty(obj, fieldName, map);
|
}catch(Exception ex){
|
ex.printStackTrace();
|
}
|
}else if (map.containsKey(fieldName.toUpperCase(Locale.ROOT))||map.containsKey(fieldName.toLowerCase(Locale.ROOT))) {
|
Object value="";
|
if (map.containsKey(fieldName.toUpperCase(Locale.ROOT))) {
|
value = map.get(fieldName.toUpperCase());
|
}else if(map.containsKey(fieldName.toLowerCase(Locale.ROOT))){
|
value = map.get(fieldName.toLowerCase());
|
}
|
try{
|
//取得值的类形
|
Class type = PropertyUtils.getPropertyType(obj, fieldName);
|
if(type!=null){
|
|
PropertyUtils.setProperty(obj, fieldName,convert(value, type));
|
}
|
}catch(Exception ex){
|
ex.printStackTrace();
|
}
|
}
|
}
|
}
|
// 将list<Map> 转为对象进行返回
|
List resListObj = Lists.newArrayList();
|
for (Object object : targetList) {
|
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
|
resListObj.add(jsonObject.toJavaObject((Type)targetClass));
|
}
|
|
//返回封装好的集合
|
return resListObj;
|
}
|
|
/**
|
* targetList 需要处理的list
|
* targetClass 目标class
|
* @return List 处理好的list
|
*/
|
public static <T> T mapParseJavaBean(Map map, Class targetClass){
|
// 先获取该类的代理对象
|
Object obj = null;
|
try {
|
obj = targetClass.newInstance();
|
} catch (InstantiationException e) {
|
e.printStackTrace();
|
} catch (IllegalAccessException e) {
|
e.printStackTrace();
|
}
|
Field[] fields = targetClass.getDeclaredFields();
|
// 将list转为list对象
|
for (Field field : fields) {
|
String fieldName = field.getName();
|
if(fieldName.equals("data")){
|
try {
|
PropertyUtils.setProperty(obj, fieldName, map);
|
}catch(Exception ex){
|
ex.printStackTrace();
|
}
|
}else if (map.containsKey(fieldName.toUpperCase(Locale.ROOT))||map.containsKey(fieldName.toLowerCase(Locale.ROOT))) {
|
Object value="";
|
if (map.containsKey(fieldName.toUpperCase(Locale.ROOT))) {
|
value = map.get(fieldName.toUpperCase());
|
}else if(map.containsKey(fieldName.toLowerCase(Locale.ROOT))){
|
value = map.get(fieldName.toLowerCase());
|
}
|
try{
|
//取得值的类形
|
Class type = PropertyUtils.getPropertyType(obj, fieldName);
|
if(type!=null){
|
PropertyUtils.setProperty(obj, fieldName,convert(value, type));
|
}
|
}catch(Exception ex){
|
ex.printStackTrace();
|
}
|
}
|
}
|
/*// 将list<Map> 转为对象进行返回
|
List resListObj = Lists.newArrayList();
|
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
|
resListObj.add(jsonObject.toJavaObject((Type)targetClass));*/
|
//返回封装好的集合
|
return (T) obj;
|
}
|
|
|
public static Object convert(Object value, Class clazz){
|
if (value == null) { // 如果获取参数值为null,则返回null
|
return null;
|
} else if (!value.equals("")) { // 如果获取参数值不为"",则通过convertGt方法进行类型转换后返回结果
|
return convertGt(value, clazz);
|
} else if (clazz.getName().equals(String.class.getName())) { // 如果获取参数值为""
|
return convertGt(value, clazz);
|
} else {// 如果获取参数值为"",并且clazz不是是String类型,则返回null
|
return null;
|
}
|
}
|
/**
|
* @param value
|
* @param clazz
|
* @return
|
*/
|
|
@SuppressWarnings("unchecked")
|
public static Object convertGt(Object value, Class clazz) {
|
if (value == null) { // 如果值为null,则返回null
|
return null;
|
} else if (value.equals("")&& !clazz.getName().equals(String.class.getName())) { // 如果value值为"",而且要转为的类型不是string类型,那么就统一返回null,也就是空字符串不能转成任何其他类型的实体,只能返回null
|
return null;
|
} else if (Date.class.getName().equalsIgnoreCase(clazz.getName())) { // 增加对从String类型到Date
|
return convertSTD(value.toString());
|
}
|
return ConvertUtils.convert(value, clazz);
|
}
|
//日期类型的转换
|
private static SimpleDateFormat simpleDateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
public static Date convertSTD(String date){
|
try {
|
return simpleDateFormate.parse(date);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
public static String convertDTS(Date date){
|
return simpleDateFormate.format(date);
|
}
|
}
|