DataUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.xjrsoft.module.xycxedu.util;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.google.gson.JsonArray;
  5. import com.google.gson.JsonElement;
  6. import com.google.gson.JsonObject;
  7. import com.xjrsoft.common.enums.DeleteMark;
  8. import com.xjrsoft.module.base.entity.BaseCourseSubject;
  9. import com.xjrsoft.module.base.service.IBaseCourseSubjectService;
  10. import com.xjrsoft.module.xycxedu.entity.ExamSubjectScore;
  11. import com.xjrsoft.module.xycxedu.entity.XycxeduExamList;
  12. import com.xjrsoft.module.xycxedu.service.IExamSubjectScoreService;
  13. import com.xjrsoft.module.xycxedu.service.IXycxeduExamListService;
  14. import com.yomahub.liteflow.util.JsonUtil;
  15. import java.time.LocalDate;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.stream.Collectors;
  23. /**
  24. * @author dzx
  25. * @date 2024/7/17
  26. */
  27. public class DataUtil {
  28. /**
  29. * 同步考试计划信息
  30. * @param examListService
  31. */
  32. public void examListData(IXycxeduExamListService examListService){
  33. JsonArray examList = ApiUtil.getExamList();
  34. List<XycxeduExamList> updateList = new ArrayList<>();
  35. List<XycxeduExamList> insertList = new ArrayList<>();
  36. Date date = new Date();
  37. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  38. for (JsonElement jsonElement : examList) {
  39. XycxeduExamList parseObject = new XycxeduExamList();
  40. JsonObject object = jsonElement.getAsJsonObject();
  41. parseObject.setMilexamname(object.get("milexamname").getAsString());
  42. parseObject.setMilexamid(object.get("milexamid").getAsLong());
  43. parseObject.setRegyear(object.get("regyear").getAsInt());
  44. parseObject.setGrade(object.get("grade").getAsString());
  45. parseObject.setSdate(LocalDate.parse(object.get("sdate").getAsString(), formatter));
  46. XycxeduExamList oldData = examListService.getOne(
  47. new QueryWrapper<XycxeduExamList>().lambda()
  48. .eq(XycxeduExamList::getMilexamid, parseObject.getMilexamid())
  49. );
  50. if(oldData != null){
  51. BeanUtil.copyProperties(parseObject, oldData);
  52. oldData.setModifyDate(date);
  53. updateList.add(oldData);
  54. continue;
  55. }
  56. parseObject.setCreateDate(date);
  57. insertList.add(parseObject);
  58. }
  59. if(!updateList.isEmpty()){
  60. examListService.updateBatchById(updateList);
  61. }
  62. if(!insertList.isEmpty()){
  63. examListService.saveBatch(insertList);
  64. }
  65. }
  66. public void studentScoreData(IXycxeduExamListService examListService, IExamSubjectScoreService scoreService, IBaseCourseSubjectService courseService){
  67. // 查询出所有的考试计划
  68. List<XycxeduExamList> list = examListService.list(
  69. new QueryWrapper<XycxeduExamList>().lambda()
  70. );
  71. Map<Integer, Integer> examidXMilexamidMap = new HashMap<>();//存考试下的科目id和考试id关系
  72. Map<Integer, String> examidXCoursenameMap = new HashMap<>();
  73. for (XycxeduExamList el : list) {
  74. // 根据考试计划,查询出所有科目信息
  75. JsonArray courseByExam = ApiUtil.getCourseByExam(el.getMilexamid());
  76. for (JsonElement jsonElement : courseByExam) {
  77. JsonObject courseObj = jsonElement.getAsJsonObject();
  78. examidXMilexamidMap.put(courseObj.get("examid").getAsInt(), courseObj.get("milexamid").getAsInt());
  79. examidXCoursenameMap.put(courseObj.get("examid").getAsInt(), courseObj.get("coursename").getAsString());
  80. }
  81. }
  82. Map<String, Long> courseMap = courseService.list(
  83. new QueryWrapper<BaseCourseSubject>().lambda().eq(BaseCourseSubject::getDeleteMark, DeleteMark.NODELETE.getCode())
  84. ).stream().collect(
  85. Collectors.toMap(BaseCourseSubject::getName, BaseCourseSubject::getId)
  86. );
  87. Date date = new Date();
  88. // 查询每次考试每个科目的学生的成绩
  89. for (Integer examid : examidXMilexamidMap.keySet()) {
  90. List<ExamSubjectScore> insertList = new ArrayList<>();
  91. JsonArray jsonArray = ApiUtil.getStudentScoreByCourse(examid);
  92. for (JsonElement jsonElement : jsonArray) {
  93. JsonObject scoreObj = jsonElement.getAsJsonObject();
  94. Long userId = scoreService.getUserIdByIdNumber(scoreObj.get("stunum").getAsString());
  95. long count = scoreService.count(
  96. new QueryWrapper<ExamSubjectScore>().lambda()
  97. .eq(ExamSubjectScore::getUserId, userId)
  98. .eq(ExamSubjectScore::getCourseSubjectId, courseMap.get(examidXCoursenameMap.get(examid)))
  99. .eq(ExamSubjectScore::getMilexamid, examidXMilexamidMap.get(examid).longValue())
  100. );
  101. if(count > 0){
  102. continue;
  103. }
  104. ExamSubjectScore score = new ExamSubjectScore() {{
  105. setUserId(userId);
  106. setScore(scoreObj.get("score").getAsInt());
  107. setMilexamid(examidXMilexamidMap.get(examid).longValue());
  108. setGradeRanking(scoreObj.get("gradeorder").getAsInt());
  109. setCoursename(examidXCoursenameMap.get(examid));
  110. setCourseSubjectId(courseMap.get(examidXCoursenameMap.get(examid)));
  111. setCreateDate(date);
  112. }};
  113. insertList.add(score);
  114. }
  115. if(!insertList.isEmpty()){
  116. scoreService.saveBatch(insertList);
  117. }
  118. }
  119. }
  120. }