mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-07 09:56:47 +08:00
345 lines
19 KiB
Java
345 lines
19 KiB
Java
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 com.xboe.module.course.dto.CourseParam;
|
||
import com.xboe.module.course.dto.ScoreParam;
|
||
import com.xboe.module.course.vo.StudyCourseVo;
|
||
import com.xboe.module.course.vo.TeacherInfoVo;
|
||
import com.xboe.module.course.vo.TeacherVo;
|
||
import com.xboe.module.dict.entity.DictDto;
|
||
import com.xboe.module.exam.entity.ExamTest;
|
||
import com.xboe.school.study.entity.StudyCourse;
|
||
import com.xboe.system.user.dao.UserDao;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.beans.factory.annotation.Value;
|
||
import org.springframework.stereotype.Service;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.net.URLEncoder;
|
||
import java.util.Collection;
|
||
import java.util.List;
|
||
import java.util.Optional;
|
||
import java.util.concurrent.ForkJoinPool;
|
||
import java.util.stream.Collectors;
|
||
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;
|
||
|
||
@Autowired
|
||
UserDao userDao;
|
||
|
||
@Value("${infrasApi.dict}")
|
||
private String infarasApiUrl;
|
||
|
||
@Value("${manageApi.stu.offcourse}")
|
||
private String manageApiStu;
|
||
|
||
@Value("${audience.getOrgUsers}")
|
||
private String searchOrgUsersUrl;
|
||
|
||
@Value("${userBasic.getTeacherIds}")
|
||
private String getTeacherIds;
|
||
|
||
|
||
@Value("${coursesuilt.getStudyStatus}")
|
||
private String getStudyStatus;
|
||
|
||
@Value("${manageApi.editExam}")
|
||
private String editExam;
|
||
|
||
@Value("${userBasic.getUserBasicInfo}")
|
||
private String getUserBasicInfo;
|
||
|
||
@Value("${coursesuilt.updateOrSaveCourse}")
|
||
private String updateOrSaveCourse;
|
||
@Value("${coursesuilt.syncCourseStudent}")
|
||
private String syncCourseStudent;
|
||
@Value("${coursesuilt.syncOnLineScore}")
|
||
private String syncOnLineScore;
|
||
@Value("${coursesuilt.updateOnLineStatua}")
|
||
private String updateOnLineStatua;
|
||
|
||
|
||
//获取例外人员的id
|
||
public List<String> getUserId(){
|
||
String responseBody = Optional.ofNullable(HttpRequest.get(infarasApiUrl+"?pid=316&type=1").execute() //prod 316
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
log.info("正在获取例外人员工号 responseBody = " + responseBody);
|
||
try {
|
||
Optional<DictResult> dictResultOptional = Optional.of(responseBody)
|
||
.map(t -> JSONUtil.toBean(t, DictResult.class));
|
||
if (!dictResultOptional.isPresent()) {
|
||
throw new RuntimeException("解析DictResult失败:字符串格式不正确。");
|
||
}
|
||
List<DictDto> dictDtos = dictResultOptional.get().getData();
|
||
if (dictDtos == null || dictDtos.isEmpty()) {
|
||
throw new RuntimeException("DictResult中的data字段为空。");
|
||
}
|
||
return dictDtos.stream().map(DictDto::getValue).collect(Collectors.toList());
|
||
}catch (Exception e){
|
||
throw new RuntimeException("解析过程发生异常:" + e.getMessage(), e);
|
||
}
|
||
|
||
}
|
||
//获取面授课id
|
||
public List<String>getOffCourseId(String userId,String token){
|
||
log.info("正在获取面授课id");
|
||
String s = Optional.ofNullable(HttpRequest.get(manageApiStu+"?userId="+userId).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
return Opt.ofBlankAble(s).map(t -> JSONUtil.toBean(t, StringResult.class)).map(StringResult::getData).orElseThrow();
|
||
}
|
||
//获取离职教师信息
|
||
public List<TeacherVo>getTeacherInfo(List<String> teacherIds, String token){
|
||
TeacherInfoVo teacherInfoVo=new TeacherInfoVo();
|
||
teacherInfoVo.setTeacherIds(teacherIds);
|
||
log.info("获取离职教师信息");
|
||
log.info(JSONUtil.toJsonStr(teacherInfoVo));
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(getTeacherIds).body(JSONUtil.toJsonStr(teacherInfoVo)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
GetTeacherIdsResult getTeacherIdsResult = JSONUtil.toBean(resp, GetTeacherIdsResult.class);
|
||
log.info("getTeacherIdsResult = " + getTeacherIdsResult.getResult());
|
||
return getTeacherIdsResult.getResult();
|
||
}
|
||
public List<TeacherVo>getTeacherInfoByCode(List<String> teacherCode,String token){
|
||
TeacherInfoVo teacherInfoVo=new TeacherInfoVo();
|
||
teacherInfoVo.setTeacherCode(teacherCode);
|
||
log.info("获取离职教师信息");
|
||
log.info("参数为"+JSONUtil.toJsonStr(teacherCode));
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(getTeacherIds).body(JSONUtil.toJsonStr(teacherInfoVo)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
GetTeacherIdsResult getTeacherIdsResult = JSONUtil.toBean(resp, GetTeacherIdsResult.class);
|
||
log.info("getTeacherIdsResult = " + getTeacherIdsResult);
|
||
return getTeacherIdsResult.getResult();
|
||
}
|
||
public List<UserInfoListVo> getAllUserList(UserListParam userListParam, String token) {
|
||
log.info("获取用户:url:{}", searchOrgUsersUrl);
|
||
log.info("获取用户:params:{}", JSONUtil.toJsonStr(userListParam));
|
||
String resp = Optional.ofNullable(HttpRequest.post(searchOrgUsersUrl).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<UserInfoListVo> 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<UserInfoListVo> userInfoLists) {
|
||
log.info("获取用户2");
|
||
String resp = Optional.ofNullable(HttpRequest.post(searchOrgUsersUrl).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<UserInfoListVo> userInfoLists, UserInfoListRootBean.ResultData t) {
|
||
log.info("nextPage 获取用户--" + 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<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.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<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);
|
||
}
|
||
|
||
public List<UserDynamic> 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<UserDynamic> 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<UserDynamic> 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 List<StudyCourse>getStudyCourseList(String studyId,String courseId, String token){
|
||
StudyCourseVo studyCourseVo = new StudyCourseVo();
|
||
studyCourseVo.setStudyId(studyId);
|
||
studyCourseVo.setCourseId(courseId);
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(getStudyStatus).body(JSONUtil.toJsonStr(studyCourseVo)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
StudyCourseResult studyCourseResult = JSONUtil.toBean(resp, StudyCourseResult.class);
|
||
log.info("getTeacherIdsResult = " + studyCourseResult);
|
||
return studyCourseResult.getResult();
|
||
}
|
||
|
||
public void sqlUpdate(ExamTest examTest, String token) {
|
||
examTest.setSysUpdateTime(null);
|
||
examTest.setSysCreateTime(null);
|
||
examTest.setPublishTime(null);
|
||
String resp = Optional.ofNullable(HttpRequest.post(editExam).body(JSONUtil.toJsonStr(examTest)).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
Opt.ofBlankAble(resp).map(t -> JSONUtil.toBean(t, DynamicBean.class).success());
|
||
}
|
||
|
||
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<UserDynamic> list = a.getResult().getList();
|
||
System.out.println(" list = " +list.size());
|
||
System.out.println(" list = " +list);
|
||
|
||
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取案例浏览记录
|
||
*/
|
||
public UserDynamicResult getAllUserdynamicListOfCaseRead(UserdynamicParam userdynamicParam, String token) {
|
||
String s = buildFormData(userdynamicParam);
|
||
String resp = Optional.ofNullable(HttpRequest
|
||
.post(userdynamicListUrl)
|
||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||
.body(s)
|
||
.header("XBOE-Access-Token", token)
|
||
.execute().body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
|
||
DynamicBean dynamicBean = JSONUtil.toBean(resp, DynamicBean.class);
|
||
UserDynamicResult userdynamicResult = dynamicBean.getResult();
|
||
return userdynamicResult;
|
||
}
|
||
|
||
private String buildFormData(UserdynamicParam param) {
|
||
StringBuilder builder = new StringBuilder();
|
||
try {
|
||
builder.append("pageIndex=").append(URLEncoder.encode(param.getPageIndex().toString(), "UTF-8"));
|
||
|
||
builder.append("&pageSize=").append(URLEncoder.encode(param.getPageSize().toString(), "UTF-8"));
|
||
builder.append("&contentType=").append(URLEncoder.encode(param.getContentType().toString(), "UTF-8"));
|
||
builder.append("&aid=").append(URLEncoder.encode(param.getAid().toString(), "UTF-8"));
|
||
builder.append("&eventKey=").append(URLEncoder.encode("ReadCase", "UTF-8"));
|
||
if (param.getHidden() != null) {
|
||
builder.append("&hidden=").append(URLEncoder.encode(param.getHidden(), "UTF-8"));
|
||
}
|
||
} catch (UnsupportedEncodingException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
return builder.toString();
|
||
}
|
||
|
||
/**
|
||
* 虽然当前已存在接口查询用户基本信息,目前仅仅包括用户名、工号、用户ID
|
||
*/
|
||
public List<UserBasicInfo> getUserBasicInfoByUserId(List<String> userIds, HttpServletRequest request) {
|
||
String token = request.getHeader("Xboe-Access-Token");
|
||
if (StringUtils.isEmpty(token)) {
|
||
token = request.getHeader("token");
|
||
}
|
||
if (StringUtils.isEmpty(token)) {
|
||
token = request.getHeader("x-access-token");
|
||
}
|
||
|
||
// 将userIds列表转换为逗号分隔的字符串
|
||
String userIdsStr = userIds.stream()
|
||
.collect(Collectors.joining(","));
|
||
String url = getUserBasicInfo +"?userIds=" + userIdsStr;
|
||
|
||
String respStr = Optional.ofNullable(HttpRequest
|
||
.get(url)
|
||
.header("token", token)
|
||
.execute().body()).orElseThrow(() -> new RuntimeException("用户中心用户数据获取失败"));
|
||
|
||
UserBasicInfoResult userBasicInfoResult = JSONUtil.parseObj(respStr).toBean(UserBasicInfoResult.class);
|
||
List<UserBasicInfo> basicInfos = userBasicInfoResult.getResult();
|
||
return basicInfos;
|
||
|
||
}
|
||
|
||
public void updateOrSaveCourse(CourseParam param, String token){
|
||
log.info("---------------准备同步在线课到讲师管理完毕 ------- param " + param);
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(updateOrSaveCourse).body(JSONUtil.toJsonStr(param)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
log.info("updateOrSaveCourse = " + resp);
|
||
}
|
||
public void syncCourseStudent(Long courseId, String token){
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(syncCourseStudent).body(JSONUtil.toJsonStr(courseId)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
log.info("syncCourseStudent = " + resp);
|
||
}
|
||
|
||
public void syncOnLineScore(ScoreParam param, String token) {
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(syncOnLineScore).body(JSONUtil.toJsonStr(param)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
log.info("syncOnLineScore = " + resp);
|
||
}
|
||
public void updateOnLineStatua(CourseParam param, String token){
|
||
String resp = Optional.ofNullable(
|
||
HttpRequest.post(updateOnLineStatua).body(JSONUtil.toJsonStr(param)).header("token", token).execute()
|
||
.body()).orElseThrow(() -> new RuntimeException("token校验失败"));
|
||
log.info("updateOrSaveCourse = " + resp);
|
||
}
|
||
}
|