Compare commits

...

2 Commits
master ... pro

Author SHA1 Message Date
whkj_liliangliang 2d0e21146d add 2 months ago
whkj_liliangliang 9d64836de1 add 2 months ago
  1. 7
      pro-admin/pom.xml
  2. 127
      pro-admin/src/main/java/com/ruoyi/web/controller/system/ScoreController.java
  3. 2
      pro-system/src/main/java/com/ruoyi/system/domain/DTO/exScore.java

7
pro-admin/pom.xml

@ -90,6 +90,13 @@
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.38</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

127
pro-admin/src/main/java/com/ruoyi/web/controller/system/ScoreController.java

@ -1,6 +1,8 @@
package com.ruoyi.web.controller.system; package com.ruoyi.web.controller.system;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.JudgeBody; import com.ruoyi.common.core.domain.model.JudgeBody;
@ -14,11 +16,13 @@ import com.ruoyi.system.domain.Projectinfo.ScoreDto;
import com.ruoyi.system.mapper.*; import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISrootService; import com.ruoyi.system.service.ISrootService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@ -155,66 +159,95 @@ public class ScoreController {
} }
@ApiOperation("导出评分表") @ApiOperation("导出评分表")
@PostMapping("/export") @PostMapping("/export")
public void export(HttpServletResponse response, Session session ) public void export(HttpServletResponse response, Session session) {
{ // 获取当前会话的评委列表
// 创建查询结果存储列表 List<Judge> judges = judgeMapper.selectList(
List<exProject> listp = new ArrayList<>(); new QueryWrapper<Judge>().eq("srole", session.getId())
List<exScore> listS = new ArrayList<>(); );
// 查询 Judge 数据,按 session ID 过滤 if (judges.isEmpty()) {
List<Judge> jlist = judgeMapper.selectList(new QueryWrapper<Judge>().eq("srole", session.getId()));
if (jlist.isEmpty()) {
// 如果 jlist 为空,直接返回空结果
return; return;
} }
// 查询 Sroot 数据
// 获取关联的Sroot信息
Sroot sroot = iSrootService.selectSrootById((long) session.getRoot()); Sroot sroot = iSrootService.selectSrootById((long) session.getRoot());
// 查询 Project 数据,按 sessionid 过滤并按 score 降序排列
List<Project> plist = projectMapper.selectList( // 获取当前会话的项目列表并按分数降序排列
new QueryWrapper<Project>().eq("sessionid", session.getId()).orderByDesc("score") List<Project> projects = projectMapper.selectList(
new QueryWrapper<Project>()
.eq("sessionid", session.getId())
.orderByDesc("score")
); );
// 获取所有评分数据并按项目 ID 和用户 ID 分组 if (projects.isEmpty()) {
Map<Long, Map<Long, Score>> scoreMap = scoreMapper.selectList(new QueryWrapper<>()) return;
.stream() }
// 收集项目ID以优化Score查询
List<Integer> projectIds = projects.stream()
.map(Project::getId)
.collect(Collectors.toList());
// 仅查询相关项目的评分记录(关键修复1:添加类型转换)
List<Score> scores = scoreMapper.selectList(
new QueryWrapper<Score>().in("pid", projectIds)
);
// 构建评分映射: pid -> (uid -> Score)(关键修复2:保持Integer类型)
Map<Integer, Map<Integer, Score>> scoreMap = scores.stream()
.collect(Collectors.groupingBy( .collect(Collectors.groupingBy(
score -> (long) score.getPid(), // 将 Integer 转换为 Long Score::getPid,
Collectors.toMap( Collectors.toMap(Score::getUid, Function.identity())
score -> (long) score.getUid(), // 将 Integer 转换为 Long
score -> score
)
)); ));
// 遍历项目列表并构建 exScore 和 exProject 列表
for (Project project : plist) {
exProject exProject = new exProject(project, session.getSession(), sroot.getTask());
listp.add(exProject);
// 遍历 Judge 列表并构建 exScore 对象 // 预计算每个项目的评分统计(关键修复3:统一使用Integer类型)
for (Judge judge : jlist) { Map<Integer, Integer> scoreCounts = new HashMap<>();
Score score = scoreMap.getOrDefault(project.getId(), Collections.emptyMap()).get(judge.getUserid()); Map<Integer, Double> averageScores = new HashMap<>();
scoreMap.forEach((pid, uidMap) -> {
List<Score> projectScores = new ArrayList<>(uidMap.values());
int count = projectScores.size();
double average = projectScores.stream()
.mapToInt(Score::getScore)
.average()
.orElse(0.0);
System.out.println("评分人数:---------pid-------"+pid+"-----count----"+count);
scoreCounts.put(pid, count);
averageScores.put(pid, average);
});
// 构建导出数据列表
List<exScore> exportData = new ArrayList<>();
for (Project project : projects) {
exProject exProj = new exProject(project, session.getSession(), sroot.getTask());
int projectId = project.getId();
System.out.println("分数信息:---------pid-------"+projectId+"-----count----");
// 获取当前项目的统计信息(关键修复4:直接使用Integer类型)
int count = ObjUtil.isNotNull(scoreCounts.get(projectId))?scoreCounts.get(projectId):0;
double average = ObjUtil.isNotNull(averageScores.get(projectId))?averageScores.get(projectId):0;
for (Judge judge : judges) {
exScore exSc = new exScore(exProj);
exSc.setPname(judge.getName());
// 获取对应评分(空值安全处理)
Score score = Optional.ofNullable(scoreMap.get(projectId))
.map(uidMap -> uidMap.get(judge.getUserid()))
.orElse(null);
// 构建 exScore 对象并设置评分信息
exScore exScore = new exScore(exProject);
if (score != null) { if (score != null) {
exScore.setPscore(score.getScore()); exSc.setPscore(score.getScore());
exScore.setMes(score.getMes()); exSc.setMes(score.getMes());
} }
exScore.setPname(judge.getName());
listS.add(exScore); // 设置统计信息(添加四舍五入处理)
exSc.setSconum(count);
exSc.setScore((int) Math.round(average));
exportData.add(exSc);
} }
} }
// 批量计算评分总数和平均值
Map<Long, List<Score>> scoresByPid = scoreMap.values().stream()
.flatMap(map -> map.values().stream())
.collect(Collectors.groupingBy(score -> (long) score.getPid())); // 将 Integer 转为 Long
for (exScore exScore : listS) { // 导出Excel
List<Score> slist = scoresByPid.getOrDefault(exScore.getId(), Collections.emptyList()); new ExcelUtil<exScore>(exScore.class)
int count = slist.size(); .exportExcel(response, exportData, "分数汇总");
exScore.setSconum(count);
exScore.setScore(count > 0 ? (int) slist.stream().mapToInt(Score::getScore).average().orElse(0) : 0);
}
ExcelUtil<exScore> util = new ExcelUtil<>(exScore.class);
util.exportExcel(response, listS, "分数汇总");
} }
public void init(){ public void init(){
List<Session> lists=sessionMapper.selectList(null); List<Session> lists=sessionMapper.selectList(null);

2
pro-system/src/main/java/com/ruoyi/system/domain/DTO/exScore.java

@ -21,7 +21,7 @@ public class exScore {
@Excel(name = "学校",lineMerge = true) @Excel(name = "学校",lineMerge = true)
String school; String school;
@Excel(name = "分属小类别",lineMerge = true) @Excel(name = "具体分类类别",lineMerge = true)
String session; String session;
@Excel(name="分属活动",lineMerge = true) @Excel(name="分属活动",lineMerge = true)

Loading…
Cancel
Save