Selaa lähdekoodia

优化path生成规则

zhouhao 7 vuotta sitten
vanhempi
commit
5276fbe1ba

+ 16 - 0
hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/AbstractTreeSortService.java

@@ -17,9 +17,11 @@
 
 package org.hswebframework.web.service;
 
+import org.hswebframework.utils.RandomUtil;
 import org.hswebframework.web.commons.entity.TreeSortSupportEntity;
 import org.hswebframework.web.commons.entity.TreeSupportEntity;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -52,9 +54,22 @@ public abstract class AbstractTreeSortService<E extends TreeSortSupportEntity<PK
         return createQuery().where(TreeSupportEntity.parentId, parentId).noPaging().list();
     }
 
+    protected void applyPath(E entity) {
+        if (!StringUtils.isEmpty(entity.getParentId())) return;
+        if (!StringUtils.isEmpty(entity.getPath())) return;
+
+        TreeSortSupportEntity<PK> parent = selectByPk(entity.getParentId());
+        if (null == parent) {
+            entity.setParentId(null);
+        } else {
+            entity.setPath(parent.getPath() + "-" + RandomUtil.randomChar(4));
+        }
+    }
+
     @Override
     public PK insert(E entity) {
         if (entity.getId() == null) entity.setId(getIDGenerator().generate());
+        applyPath(entity);
         List<E> childrenList = new ArrayList<>();
         TreeSupportEntity.expandTree2List(entity, childrenList, getIDGenerator());
         super.insert(entity);
@@ -87,6 +102,7 @@ public abstract class AbstractTreeSortService<E extends TreeSortSupportEntity<PK
     public PK saveOrUpdateForSingle(E entity) {
         assertNotNull(entity);
         PK id = entity.getId();
+        applyPath(entity);
         if (null == id || this.selectByPk(id) == null) {
             if (null == id)
                 entity.setId(getIDGenerator().generate());