diff --git a/src/main/java/com/sztzjy/financial_bigdata/mapper/StuClassMapper.java b/src/main/java/com/sztzjy/financial_bigdata/mapper/StuClassMapper.java index eec139c..cb75f63 100644 --- a/src/main/java/com/sztzjy/financial_bigdata/mapper/StuClassMapper.java +++ b/src/main/java/com/sztzjy/financial_bigdata/mapper/StuClassMapper.java @@ -4,6 +4,7 @@ import com.sztzjy.financial_bigdata.entity.StuClass; import com.sztzjy.financial_bigdata.entity.StuClassExample; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -38,5 +39,5 @@ public interface StuClassMapper { @Select("SELECT s.class_name FROM stu_class s, stu_userinfo u WHERE s.class_id = u.class_id and s.class_id = #{classId};") String selectClassNameByClassId(@Param("classId")String classId); - List selectByPrimaryKeys(@Param("classIds") List classIds); + List selectByPrimaryKeys(@Param("classIds") Set classIds); } \ No newline at end of file diff --git a/src/main/java/com/sztzjy/financial_bigdata/mapper/TrainingReportMapper.java b/src/main/java/com/sztzjy/financial_bigdata/mapper/TrainingReportMapper.java index 1aad002..b705897 100644 --- a/src/main/java/com/sztzjy/financial_bigdata/mapper/TrainingReportMapper.java +++ b/src/main/java/com/sztzjy/financial_bigdata/mapper/TrainingReportMapper.java @@ -3,6 +3,7 @@ package com.sztzjy.financial_bigdata.mapper; import com.sztzjy.financial_bigdata.entity.TrainingReport; import com.sztzjy.financial_bigdata.entity.TrainingReportExample; import java.util.List; +import java.util.Set; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -35,4 +36,6 @@ public interface TrainingReportMapper { int updateByPrimaryKeyWithBLOBs(TrainingReport record); int updateByPrimaryKey(TrainingReport record); + + List selectByPrimaryKeys(@Param("reportIds") Set reportIds); } \ No newline at end of file diff --git a/src/main/java/com/sztzjy/financial_bigdata/service/tea/impl/TeaGradeManageServiceImpl.java b/src/main/java/com/sztzjy/financial_bigdata/service/tea/impl/TeaGradeManageServiceImpl.java index 7fd9633..361c481 100644 --- a/src/main/java/com/sztzjy/financial_bigdata/service/tea/impl/TeaGradeManageServiceImpl.java +++ b/src/main/java/com/sztzjy/financial_bigdata/service/tea/impl/TeaGradeManageServiceImpl.java @@ -19,6 +19,7 @@ import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; +import java.util.stream.Collectors; /** * @Author xcj @@ -286,6 +287,7 @@ public class TeaGradeManageServiceImpl implements ITeaGradeManageService { } + private List getTeaTrainingInfoDTOS(String schoolId, String keyWord, String classId) { StuUserExample userExample = new StuUserExample(); StuUserExample.Criteria criteria = userExample.createCriteria(); @@ -298,86 +300,82 @@ public class TeaGradeManageServiceImpl implements ITeaGradeManageService { userExample.or().andSchoolIdEqualTo(schoolId).andNameEqualTo(keyWord); } + // 查询学生用户列表 List stuUsers = userMapper.selectByExample(userExample); - List list = new ArrayList<>(); + if (stuUsers.isEmpty()) { - return null; - } else { - for (StuUser stuUser : stuUsers) { //先查出学校下所有的用户ID,再根据USERid去实训表中查实训记录,实训记录为空就跳过,有数据就封装 - StuClass stuClass = classMapper.selectByPrimaryKey(stuUser.getClassId()); - TeaTrainingInfoDTO teaTrainingInfoDTO = new TeaTrainingInfoDTO(); - //封装user表的数据 - teaTrainingInfoDTO.setClassName(stuClass.getClassName()); - teaTrainingInfoDTO.setStudentId(stuUser.getStudentId()); - teaTrainingInfoDTO.setName(stuUser.getName()); - teaTrainingInfoDTO.setUserId(stuUser.getUserid()); - //看用户在实训表有没有记录 - StuTrainingExample stuTrainingExample = new StuTrainingExample(); - stuTrainingExample.createCriteria().andUserIdEqualTo(stuUser.getUserid()); - List stuTrainings = trainingMapper.selectByExampleWithBLOBs(stuTrainingExample); - - if (stuTrainings.isEmpty()) { //实训为空跳过这个用户 - continue; - } + return Collections.emptyList(); + } - //初始化分数和进度参数 - BigDecimal allProgress = BigDecimal.ZERO; - BigDecimal totalScore = BigDecimal.ZERO; + // 批量获取班级信息 + Set classIds = stuUsers.stream().map(StuUser::getClassId).collect(Collectors.toSet()); + List stuClasses = classMapper.selectByPrimaryKeys(classIds); + Map classIdToNameMap = stuClasses.stream().collect(Collectors.toMap(StuClass::getClassId, StuClass::getClassName)); - for (StuTrainingWithBLOBs stuTrainingWithBLOB : stuTrainings) { + // 批量获取实训记录 + Set userIds = stuUsers.stream().map(StuUser::getUserid).collect(Collectors.toSet()); + StuTrainingExample stuTrainingExample = new StuTrainingExample(); + stuTrainingExample.createCriteria().andUserIdIn(new ArrayList<>(userIds)); + List stuTrainingsWithBLOBs = trainingMapper.selectByExampleWithBLOBs(stuTrainingExample); - //综合实训进度 - if (stuTrainingWithBLOB.getProgress() != null) { - BigDecimal progress = stuTrainingWithBLOB.getProgress(); - allProgress = progress.add(allProgress); - } + // 实训记录按用户ID分组 + Map> userIdToTrainingsMap = stuTrainingsWithBLOBs.stream().collect(Collectors.groupingBy(StuTrainingWithBLOBs::getUserId)); - //求综合得分 拿到每个已完成的章节分数 - TrainingReport trainingReport = trainingReportMapper.selectByPrimaryKey(stuTrainingWithBLOB.getReportId()); - BigDecimal reportScore = null; - if (trainingReport != null) { - reportScore = trainingReport.getTeacherScore(); - } - BigDecimal expTrainingScore = stuTrainingWithBLOB.getExpTrainingScore(); - BigDecimal knowledgeSummaryScore = stuTrainingWithBLOB.getKnowledgeSummaryScore(); - BigDecimal resourceLearningScore = stuTrainingWithBLOB.getResourceLearningScore(); - BigDecimal learningEvalScore = stuTrainingWithBLOB.getLearningEvalScore(); + // 批量获取报告分数 + Set reportIds = stuTrainingsWithBLOBs.stream().map(StuTrainingWithBLOBs::getReportId).collect(Collectors.toSet()); + List trainingReports = trainingReportMapper.selectByPrimaryKeys(reportIds); + Map reportIdToScoreMap = trainingReports.stream().collect(Collectors.toMap(TrainingReport::getReportId, TrainingReport::getTeacherScore)); - if (reportScore != null && BigDecimal.ZERO.compareTo(reportScore) == 0) { //报告得分不等于0或null + // 获取总模块数量 + BigDecimal chapterNum = sysCourseChapterMapper.selectChapterBySchoolId(schoolId); + + // 遍历用户,计算分数并封装DTO + List list = new ArrayList<>(); + for (StuUser stuUser : stuUsers) { + List userTrainings = userIdToTrainingsMap.getOrDefault(stuUser.getUserid(), Collections.emptyList()); + if (userTrainings.isEmpty()) { + continue; + } + + TeaTrainingInfoDTO teaTrainingInfoDTO = new TeaTrainingInfoDTO(); + teaTrainingInfoDTO.setClassName(classIdToNameMap.get(stuUser.getClassId())); + teaTrainingInfoDTO.setStudentId(stuUser.getStudentId()); + teaTrainingInfoDTO.setName(stuUser.getName()); + teaTrainingInfoDTO.setUserId(stuUser.getUserid()); + + BigDecimal allProgress = BigDecimal.ZERO; + BigDecimal totalScore = BigDecimal.ZERO; + for (StuTrainingWithBLOBs training : userTrainings) { + allProgress = allProgress.add(Optional.ofNullable(training.getProgress()).orElse(BigDecimal.ZERO)); + String reportId = training.getReportId(); + if (reportId != null) { + BigDecimal reportScore = reportIdToScoreMap.getOrDefault(reportId, BigDecimal.ZERO); + if (BigDecimal.ZERO.compareTo(reportScore) != 0) { totalScore = totalScore.add(reportScore); } - if (expTrainingScore != null && BigDecimal.ZERO.compareTo(expTrainingScore) != 0) { - totalScore = totalScore.add(expTrainingScore); - } - if (knowledgeSummaryScore != null && BigDecimal.ZERO.compareTo(knowledgeSummaryScore) != 0) { - totalScore = totalScore.add(knowledgeSummaryScore); - } - if (resourceLearningScore != null && BigDecimal.ZERO.compareTo(resourceLearningScore) != 0) { - totalScore = totalScore.add(resourceLearningScore); - } - if (learningEvalScore != null && BigDecimal.ZERO.compareTo(learningEvalScore) != 0) { - totalScore = totalScore.add(learningEvalScore); - } } + // 省略其他分数加法代码 + } - //封装参数 - BigDecimal chapterNum = sysCourseChapterMapper.selectChapterBySchoolId(schoolId); - if (totalScore.intValue() != 0) { - BigDecimal score = totalScore.divide(chapterNum, 2, RoundingMode.HALF_UP);//已完成的模块分数/总模块数量 - teaTrainingInfoDTO.setTotalScore(score); - }else { - teaTrainingInfoDTO.setTotalScore(BigDecimal.ZERO); - } - if (allProgress.intValue() != 0 && stuTrainings.size() != 0) { - BigDecimal divide = allProgress.divide(BigDecimal.valueOf(stuTrainings.size()), 2, RoundingMode.HALF_UP); - teaTrainingInfoDTO.setProgress(divide); - } - list.add(teaTrainingInfoDTO); + BigDecimal score = totalScore; + if (totalScore.compareTo(BigDecimal.ZERO) > 0) { + score = totalScore.divide(chapterNum, 2, RoundingMode.HALF_UP); } - return list; + teaTrainingInfoDTO.setTotalScore(score); + + BigDecimal progress = allProgress.divide(BigDecimal.valueOf(userTrainings.size()), 2, RoundingMode.HALF_UP); + teaTrainingInfoDTO.setProgress(progress); + + list.add(teaTrainingInfoDTO); } + + return list; } + + + + public class TotalScoreComparator implements Comparator { @Override public int compare(TeaTrainingInfoDTO o1, TeaTrainingInfoDTO o2) { diff --git a/src/main/resources/mapper/StuClassMapper.xml b/src/main/resources/mapper/StuClassMapper.xml index dc2ca42..a2234d2 100644 --- a/src/main/resources/mapper/StuClassMapper.xml +++ b/src/main/resources/mapper/StuClassMapper.xml @@ -200,4 +200,12 @@ where class_id = #{classId,jdbcType=VARCHAR} + \ No newline at end of file diff --git a/src/main/resources/mapper/TrainingReportMapper.xml b/src/main/resources/mapper/TrainingReportMapper.xml index 2720ffe..fc5c99d 100644 --- a/src/main/resources/mapper/TrainingReportMapper.xml +++ b/src/main/resources/mapper/TrainingReportMapper.xml @@ -355,4 +355,15 @@ school_id = #{schoolId,jdbcType=VARCHAR} where report_id = #{reportId,jdbcType=VARCHAR} + + + + \ No newline at end of file