在进行forEach循环时,需要通过当前实体编号查询另一服务的账号信息.下面给出了两种解决方案,先将所有的编号循环后add进定义好的DTO实体,根据此DTO实体中的编号信息查询账号信息,返回list后,将其存入Map中即可,通过foreach循环map将所有查询的信息拿出来后在进行set即可.
// 定义storeNo
List<String> storeNos = Lists.newArrayList();
// 循环storeNo
simpleOrderResultDTOIPage.getRecords().forEach(simpleOrderResultDTO -> {
storeNos.add(simpleOrderResultDTO.getStoreNo());
});
StoreAccountInfoQueryDTO storeAccountInfo = new StoreAccountInfoQueryDTO();
storeAccountInfo.setStoreAccountInfos(storeNos);
R<List<StoreAccountInfo>> storeAccountInfoDTOs = remoteStoreInfoService.getStoreAccountInfoByNo(storeAccountInfo, SecurityConstants.FROM_IN);
Map<String, List<StoreAccountInfo>> collect = storeAccountInfoDTOs.getData().stream().collect(Collectors.groupingBy(StoreAccountInfo::getStoreNo));
simpleOrderResultDTOIPage.getRecords().forEach(simpleOrderResultDTO -> {
StoreAccountInfo storeAccountInfo1 = collect.get(simpleOrderResultDTO.getStoreNo()).get(0);
simpleOrderResultDTO.setAccountBank(storeAccountInfo1.getAccountBank());
simpleOrderResultDTO.setAccountName(storeAccountInfo1.getAccountName());
simpleOrderResultDTO.setAccountId(storeAccountInfo1.getAccountId());
});
@Select("<script> " +
" select store_no as storeNo,account_id as accountId,account_name as accountName,account_bank as accountBank " +
" from store_account_info as info where info.deleted =0 " +
" <if test='storeAccountInfoQueryDTO.storeAccountInfos !=null and storeAccountInfoQueryDTO.storeAccountInfos.size() > 0 '>" +
" and info.store_no in " +
" <foreach item=\"storeNo\" collection=\"storeAccountInfoQueryDTO.storeAccountInfos\" open=\"(\" separator=\",\" close=\")\">" +
" #{storeNo}" +
" </foreach>" +
"</if>" +
" </script>")
List<StoreAccountInfo> getStoreAccountInfoByNo(@Param("storeAccountInfoQueryDTO") StoreAccountInfoQueryDTO storeAccountInfoDTO);
@Data
@EqualsAndHashCode
@ApiModel
public class StoreAccountInfoQueryDTO {
private List<String> storeAccountInfos;
}
最简单的实现方式是直接进行循环编号并进行查询,然后进行set,但这样对于数据库的操作的影响巨大
StoreAccountInfo data = remoteStoreInfoService.getStoreAccountInfoByNo1(simpleOrderResultDTO.getStoreNo(), SecurityConstants.FROM_IN).getData();
simpleOrderResultDTO.setAccountBank(data.getAccountBank());
simpleOrderResultDTO.setAccountId(data.getAccountId());
simpleOrderResultDTO.setAccountName(data.getAccountName());
@Select("<script> " +
" select store_no as storeNo,account_id as accountId,account_name as accountName,account_bank as accountBank " +
" from store_account_info as info where info.deleted =0 and info.store_no=#{storeNo}" +
" </script>")
StoreAccountInfo getStoreAccountInfoByNo1(@Param("storeNo") String storeNo);