瀏覽代碼

优化:树型表加字段level

master
郑根木 2 年之前
父節點
當前提交
1a84c06e5b
共有 3 個檔案被更改,包括 22 行新增6 行删除
  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 查看文件

@@ -39,7 +39,8 @@ public interface SwEnum {
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 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_TIME = instance.addEnum(8, "创建时间", DataType.DATETIME.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 查看文件

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

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

return null;
}

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


+ 10
- 5
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/mvc/service/TreeHelper.java 查看文件

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

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


@@ -58,16 +59,19 @@ public class TreeHelper<T extends DefaultEntity> {
cache = CacheManager.getIntance().getCache(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) {
return bean.getLong(fieldParent);
}

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

public String getLevelCode(T bean) {
return bean.getStr(fieldLevelCode);
}
@@ -119,7 +123,8 @@ public class TreeHelper<T extends DefaultEntity> {
T parent = cache.get(getParentId(bean));
if (parent != null) {
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);
}
Collection<T> children = getChildren(bean);


Loading…
取消
儲存