Browse Source

添加导出主观题json文件相关功能

lideyin 5 years ago
parent
commit
30ea199045

+ 186 - 0
src/main/java/cn/com/qmth/examcloud/bridge/modules/cloudmarking/controller/CloudMarkingController.java

@@ -0,0 +1,186 @@
+package cn.com.qmth.examcloud.bridge.modules.cloudmarking.controller;
+
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.commons.util.IOUtil;
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
+import cn.com.qmth.examcloud.commons.util.ZipUtil;
+import cn.com.qmth.examcloud.core.oe.admin.api.ExamRecordQuestionsCloudService;
+import cn.com.qmth.examcloud.core.oe.admin.api.bean.SubjectiveAnswerBean;
+import cn.com.qmth.examcloud.core.oe.admin.api.request.GetSubjectiveAnswerReq;
+import cn.com.qmth.examcloud.core.oe.admin.api.response.GetSubjectiveAnswerResp;
+import com.google.common.collect.Maps;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @Description 云阅卷接口
+ * 错误码范围(101001 至 101999)
+ * @Author lideyin
+ * @Date 2019/11/20 14:49
+ * @Version 1.0
+ */
+@RestController
+@RequestMapping("cmc")
+public class CloudMarkingController {
+    @Autowired
+    ExamRecordQuestionsCloudService examRecordQuestionsCloudService;
+
+    /**
+     * 生成主观题答案
+     */
+    @GetMapping(value = "gsAnswer/{examId}")
+    public ResponseEntity<?> generateSubjectiveAnswer(@PathVariable Long examId) {
+        if (null == examId) {
+            throw new StatusException("101001", "考试批次id不允许为空");
+        }
+
+        //答案临时存放目录
+        final String tempAnswerDir = "tempAnswer";
+        //答案临时压缩目录
+        final String tempAnswerZipDir = "tempAnswerZip";
+
+        /*//清空临时目录中的文件
+        File t1 = new File(tempAnswerDir);
+        for (File f : t1.listFiles()) {
+            FileUtils.deleteQuietly(f);
+        }
+        File t2 = new File(tempAnswerZipDir);
+        for (File f : t2.listFiles()) {
+            FileUtils.deleteQuietly(f);
+        }*/
+
+        Long startId = 0L;
+
+        /*String json1 = "{\n" +
+                "    \"cost\": 6317,\n" +
+                "    \"nextExamRecordDataId\": 80703,\n" +
+                "    \"subjectiveAnswerList\": [{\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 5,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834d8\",\n" +
+                "        \"order\": 19,\n" +
+                "        \"studentAnswer\": \"https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433562519.jpg|https://ecs-test-static.qmth.com.cn/oe-answer-file/1837550/80507/19/1837550_80507_19_1558433889599.png\",\n" +
+                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
+                "        \"realAnswerType\": \"audio\"\n" +
+                "    }, {\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 5,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834d9\",\n" +
+                "        \"order\": 20,\n" +
+                "        \"studentAnswer\": null,\n" +
+                "        \"answerType\": \"SINGLE_AUDIO\",\n" +
+                "        \"realAnswerType\": \"audio\"\n" +
+                "    }, {\n" +
+                "        \"examId\": 198,\n" +
+                "        \"courseCode\": \"6000459\",\n" +
+                "        \"examStudentId\": 1837550,\n" +
+                "        \"examRecordDataId\": 80507,\n" +
+                "        \"mainNumber\": 6,\n" +
+                "        \"questionId\": \"5c45384def8fce45257834da\",\n" +
+                "        \"order\": 23,\n" +
+                "        \"studentAnswer\": null,\n" +
+                "        \"answerType\": null,\n" +
+                "        \"realAnswerType\": \"text\"\n" +
+                "    }]\n" +
+                "}";*/
+
+        while (true) {
+            GetSubjectiveAnswerReq req = new GetSubjectiveAnswerReq();
+            req.setExamId(examId);
+            req.setStartExamRecordId(startId);
+
+
+            GetSubjectiveAnswerResp resp = examRecordQuestionsCloudService.getSubjectiveAnswerList(req);
+
+            /*GetSubjectiveAnswerResp resp = JsonUtil.fromJson(json1, GetSubjectiveAnswerResp.class);*/
+            if (resp.getNextExamRecordDataId().equals(startId)) {
+                break;
+            }
+
+
+            List<SubjectiveAnswerBean> subjectiveAnswerList = resp.getSubjectiveAnswerList();
+
+            List<Long> examStudentIdList = subjectiveAnswerList.stream().
+                    map(p -> p.getExamStudentId()).
+                    distinct().collect(Collectors.toList());
+
+            String courseCode = subjectiveAnswerList.get(0).getCourseCode();
+
+            for (int i = 0; i < examStudentIdList.size(); i++) {
+                Long examStudentId = examStudentIdList.get(i);
+
+                List<Map<String, Object>> answerMapList = new ArrayList<>();
+
+                //当前学生的作答记录
+                List<SubjectiveAnswerBean> curStuAnswerList = subjectiveAnswerList.
+                        stream().
+                        filter(p -> p.getExamStudentId().equals(examStudentId)).
+                        collect(Collectors.toList());
+
+                for (SubjectiveAnswerBean sab : curStuAnswerList) {
+                    Map<String, Object> answerMap = Maps.newHashMap();
+                    answerMap.put("mainNumber", sab.getMainNumber());
+                    answerMap.put("subNumber", sab.getOrder());
+
+                    Map<String, Object> bodyMap = Maps.newHashMap();
+
+                    List<Map<String, Object>> sectionMapList = new ArrayList<>();
+                    Map<String, Object> sectionMap = Maps.newHashMap();
+                    List<Map<String, Object>> blockMapList = new ArrayList<>();
+
+                    Map<String, Object> blockMap = Maps.newHashMap();
+                    blockMap.put("type", sab.getRealAnswerType());
+                    blockMap.put("value", sab.getStudentAnswer());
+                    blockMapList.add(blockMap);
+
+                    sectionMap.put("blocks", blockMapList);
+                    sectionMapList.add(sectionMap);
+
+                    bodyMap.put("sections", sectionMapList);
+                    answerMap.put("body", bodyMap);
+
+                    answerMapList.add(answerMap);
+
+                }
+
+                //将考生结果保存至临时文件
+                String json = JsonUtil.toJson(answerMapList);
+                String filePath = tempAnswerDir + "/" + examId + "-" + courseCode + "-" + examStudentId + ".json";
+                IOUtil.toFile(json.getBytes(Charset.forName("UTF-8")), filePath);
+            }
+
+
+            startId = resp.getNextExamRecordDataId();
+        }
+        String zipPath = tempAnswerZipDir + "/" + examId + "-" + System.currentTimeMillis() + ".zip";
+        File zipFile = new File(zipPath);
+        ZipUtil.zip(new File(tempAnswerDir), zipFile);
+
+        byte[] bytes = IOUtil.toByteArray(zipFile);
+
+        String downLoadFileName = "exam-" + examId + ".zip";
+        ResponseEntity<byte[]> responseEntity = ResponseEntity.ok().
+                header("Content-Disposition", "attachment; filename=" + downLoadFileName).
+                contentType(MediaType.APPLICATION_OCTET_STREAM).body(bytes);
+
+        return responseEntity;
+    }
+}