@@ -15,26 +15,33 @@ public interface SwEnum { | |||||
*/ | */ | ||||
class FieldTypeBean extends AbstractEnum.IntEnumBean { | class FieldTypeBean extends AbstractEnum.IntEnumBean { | ||||
public String dataType; | public String dataType; | ||||
public int notNull; | |||||
public FieldTypeBean(Integer value, String name, String dataType) { | public FieldTypeBean(Integer value, String name, String dataType) { | ||||
super(value, name); | super(value, name); | ||||
this.dataType = dataType; | this.dataType = dataType; | ||||
this.notNull = 0; | |||||
} | |||||
public FieldTypeBean(Integer value, String name, String dataType, int notNull) { | |||||
super(value, name); | |||||
this.dataType = dataType; | |||||
this.notNull = notNull; | |||||
} | } | ||||
} | } | ||||
class FieldType extends AbstractEnum<Integer, FieldTypeBean> { | class FieldType extends AbstractEnum<Integer, FieldTypeBean> { | ||||
public static FieldType instance = new FieldType(); | public static FieldType instance = new FieldType(); | ||||
public static IntEnumBean ID = instance.addEnum(1, "主键", DataType.ID.value); | |||||
public static IntEnumBean CODE = instance.addEnum(2, "编码字段", DataType.CODE.value); | |||||
public static IntEnumBean NAME = instance.addEnum(3, "名称字段", DataType.NAME.value); | |||||
public static IntEnumBean PARENT_ID = instance.addEnum(4, "父ID", DataType.ID.value); | |||||
public static IntEnumBean LEVEL_CODE = instance.addEnum(5, "级次码", DataType.CODE.value); | |||||
public static IntEnumBean ORDER = instance.addEnum(6, "排序字段", ""); | |||||
public static IntEnumBean CREATE_USER = instance.addEnum(7, "创建人", DataType.ID.value); | |||||
public static IntEnumBean CREATE_TIME = instance.addEnum(8, "创建时间", DataType.DATETIME.value); | |||||
public static IntEnumBean UPDATE_USER = instance.addEnum(9, "更新人", DataType.ID.value); | |||||
public static IntEnumBean LAST_TIME = instance.addEnum(10, "更新时间", DataType.DATETIME.value); | |||||
public static FieldTypeBean ID = instance.addEnum(1, "主键", DataType.ID.value,1); | |||||
public static FieldTypeBean CODE = instance.addEnum(2, "编码字段", DataType.CODE.value,1); | |||||
public static FieldTypeBean NAME = instance.addEnum(3, "名称字段", DataType.NAME.value,1); | |||||
public static FieldTypeBean PARENT_ID = instance.addEnum(4, "父ID", DataType.ID.value); | |||||
public static FieldTypeBean LEVEL_CODE = instance.addEnum(5, "级次码", DataType.CODE.value); | |||||
public static FieldTypeBean ORDER = instance.addEnum(6, "排序字段", ""); | |||||
public static FieldTypeBean CREATE_USER = instance.addEnum(7, "创建人", DataType.ID.value); | |||||
public static FieldTypeBean CREATE_TIME = instance.addEnum(8, "创建时间", DataType.DATETIME.value); | |||||
public static FieldTypeBean UPDATE_USER = instance.addEnum(9, "更新人", DataType.ID.value); | |||||
public static FieldTypeBean LAST_TIME = instance.addEnum(10, "更新时间", DataType.DATETIME.value); | |||||
@Override | @Override | ||||
protected FieldTypeBean buildBean(Integer value, String name) { | protected FieldTypeBean buildBean(Integer value, String name) { | ||||
@@ -46,6 +53,11 @@ public interface SwEnum { | |||||
mapAll.put(value, bean); | mapAll.put(value, bean); | ||||
return bean; | return bean; | ||||
} | } | ||||
public FieldTypeBean addEnum(Integer value, String name, String dataType, int notNull) { | |||||
final FieldTypeBean bean = new FieldTypeBean(value, name, dataType,notNull); | |||||
mapAll.put(value, bean); | |||||
return bean; | |||||
} | |||||
} | } | ||||
/** | /** | ||||
@@ -105,7 +117,7 @@ public interface SwEnum { | |||||
public static DataTypeBean CURRENCY = instance.addEnum("currency", "金额型", "bigint", 0, "long", "0", EditorType.NUMBER.value); | public static DataTypeBean CURRENCY = instance.addEnum("currency", "金额型", "bigint", 0, "long", "0", EditorType.NUMBER.value); | ||||
public static DataTypeBean DATE = instance.addEnum("date", "日期型", "bigint", 0, "long", "0", EditorType.DATE.value); | public static DataTypeBean DATE = instance.addEnum("date", "日期型", "bigint", 0, "long", "0", EditorType.DATE.value); | ||||
public static DataTypeBean TIME = instance.addEnum("time", "时间型", "bigint", 0, "long", "0", EditorType.TIME.value); | public static DataTypeBean TIME = instance.addEnum("time", "时间型", "bigint", 0, "long", "0", EditorType.TIME.value); | ||||
public static DataTypeBean DATETIME = instance.addEnum("datetime", "时间型", "bigint", 0, "long", "0", EditorType.DATETIME.value); | |||||
public static DataTypeBean DATETIME = instance.addEnum("datetime", "日期时间型", "bigint", 0, "long", "0", EditorType.DATETIME.value); | |||||
@Override | @Override | ||||
protected DataTypeBean buildBean(String value, String name) { | protected DataTypeBean buildBean(String value, String name) { | ||||
@@ -128,7 +140,7 @@ public interface SwEnum { | |||||
public static AbstractEnum.IntEnumBean TYPE_GENERAL = instance.addEnum(0, "普通表"); | public static AbstractEnum.IntEnumBean TYPE_GENERAL = instance.addEnum(0, "普通表"); | ||||
public static AbstractEnum.IntEnumBean TYPE_TREE = instance.addEnum(1, "树型表"); | public static AbstractEnum.IntEnumBean TYPE_TREE = instance.addEnum(1, "树型表"); | ||||
public static AbstractEnum.IntEnumBean TYPE_CODE = instance.addEnum(2, "编码表"); | public static AbstractEnum.IntEnumBean TYPE_CODE = instance.addEnum(2, "编码表"); | ||||
public static AbstractEnum.IntEnumBean TYPE_ABSTACT = instance.addEnum(3, "虚拟抽象表"); | |||||
public static AbstractEnum.IntEnumBean TYPE_ABSTRACT = instance.addEnum(3, "虚拟抽象表"); | |||||
public static AbstractEnum.IntEnumBean TYPE_VIEW = instance.addEnum(4, "视图"); | public static AbstractEnum.IntEnumBean TYPE_VIEW = instance.addEnum(4, "视图"); | ||||
} | } | ||||
@@ -52,7 +52,7 @@ public class DefaultProvider<T extends DefaultEntity> extends AbstractCompProvid | |||||
String sn = (String) dao.readValue(b, l.getLinkNameField()); | String sn = (String) dao.readValue(b, l.getLinkNameField()); | ||||
if (StringUtils.isNotEmpty(sn)) names += "," + sn; | if (StringUtils.isNotEmpty(sn)) names += "," + sn; | ||||
} | } | ||||
bean.put(l.getFieldName() + "_text", names.substring(1)); | |||||
bean.put(l.getFieldName() + "_text", names.length()>1 ? names.substring(1) : ""); | |||||
} else { | } else { | ||||
List<String> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>()); | List<String> list = mapIds.computeIfAbsent(l.getLinkTable().getName(), k -> new ArrayList<>()); | ||||
Collections.addAll(list, ids); | Collections.addAll(list, ids); | ||||
@@ -0,0 +1,118 @@ | |||||
package cc.smtweb.framework.test; | |||||
import cc.smtweb.framework.core.common.AbstractEnum; | |||||
import cc.smtweb.framework.core.common.IntEnum; | |||||
import cc.smtweb.framework.core.common.StrEnum; | |||||
import cc.smtweb.framework.core.common.SwEnum; | |||||
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl; | |||||
import java.lang.reflect.Field; | |||||
import java.lang.reflect.Type; | |||||
/** | |||||
* Created by Akmm at 2017/5/12 17:14 | |||||
* 枚举类转js定义 | |||||
*/ | |||||
public class ToolEnum2Js { | |||||
public static void main(String[] args) throws Exception { | |||||
buildJs(SwEnum.class);//传枚举定义类 | |||||
} | |||||
private static void buildJs(Class<?> cls) throws Exception { | |||||
Class<?>[] classes = cls.getDeclaredClasses();// 获得代表内部类的Class对象组成的数组 | |||||
StringBuilder sb = new StringBuilder(); | |||||
for (Class<?> clazz : classes) {// 遍历Class对象数组 | |||||
// System.out.println("类的标准名称:" + clazz.getCanonicalName()); | |||||
// System.out.println("类的修饰符:" + Modifier.toString(clazz.getModifiers())); | |||||
Type superClass = clazz.getGenericSuperclass();// 获得直接父类 | |||||
// System.out.print("类的直接继承类:"); | |||||
if (superClass.equals(IntEnum.class)) { | |||||
buildJsInt(clazz, sb); | |||||
}else if (superClass.equals(StrEnum.class)) { | |||||
buildJsStr(clazz, sb); | |||||
}else if (superClass.getTypeName().startsWith(AbstractEnum.class.getTypeName()+"<")){ | |||||
buildJsBean(clazz,sb); | |||||
} | |||||
} | |||||
System.out.println(sb.substring(1)); | |||||
} | |||||
private static void buildJsInt(Class<?> clazz, StringBuilder sb) throws Exception { | |||||
IntEnum instance = (IntEnum) clazz.newInstance(); | |||||
Field[] fields = clazz.getDeclaredFields();// 获得该类对象的所有非继承域 | |||||
if (fields.length == 0) return; | |||||
StringBuilder sbc = new StringBuilder(128); | |||||
StringBuilder sbDefines = new StringBuilder(128); | |||||
for (Field field : fields) { | |||||
if (!field.getType().equals(AbstractEnum.IntEnumBean.class)) continue; | |||||
AbstractEnum.IntEnumBean value = (AbstractEnum.IntEnumBean) field.get(instance); | |||||
sbc.append(",\n").append(field.getName()).append(": ").append(value.value); | |||||
sbDefines.append(",\n").append("{ value: ").append(value.value).append(", label: \"").append(value.name).append("\" }"); | |||||
} | |||||
sb.append("\n").append("export const ").append(clazz.getSimpleName()).append(" = {\n").append(sbc.length()>2?sbc.substring(2):"").append(",\n...useEnumProps([\n").append(sbDefines.length()>2?sbDefines.substring(2):"").append("\n])").append("\n}"); | |||||
} | |||||
private static void buildJsStr(Class<?> clazz, StringBuilder sb) throws Exception { | |||||
StrEnum instance = (StrEnum) clazz.newInstance(); | |||||
Field[] fields = clazz.getDeclaredFields();// 获得该类对象的所有非继承域 | |||||
if (fields.length == 0) return; | |||||
StringBuilder sbc = new StringBuilder(128); | |||||
StringBuilder sbDefines = new StringBuilder(128); | |||||
for (Field field : fields) { | |||||
if (!field.getType().equals(AbstractEnum.StrEnumBean.class)) continue; | |||||
AbstractEnum.StrEnumBean value = (AbstractEnum.StrEnumBean) field.get(instance); | |||||
sbc.append(",\n").append(field.getName()).append(": \"").append(value.value).append("\""); | |||||
sbDefines.append(",\n").append("{ value: \"").append(value.value).append("\", label: \"").append(value.name).append("\" }"); | |||||
} | |||||
sb.append("\n").append("export const ").append(clazz.getSimpleName()).append(" = {\n").append(sbc.length()>2?sbc.substring(2):"").append(",\n...useEnumProps([\n").append(sbDefines.length()>2?sbDefines.substring(2):"").append("\n])").append("\n}"); | |||||
} | |||||
private static void buildJsBean(Class<?> clazz, StringBuilder sb)throws Exception{ | |||||
Object instance = clazz.newInstance(); | |||||
Field[] fields = clazz.getDeclaredFields();// 获得该类对象的所有非继承域 | |||||
Type type1 = ((ParameterizedTypeImpl) clazz.getGenericSuperclass()).getActualTypeArguments()[0]; | |||||
Type type2 = ((ParameterizedTypeImpl) clazz.getGenericSuperclass()).getActualTypeArguments()[1]; | |||||
if (fields.length == 0) return; | |||||
boolean isNumber = Integer.class.getTypeName().equals(type1.getTypeName()); | |||||
StringBuilder sbc = new StringBuilder(128); | |||||
StringBuilder sbDefines = new StringBuilder(128); | |||||
for (Field field : fields) { | |||||
if (!field.getType().equals(type2)) continue; | |||||
Object value = field.get(instance); | |||||
if (isNumber){ | |||||
AbstractEnum.IntEnumBean tempValue = (AbstractEnum.IntEnumBean) field.get(instance); | |||||
sbc.append(",\n").append(field.getName()).append(": ").append(tempValue.value); | |||||
sbDefines.append(",\n").append("{ value: ").append(tempValue.value).append(", label: \"").append(tempValue.name).append("\""); | |||||
}else { | |||||
AbstractEnum.StrEnumBean tempValue = (AbstractEnum.StrEnumBean) field.get(instance); | |||||
sbc.append(",\n").append(field.getName()).append(": \"").append(tempValue.value).append("\""); | |||||
sbDefines.append(",\n").append("{ value: \"").append(tempValue.value).append("\", label: \"").append(tempValue.name).append("\""); | |||||
} | |||||
// 处理bean | |||||
Field[] beanFields = value.getClass().getFields();// 获得该类对象的所有字段 | |||||
StringBuilder sbBean = new StringBuilder(); | |||||
sbBean.append(" bean: { "); | |||||
if(beanFields.length >0 ){ | |||||
for(Field beanField: beanFields){ | |||||
Object beanValue = beanField.get(value); | |||||
String name = beanField.getName(); | |||||
sbBean.append("name".equals(name)?"label":name).append(": ").append(formatValue(beanValue)).append(", "); | |||||
} | |||||
} | |||||
sbBean.append("}"); | |||||
sbDefines.append(",").append(sbBean.toString()).append(" }"); | |||||
} | |||||
sb.append("\n").append("export const ").append(clazz.getSimpleName()).append(" = {\n"). | |||||
append(sbc.length()>2?sbc.substring(2):"").append(",\n...useEnumProps([\n").append(sbDefines.length()>2?sbDefines.substring(2):"").append("\n])").append("\n}"); | |||||
} | |||||
private static String formatValue(Object obj){ | |||||
if(obj instanceof String){ | |||||
return "\""+obj.toString()+"\""; | |||||
} | |||||
return obj.toString(); | |||||
} | |||||
} |