Java-代码-根据userIdList分页批量查询两处数据源-一个能批量一个不能批量-结合批量入参和数据源完整性
背景:数据源1,接口支持List,但是数据不完整
数据源2,接口不支持Lst,但是数据完整
综上:优先查询数据源1,没有查询到继续查询数据源2,整体再包装个分页 【可以修改分页pageSize和总体查询时间】
@GetMapping("/testQueryUserList")
public List<PaasUserOVo> testQueryUserList() throws InterruptedException {
List<String> userIdList = Lists.newArrayList();
for (int i = 50; i < 100; i++) {
userIdList.add("admin_"+i);
}
List<PaasUserOVo> list = eduTrainUserService.queryUserInfoByUserIdListByPaging(userIdList,true,true);
return list;
}
/**
* 分页获取人员信息并转换成map
* 取值使用 MapUtils.getByUserId 忽悠key大小写,示例: PaasUserOVo paasUser = MapUtils.getByUserId(userInfoMap, vo.getUserId());
* @param userIdList
* @param isRequreNotEmptyDepartmentParentId
* @param getFromPaasHignPriorityInvalid
* @return
*/
public ImmutableMap<String, PaasUserOVo> queryUserInfoMapByUserIdByPaging(List<String> userIdList, boolean isRequreNotEmptyDepartmentParentId, boolean getFromPaasHignPriorityInvalid) {
log.info("分页多数据源查人员转map#queryUserInfoMapByUserIdByPaging 开始分页查询userInfo,返回map对象,入参userIdList size:{}", userIdList != null ? userIdList.size() : "null");
if (CollectionUtils.isEmpty(userIdList)) {
return ImmutableMap.of();
}
List<PaasUserOVo> userOVoList = queryUserInfoByUserIdListByPaging(userIdList, isRequreNotEmptyDepartmentParentId, getFromPaasHignPriorityInvalid);
//按userId去重
List<PaasUserOVo> resultList = ListUtils.getInstance().getUniqueKeyList(userOVoList, PaasUserOVo::getUserId);
//返回的结果 key 是忽略大小写的
//MapUtils是自己改写guava的,见笔记 《Java-guava-list转map-MapUtils-key字段存在大小写问题导致get不出来》
ImmutableMap<String, PaasUserOVo> map = MapUtils.uniqueIndex(resultList, PaasUserOVo::getUserId);
return map;
}
// 5分钟
private final static int QUERY_USER_MAX_REQUEST_SECOND = 5 * 60;
@Value(value = "${queryUserInfoByUserIdListPageSize:500}")
private Integer queryUserInfoByUserIdListPageSize;
public List<PaasUserOVo> queryUserInfoByUserIdListByPaging(List<String> userIdList, boolean isRequreNotEmptyDepartmentParentId, boolean getFromPaasHignPriorityInvalid) {
log.info("分页多数据源查人员转map#queryUserInfoByUserIdListByPaging 开始分页查询userInfo,返回list对象, 入参userIdList size:{}", userIdList != null ? userIdList.size() : "null");
if (CollectionUtils.isEmpty(userIdList)) {
return Collections.emptyList();
}
int pageNum = 1;
int pageSize = bizzConfig.getQueryUserInfoByUserIdListPageSize();
List<PaasUserOVo> resultList = CollectionUtil.newArrayList();//这个对象用hutool,不然没有重写一些方法
long start = System.currentTimeMillis();
while (true) {
int costSecond = 0;
if (pageNum > 1) {
long now = System.currentTimeMillis();
costSecond = ((int) (now - start) / 1000);
if (costSecond > QUERY_USER_MAX_REQUEST_SECOND) {
log.warn("分页查询人员数据超时,当前userIdList size:{},耗时:{}秒", userIdList.size(), costSecond);
throw new BizzException("请求超时");
}
}
try {
List<String> pagingUserIdList = userIdList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
if (CollectionUtils.isEmpty(pagingUserIdList)) {
break;
}
List<PaasUserOVo> pagingUserOVoList = queryUserInfoByUserIdList(pagingUserIdList, isRequreNotEmptyDepartmentParentId, getFromPaasHignPriorityInvalid);
if (CollectionUtils.isNotEmpty(pagingUserOVoList)) {
resultList.addAll(pagingUserOVoList);
}
if (pagingUserIdList.size() < pageSize) {
break;
}
pageNum += 1;
} catch (Exception e) {
log.warn("分页查询人员数据出现异常,当前userIdList size:{},耗时:{}秒", userIdList.size(), costSecond);
break;
}
}
log.info("分页多数据源查人员转map#queryUserInfoByUserIdListByPaging 完成分页查询userInfo,返回list对象, 入参userIdList size:{},耗时:{}"
,userIdList.size() , DateUtils.ms2Hms(start));
return resultList;
}
public List<PaasUserOVo> queryUserInfoByUserIdList(List<String> userIdList, boolean isRequreNotEmptyDepartmentParentId, boolean getFromPaasHignPriorityInvalid) {
log.info("分页多数据源查人员转map#queryUserInfoByUserIdList 开始多数据源查询userInfo,返回list对象,入参userIdList size:{}", userIdList != null ? userIdList.size() : "null");
if (CollectionUtils.isEmpty(userIdList)) {
return Collections.emptyList();
}
//针对userIdList场景做个优化,先查paas-server,查不到在paas平台(不过对于编辑场景的数据可能还是因为没同步导致查不到最新的,但是这里主要的应用场景是培训加人,所以满足要求)
PaasServerUserDto dto = new PaasServerUserDto();
dto.setContainerId(UserInfoUtil.getCurContainerId());
dto.setPageNum(1);
dto.setPageSize(userIdList.size());
dto.setShowDel(1);
dto.setUserIds(userIdList);
ResultVo<PaasUserOVo> resultVo = paasUserService.getPaasUserList(dto);
List<PaasUserOVo> userList = resultVo.getList();
if (userList.size() < userIdList.size()) {
//表示没查全
List<String> successUserIdList = userList.stream().map(PaasUserOVo::getUserId).distinct().collect(Collectors.toList());
List<String> queryAgainUserList = userIdList.stream().filter(userIdFromParam -> !successUserIdList.contains(userIdFromParam)).distinct().collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(queryAgainUserList)) {
log.info("查询人员缺少部分人员信息,继续查询paas平台:queryAgainUserList:{}", queryAgainUserList);
List<PaasUserOVo> paasUserListByUserIdFromPaas = paasUserService.getPaasUserListByUserIdFromPaas(UserInfoUtil.getCurContainerId(), queryAgainUserList, true, false).getList();
userList.addAll(paasUserListByUserIdFromPaas);
}
}
if (resultVo != null && CollectionUtils.isNotEmpty(userList)) {
log.info("查询人员成功,人员信息:{}", JSON.toJSONString(resultVo.getList()));
if (isRequreNotEmptyDepartmentParentId) {
List<PaasUserOVo> paasUserOVoList = resultVo.getList();
if (CollectionUtils.isNotEmpty(paasUserOVoList)) {
paasUserOVoList.forEach(user -> {
if (StringUtils.isBlank(user.getDepartmentParentId())) {
log.error("查询到的departmentParentId为空,重试中");//这里departmentParentId为空,最好给个参数,如果为空也不行
throw new BizzException("未查询人员");//关系到paas-server同步及时性问题
}
});
}
}
return resultVo.getList();
}
log.warn("查询人员失败,需要确认paas-server是否有userIdList:{}", userIdList);
return Collections.emptyList();
}
- 本文标签: Spring Java Spring Boot
- 本文链接: https://code.jiangjiesheng.cn/article/220
- 版权声明: 本文由小江同学原创发布,转载请先联系本站长,谢谢。