diff --git a/src/main/java/com/lyt/bean/PaperQuestionStuuser.java b/src/main/java/com/lyt/bean/PaperQuestionStuuser.java new file mode 100644 index 0000000000000000000000000000000000000000..fe0edb32b27dfea94c338a439aa279b8148cc918 --- /dev/null +++ b/src/main/java/com/lyt/bean/PaperQuestionStuuser.java @@ -0,0 +1,21 @@ +package com.lyt.bean; + +import lombok.*; +/** + * @author OuyangHao + * @date 5/6/2023 + * @version 1.0 + * @info 批卷表 + */ + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class PaperQuestionStuuser { + private Integer id; + private Integer testId; + private Integer userId; + private Integer questionId; + private String answer; + private Integer score; +} diff --git a/src/main/java/com/lyt/controller/GradeController.java b/src/main/java/com/lyt/controller/GradeController.java new file mode 100644 index 0000000000000000000000000000000000000000..829d0de647a582af7ecb5abdd8fbc645c518a837 --- /dev/null +++ b/src/main/java/com/lyt/controller/GradeController.java @@ -0,0 +1,45 @@ +package com.lyt.controller; + +import com.lyt.bean.PaperQuestionStuuser; +import com.lyt.service.GradeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +/** + * @author OuyangHao + * @date 5/6/2023 + * @version 1.0 + * @info 批卷接口 + */ + +@RestController +@RequestMapping("/grade") +public class GradeController { + @Autowired + private GradeService gradeService; + + + @PostMapping("/autoGrade") + public String autoGrade() { + try { + // 调用批卷service中的方法,进行批卷并打印日志 + gradeService.insertStudentAnswerAndAutoScore(); + return "批卷成功!"; + } catch (Exception e) { + e.printStackTrace(); + return "批卷失败!"; + } + } + + /** + * 查询所有试卷题目-学生-答案关联表记录接口 + */ + @GetMapping("/paperQuestionStuuser") + public List getAllPaperQuestionStuuser() { + return gradeService.getAllPaperQuestionStuuser(); + } +} diff --git a/src/main/java/com/lyt/dao/GradeDao.java b/src/main/java/com/lyt/dao/GradeDao.java new file mode 100644 index 0000000000000000000000000000000000000000..fff4de9ac9e130d51b24a06f6faf7b3df1239922 --- /dev/null +++ b/src/main/java/com/lyt/dao/GradeDao.java @@ -0,0 +1,49 @@ +package com.lyt.dao; +import com.lyt.bean.PaperQuestionStuuser; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Update; +import org.springframework.stereotype.Repository; + +import java.util.List; + +/** + * @author OuyangHao + * @date 30/5/2023 + * @version 1.0 + * @info 批卷功能实现 + */ + +@Repository +public interface GradeDao { + +//原理解析: +//alter table paper_question_stuuser auto_increment=0; + // 如果以上sql无效,是因为您不能将计数器重置为小于或等于已使用的值。 + //对于MyISAM,如果该值小于或等于AUTO_INCREMENT列中当前的最大值,则该值将重置为当前的最大值加1。 + //对于InnoDB,如果该值小于该列中的当前最大值,则不会发生错误,并且当前序列值不会更改。 + // + //也许这就是问题的原因:您试图将auto_increment计数器恢复为0,但它已经高于该值-并且由于无法将其重置为小于已使用的任何值的值,因此不会工作。 + + + //插入学生测试答案到paper_question_stuuser表中 + @Insert("INSERT IGNORE INTO paper_question_stuuser (test_id,user_id,question_id,answer) " + + "SELECT ta.test_id,ta.stu_test_id,la.question_id,la.answer " + + "FROM link_answer la " + + "JOIN test_answer ta ON FIND_IN_SET(la.id, REPLACE(REPLACE(ta.linkAnswers,'[',''),']','')) > 0 " + + "JOIN question ON question.question_id = la.question_id") + public void insertStudentAnswerToPaper(); + + //在第一个方法运行后,进行自动批卷。 + @Update("UPDATE paper_question_stuuser p " + + "JOIN question q ON p.question_id = q.question_id " + + "SET p.score = IF(p.answer COLLATE utf8mb4_general_ci = q.answer COLLATE utf8mb4_general_ci, q.score, 0)") + public void updatePaperQuestionStuuserScore(); + + // 查询 paper_question_stuuser 表中的所有数据 + @Select("SELECT * FROM paper_question_stuuser") + List selectAllPaperQuestionStuuser(); + + + +} diff --git a/src/main/java/com/lyt/service/GradeService.java b/src/main/java/com/lyt/service/GradeService.java new file mode 100644 index 0000000000000000000000000000000000000000..4e37875c3ba193141802c54846a1fdd37f0bdaf1 --- /dev/null +++ b/src/main/java/com/lyt/service/GradeService.java @@ -0,0 +1,37 @@ +package com.lyt.service; + +import com.lyt.bean.PaperQuestionStuuser; +import com.lyt.dao.GradeDao; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * @author OuyangHao + * @date 1/6/2023 + * @version 1.0 + * @info 批卷service + */ +@Service +public class GradeService { + @Autowired + GradeDao gradeDao; + + @Transactional + public void insertStudentAnswerAndAutoScore() { + // 插入学生答案到试卷题目-学生-答案关联表 + gradeDao.insertStudentAnswerToPaper(); + System.out.println("插入学生答案成功!"); + + // 自动批卷并更新成绩 + gradeDao.updatePaperQuestionStuuserScore(); + System.out.println("自动批卷成功!"); + } + + public List getAllPaperQuestionStuuser() { + // 查询 paper_question_stuuser 表中的所有数据 + return gradeDao.selectAllPaperQuestionStuuser(); + } +} diff --git a/src/paper_question_stuuser.sql b/src/paper_question_stuuser.sql new file mode 100644 index 0000000000000000000000000000000000000000..650bb68d3ea5b6d4b563f353f6adb426097a3f47 --- /dev/null +++ b/src/paper_question_stuuser.sql @@ -0,0 +1,42 @@ +/* + Navicat Premium Data Transfer + + Source Server : db_test + Source Server Type : MySQL + Source Server Version : 80030 + Source Host : localhost:3306 + Source Schema : test + + Target Server Type : MySQL + Target Server Version : 80030 + File Encoding : 65001 + + Date: 06/06/2023 10:24:50 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for paper_question_stuuser +-- ---------------------------- +DROP TABLE IF EXISTS `paper_question_stuuser`; +CREATE TABLE `paper_question_stuuser` ( + `id` int(0) NOT NULL AUTO_INCREMENT, + `test_id` int(0) NULL DEFAULT NULL, + `user_id` int(0) NULL DEFAULT NULL, + `question_id` int(0) NULL DEFAULT NULL, + `answer` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, + `score` int(0) NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `idx_test_question`(`test_id`, `question_id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of paper_question_stuuser +-- ---------------------------- +INSERT INTO `paper_question_stuuser` VALUES (1, 7, 20203778, 4140, '[\"B\"]', 0); +INSERT INTO `paper_question_stuuser` VALUES (2, 7, 20203778, 4141, '[\"B\",\"C\",\"A\",\"D\"]', 0); +INSERT INTO `paper_question_stuuser` VALUES (3, 7, 20203778, 4143, '[\"A\"]', 3); + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/src/test/java/com/lyt/GradeIntegrationTest.java b/src/test/java/com/lyt/GradeIntegrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a944555f4d28958261bd604d3847a1ddb1720bb1 --- /dev/null +++ b/src/test/java/com/lyt/GradeIntegrationTest.java @@ -0,0 +1,43 @@ +package com.lyt; + +import com.lyt.bean.PaperQuestionStuuser; +import com.lyt.service.GradeService; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +/** + * @author OuyangHao + * @date 5/6/2023 + * @version 1.0 + * @info 测试批卷数据库的集成测试 + */ + +@RunWith(SpringRunner.class) +@SpringBootTest +class GradeIntegrationTest { + + @Autowired + private GradeService gradeService; + + /** + * 测试批卷 + */ + @Test + void testAutoGrade() { + // 调用需要测试的服务方法 + gradeService.insertStudentAnswerAndAutoScore(); + + // 验证数据库操作是否正确 + List list = gradeService.getAllPaperQuestionStuuser(); + for (PaperQuestionStuuser item : list) { + System.out.println(item); // 打印所有记录 + // TODO: 进行数据库操作验证 + } +} + +} diff --git a/src/test/java/com/lyt/GradeTest.java b/src/test/java/com/lyt/GradeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9e57556846042858da8e47d65168a8d4088594a7 --- /dev/null +++ b/src/test/java/com/lyt/GradeTest.java @@ -0,0 +1,49 @@ +package com.lyt; + +import com.lyt.service.GradeService; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.mockito.Mockito.*; +/** + * @author OuyangHao + * @date 5/6/2023 + * @version 1.0 + * @info 测试批卷请求的单元测试 + */ + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +class GradeTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private GradeService gradeService; + + /** + * 测试批卷 + */ + @Test + void testAutoGrade() throws Exception { + // 模拟接口请求 + this.mockMvc.perform(post("/grade/autoGrade")) + // 验证请求是否成功 + .andExpect(status().isOk()) + // 验证返回结果是否正确 + .andExpect(content().string("批卷成功!")); + + // 验证是否调用了批卷service中的 insertStudentAnswerAndAutoScore() 方法 + verify(gradeService, times(1)).insertStudentAnswerAndAutoScore(); + } +}