业务场景:
查询某个业务表,业务表中有部门编码和字典编码,这俩需要替换为对应名称返回前台,但这俩表均在其他数据库不能直接进行关联查询直接出数据,这里介绍的是其中的一种处理方式
首先根据需求查询出对应的业务表列表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;
}
}