diff --git a/casic-alarm/src/main/java/com/casic/missiles/modular/system/service/impl/DataScopeBuilder.java b/casic-alarm/src/main/java/com/casic/missiles/modular/system/service/impl/DataScopeBuilder.java new file mode 100644 index 0000000..9c5f957 --- /dev/null +++ b/casic-alarm/src/main/java/com/casic/missiles/modular/system/service/impl/DataScopeBuilder.java @@ -0,0 +1,115 @@ +package com.casic.missiles.modular.system.service.impl; + +import com.casic.missiles.modular.system.dao.DataScopeMapper; +import com.casic.missiles.modular.system.model.User; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Service +public class DataScopeBuilder { + + private final DataScopeMapper dataScopeMapper; + + public DataScopeBuilder(DataScopeMapper dataScopeMapper) { + this.dataScopeMapper = dataScopeMapper; + } + + /** + * 1、所有数据,得到的用户ID + * 2、所属查上级及上级所有部门,得到一个list + * 3、所属本部门 得到list + * 4、本部门自定义的一批部门信息 得到一个list + * + * @retur + */ + public List DataScopeProvider(Long deptId) { + List> userIdDataScopeList = dataScopeMapper.getUserIdDataScope(); + Map>> userDataScopeListMap = userIdDataScopeList.stream() + .collect(Collectors.groupingBy(a -> a.get("scopeType"))); + List userIdList = new ArrayList<>(); + userIdList.addAll(getAllUser(userDataScopeListMap.get("1"))); + userIdList.addAll(customDeptList(userDataScopeListMap.get("4"), deptId)); + List> deptUserList = new ArrayList<>(); + deptUserList.addAll(userDataScopeListMap.get("2")); + deptUserList.addAll(userDataScopeListMap.get("3")); + userIdList.addAll(getParentDeptList(deptUserList, deptId)); + List userList = userProvider(userIdList); + return userList; + } + + /** + * 1、所有数据的用户要发送 + * 2、 + * + * @return + */ + private List getAllUser(List> allUserList) { + List userIdList = new ArrayList<>(); + allUserList.forEach( + userDataScope -> userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId")))) + ); + return userIdList; + } + + /** + * 1 所有数据的用户要发送 + * + * @return + */ + private List getParentDeptList(List> allUserList, Long deptId) { + List userIdList = new ArrayList<>(); + String tempDeptIds = ""; + List> deptList = dataScopeMapper.getDeptList(); + Map deptMap = deptList.stream().collect( + Collectors.toMap(e -> Long.valueOf(e.get("id")), e -> Long.valueOf(e.get("pid"))) + ); + while (deptId != -1) { + tempDeptIds += "-" + deptId; + deptId = deptMap.get(deptId); + } + final String deptIds = tempDeptIds; + allUserList.forEach( + userDataScope -> { + if (deptIds.contains(String.valueOf(userDataScope.get("deptid")))) { + userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId")))); + } + } + ); + return userIdList; + } + + private List customDeptList(List> userList, Long deptId) { + List userIdList = new ArrayList<>(); + userList.forEach( + userDataScope -> { + if (userDataScope.get("dataScope").contains(String.valueOf(deptId))) { + userIdList.add(Long.valueOf(String.valueOf(userDataScope.get("userId")))); + } + } + ); + return userIdList; + } + + + private List userProvider(List userIdList) { + List allUserList = dataScopeMapper.getUserList(); + Map usersMap = allUserList.stream().collect( + Collectors.toMap(e -> e.getId(), Function.identity()) + ); + List scopeUserList = new ArrayList<>(); + userIdList.forEach( + userId -> { + if (usersMap.containsKey(userId)) { + scopeUserList.add(usersMap.get(userId)); + } + } + ); + return scopeUserList; + } + +}