From 61b3c169a9a126e6f2ffbebe4bf896898a11f2b9 Mon Sep 17 00:00:00 2001 From: chenyuan Date: Fri, 9 Oct 2026 17:06:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=80=83=E8=AF=95=E5=88=86=E6=9E=90-?= =?UTF-8?q?=E6=9C=80=E9=9A=BE=E9=A2=98=E7=9B=AESQL=E5=9C=A8=E4=B8=8D?= =?UTF-8?q?=E9=80=89=E7=8F=AD=E7=BA=A7=E6=97=B6=E7=BC=BA=E5=B0=91=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC=E5=AF=BC=E8=87=B4=E8=AF=AD=E6=B3=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=EF=BC=8C=E7=82=B9=E5=88=86=E6=9E=90=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../linkCommerce/service/ExamService.java | 20 +++++++++++-------- .../linkCommerce/service/ExamServiceTest.java | 9 +++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/sztzjy/linkCommerce/service/ExamService.java b/src/main/java/com/sztzjy/linkCommerce/service/ExamService.java index e5398d1..78c3518 100644 --- a/src/main/java/com/sztzjy/linkCommerce/service/ExamService.java +++ b/src/main/java/com/sztzjy/linkCommerce/service/ExamService.java @@ -433,14 +433,7 @@ public class ExamService { bucket("良好", scores, total.multiply(new BigDecimal("0.80")), total.multiply(new BigDecimal("0.90"))), bucket("及格", scores, pass, total.multiply(new BigDecimal("0.80"))), bucket("不及格", scores, BigDecimal.ZERO, pass)); - String hardestSql = "select p.question_id id,p.stem,p.question_score, count(distinct t.id) student_count, " + - "sum(case when coalesce(a.awarded_score,0) < p.question_score then 1 else 0 end) wrong_count, " + - "coalesce(sum(coalesce(a.awarded_score,0)),0) earned_score " + - "from exam_assessment_question p join exam_attempt t on t.exam_id=p.exam_id and t.attempt_status='SUBMITTED' " + - "left join exam_attempt_answer a on a.attempt_id=t.id and a.question_id=p.question_id " + - "where p.exam_id=?" + (StringUtils.isBlank(classId) ? "" : " and t.teaching_class_id=? ") + - "group by p.question_id,p.stem,p.question_score " + - "order by (case when count(distinct t.id)=0 then 100 else coalesce(sum(coalesce(a.awarded_score,0)),0) * 100.0 / (count(distinct t.id) * p.question_score) end) asc, wrong_count desc limit 3"; + String hardestSql = analysisHardestSql(StringUtils.isNotBlank(classId)); List> hardest = jdbc.query(hardestSql, (rs, n) -> { int students = rs.getInt("student_count"); @@ -754,6 +747,17 @@ public class ExamService { if (!expected.equals(actual)) throw new IllegalArgumentException(message); } + static String analysisHardestSql(boolean withClassFilter) { + return "select p.question_id id,p.stem,p.question_score, count(distinct t.id) student_count, " + + "sum(case when coalesce(a.awarded_score,0) < p.question_score then 1 else 0 end) wrong_count, " + + "coalesce(sum(coalesce(a.awarded_score,0)),0) earned_score " + + "from exam_assessment_question p join exam_attempt t on t.exam_id=p.exam_id and t.attempt_status='SUBMITTED' " + + "left join exam_attempt_answer a on a.attempt_id=t.id and a.question_id=p.question_id " + + "where p.exam_id=? " + (withClassFilter ? "and t.teaching_class_id=? " : "") + + "group by p.question_id,p.stem,p.question_score " + + "order by (case when count(distinct t.id)=0 then 100 else coalesce(sum(coalesce(a.awarded_score,0)),0) * 100.0 / (count(distinct t.id) * p.question_score) end) asc, wrong_count desc limit 3"; + } + static void validateDelete(String status, Object exportedAt) { if ("DRAFT".equals(status)) return; requireState(status, "ENDED", "只有未发布或已结束的考试可以删除"); diff --git a/src/test/java/com/sztzjy/linkCommerce/service/ExamServiceTest.java b/src/test/java/com/sztzjy/linkCommerce/service/ExamServiceTest.java index c929c07..6a78825 100644 --- a/src/test/java/com/sztzjy/linkCommerce/service/ExamServiceTest.java +++ b/src/test/java/com/sztzjy/linkCommerce/service/ExamServiceTest.java @@ -58,6 +58,15 @@ class ExamServiceTest { } } + @Test + void analysisHardestSqlKeepsParameterAndGroupBySeparated() { + for (String sql : Arrays.asList(ExamService.analysisHardestSql(false), ExamService.analysisHardestSql(true))) { + assertFalse(sql.contains("?group")); + assertTrue(sql.contains("? group by p.question_id")); + } + assertTrue(ExamService.analysisHardestSql(true).contains("? and t.teaching_class_id=? group by")); + } + @Test void studentAnswerFilesStillRejectExcel() { IllegalArgumentException error = assertThrows(IllegalArgumentException.class,