Browse Source

优化:树型表加字段level

master
郑根木 2 years ago
parent
commit
1a84c06e5b
3 changed files with 22 additions and 6 deletions
  1. +2
    -1
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java
  2. +10
    -0
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/vo/ModelTable.java
  3. +10
    -5
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/TreeHelper.java

+ 2
- 1
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/common/SwEnum.java View File

@@ -39,7 +39,8 @@ public interface SwEnum {
public static FieldTypeBean NAME = instance.addEnum(3, "名称字段", DataType.NAME.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 PARENT_ID = instance.addEnum(4, "父ID", DataType.ID.value);
public static FieldTypeBean LEVEL_CODE = instance.addEnum(5, "级次码", DataType.CODE.value); public static FieldTypeBean LEVEL_CODE = instance.addEnum(5, "级次码", DataType.CODE.value);
public static FieldTypeBean ORDER = instance.addEnum(6, "排序字段", "");
public static FieldTypeBean LEVEL = instance.addEnum(6, "级次", DataType.SHORT.value);
// public static FieldTypeBean ORDER = instance.addEnum(6, "排序字段", "");
public static FieldTypeBean CREATE_USER = instance.addEnum(7, "创建人", DataType.ID.value); 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 CREATE_TIME = instance.addEnum(8, "创建时间", DataType.DATETIME.value);
public static FieldTypeBean UPDATE_USER = instance.addEnum(9, "更新人", DataType.ID.value); public static FieldTypeBean UPDATE_USER = instance.addEnum(9, "更新人", DataType.ID.value);


+ 10
- 0
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/db/vo/ModelTable.java View File

@@ -231,6 +231,16 @@ public class ModelTable extends DefaultEntity {
return null; return null;
} }


public String findFieldNameByType(int type) {
for (ModelField value : fields) {
if (type == value.getFieldType()) {
return value.getName();
}
}

return null;
}

public ModelField findIdField() { public ModelField findIdField() {
ModelIndex index = findPrimaryIndex(); ModelIndex index = findPrimaryIndex();
if (index != null) { if (index != null) {


+ 10
- 5
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/TreeHelper.java View File

@@ -31,6 +31,7 @@ public class TreeHelper<T extends DefaultEntity> {
private EntityDao<T> dao; private EntityDao<T> dao;


private String fieldParent; private String fieldParent;
private String fieldLevel;
private String fieldLevelCode; private String fieldLevelCode;




@@ -58,16 +59,19 @@ public class TreeHelper<T extends DefaultEntity> {
cache = CacheManager.getIntance().getCache(tableName); cache = CacheManager.getIntance().getCache(tableName);
dao = DbEngine.getInstance().findDao(tableName); dao = DbEngine.getInstance().findDao(tableName);


ModelField field = table.findFieldByType(SwEnum.FieldType.PARENT_ID.value);
fieldParent = field.getName();
field = table.findFieldByType(SwEnum.FieldType.LEVEL_CODE.value);
fieldLevelCode = field.getName();
fieldParent = table.findFieldNameByType(SwEnum.FieldType.PARENT_ID.value);
fieldLevelCode = table.findFieldNameByType(SwEnum.FieldType.LEVEL_CODE.value);
fieldLevel = table.findFieldNameByType(SwEnum.FieldType.LEVEL.value);
} }


public long getParentId(T bean) { public long getParentId(T bean) {
return bean.getLong(fieldParent); return bean.getLong(fieldParent);
} }


public long getLevel(T bean) {
return bean.getLong(fieldLevel);
}

public String getLevelCode(T bean) { public String getLevelCode(T bean) {
return bean.getStr(fieldLevelCode); return bean.getStr(fieldLevelCode);
} }
@@ -119,7 +123,8 @@ public class TreeHelper<T extends DefaultEntity> {
T parent = cache.get(getParentId(bean)); T parent = cache.get(getParentId(bean));
if (parent != null) { if (parent != null) {
bean.put(fieldLevelCode, getLevelCode(parent) + SwConsts.SPLIT_CHAR + parent.getEntityId()); bean.put(fieldLevelCode, getLevelCode(parent) + SwConsts.SPLIT_CHAR + parent.getEntityId());
dao.updateEntity(bean, fieldLevelCode);
bean.put(fieldLevel, getLevel(parent) + 1);
dao.updateEntity(bean, fieldLevelCode, fieldLevel);
list.add(bean); list.add(bean);
} }
Collection<T> children = getChildren(bean); Collection<T> children = getChildren(bean);


Loading…
Cancel
Save