package com.xboe.api; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Opt; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.xboe.api.vo.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.concurrent.ForkJoinPool; import java.util.stream.IntStream; @Service @Slf4j public class ThirdApi { public static final ForkJoinPool REQUEST_TASK = new ForkJoinPool(100); @Value("${orgTree.orgChildTreeList}") private String orgChildTreeListUrl; @Value("${audience.usersByAudienceList}") private String usersByAudienceList; @Value("${userBasic.searchUserList}") private String searchUserListUrl; @Value("${statApi.userdynamicList}") private String userdynamicListUrl; public List getAllUserList(UserListParam userListParam, String token) { log.info("获取用户"); String resp = Optional.ofNullable(HttpRequest.post(searchUserListUrl).body(JSONUtil.toJsonStr(userListParam)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); UserInfoListRootBean userInfoListRootBean1 = JSONUtil.toBean(resp, UserInfoListRootBean.class); log.info("userInfoListRootBean1 = " + userInfoListRootBean1); List list = userInfoListRootBean1.getResult().getList(); log.info("list = " + list); log.info("获取用户返回值1 {}",resp); return Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(resp, UserInfoListRootBean.class).success()) .map(UserInfoListRootBean::getResult) .map(result -> Opt.ofEmptyAble(result.getList()).peek(t -> nextPage(userListParam, token, t, result)).orElse(ListUtil.toList())) .orElse(ListUtil.toList()); } private void getAllUserList(UserListParam userListParam, String token, List userInfoLists) { log.info("获取用户2"); String resp = Optional.ofNullable(HttpRequest.post(searchUserListUrl).body(JSONUtil.toJsonStr(userListParam)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); log.info("获取用户返回值2 {}",resp); Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(t, UserInfoListRootBean.class).success()).map(UserInfoListRootBean::getResult).map(UserInfoListRootBean.ResultData::getList).stream().flatMap(Collection::stream).forEach(userInfoLists::add); } private void nextPage(UserListParam userListParam, String token, List userInfoLists, UserInfoListRootBean.ResultData t) { log.info("获取用户--" + userListParam.getPageNo()); if (t.getTotalPage() > userListParam.getPageNo()) { REQUEST_TASK.submit(() -> IntStream.range(userListParam.getPageNo(), t.getTotalPage()).parallel().forEach(i -> getAllUserList(userListParam.withPageNo(i + 1), token, userInfoLists))).join(); } } public List getOrgChildTree(String id, String token) { log.info("getOrgChildTree"); String resp = Optional.ofNullable(HttpRequest.post(orgChildTreeListUrl).header("token", token).body(JSONUtil.toJsonStr(TreeSearchVo.builder().orgId(id).build())).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); return Opt.ofNullable(JSONUtil.toBean(resp, OrgRootBean.class).success()).map(OrgRootBean::getResult).orElse(ListUtil.empty()); } public List getAllAudienceList(AuditListParam userListParam, String token) { String resp = Optional.ofNullable(HttpRequest.post(usersByAudienceList).body(JSONUtil.toJsonStr(userListParam)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); return Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(resp, AuditRootBean.class).success()) .map(AuditRootBean::getResult) .map(result -> Opt.ofEmptyAble(result.getList()).peek(t -> nextPage(userListParam, t, result, token)).orElse(ListUtil.toList())) .orElse(ListUtil.toList()); } private void nextPage(AuditListParam userListParam, List t, Result result, String token) { if (result.getTotalPage() > userListParam.getPageNo()) { REQUEST_TASK.submit(() -> IntStream.range(userListParam.getPageNo(), result.getTotalPage()).parallel().forEach(i -> getAllAudienceList(userListParam.withPageNo(i + 1), t, token))).join(); } } private void getAllAudienceList(AuditListParam userListParam, List list, String token) { String resp = Optional.ofNullable(HttpRequest.post(usersByAudienceList).body(JSONUtil.toJsonStr(userListParam)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(t, AuditRootBean.class).success()).map(AuditRootBean::getResult).map(Result::getList).stream().flatMap(Collection::stream).forEach(list::add); } public List getAllUserdynamicList(UserdynamicParam userdynamicParam, String token) { String resp = Optional.ofNullable(HttpRequest.post(userdynamicListUrl).body(JSONUtil.toJsonStr(userdynamicParam)).header("XBOE-Access-Token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); log.error("=1----getAllUserdynamicList----- 开始课程信息 ---------------------------------resp = " + resp ); return Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(resp, DynamicBean.class).success()) .map(DynamicBean::getResult) .map(result -> Opt.ofEmptyAble(result.getList()).peek(t -> nextPage(userdynamicParam, t, result, token)).orElse(ListUtil.toList())) .orElse(ListUtil.toList()); } private void nextPage(UserdynamicParam userdynamicParam, List t, UserDynamicResult result, String token) { log.error("=2----getAllUserdynamicList----- 开始课程信息 ---------------------------------resp = " + userdynamicParam ); if (result.getTotalPages() > userdynamicParam.getPageIndex()) { REQUEST_TASK.submit(() -> IntStream.range(userdynamicParam.getPageIndex(), result.getTotalPages()).parallel().forEach(i -> getAllUserdynamicList(userdynamicParam.withPageIndex(i + 1), t, token))).join(); } } private void getAllUserdynamicList(UserdynamicParam userdynamicParam, List list, String token) { log.error("=3----getAllUserdynamicList----- 开始课程信息 ---------------------------------resp = " + userdynamicParam ); String resp = Optional.ofNullable(HttpRequest.post(usersByAudienceList).body(JSONUtil.toJsonStr(userdynamicParam.builder().pageSize(1000).build())).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(t, DynamicBean.class).success()).map(DynamicBean::getResult).map(UserDynamicResult::getList).stream().flatMap(Collection::stream).forEach(list::add); } public static void main(String[] args) { String token = "eyJ0eXBlIjoidG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55Q29kZSI6IkMwMDEiLCJ1SWQiOiI5NjUzNDIwMjc0OTc2MDcxNjgiLCJjb21wYW55SWQiOiIxMDQxNjczOTc3Mzc5OTQ2NDk2IiwibG9naW5JZCI6IjE2ODg0NDg5MjIwNzY0OTE3NzgiLCJpc3MiOiJodHRwOi8vdS5ib2UuY29tIiwiR2l2ZW5OYW1lIjoiYm9ldSIsImV4cCI6MTY5MTM5OTc2NzU1OCwidXNlck5hbWUiOiLmnY7njonlhrAiLCJ1c2VySWQiOiI2QjA0OUZBRi1DMzE0LTdDQ0YtMEQyOC0wRDIzRjRDNDI1MzEifQ==.8b52dcf4d48a790ed258b9ca2b279bb269f5301722095382fbd352705b51c893"; String resp = Optional.ofNullable(HttpRequest.post("https://u-pre.boe.com/statApi/xboe/m/stat/userdynamic/list").body(JSONUtil.toJsonStr(UserdynamicParam.builder(). aid(Long.parseLong("965342027497607168")).pageSize(1000).pageIndex(1).build())).header("XBOE-Access-Token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); System.out.println(" resp = " +resp); DynamicBean a = JSONUtil.toBean(resp, DynamicBean.class); System.out.println(" a = " +a); System.out.println(" a = " + a.getResult().getCount()); List list = a.getResult().getList(); System.out.println(" list = " +list.size()); System.out.println(" list = " +list); } }