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.alibaba.nacos.shaded.com.google.gson.*; import com.xboe.api.vo.*; 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.AloneExam; 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 java.time.LocalDateTime; import java.util.*; 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.getUserIdByWorkNum}") private String getUserIdByWorkNum; @Value("${coursesuilt.checkBanding}") private String checkBanding; @Value("${coursesuilt.syncExamScoreToCourseSuite}") private String syncExamScoreToCourseSuite; //获取例外人员的id public List getUserId(){ String responseBody = Optional.ofNullable(HttpRequest.get(infarasApiUrl+"?pid=136&type=1").execute() //prod 316 .body()).orElseThrow(() -> new RuntimeException("token校验失败")); log.info("正在获取例外人员工号 responseBody = " + responseBody); try { Optional dictResultOptional = Optional.of(responseBody) .map(t -> JSONUtil.toBean(t, DictResult.class)); if (!dictResultOptional.isPresent()) { throw new RuntimeException("解析DictResult失败:字符串格式不正确。"); } List 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 ListgetOffCourseId(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 ListgetTeacherInfo(List 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 ListgetTeacherInfoByCode(List 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 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 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(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 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 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 ListgetStudyCourseList(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 list = a.getResult().getList(); System.out.println(" list = " +list.size()); System.out.println(" list = " +list); } public HashMap getUserIdByWorkNum(String token, List workNumList) { // 将workNum列表转换为逗号分隔的字符串 String workNums = workNumList.stream() .collect(Collectors.joining(",")); // 构建请求URL String url = getUserIdByWorkNum+"?workNums=" + workNums; String respStr = Optional.ofNullable( HttpRequest.get(url) .header("token", token) .execute().body() ).orElseThrow(() -> new RuntimeException("Token校验失败")); // 解析JSON字符串为HashMap GetUserIdByWorkNumResult getUserIdByWorkNumResult = JSONUtil.parseObj(respStr).toBean(GetUserIdByWorkNumResult.class); HashMap result = getUserIdByWorkNumResult.getResult(); return result; } public String checkExamBanding(String testId, String token){ String url = checkBanding+"?testId=" + testId; String resp = Optional.ofNullable( HttpRequest.get(url).header("token", token).execute().body()).orElseThrow(() -> new RuntimeException("token校验失败")); Gson gson = new Gson(); CheckExamBandingBean checkExamBandingBean = gson.fromJson(resp, CheckExamBandingBean.class); return checkExamBandingBean.getData().toString(); } public void syncExamScoreToCourseSuite(AloneExam aloneExam,String token) { Gson gson = new GsonBuilder() .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()) .create(); String json = gson.toJson(aloneExam); String resp = HttpRequest.post(syncExamScoreToCourseSuite) .body(json) .header("token", token) .execute() .body(); if (StringUtils.isBlank(resp)){ return; } JSONUtil.toBean(resp, SyncExamScoreBean.class).success(); } }