Forráskód Böngészése

提交上传机考数据包代码

weiwenhai 8 éve
szülő
commit
4a28188fe8

+ 13 - 0
cqb-paper/src/main/java/com/qmth/cqb/paper/model/ExamFile.java

@@ -97,6 +97,19 @@ public class ExamFile implements Serializable{
     	this.examFileType = examFileType;
     }
     
+    public ExamFile(ExtractConfig extractConfig,String filePath,ExamFileType examFileType,String paperName,String fileSuffix){
+    	this.orgId = extractConfig.getOrgId();
+    	this.orgName = extractConfig.getOrgName();
+    	this.examId = extractConfig.getExamId()+"";
+    	this.examName = extractConfig.getExamName();
+    	this.examType = ExamType.strToEnum(extractConfig.getExamType());
+    	this.courseId = extractConfig.getCourseCode();
+    	this.courseName = extractConfig.getCourseName();
+    	this.fileName = extractConfig.getCourseCode()+"_"+paperName+fileSuffix;
+    	this.filePath = filePath;
+    	this.examFileType = examFileType;
+    }
+    
     public ExamFile(String examId,ExamType examType){
     	this.examId = examId;
     	this.examType = examType;

+ 10 - 0
cqb-paper/src/main/java/com/qmth/cqb/paper/model/computerTestModel/ComputerTestOption.java

@@ -8,6 +8,8 @@ package com.qmth.cqb.paper.model.computerTestModel;
 public class ComputerTestOption {
 	private Integer number;
 	
+	private Boolean correct;
+	
 	private Sections body;
 
 	public Integer getNumber() {
@@ -18,6 +20,14 @@ public class ComputerTestOption {
 		this.number = number;
 	}
 
+	public Boolean getCorrect() {
+		return correct;
+	}
+
+	public void setCorrect(Boolean correct) {
+		this.correct = correct;
+	}
+
 	public Sections getBody() {
 		return body;
 	}

+ 37 - 3
cqb-paper/src/main/java/com/qmth/cqb/paper/model/computerTestModel/ComputerTestQuestion.java

@@ -3,6 +3,11 @@ package com.qmth.cqb.paper.model.computerTestModel;
 import java.util.List;
 import java.util.Map;
 
+import cn.com.qmth.examcloud.common.dto.question.enums.QuesStructType;
+
+import com.qmth.cqb.paper.model.PaperDetailUnit;
+import com.qmth.cqb.question.model.Question;
+
 /**
  * @author  	chenken
  * @date    	2017年7月25日 上午9:51:37
@@ -21,7 +26,7 @@ public class ComputerTestQuestion {
 	/**
 	 * 1-单选,2-多选,3-判断,4-填空,5-问答,6-套题
 	 */
-	private Integer structType;
+	private Long structType;
 	/**
 	 * 是否客观题
 	 */
@@ -42,6 +47,35 @@ public class ComputerTestQuestion {
 	private List<ComputerTestOption> options;
 	
 	private Map<String,Object> param;
+	
+	public ComputerTestQuestion(){}
+	
+	public ComputerTestQuestion(PaperDetailUnit paperDetailUnit){
+		this.id = paperDetailUnit.getId();
+		this.number = paperDetailUnit.getNumber();
+		this.score = paperDetailUnit.getScore();
+		this.structType = paperDetailUnit.getQuestionType().getId();
+		if(paperDetailUnit.getQuestionType() == QuesStructType.SINGLE_ANSWER_QUESTION || 
+				paperDetailUnit.getQuestionType() == QuesStructType.MULTIPLE_ANSWER_QUESTION ||
+				paperDetailUnit.getQuestionType() == QuesStructType.BOOL_ANSWER_QUESTION){
+			this.objective = true;
+		}else{
+			this.objective = false;
+		}
+	}
+	
+	public ComputerTestQuestion(Question question){
+		this.id = question.getId();
+		this.score = question.getScore();
+		this.structType = question.getQuestionType().getId();
+		if(question.getQuestionType() == QuesStructType.SINGLE_ANSWER_QUESTION || 
+				question.getQuestionType() == QuesStructType.MULTIPLE_ANSWER_QUESTION || 
+				question.getQuestionType() == QuesStructType.BOOL_ANSWER_QUESTION){
+			this.objective = true;
+		}else {
+			this.objective = false;
+		}
+	}
 
 	public String getId() {
 		return id;
@@ -67,11 +101,11 @@ public class ComputerTestQuestion {
 		this.score = score;
 	}
 
-	public Integer getStructType() {
+	public Long getStructType() {
 		return structType;
 	}
 
-	public void setStructType(Integer structType) {
+	public void setStructType(Long structType) {
 		this.structType = structType;
 	}
 

+ 5 - 1
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/ExportPaperAbstractService.java

@@ -92,7 +92,11 @@ public abstract class ExportPaperAbstractService {
 	 * Excel文件后缀
 	 */
 	public static final String EXCEL_SUFFIX = ".xlsx";
-
+	/**
+	 * ZIP文件后缀
+	 */
+	public static final String ZIP_SUFFIX = ".zip";
+	
 	public static Configuration CONFIGURATION;
 
 	public static final String ENCODING = "UTF-8";

+ 209 - 5
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/SxsfExportPaperService.java

@@ -1,18 +1,30 @@
 package com.qmth.cqb.paper.service.export;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 import javax.servlet.http.HttpServletResponse;
 
 import main.java.com.UpYun;
 
+import org.apache.commons.io.FileUtils;
+import org.docx4j.docProps.core.dc.terms.DDC;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -20,6 +32,7 @@ import cn.com.qmth.examcloud.common.dto.question.enums.QuesStructType;
 import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
 import cn.com.qmth.examcloud.common.util.excel.ExcelWriter;
 
+import com.google.gson.Gson;
 import com.qmth.cqb.paper.dao.PaperRepo;
 import com.qmth.cqb.paper.dto.ObjectiveQuestionStructure;
 import com.qmth.cqb.paper.dto.PaperDetailExp;
@@ -32,10 +45,21 @@ import com.qmth.cqb.paper.model.ExportStructure;
 import com.qmth.cqb.paper.model.ExtractConfig;
 import com.qmth.cqb.paper.model.Paper;
 import com.qmth.cqb.paper.model.PaperDetail;
+import com.qmth.cqb.paper.model.PaperDetailUnit;
 import com.qmth.cqb.paper.model.QuestionTypeNum;
+import com.qmth.cqb.paper.model.computerTestModel.Block;
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestOption;
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaper;
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaperDetail;
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestQuestion;
+import com.qmth.cqb.paper.model.computerTestModel.Section;
+import com.qmth.cqb.paper.model.computerTestModel.Sections;
+import com.qmth.cqb.paper.service.PaperDetailService;
 import com.qmth.cqb.paper.service.PaperService;
+import com.qmth.cqb.question.model.QuesOption;
 import com.qmth.cqb.question.model.Question;
 import com.qmth.cqb.utils.CommonUtils;
+import com.qmth.cqb.utils.FileDisposeUtil;
 import com.qmth.cqb.utils.enums.ExamFileType;
 import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
@@ -49,9 +73,14 @@ import com.qmth.cqb.utils.word.DocxProcessUtil;
 @Service("sxsfExportPaperService")
 public class SxsfExportPaperService extends ExportPaperAbstractService {
 	
+	private static final Logger logger = LoggerFactory.getLogger(FileDisposeUtil.class);
+	
 	@Autowired
 	private PaperService paperService;
 	
+	@Autowired
+	private PaperDetailService paperDetailService;
+	
 	@Autowired
     PaperRepo paperRepo;
 	
@@ -95,7 +124,7 @@ public class SxsfExportPaperService extends ExportPaperAbstractService {
 	        	uploadAnswerWord(dataMap,extractConfig,paperId,accessUser,currNum);
         	}else if(exportStructure.getExportType()==ExportType.ONLINE){
         		//上传机考JSON文件
-        		uploadComputerTestFile(extractConfig,currNum);
+        		uploadComputerTestFile(extractConfig,accessUser);
         	}
         	//上传试卷结构
         	if(questionTypeNums.size()>0){
@@ -288,17 +317,192 @@ public class SxsfExportPaperService extends ExportPaperAbstractService {
      * 创建机考文件,并打包上传至又拍云
      * @param paperId
      * @param currNum
+     * ObjectId("591ead1290994d57cd453464")
+     * @throws IOException 
      */
-    private void uploadComputerTestFile(ExtractConfig extractConfig,String currNum){
+    public  void uploadComputerTestFile(ExtractConfig extractConfig,AccessUser accessUser) throws IOException{
+    	List<ComputerTestPaper>  computerTestPaperList = buildComputerTestPapers(extractConfig);
+    	for(ComputerTestPaper computerTestPaper:computerTestPaperList){
+    		String jsonDirectoryName = extractConfig.getCourseCode()+"_"+computerTestPaper.getName();
+    		String jsonDirectoryPath = TEMP_FILE_EXP + File.separator + jsonDirectoryName;
+    		//新建文件夹
+    		File dirFile = new File(jsonDirectoryPath);
+    		if(!dirFile.exists()){
+    			dirFile.mkdirs();
+    		}
+    		//创建新的JSON文件
+    		File file = new File(jsonDirectoryPath+File.separator+extractConfig.getCourseCode()+".json");
+    		//将对象转成 json对象
+    		Gson gson = new Gson();
+    		String strJSON = gson.toJson(computerTestPaper);
+    		//生成文件流写入JSON文件
+			FileOutputStream outputStream = new FileOutputStream(file);
+			byte b[] = strJSON.getBytes();
+			outputStream.write(b);
+			outputStream.close();
+			FileDisposeUtil.fileToZip(jsonDirectoryPath,"docxExport",jsonDirectoryName);
+			//上传到又拍云
+			File zipFile = new File(TEMP_FILE_EXP+jsonDirectoryName+".zip");
+			String zipUpyunFilePath = uploadUrl+jsonDirectoryName+".zip";
+			UpYun upyun = new UpYun(bucketName,userName,password);
+			upyun.writeFile(zipUpyunFilePath,zipFile,true);
+			//保存数据库记录
+			examFileService.saveExamFile(new ExamFile(extractConfig,zipUpyunFilePath,ExamFileType.COMPUTERTEST_PACKAGE,computerTestPaper.getName(),ZIP_SUFFIX),accessUser);
+			//删除本地生成的文件
+			zipFile.delete();
+			FileUtils.deleteQuietly(dirFile);
+    	}
+    }
+    
+    public List<ComputerTestPaper> buildComputerTestPapers(ExtractConfig extractConfig){
     	List<ExamPaper> examPapers = extractConfig.getExamPaperList();
+    	List<ComputerTestPaper> computerTestPapers = new ArrayList<ComputerTestPaper>();
+    	//循环得到试卷
     	for(ExamPaper examPaper:examPapers){
     		Paper paper = examPaper.getPaper();
+    		//得到所有旧对象的大题对象
     		List<PaperDetail> paperDetails = paperService.findPaperDetailsById(paper.getId());
-    		
-    		
-    		
+    		//通过 paper 对象 ,生成新的  ComputerTestPaper 对象
+    		ComputerTestPaper computerTestPaper = new ComputerTestPaper(paper);
+    		List<ComputerTestPaperDetail> details = new ArrayList<ComputerTestPaperDetail>();
+    		//遍历所有旧大题对象,得到小题对象的集合
+    		for(PaperDetail paperDetail:paperDetails){
+    			List<PaperDetailUnit> paperDetailUnits = paperDetailService.getUnitsByPaperDetailId(paperDetail.getId());
+    			ComputerTestPaperDetail computerTestPaperDetail = new ComputerTestPaperDetail(paperDetail);
+    			List<ComputerTestQuestion> questions = new ArrayList<ComputerTestQuestion>();
+    			//遍历所有的小题对象
+    			for(PaperDetailUnit paperDetailUnit:paperDetailUnits){
+    				//根据旧的小题对象,生成新的小题对象
+    				ComputerTestQuestion  computerTestQuestion = new ComputerTestQuestion(paperDetailUnit);
+    				//得到小题题干
+    				computerTestQuestion.setBody(getBody(paperDetailUnit.getQuestion()));
+    				//得到小题所有选项
+    				computerTestQuestion.setOptions(getOption(paperDetailUnit.getQuestion()));
+    				//查询小题中的 套题
+    				List<Question> subQuestionsList = paperDetailUnit.getQuestion().getSubQuestions();
+    				//判断这个小题中是否有套题
+    				if(subQuestionsList!=null&&subQuestionsList.size()>0){
+    					List<ComputerTestQuestion> subQuestions = new ArrayList<ComputerTestQuestion>();
+    					//遍历每个套题
+    					for(Question subQuestion:subQuestionsList){
+        					ComputerTestQuestion subcomputerTestQuestion = new ComputerTestQuestion(subQuestion);
+        					subcomputerTestQuestion.setBody(getBody(subQuestion));
+        					subcomputerTestQuestion.setOptions(getOption(subQuestion));
+        					subQuestions.add(subcomputerTestQuestion);
+    					}
+        				computerTestQuestion.setSubQuestions(subQuestions);
+    				}
+    				questions.add(computerTestQuestion);
+    			}
+    			computerTestPaperDetail.setQuestions(questions);
+    			details.add(computerTestPaperDetail);
+    		}
+    		computerTestPaper.setDetails(details);
+    		computerTestPapers.add(computerTestPaper);
     	}
+    	return computerTestPapers;
     }
     
+    private Sections getBody(Question question){
+    	Sections body = new Sections();
+		List<Section> sections = new ArrayList<Section>(); 
+		Section section = new Section();
+		//得到小题题干
+		String questionBodyString = question.getQuesBody();
+		//得到小题题干行数
+		String[] questionRowStrings = questionBodyString.split("</p>");
+		//将小题题干拆分为Block集合
+		section.setBlocks(disposeQuestionBodyOrOption(questionRowStrings));
+		sections.add(section);
+		body.setSections(sections);
+		return body;
+    }
+    
+    private List<ComputerTestOption> getOption(Question question){
+    	//得到小题选项
+		List<QuesOption> quesOptions = question.getQuesOptions();
+		List<ComputerTestOption> options = new ArrayList<ComputerTestOption>();
+		//遍历小题选项
+		if(quesOptions!=null&&quesOptions.size()>0){
+			for(QuesOption quesOption: quesOptions){
+				ComputerTestOption option = new ComputerTestOption();
+				option.setNumber(new Integer(quesOption.getNumber()));
+				option.setCorrect(quesOption.getIsCorrect()==1?true:false);
+				Sections body2 = new Sections();
+				Section section2 = new Section();
+				List<Section> sections2 = new ArrayList<Section>();
+				//得到小题选项
+				String optionString = quesOption.getOptionBody();
+				String[] optionStrings = optionString.split("</p>");
+				section2.setBlocks(disposeQuestionBodyOrOption(optionStrings));
+				sections2.add(section2);
+				body2.setSections(sections2);
+				option.setBody(body2);
+				options.add(option);
+			}
+		}
+		return options;
+    }
+      
+    private List<Block> disposeQuestionBodyOrOption(String[] questionRowStrings) {
+    	List<Block> blocks = new ArrayList<Block>();
+    	for(int i=0;i<questionRowStrings.length;i++){
+			Block block = new Block();
+			//去掉每行里面的<p>,<span>,</span>标签
+			String questionRowString = questionRowStrings[i].replaceAll("<p>", "").replaceAll("<span>", "").replaceAll("</span>", "");
+			//判断是否有图片
+			if(questionRowString.contains("<img")){
+				Map<String, Object> param  = new HashMap<String, Object>();
+				//需要继续做截取,取到Parma
+				block.setType("image");
+				//获取图片的路径
+				List<String> strSrcList = getImg(questionRowString, "src");
+				if(strSrcList.size()>0){
+					String strSrc = strSrcList.get(0).replaceAll("src=\"", "").replaceAll("\"", "");
+					block.setValue(strSrc);
+				}
+				//获取图片的高度
+				List<String> strHeightList = getImg(questionRowString, "height");
+				if(strHeightList.size()>0){
+					String strHeight = strHeightList.get(0).replaceAll("height=\"", "").replaceAll("\"", "");
+					param.put("height", strHeight);
+				}
+				//获取图片的宽度
+				List<String> strWidthList = getImg(questionRowString, "width");
+				if(strHeightList.size()>0){
+					String strWidth = strWidthList.get(0).replaceAll("width=\"", "").replaceAll("\"", "");
+					param.put("width", strWidth);
+				}
+				block.setParam(param);
+				blocks.add(block);
+				continue;
+			}
+			block.setType("text");
+			block.setValue(questionRowString);
+			blocks.add(block);
+		}
+		return blocks;
+	}
+
+	/**
+     * 获取图片里面的路径,长度,宽度
+     * @param s
+     * @param str
+     * @return
+     */
+    public static List<String> getImg(String s,String str)
+    {
+        String regex;
+        List<String> list = new ArrayList<String>();
+        regex = str + "=\"(.*?)\"";
+        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);
+        Matcher ma = pa.matcher(s);
+        while (ma.find())
+        {
+            list.add(ma.group());
+        }
+        return list;
+    }
+
 }
 

+ 1 - 0
cqb-paper/src/main/java/com/qmth/cqb/paper/service/impl/ExamFileServiceImpl.java

@@ -66,6 +66,7 @@ public class ExamFileServiceImpl implements ExamFileService{
 		oldExamFile.setExamType(examFile.getExamType());
 		oldExamFile.setCourseId(examFile.getCourseId());
 		oldExamFile.setExamFileType(examFile.getExamFileType());
+		oldExamFile.setFileName(examFile.getFileName());
 		oldExamFile = examFileRepo.findOne(Example.of(oldExamFile));
 		if(oldExamFile!=null){
 			//删除又拍云上的文件

+ 7 - 0
cqb-paper/src/main/java/com/qmth/cqb/paper/web/ExtractConfigController.java

@@ -1,6 +1,7 @@
 package com.qmth.cqb.paper.web;
 
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -31,7 +32,9 @@ import cn.com.qmth.examcloud.common.util.ErrorMsg;
 import com.qmth.cqb.paper.dto.ExportPaperInfoModel;
 import com.qmth.cqb.paper.model.ExamFile;
 import com.qmth.cqb.paper.model.ExtractConfig;
+import com.qmth.cqb.paper.model.computerTestModel.ComputerTestPaper;
 import com.qmth.cqb.paper.service.ExtractConfigService;
+import com.qmth.cqb.paper.service.export.SxsfExportPaperService;
 import com.qmth.cqb.utils.enums.ExportWay;
 
 
@@ -50,6 +53,9 @@ public class ExtractConfigController {
 	@Autowired
 	private ExtractConfigService extractConfigService;
 	
+	@Autowired
+	private SxsfExportPaperService sxsfExportPaperService;
+	
 	@ApiOperation(value = "根据考试ID和课程ID获取调卷规则", notes = "根据考试ID和课程ID获取调卷规则")
     @GetMapping(value = "/extractConfig/{examId}/{courseCode}")
 	public ResponseEntity<ExtractConfig> findExtractConfig(@PathVariable Long examId,@PathVariable String courseCode){
@@ -213,4 +219,5 @@ public class ExtractConfigController {
 		List<ExamFile> examFiles = extractConfigService.findPaperStructure(examId,courseId);
 		return new ResponseEntity(examFiles,HttpStatus.OK);
 	}
+
 }