package com.doumee.service.aware; import com.doumee.core.aware.DataPermissionMapping; import com.doumee.core.aware.DefaultDataPermissionAware; import com.doumee.core.constants.Constants; import com.doumee.core.constants.DataPermissionConstants; import com.doumee.dao.system.dto.QuerySystemDepartmentDTO; import com.doumee.dao.system.model.SystemDepartmentUser; import com.doumee.dao.system.vo.SystemDepartmentListVO; import com.doumee.service.system.SystemDepartmentService; import com.doumee.service.system.SystemDepartmentUserService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 部门数据权限控制 * @author Eva.Caesar Liu * @date 2022/03/15 09:54 */ @Component public class DepartmentDataPermissionAware extends DefaultDataPermissionAware { @Autowired private SystemDepartmentService systemDepartmentService; @Autowired private SystemDepartmentUserService systemDepartmentUserService; @Override public DataPermissionConstants.Module module() { return DataPermissionConstants.Module.DEPARTMENT; } @Override public List defaultData(String userId) { return onlyUser(userId); } /** * 全部数据 * * @return List */ @DataPermissionMapping(value = DataPermissionConstants.Type.ALL, priority = 1) public List all() { QuerySystemDepartmentDTO dto = new QuerySystemDepartmentDTO(); return this.getRootList(systemDepartmentService.findList(dto)); } /** * 自定义 * * @param customData 自定义数据ID集 * @return List */ @DataPermissionMapping(value = DataPermissionConstants.Type.DEPARTMENT_CUSTOM, priority = 2, injectCustomData = true) public List custom(String customData) { if (StringUtils.isBlank(customData)) { return Collections.emptyList(); } List ids = new ArrayList<>(); String[] stringIds = customData.split(","); for(String stringId : stringIds) { ids.add(Integer.valueOf(stringId)); } QuerySystemDepartmentDTO dto = new QuerySystemDepartmentDTO(); dto.setIds(ids); List departmentListVo = systemDepartmentService.findList(dto); return this.getRootList(departmentListVo); } /** * 用户所属部门及其子孙部门 * * @param userId 用户ID * @return List */ @DataPermissionMapping(value = DataPermissionConstants.Type.DEPARTMENT_CHILDREN, priority = 3, injectUser = true) public List children(String userId) { return this.getRootList(getUserChildren(userId)); } /** * 用户所属部门及其子部门 * * @param userId 用户ID * @return List */ @DataPermissionMapping(value = DataPermissionConstants.Type.DEPARTMENT_CHILD, priority = 4, injectUser = true) public List child(String userId) { List children = this.getRootList(getUserChildren(userId)); for (SystemDepartmentListVO root : children) { if (CollectionUtils.isEmpty(root.getChildren())) { continue; } for (SystemDepartmentListVO child : root.getChildren()) { if (CollectionUtils.isEmpty(child.getChildren())) { continue; } child.setHasChildren(Boolean.TRUE); child.setChildren(null); } } return children; } /** * 仅用户所属部门 * * @param userId 用户ID * @return List */ @DataPermissionMapping(value = DataPermissionConstants.Type.DEPARTMENT, priority = 5, injectUser = true) public List onlyUser(String userId) { SystemDepartmentListVO userDepartment = this.getUserDepartment(userId); if (userDepartment == null) { return Collections.emptyList(); } return new ArrayList(){{ this.add(userDepartment); }}; } /** * 获取根部门 */ private List getRootList(List departments) { if (CollectionUtils.isEmpty(departments)) { return Collections.emptyList(); } List rootDepartments = new ArrayList<>(); // 添加根部门 for (SystemDepartmentListVO currentDepartment : departments) { boolean hasParent = false; for (SystemDepartmentListVO department: departments) { if (department.getId().equals(currentDepartment.getParentId())) { hasParent = true; break; } } if (!hasParent) { rootDepartments.add(currentDepartment); } } // 移除根部门 for (SystemDepartmentListVO rootDepartment : rootDepartments) { departments.removeIf(department -> department.getId().equals(rootDepartment.getId())); } // 填充子部门 for (SystemDepartmentListVO child : rootDepartments) { this.fillChildren(child, departments); } return rootDepartments; } /** * 获取用户部门 */ private SystemDepartmentListVO getUserDepartment (String userId) { SystemDepartmentUser queryDto = new SystemDepartmentUser(); queryDto.setUserId(userId); queryDto.setDeleted(Constants.ONE); SystemDepartmentUser departmentUser = systemDepartmentUserService.findOne(queryDto); if (departmentUser == null) { return null; } QuerySystemDepartmentDTO dto = new QuerySystemDepartmentDTO(); dto.setId(departmentUser.getDepartmentId()); List systemDepartments = systemDepartmentService.findList(dto); if (systemDepartments.isEmpty()) { return null; } return systemDepartments.get(0); } /** * 获取用户子孙部门 */ private List getUserChildren (String userId) { SystemDepartmentListVO userDepartment = this.getUserDepartment(userId); if (userDepartment == null) { return Collections.emptyList(); } // 查询用户所在部门以下部门 List departmentListVo = new ArrayList<>(); List ids = systemDepartmentService.findChildren(userDepartment.getId()); if (!ids.isEmpty()) { QuerySystemDepartmentDTO dto = new QuerySystemDepartmentDTO(); dto.setIds(ids); departmentListVo = systemDepartmentService.findList(dto); } departmentListVo.add(userDepartment); return departmentListVo; } /** * 填充子部门 */ private void fillChildren(SystemDepartmentListVO parent, List departments) { if (departments.size() == 0) { return; } if (parent.getChildren() == null) { parent.setChildren(new ArrayList<>()); } List handledIds = new ArrayList<>(); for (SystemDepartmentListVO department : departments) { if (parent.getId().equals(department.getParentId())) { SystemDepartmentListVO child = new SystemDepartmentListVO(); BeanUtils.copyProperties(department, child, "children"); child.setChildren(new ArrayList<>()); parent.getChildren().add(child); handledIds.add(department.getId()); } } departments.removeIf(menu -> handledIds.contains(menu.getId())); parent.setHasChildren(Boolean.TRUE); if (parent.getChildren().size() > 0) { parent.setHasChildren(Boolean.FALSE); for (SystemDepartmentListVO child : parent.getChildren()) { this.fillChildren(child, departments); } } } }