提交发起

This commit is contained in:
Guava
2023-06-29 18:01:07 +08:00
parent ee9ace5d9c
commit e1fc23ba57
16 changed files with 431 additions and 25 deletions

View File

@@ -0,0 +1,85 @@
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;
public List<UserInfoList> 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校验失败"));
System.out.println(resp);
return Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(resp, UserInfoListRootBean.class).success())
.map(UserInfoListRootBean::getResult)
.map(result -> Opt.ofEmptyAble(result.getUserInfoList()).peek(t -> nextPage(userListParam, token, t, result)).orElse(ListUtil.toList()))
.orElse(ListUtil.toList());
}
private void getAllUserList(UserListParam userListParam, String token, List<UserInfoList> userInfoLists) {
log.info("获取用户2");
String resp = Optional.ofNullable(HttpRequest.post(searchUserListUrl).body(JSONUtil.toJsonStr(userListParam)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败"));
System.out.println(resp);
Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(t, UserInfoListRootBean.class).success()).map(UserInfoListRootBean::getResult).map(UserInfoListRootBean.ResultData::getUserInfoList).stream().flatMap(Collection::stream).forEach(userInfoLists::add);
}
private void nextPage(UserListParam userListParam, String token, List<UserInfoList> userInfoLists, UserInfoListRootBean.ResultData t) {
System.out.println("获取用户--" + userListParam.getPage());
if (t.getTotalPage() > userListParam.getPage()) {
REQUEST_TASK.submit(() -> IntStream.range(userListParam.getPage(), t.getTotalPage()).parallel().forEach(i -> getAllUserList(userListParam.withPage(i + 1), token, userInfoLists))).join();
}
}
public List<OrgRootBean.ResultData> 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<AuditList> 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<AuditList> t, Result result, String token) {
if (result.getTotalPage() > userListParam.getPage()) {
REQUEST_TASK.submit(() -> IntStream.range(userListParam.getPage(), result.getTotalPage()).parallel().forEach(i -> getAllAudienceList(userListParam.withPage(i + 1), t, token))).join();
}
}
private void getAllAudienceList(AuditListParam userListParam, List<AuditList> 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);
}
}