关联查询时表在不同数据库,使用stream流进行集合中元素的某一属性进行替换

业务场景:

查询某个业务表,业务表中有部门编码和字典编码,这俩需要替换为对应名称返回前台,但这俩表均在其他数据库不能直接进行关联查询直接出数据,这里介绍的是其中的一种处理方式

首先根据需求查询出对应的业务表列表List<Map>集合,然后通过feign调取部门和字典提供的接口查询出对应的部门和字典集合,然后进行字段的替换:

        mapList = technologyInnovationPromotionMapper.queryPage(page,params);//业务实体列表集合

        Result<Map<String, List<DicEntity>>> dictResult = uaaClient.queryByCodes(Constant.DICT_CODE);
        Map<String, List<DicEntity>> dictResultData = dictResult.getData();
        List<DicEntity> dicEntityList = dictResultData.get(Constant.DICT_CODE);//字典列表集合
        mapList.stream().forEach(e -> e.put(Constant.DICT_CODE,ReplaceUtils.replaceDict(dicEntityList,(String)e.get(Constant.DICT_CODE))));

        Result<List<QltdDeptEntity>> deptResult = uaaClient.queryDeptList(Constant.DEPT_CODE);
        List<QltdDeptEntity> deptEntityList = deptResult.getData();//部门列表集合
        mapList.stream().forEach(e -> e.put(Constant.COMPANY_NAME, ReplaceUtils.replaceDept(deptEntityList,(String)e.get(Constant.COMPANY_NAME))));

        page.setRecords(mapList);
        return page;

对应工具类:


/**
 * 编号名称替换工具类
 *
 * @author gbx
 * @since 2020-07-09 09:10:21
 */
public class ReplaceUtils {
    /**
     * 根据部门编号替换为部门名称
     * @param deptEntityList 封装部门实体的集合
     * @param deptCode 部门编号
     * @return 部门名称
     */
    public static String replaceDept(List<QltdDeptEntity> deptEntityList, String deptCode){
        Optional<QltdDeptEntity> optional = deptEntityList.stream().filter(e -> e.getDeptCode().equals(deptCode)).findFirst();
        if (optional.isPresent()){
            return optional.get().getName();
        }
        return deptCode;
    }

    /**
     * 根据字典编号替换为字典名称
     * @param dicEntityList 封装字典实体的集合
     * @param dictCode 字典编号
     * @return 字典名称
     */
    public static String replaceDict(List<DicEntity> dicEntityList, String dictCode){
        Optional<DicEntity> optional = dicEntityList.stream().filter(e -> e.getDictCode().equals(dictCode)).findFirst();
        if (optional.isPresent()){
            return optional.get().getDictName();
        }
        return dictCode;
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值