|
@@ -0,0 +1,145 @@
|
|
|
|
+package org.springblade.manager.vo;
|
|
|
|
+import cn.hutool.log.StaticLog;
|
|
|
|
+import com.alibaba.fastjson.annotation.JSONField;
|
|
|
|
+import org.springblade.common.utils.BaseUtils;
|
|
|
|
+import org.springblade.core.tool.utils.StringPool;
|
|
|
|
+
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author yangyj
|
|
|
|
+ * @Date 2023/4/22 10:55
|
|
|
|
+ * @description 数据模型接口
|
|
|
|
+ */
|
|
|
|
+public interface DataModel {
|
|
|
|
+ List<Class<?>> classes=new ArrayList<>();
|
|
|
|
+ /*扫描路径*/
|
|
|
|
+ String PATH="org.springblade.manager.vo";
|
|
|
|
+ /* static void main(String[] args) {
|
|
|
|
+ List<Class<?>> classes = findClassesByInterface("org.springblade.manager.vo", DataModel.class);
|
|
|
|
+ Map<String,List<WbsFormElementVO>> MODEL_MAP = new LinkedHashMap<>();
|
|
|
|
+ classes.forEach(c->{
|
|
|
|
+ MODEL_MAP.put(getStaticFieldValue(c,"ID"),toElementVos(c));
|
|
|
|
+ });
|
|
|
|
+ System.out.println();
|
|
|
|
+ }*/
|
|
|
|
+
|
|
|
|
+ String TBN_FN="TBN";
|
|
|
|
+ /*把模型类转换成共识配置选项*/
|
|
|
|
+ static List<WbsFormElementVO> toElementVos(Class<?> clazz) {
|
|
|
|
+ List<WbsFormElementVO> list =new ArrayList<>();
|
|
|
|
+ try {
|
|
|
|
+ Field tbnField = clazz.getField(TBN_FN);
|
|
|
|
+ String TBN = (String) tbnField.get(null);
|
|
|
|
+ for (Field field : clazz.getDeclaredFields()) {
|
|
|
|
+ JSONField jf = field.getAnnotation(JSONField.class);
|
|
|
|
+ if (jf != null) {
|
|
|
|
+ WbsFormElementVO vo = new WbsFormElementVO();
|
|
|
|
+ vo.setEName(jf.label());
|
|
|
|
+ vo.setTableElementKey(TBN+ StringPool.COLON +jf.name());
|
|
|
|
+ vo.setInitTableName(TBN);
|
|
|
|
+ vo.setId(BaseUtils.str2Long(vo.getTableElementKey()));
|
|
|
|
+ list.add(vo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+ static LinkedHashMap<String,List<WbsFormElementVO>> getElements(){
|
|
|
|
+ LinkedHashMap<String,List<WbsFormElementVO>> map = new LinkedHashMap<>();
|
|
|
|
+ if(classes.size()==0) {
|
|
|
|
+ classes.addAll(findClassesByInterface(PATH, DataModel.class));
|
|
|
|
+ }
|
|
|
|
+ classes.forEach(c->{
|
|
|
|
+ map.put(getStaticFieldValue(c,"ID"),toElementVos(c));
|
|
|
|
+ });
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static List<TreeNodeVOByTabType> getTreeNodeList(){
|
|
|
|
+ if(classes.size()==0) {
|
|
|
|
+ classes.addAll(findClassesByInterface(PATH, DataModel.class));
|
|
|
|
+ }
|
|
|
|
+ return classes.stream().map(c -> {
|
|
|
|
+ TreeNodeVOByTabType tn = new TreeNodeVOByTabType();
|
|
|
|
+ tn.setTitle(getStaticFieldValue(c,"TBN_CH"));
|
|
|
|
+ tn.setTabType(SelectorModel.NODE_NAME);
|
|
|
|
+ tn.setParentId(SelectorModel.ID);
|
|
|
|
+ String id=getStaticFieldValue(c,"ID");
|
|
|
|
+ assert id != null;
|
|
|
|
+ tn.setId(Long.parseLong(id));
|
|
|
|
+ tn.setPrimaryKeyId(id);
|
|
|
|
+ tn.setInitTableId(id);
|
|
|
|
+ tn.setHasChildren(false);
|
|
|
|
+ tn.setInitTableName(getStaticFieldValue(c,"TBN"));
|
|
|
|
+ return tn;
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*获取静态变量值*/
|
|
|
|
+ static String getStaticFieldValue(Class<?> clazz,String fieldName){
|
|
|
|
+ try {
|
|
|
|
+ Field idField = clazz.getField(fieldName);
|
|
|
|
+ return (String)idField.get(null);
|
|
|
|
+ } catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
+ StaticLog.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*扫描指定包,并获取所有实现DataModel的类,按照ID排序返回*/
|
|
|
|
+ static List<Class<?>> findClassesByInterface(String packageName, Class<?> interfaceClass) {
|
|
|
|
+ List<Class<?>> targetList = new ArrayList<>();
|
|
|
|
+ Map<Class<?>, String> classIdMap = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
|
|
+ String path = packageName.replace('.', '/');
|
|
|
|
+ java.net.URL resource = classLoader.getResource(path);
|
|
|
|
+ if (resource == null) {
|
|
|
|
+ return targetList;
|
|
|
|
+ }
|
|
|
|
+ java.io.File dir = new java.io.File(resource.getFile());
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ return targetList;
|
|
|
|
+ }
|
|
|
|
+ java.io.File[] files = dir.listFiles();
|
|
|
|
+ if (files == null) {
|
|
|
|
+ return targetList;
|
|
|
|
+ }
|
|
|
|
+ for (java.io.File file : files) {
|
|
|
|
+ String fileName = file.getName();
|
|
|
|
+ if (fileName.endsWith(".class")) {
|
|
|
|
+ String className = packageName + '.' + fileName.substring(0, fileName.length() - 6);
|
|
|
|
+ try {
|
|
|
|
+ Class<?> clazz = Class.forName(className);
|
|
|
|
+ if (interfaceClass.isAssignableFrom(clazz) && !clazz.isInterface()) {
|
|
|
|
+ try {
|
|
|
|
+ Field idField = clazz.getField("ID");
|
|
|
|
+ String idValue = (String) idField.get(null);
|
|
|
|
+ classIdMap.put(clazz, idValue);
|
|
|
|
+ } catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
+ StaticLog.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ targetList.add(clazz);
|
|
|
|
+ }
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
+ StaticLog.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ StaticLog.error(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ targetList.sort((c1, c2) -> {
|
|
|
|
+ String id1 = classIdMap.get(c1);
|
|
|
|
+ String id2 = classIdMap.get(c2);
|
|
|
|
+ return id1.compareTo(id2);
|
|
|
|
+ });
|
|
|
|
+ return targetList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|