java根据list数据通过接口姓名并填充

    @Resource
    DockingAccount dockingAccount;

    public <T> void convertVOName(List<T> list, String getIdFile, String setNameFile) {
        if (CollectionUtil.isNotEmpty(list)) {
            String getIdFileStr = "get" + getIdFile;
            String setNameFileStr = "set" + setNameFile;
            List<String> ids = list.stream().map(p -> ReflectUtil.invoke(p, getIdFileStr).toString()).distinct().collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(ids)) {
                DockingResult<List<UserBasicInfo>> userListDockingResult = dockingAccount.getUserBasicInfoByBatch(ids);
                if (userListDockingResult.isSuccess() && CollectionUtil.isNotEmpty(userListDockingResult.getBody())) {
                    list.forEach((p) -> {
                        Optional<UserBasicInfo> userBasicInfoOptional = userListDockingResult.getBody().stream().filter(x ->
                                x.getId().equals(ReflectUtil.invoke(p, getIdFileStr).toString())).findFirst();
                        if (userBasicInfoOptional.isPresent()) {
                            ReflectUtil.invoke(p, setNameFileStr, userBasicInfoOptional.get().getName());
                        }
                    });
                }
            }
        }
    }

 

posted @ 2019-10-30 13:59  一千零一夜  阅读(444)  评论(0编辑  收藏  举报