Browse Source

拆解ExamStudentPaper模型中的结构json解析方法;
API层增加上传考生个人试卷结构接口

luoshi 7 years ago
parent
commit
2f888d6e3f

+ 19 - 15
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/exam/model/ExamStudentPaper.java

@@ -71,22 +71,8 @@ public class ExamStudentPaper implements Serializable {
         this.struct = struct;
     }
 
-    @SuppressWarnings({ "unchecked", "rawtypes" })
     public List<QuestionDetail> getDetailList() {
-        List<QuestionDetail> list = null;
-        try {
-            JsonConfig config = new JsonConfig();
-            Map classMap = new HashMap();
-            classMap.put("questions", QuestionUnit.class);
-            config.setClassMap(classMap);
-            list = JSONArray.toList(JSONArray.fromObject(StringUtils.trimToEmpty(struct)), new QuestionDetail(),
-                    config);
-            if (list != null) {
-                Collections.sort(list);
-            }
-        } catch (Exception e) {
-        }
-        return list;
+        return parseFromJson(struct);
     }
 
     public List<ExamQuestion> getQuestionList() {
@@ -137,6 +123,24 @@ public class ExamStudentPaper implements Serializable {
         return list;
     }
 
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    public static List<QuestionDetail> parseFromJson(String struct) {
+        List<QuestionDetail> list = null;
+        try {
+            JsonConfig config = new JsonConfig();
+            Map classMap = new HashMap();
+            classMap.put("questions", QuestionUnit.class);
+            config.setClassMap(classMap);
+            list = JSONArray.toList(JSONArray.fromObject(StringUtils.trimToEmpty(struct)), new QuestionDetail(),
+                    config);
+            if (list != null) {
+                Collections.sort(list);
+            }
+        } catch (Exception e) {
+        }
+        return list;
+    }
+
     public static void main(String[] args) {
         ExamStudentPaper paper = new ExamStudentPaper();
         paper.struct = "[{\"title\":\"多选题\",\"number\":2,\"index\":1,\"questions\":[{\"number\":1,\"answer\":\"ACD\",\"score\":2.0},{\"number\":2,\"answer\":\"ABD\",\"score\":2.0},{\"number\":3,\"answer\":\"ABD\",\"score\":2.0},{\"number\":4,\"answer\":\"ABC\",\"score\":2.0},{\"number\":5,\"answer\":\"ABD\",\"score\":2.0}]},{\"title\":\"单选题\",\"number\":1,\"index\":21,\"questions\":[{\"number\":1,\"answer\":\"A\",\"score\":1.0},{\"number\":2,\"answer\":\"A\",\"score\":1.0},{\"number\":3,\"answer\":\"B\",\"score\":1.0},{\"number\":4,\"answer\":\"C\",\"score\":1.0},{\"number\":5,\"answer\":\"A\",\"score\":1.0},{\"number\":6,\"answer\":\"D\",\"score\":1.0},{\"number\":7,\"answer\":\"B\",\"score\":1.0},{\"number\":8,\"answer\":\"D\",\"score\":1.0},{\"number\":9,\"answer\":\"D\",\"score\":1.0},{\"number\":10,\"answer\":\"A\",\"score\":1.0},{\"number\":11,\"answer\":\"C\",\"score\":2.0},{\"number\":12,\"answer\":\"C\",\"score\":2.0},{\"number\":13,\"answer\":\"C\",\"score\":2.0},{\"number\":14,\"answer\":\"C\",\"score\":2.0},{\"number\":15,\"answer\":\"C\",\"score\":2.0}]}]";

+ 82 - 0
stmms-web/src/main/java/cn/com/qmth/stmms/api/controller/ExamStudentController.java

@@ -25,15 +25,21 @@ import cn.com.qmth.stmms.biz.campus.service.CampusService;
 import cn.com.qmth.stmms.biz.exam.model.Exam;
 import cn.com.qmth.stmms.biz.exam.model.ExamPackage;
 import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
+import cn.com.qmth.stmms.biz.exam.model.ExamStudentPaper;
 import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
+import cn.com.qmth.stmms.biz.exam.model.QuestionDetail;
+import cn.com.qmth.stmms.biz.exam.model.QuestionUnit;
 import cn.com.qmth.stmms.biz.exam.query.ExamStudentSearchQuery;
 import cn.com.qmth.stmms.biz.exam.service.ExamPackageService;
 import cn.com.qmth.stmms.biz.exam.service.ExamService;
+import cn.com.qmth.stmms.biz.exam.service.ExamStudentPaperService;
 import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
 import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
 import cn.com.qmth.stmms.biz.mark.model.MarkLibrary;
 import cn.com.qmth.stmms.biz.mark.service.MarkLibraryService;
+import cn.com.qmth.stmms.biz.user.model.User;
 import cn.com.qmth.stmms.common.enums.LibraryStatus;
+import cn.com.qmth.stmms.common.utils.RequestUtils;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 
@@ -61,6 +67,9 @@ public class ExamStudentController {
     @Autowired
     private MarkLibraryService markLibraryService;
 
+    @Autowired
+    private ExamStudentPaperService studentPaperService;
+
     @AuthValidate("adminUser")
     @RequestMapping(value = "/package/{examId}", method = RequestMethod.GET)
     @ResponseBody
@@ -504,4 +513,77 @@ public class ExamStudentController {
             }
         }
     }
+
+    /**
+     * 上传考生个人试卷结构接口
+     * 
+     * @param request
+     * @param examId
+     * @param examNumber
+     * @param objective
+     * @param struct
+     * @return
+     */
+    @AuthValidate("adminUser")
+    @RequestMapping(value = "/student/struct", method = RequestMethod.POST)
+    @ResponseBody
+    private boolean uploadStruct(HttpServletRequest request, @RequestParam Integer examId,
+            @RequestParam String examNumber, @RequestParam Boolean objective, @RequestParam String struct) {
+        // 验证考试与管理员账号
+        User user = RequestUtils.getApiUser(request);
+        Exam exam = examService.findById(examId);
+        if (exam == null || !exam.getCreatorId().equals(user.getId())) {
+            return false;
+        }
+        // 验证考生是否存在
+        ExamStudent student = examStudentService.findByExamIdAndExamNumber(examId, examNumber);
+        if (student == null) {
+            return false;
+        }
+        // 尝试解析试卷结构
+        struct = StringUtils.trimToEmpty(struct);
+        List<QuestionDetail> list = ExamStudentPaper.parseFromJson(struct);
+        if (list == null || list.isEmpty()) {
+            return false;
+        }
+        // 保存个人试卷结构
+        ExamStudentPaper paper = studentPaperService.find(examId, examNumber, objective);
+        if (paper == null) {
+            paper = new ExamStudentPaper();
+            paper.setExamId(examId);
+            paper.setExamNumber(examNumber);
+            paper.setObjective(objective);
+        }
+        paper.setStruct(struct);
+        studentPaperService.save(paper);
+        // 同步科目总分信息
+        ExamSubject subject = examSubjectService.find(examId, student.getSubjectCode());
+        if (objective.booleanValue() && subject.getObjectiveScore() == null) {
+            subject.setObjectiveScore(sumTotalScore(list));
+            examSubjectService.save(subject);
+        } else if (!objective.booleanValue() && subject.getSubjectiveScore() == null) {
+            subject.setSubjectiveScore(sumTotalScore(list));
+            examSubjectService.save(subject);
+        }
+        return true;
+    }
+
+    /**
+     * 计算个人试卷结构中的总分
+     * 
+     * @param list
+     * @return
+     */
+    private double sumTotalScore(List<QuestionDetail> list) {
+        double score = 0d;
+        for (QuestionDetail detail : list) {
+            if (detail.getQuestions() == null) {
+                continue;
+            }
+            for (QuestionUnit unit : detail.getQuestions()) {
+                score += unit.getScore();
+            }
+        }
+        return score;
+    }
 }