【复杂数据】 记录工作中 遇到因业务造成复杂数据 及其处理

一、对比两个Map<String,List> map 将数据多的map 每一个list中的实体类对象的某个字段设置为指定值

数据示例:
在这里插入图片描述
业务示例:对比数据库中 子公司及父公司的所有权限 , 仅存在于父公司的 表示子公司没有该权限,前端界面统一展示所有权限(父公司权限) 当子公司没有该权限时,表明它可以被授权 grant set为1

代码示例:
业务场景1:子公司不存在某个list中的某一条数据,整个key下所有list都可以被授权:

// 谷歌工具包
 import com.google.common.collect.Maps;
    /**
     * Description: 查看子公司参数权限
     * date: 2021/5/25
     * @author:qkj
     *
     * @since JDK 1.8
     */
    public Map<String,List<CompanyParameterVO>> getSubCompanyParameterList(CompanyGrantDTO companyGrantDTO) {
    
    

        if (!CheckUtils.isNotEmpty(companyGrantDTO) || !CheckUtils.isNotEmpty(companyGrantDTO.getSubCompanyId()) || !CheckUtils.isNotEmpty(companyGrantDTO.getTopCompanyId())) {
    
    
            throw new CustomException(500,"参数不能为空");
        }

        Map<String, List<CompanyParameterVO>> topCompanyParameters = this.getCompanyParameters(companyGrantDTO.getTopCompanyId());
        Map<String, List<CompanyParameterVO>> subCompanyParameters = this.getCompanyParameters(companyGrantDTO.getSubCompanyId());

		
        MapDifference<String, List<CompanyParameterVO>> difference = Maps.difference(topCompanyParameters, subCompanyParameters);
        // 树顶公司独有  
        // difference.entriesOnlyOnLeft()方法 : key-value中 只能将value作为一个整体判断 不能精确区分list中的具体数据
        Map<String, List<CompanyParameterVO>> onlyTop = difference.entriesOnlyOnLeft();

       // 将独有数据set grant为1  表示可以被授权
        Set<Map.Entry<String, List<CompanyParameterVO>>> entries = onlyTop.entrySet();
        for (Map.Entry<String, List<CompanyParameterVO>> entry : entries) {
    
    
            List<CompanyParameterVO> value = entry.getValue();
            for (CompanyParameterVO companyParameterVO : value) {
    
    
                companyParameterVO.setGrant(1);
            }
        }

        Map resultMap = new HashMap(16);
        resultMap.putAll(onlyTop);
        resultMap.putAll(subCompanyParameters);
        
        // 将所有公司id替换为子公司id
        Set<Map.Entry<String, List<CompanyParameterVO>>> set = resultMap.entrySet();
        for (Map.Entry<String, List<CompanyParameterVO>> entry : set) {
    
    
            List<CompanyParameterVO> value = entry.getValue();
            for (CompanyParameterVO companyParameterVO : value) {
    
    
                companyParameterVO.setCompanyId(companyGrantDTO.getSubCompanyId());
            }

        }
        return resultMap;

    }

代码示例2:
业务场景2:子公司不存在某个list中的某一条数据,需要精确到该具体权限

 public Map<String,List<CompanyParameterVO>> getSubCompanyParameterList(CompanyGrantDTO companyGrantDTO) {
    
    

        if (!CheckUtils.isNotEmpty(companyGrantDTO) || !CheckUtils.isNotEmpty(companyGrantDTO.getSubCompanyId()) || !CheckUtils.isNotEmpty(companyGrantDTO.getTopCompanyId())) {
    
    
            throw new CustomException(500,"参数不能为空");
        }

        Map<String, List<CompanyParameterVO>> topCompanyParameters = this.getCompanyParameters(companyGrantDTO.getTopCompanyId());
        Map<String, List<CompanyParameterVO>> subCompanyParameters = this.getCompanyParameters(companyGrantDTO.getSubCompanyId());

        Set<Map.Entry<String, List<CompanyParameterVO>>> topEntry = topCompanyParameters.entrySet();
        Set<Map.Entry<String, List<CompanyParameterVO>>> subEntry = subCompanyParameters.entrySet();

        List<CompanyParameterVO> topAllList = new ArrayList<>();
        for (Map.Entry<String, List<CompanyParameterVO>> top : topEntry) {
    
    
            List<CompanyParameterVO> topList = top.getValue();
            topAllList.addAll(topList);
        }

        List<CompanyParameterVO> subAllList = new ArrayList<>();
        for (Map.Entry<String, List<CompanyParameterVO>> sub : subEntry) {
    
    
            List<CompanyParameterVO> subList = sub.getValue();
            subAllList.addAll(subList);
        }


        List<CompanyParameterVO> onlyOnTopList = topAllList.stream().filter(item -> !subAllList.contains(item)).collect(Collectors.toList());
        for (CompanyParameterVO each : onlyOnTopList) {
    
    
            each.setGrant(1);
        }

        List<CompanyParameterVO> topNewList = new ArrayList<>();
        topNewList.addAll(onlyOnTopList);
        topNewList.addAll(subAllList);

        for (CompanyParameterVO companyParameterVO : topNewList) {
    
    
            companyParameterVO.setCompanyId(companyGrantDTO.getSubCompanyId());
        }

        Map<String, List<CompanyParameterVO>> resultMap = topNewList.stream().collect(Collectors.groupingBy(CompanyParameterVO::getParameterBelong));


        return resultMap;

    }



二、判断树子节点 当同一级子节点 某个字段有不同状态时,修改父节点该字段为另一个值 修改父节点时 父节点的父节点很可能因此改变

数据示例:
在这里插入图片描述

业务示例:当子节点部分勾选时,即isPermission 同时存在0,1 父节点isPermission 设置为2 (部分权限),且只要树叉存在2 上级节点都需要为2

代码示例:

// 多树结构 森林
//List<RoleMenuTree> roleMenuTrees = xxx
        
// 把子节点对象权限状态不一致的父节点标致设为2 前端需要该标致判断
//        for (RoleMenuTree roleMenuTree : roleMenuTrees) {
    
    
//            this.getChildValue(roleMenuTree);
 //       }
 
/**
     * Description: 处理树数据
     * date: 2021/5/25
     * @author:qkj
     *
     * @since JDK 1.8
     */

    public String getChildValue(TreeNode treeNode) {
    
    

        List<TreeNode> children = treeNode.getChildren();
        // 防止空指针
        if (children == null) {
    
    
            children = new ArrayList<>();
        }

        Set<String>permissionSet = new HashSet<>();
        for (TreeNode node : children) {
    
    

            String childPermissionValue = this.getChildValue(node);
            permissionSet.add(childPermissionValue);
        }

        RoleMenuTree tree = (RoleMenuTree) treeNode;
        // 子节点有不同状态或包含 “2”的时候 父节点设置为2
        if (permissionSet.size() >= 2 || permissionSet.contains("2")) {
    
    

            tree.setIsPermission("2");
        }
        return tree.getIsPermission();

    }

Guess you like

Origin blog.csdn.net/qq_36268103/article/details/117260680