xiejun
2025-01-23 9ac3bd680a350c1cc4baad082d92cd2c5f158f3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 org.apache.poi.ss.formula.functions.T;
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);
    }
}