优化练习模式页面接口查询速度

newBigdata
xiaoCJ 10 months ago
parent 66648a406e
commit 3abd85cbb1

@ -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<StuClass> selectByPrimaryKeys(@Param("classIds") List<String> classIds);
List<StuClass> selectByPrimaryKeys(@Param("classIds") Set<String> classIds);
}

@ -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<TrainingReport> selectByPrimaryKeys(@Param("reportIds") Set<String> reportIds);
}

@ -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<TeaTrainingInfoDTO> 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<StuUser> stuUsers = userMapper.selectByExample(userExample);
List<TeaTrainingInfoDTO> 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<StuTrainingWithBLOBs> stuTrainings = trainingMapper.selectByExampleWithBLOBs(stuTrainingExample);
if (stuTrainings.isEmpty()) { //实训为空跳过这个用户
continue;
}
return Collections.emptyList();
}
//初始化分数和进度参数
BigDecimal allProgress = BigDecimal.ZERO;
BigDecimal totalScore = BigDecimal.ZERO;
// 批量获取班级信息
Set<String> classIds = stuUsers.stream().map(StuUser::getClassId).collect(Collectors.toSet());
List<StuClass> stuClasses = classMapper.selectByPrimaryKeys(classIds);
Map<String, String> classIdToNameMap = stuClasses.stream().collect(Collectors.toMap(StuClass::getClassId, StuClass::getClassName));
for (StuTrainingWithBLOBs stuTrainingWithBLOB : stuTrainings) {
// 批量获取实训记录
Set<String> userIds = stuUsers.stream().map(StuUser::getUserid).collect(Collectors.toSet());
StuTrainingExample stuTrainingExample = new StuTrainingExample();
stuTrainingExample.createCriteria().andUserIdIn(new ArrayList<>(userIds));
List<StuTrainingWithBLOBs> stuTrainingsWithBLOBs = trainingMapper.selectByExampleWithBLOBs(stuTrainingExample);
//综合实训进度
if (stuTrainingWithBLOB.getProgress() != null) {
BigDecimal progress = stuTrainingWithBLOB.getProgress();
allProgress = progress.add(allProgress);
}
// 实训记录按用户ID分组
Map<String, List<StuTrainingWithBLOBs>> 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<String> reportIds = stuTrainingsWithBLOBs.stream().map(StuTrainingWithBLOBs::getReportId).collect(Collectors.toSet());
List<TrainingReport> trainingReports = trainingReportMapper.selectByPrimaryKeys(reportIds);
Map<String, BigDecimal> 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<TeaTrainingInfoDTO> list = new ArrayList<>();
for (StuUser stuUser : stuUsers) {
List<StuTrainingWithBLOBs> 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<TeaTrainingInfoDTO> {
@Override
public int compare(TeaTrainingInfoDTO o1, TeaTrainingInfoDTO o2) {

@ -200,4 +200,12 @@
where class_id = #{classId,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKeys" parameterType="java.util.Set" resultMap="BaseResultMap">
SELECT *
FROM stu_class
WHERE class_id IN
<foreach collection="classIds" item="classId" open="(" separator="," close=")">
#{classId}
</foreach>
</select>
</mapper>

@ -355,4 +355,15 @@
school_id = #{schoolId,jdbcType=VARCHAR}
where report_id = #{reportId,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKeys" parameterType="java.util.Set" resultMap="BaseResultMap">
SELECT *
FROM training_report
WHERE report_id IN
<foreach collection="reportIds" item="reportId" open="(" separator="," close=")">
#{reportId}
</foreach>
</select>
</mapper>
Loading…
Cancel
Save