Browse Source

优化关系获取逻辑

zhouhao 6 years ago
parent
commit
331876cfd9

+ 1 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-authorization/src/main/java/org/hswebframework/web/organizational/authorization/relation/OrgRelations.java

@@ -22,7 +22,7 @@ public interface OrgRelations extends LinkedRelations<OrgRelations> {
      *
      * @return 部门关系链
      */
-    DepartmentRelations departments();
+    DepartmentRelations department();
 
 
 }

+ 1 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-authorization/src/main/java/org/hswebframework/web/organizational/authorization/relation/SimpleRelation.java

@@ -22,4 +22,5 @@ public class SimpleRelation implements Relation {
     private String name;
 
     private Direction direction;
+
 }

+ 11 - 5
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/SimpleDepartmentService.java

@@ -21,6 +21,7 @@ import org.hswebframework.web.BusinessException;
 import org.hswebframework.web.dao.organizational.DepartmentDao;
 import org.hswebframework.web.dao.organizational.PositionDao;
 import org.hswebframework.web.entity.organizational.DepartmentEntity;
+import org.hswebframework.web.entity.organizational.OrganizationalEntity;
 import org.hswebframework.web.entity.organizational.PositionEntity;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.AbstractTreeSortService;
@@ -28,6 +29,7 @@ import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.EnableCacheAllEvictTreeSortService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.organizational.DepartmentService;
+import org.hswebframework.web.service.organizational.OrganizationalService;
 import org.hswebframework.web.service.organizational.event.ClearPersonCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheConfig;
@@ -58,6 +60,9 @@ public class SimpleDepartmentService
     @Autowired
     private ApplicationEventPublisher publisher;
 
