zouyu
2024-08-05 9f95745a06d65d6627765e0b9135746beff3158e
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
package com.yuanchu.mom.common;
 
import cn.hutool.core.util.StrUtil;
import com.yuanchu.mom.annotation.ValueTableShow;
import io.swagger.annotations.ApiModelProperty;
 
import java.lang.reflect.Field;
import java.util.*;
 
/*李林
* 通过注解快速生成table表头*/
 
public class PrintChina {
 
    public static <T> List<Map<String, Object>> printChina(Class<T> tClass) {
        //将实体类的属性和注释转换成参数
        List<Field> fieldList = new ArrayList<>();
        while (tClass != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(tClass.getDeclaredFields())));
            tClass = (Class<T>) tClass.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (Field field : fields) {
            Map<String, Object> soundVo = new HashMap<>();
            boolean bool = field.isAnnotationPresent(ApiModelProperty.class);
            boolean bool2 = field.isAnnotationPresent(ValueTableShow.class);
            if (bool2) {
                int order = field.getAnnotation(ValueTableShow.class).value();
                String value = field.getAnnotation(ValueTableShow.class).name();
                int width = field.getAnnotation(ValueTableShow.class).width();
                if (bool){
                    value = value.isEmpty() ? field.getAnnotation(ApiModelProperty.class).value() : value;
                }
                soundVo.put("label", field.getName());
                soundVo.put("value", value);
                soundVo.put("width", width);
                soundVo.put("order", order + "");
                if(list.size() == 0){
                    list.add(soundVo);
                }else{
                    boolean isAdd = false;
                    for (int i = 0; i < list.size(); i++) {
                        Map<String, Object> map = list.get(i);
                        if(order < Integer.parseInt(map.get("order").toString())){
                            list.add(i, soundVo);
                            isAdd = true;
                            break;
                        }
                    }
                    if (!isAdd) list.add(soundVo);
                }
            }
        }
        return list;
    }
 
}