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;
|
}
|
|
}
|