+    @Autowired
+    private OrganizationalService organizationalService;
+
     @Override
     public DepartmentDao getDao() {
         return departmentDao;
@@ -80,23 +85,24 @@ public class SimpleDepartmentService
         if (CollectionUtils.isEmpty(orgId)) {
             return Collections.emptyList();
         }
-        Set<String> allOrgId = new HashSet<>();
+        Set<String> allOrgId = new HashSet<>(orgId);
 
         if (children) {
             allOrgId.addAll(orgId.stream()
-                    .map(this::selectAllChildNode)
+                    .map(organizationalService::selectAllChildNode)
                     .flatMap(Collection::stream)
-                    .map(DepartmentEntity::getId)
+                    .map(OrganizationalEntity::getId)
                     .collect(Collectors.toSet()));
 
         }
         if (parent) {
             allOrgId.addAll(orgId.stream()
-                    .map(this::selectParentNode)
+                    .map(organizationalService::selectParentNode)
                     .flatMap(Collection::stream)
-                    .map(DepartmentEntity::getId)
+                    .map(OrganizationalEntity::getId)
                     .collect(Collectors.toSet()));
         }
+
         return createQuery()
                 .where()
                 .in(DepartmentEntity.orgId, allOrgId)

+ 43 - 27
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/relations/DefaultDepartmentRelations.java

@@ -1,8 +1,8 @@
 package org.hswebframework.web.service.organizational.simple.relations;
 
+import io.vavr.Lazy;
 import org.hswebframework.ezorm.core.NestConditional;
 import org.hswebframework.ezorm.core.dsl.Query;
-import org.hswebframework.ezorm.core.param.TermType;
 import org.hswebframework.web.commons.entity.param.QueryParamEntity;
 import org.hswebframework.web.entity.organizational.DepartmentEntity;
 import org.hswebframework.web.entity.organizational.PersonEntity;
@@ -50,7 +50,7 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
     @Override
     public DepartmentRelations relations(Relation.Direction direction, String dimension, String relation) {
 
-        if (dimension == null && direction != Relation.Direction.REVERSE) {
+        if ((dimension == null || Relation.TYPE_POSITION.equals(dimension)) && direction != Relation.Direction.REVERSE) {
             //没指定维度,尝试获取岗位关系
             positionQuery.is(PositionEntity.name, relation);
         }
@@ -75,7 +75,7 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
     @Override
     public DepartmentRelations not(String property, Object value) {
         departmentQuery.not(property, value);
-        positionQuery.not(property, value);
+//        positionQuery.not(property, value);
 
         return super.not(property, value);
     }
@@ -83,7 +83,7 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
     @Override
     public DepartmentRelations is(String property, Object value) {
         departmentQuery.is(property, value);
-        positionQuery.is(property, value);
+//        positionQuery.is(property, value);
         return super.is(property, value);
     }
 
@@ -92,6 +92,7 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
 
         Map<String, List<PersonEntity>> cache = new HashMap<>();
 
+        List<PositionEntity> positions = positionSupplier.get();
 
         List<String> positionIdList = getAllPerson()
                 .stream()
@@ -100,12 +101,11 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
                 .flatMap(bind -> bind.getPositionIds().stream().peek(positionId -> cache.computeIfAbsent(positionId, i -> new ArrayList<>()).add(bind)))
                 .collect(Collectors.toList());
 
-        QueryParamEntity positionQueryParam = positionQuery.end().in(PositionEntity.id, positionIdList)
-                .getParam();
+        if (CollectionUtils.isEmpty(positionIdList)) {
+            return super.relationStream(allDepartmentId);
+        }
 
-        Stream<Relation> relationStream = serviceContext
-                .getPositionService()
-                .select(positionQueryParam).stream()
+        Stream<Relation> relationStream = positions.stream()
                 .flatMap(position -> {
                     List<PersonEntity> personEntities = cache.get(position.getId());
                     if (CollectionUtils.isEmpty(personEntities)) {
@@ -130,9 +130,33 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
         return Stream.concat(relationStream, super.relationStream(allDepartmentId));
     }
 
+    public List<String> getAllDepartmentId() {
+        return allDepartmentId.get();
+    }
+
+    @SuppressWarnings("all")
+    private Supplier<List<PositionEntity>> positionSupplier = Lazy.val(() -> {
+        List<String> departmentId = getAllDepartmentId();
+        if (CollectionUtils.isEmpty(departmentId)) {
+            return (Supplier) () -> Collections.emptyList();
+        }
+        QueryParamEntity positionQueryParam = positionQuery.end()
+                .in(PositionEntity.departmentId, departmentId)
+                .getParam();
+
+        List<PositionEntity> positions = serviceContext
+                .getPositionService()
+                .select(positionQueryParam).stream()
+                .collect(Collectors.toList());
+
+        return (Supplier) () -> positions;
+    }, Supplier.class);
+
     private Supplier<List<String>> allDepartmentId = createLazyIdSupplier(() -> {
         Set<String> departmentId = new HashSet<>(targetIdSupplier.get());
-
+        if (CollectionUtils.isEmpty(departmentId)) {
+            return Collections.emptyList();
+        }
         Set<String> allParent = null, allChildren = null;
         //包含父级
         if (includeParent) {
@@ -162,9 +186,9 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
         if (paramEntity.getTerms().isEmpty()) {
             return new ArrayList<>(departmentId);
         }
-        paramEntity.noPaging()
-                .includes(DepartmentEntity.id)
-                .and(DepartmentEntity.id, TermType.in, departmentId);
+        paramEntity = departmentQuery.end()
+                .select(DepartmentEntity.id)
+                .in(DepartmentEntity.id, departmentId).getParam();
 
         return serviceContext.getDepartmentService()
                 .select(paramEntity)
@@ -175,27 +199,19 @@ public class DefaultDepartmentRelations extends DefaultLinkedRelations<Departmen
 
 
     public List<PersonEntity> getAllPerson() {
-        List<String> departmentId = allDepartmentId.get();
-
-        QueryParamEntity positionQueryParam = positionQuery.end()
-                .in(PositionEntity.departmentId, departmentId)
-                .getParam();
 
-        List<String> positionIds = serviceContext
-                .getPositionService()
-                .select(positionQueryParam).stream()
-                .map(PositionEntity::getId)
-                .collect(Collectors.toList());
-        if (CollectionUtils.isEmpty(positionIds)) {
+        List<PositionEntity> positionEntities = positionSupplier.get();
 
+        if (CollectionUtils.isEmpty(positionEntities)) {
             return Collections.emptyList();
         }
 
         return serviceContext
                 .getPersonService()
-                .selectByPositionIds(positionIds);
-
-
+                .selectByPositionIds(
+                        positionEntities.stream()
+                                .map(PositionEntity::getId)
+                                .collect(Collectors.toList()));
     }
 
     public List<String> getAllUserId() {

+ 1 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-local/src/main/java/org/hswebframework/web/service/organizational/simple/relations/DefaultOrgRelations.java

@@ -31,7 +31,7 @@ public class DefaultOrgRelations extends DefaultLinkedRelations<OrgRelations> im
     }
 
     @Override
-    public DepartmentRelations departments() {
+    public DepartmentRelations department() {
         return new DefaultDepartmentRelations(serviceContext, () -> serviceContext
                 .getDepartmentService()
                 .selectByOrgIds(targetIdSupplier.get(),includeChildren,includeParents)

+ 194 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-starter/src/test/java/org/hswebframework/web/starter/organizational/RelationsManagerTests.groovy

@@ -0,0 +1,194 @@
+package org.hswebframework.web.starter.organizational
+
+import org.hswebframework.web.bean.FastBeanCopier
+import org.hswebframework.web.entity.organizational.SimpleDepartmentEntity
+import org.hswebframework.web.entity.organizational.SimpleOrganizationalEntity
+import org.hswebframework.web.entity.organizational.SimplePersonAuthBindEntity
+import org.hswebframework.web.entity.organizational.SimplePositionEntity
+import org.hswebframework.web.organizational.authorization.relation.PersonRelations
+import org.hswebframework.web.organizational.authorization.relation.Relation
+import org.hswebframework.web.organizational.authorization.relation.RelationsManager
+import org.hswebframework.web.service.organizational.simple.relations.ServiceContext
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.context.SpringBootTest
+import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.web.WebAppConfiguration
+import spock.lang.Specification
+
+/**
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+@WebAppConfiguration
+@ContextConfiguration
+@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"])
+class RelationsManagerTests extends Specification {
+
+    @Autowired
+    RelationsManager relationsManager;
+
+    @Autowired
+    ServiceContext serviceContext;
+
+    def createData() {
+        def orgData = [
+                id      : "10001",
+                name    : "总公司",
+                code    : "000001",
+                children: [
+                        [id: "10002", name: "重庆分公司", code: "0000011"],
+                        [id: "10003", name: "北京分公司", code: "0000012"]
+                ]
+        ]
+        def departmentData = [
+                [
+                        id      : "100010001",
+                        name    : "人事部",
+                        code    : "100010001",
+                        orgId   : "10001",
+                        position: [
+
+                                [
+                                        id      : "1",
+                                        name    : "总监",
+                                        children:
+                                                [[
+                                                         id          : "10001",
+                                                         name        : "助理",
+                                                         departmentId: "100010001"
+                                                 ]]
+                                ], [
+                                        id  : "2",
+                                        name: "人事专员"
+                                ]
+
+                        ]
+                ],
+                [
+                        id      : "100020001",
+                        name    : "研发部",
+                        code    : "100020001",
+                        orgId   : "10002",
+                        position: [
+                                [
+                                        id      : "3",
+                                        name    : "经理",
+                                        children:
+                                                [[
+                                                         id          : "3001",
+                                                         name        : "助理",
+                                                         departmentId: "100020001"
+                                                 ]]
+                                ], [
+                                        id  : "4",
+                                        name: "技术人员"
+                                ]
+
+                        ]
+                ],
+                [
+                        id      : "100030001",
+                        name    : "研发部",
+                        code    : "100030001",
+                        orgId   : "10003",
+                        position: [
+                                [
+                                        id      : "5",
+                                        name    : "经理",
+                                        children:
+                                                [[
+                                                         id          : "5001",
+                                                         name        : "助理",
+                                                         departmentId: "100030001"
+                                                 ]]
+                                ], [
+                                        id  : "6",
+                                        name: "技术人员"
+                                ]
+
+                        ]
+                ]
+        ]
+
+        def personData = [
+                [
+                        id         : "1",
+                        name       : "张三",
+                        positionIds: ["1"]
+                ],
+                [
+                        id         : "2",
+                        name       : "李四",
+                        positionIds: ["10001"]
+                ],
+                [
+                        id         : "3",
+                        name       : "王五",
+                        positionIds: ["3"]
+                ],
+                [
+                        id         : "4",
+                        name       : "赵六",
+                        positionIds: ["4"]
+                ],
+                [
+                        id         : "5",
+                        name       : "周七",
+                        positionIds: ["2"]
+                ],
+                [
+                        id         : "6",
+                        name       : "宋九",
+                        positionIds: ["2"]
+                ]
+        ]
+        serviceContext.getOrganizationalService()
+                .insert(FastBeanCopier.copy(orgData, new SimpleOrganizationalEntity()));
+        departmentData.forEach({ department ->
+            serviceContext.getDepartmentService().insert(FastBeanCopier.copy(department, new SimpleDepartmentEntity()));
+            department.position.forEach({ position ->
+                position.departmentId = department.id;
+                serviceContext.getPositionService().insert(FastBeanCopier.copy(position, new SimplePositionEntity()));
+            })
+        })
+        personData.forEach({ person ->
+            serviceContext.getPersonService().insert(FastBeanCopier.copy(person, new SimplePersonAuthBindEntity()))
+        })
+    }
+
+    def setup() {
+        expect:
+        relationsManager != null
+
+    }
+
+    def "Test"() {
+        setup:
+        createData()
+        and:
+        def me = relationsManager.getPersonRelationsByPersonId("2")
+        def pre = relationsManager.getPersonRelationsByPersonId("4")
+
+        def relationList = me
+                .department()
+                .relations("总监")
+                .all()
+
+
+        def orgRelationList = pre
+                .org()
+                .andParents()
+                .department()
+                .relations("人事专员")
+                .all()
+
+        expect:
+        relationList != null
+        !relationList.isEmpty()
+        orgRelationList != null
+        !orgRelationList.isEmpty()
+        println relationList
+        println orgRelationList
+    }
+
+}

+ 14 - 0
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-starter/src/test/java/org/hswebframework/web/starter/organizational/TestApplication.java

@@ -0,0 +1,14 @@
+package org.hswebframework.web.starter.organizational;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.test.context.web.WebAppConfiguration;
+
+/**
+ * @author zhouhao
+ * @since 3.0.0-RC
+ */
+@SpringBootApplication
+@WebAppConfiguration
+public class TestApplication {
+
+}

+ 4 - 1
hsweb-system/hsweb-system-organizational/hsweb-system-organizational-starter/src/test/resources/application.yml

@@ -10,4 +10,7 @@ spring:
 hsweb:
     app:
       name: 组织架构测试
-      version: 3.0.0
+      version: 3.0.0
+logging:
+  level:
+    org.hswebframework: debug