admin
2022-03-25 17055fd8d36504b79a5def28f5d4b4740faf012d
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
package org.yeshi.utils.generater.util;
 
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.generater.annotation.admin.Show;
import org.yeshi.utils.generater.annotation.admin.form.*;
import org.yeshi.utils.generater.entity.KeyValue;
import org.yeshi.utils.generater.entity.admin.FormRowData;
import org.yeshi.utils.generater.exception.AnotationException;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.*;
 
/**
 * @author Administrator
 * @title: FormAnotationUtil
 * @description: 注解帮助类
 * @date 2021/9/24 10:31
 */
public class AnotationUtil {
 
    public static Map<String, Object> getParamsMap(Annotation a) throws InvocationTargetException, IllegalAccessException {
        Map<String, Object> params = new HashMap<>();
        Method[] fs = a.getClass().getDeclaredMethods();
        for (Method md : fs) {
            if (md.getGenericParameterTypes().length > 0)
                continue;
            if (md.getName().equals("toString") || md.getName().equals("hashCode") || md.getName().equalsIgnoreCase("annotationType"))
                continue;
            Object value = md.invoke(a);
            if (value instanceof String[]) {
                params.put(md.getName(), value);
                List<KeyValue> values = new ArrayList<>();
                String[] arrays = (String[]) value;
                if (arrays.length == 1 && StringUtil.isNullOrEmpty(arrays[0])) {
                } else {
                    for (String st : (String[]) value) {
                        values.add(new KeyValue(st.substring(0, st.indexOf(":")), st.substring(st.indexOf(":") + 1)));
                    }
                }
                params.put(md.getName(), values);
            } else {
                params.put(md.getName(), value);
            }
        }
        return params;
    }
 
    //获取表单的行数据
    public static List<FormRowData> getFormRowData(Class entity) throws AnotationException, Exception {
        List<FormRowData> list = new ArrayList<>();
        Field[] fields = entity.getDeclaredFields();
        for (Field fd : fields) {
            Annotation[] as = fd.getAnnotations();
            FormRowData formRowData = null;
            for (Annotation an : as) {
 
                formRowData = new FormRowData();
 
                if (an instanceof CheckBox) {
                    FormAnotationValidUtil.valid((CheckBox) an);
                } else if (an instanceof Img) {
                    FormAnotationValidUtil.valid((Img) an);
                } else if (an instanceof Password) {
                    FormAnotationValidUtil.valid((Password) an);
                } else if (an instanceof RadioGroup) {
                    FormAnotationValidUtil.valid((RadioGroup) an);
                } else if (an instanceof Select) {
                    FormAnotationValidUtil.valid((Select) an);
                } else if (an instanceof Switch) {
                    FormAnotationValidUtil.valid((Switch) an);
                } else if (an instanceof Text) {
                    FormAnotationValidUtil.valid((Text) an);
                } else if (an instanceof TextArea) {
                    FormAnotationValidUtil.valid((TextArea) an);
                } else {
                    formRowData = null;
                }
 
                if (formRowData != null) {
                    formRowData.setKey(fd.getName());
                    formRowData.setType(an.annotationType().getSimpleName());
                    formRowData.setParams(getParamsMap(an));
                    list.add(formRowData);
                    break;
                }
            }
        }
        return list;
    }
 
 
    //获取可以更新的字段
    public static List<FormRowData> getUpdateFormRowData(Class entity) throws AnotationException, Exception {
        List<FormRowData> list = getFormRowData(entity);
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == null || list.get(i).getParams() == null || !((Boolean) list.get(i).getParams().get("update"))) {
                    //没有配置更新项目
                    list.remove(i--);
                }
            }
        }
        return list;
    }
 
    //获取显示的字段数据
    public static List<Map<String, Object>> getShowDataList(Class entity) throws InvocationTargetException, IllegalAccessException {
        List<Map<String, Object>> list = new ArrayList<>();
        Field[] fields = entity.getDeclaredFields();
        for (Field fd : fields) {
            Annotation[] as = fd.getAnnotations();
            for (Annotation an : as) {
                if (an instanceof Show) {
                    Map<String, Object> map = getParamsMap(an);
                    map.put("identifier", fd.getName());
                    list.add(map);
                }
            }
        }
        if (list != null && list.size() > 0) {
            Comparator<Map<String, Object>> cm = (Map<String, Object> o1, Map<String, Object> o2) -> {
                return (int) (o1.get("order")) - (int) (o2.get("order"));
            };
            Collections.sort(list, cm);
        }
        return list;
    }
 
}