Browse Source

为阅卷处理试卷及答案的媒体文件为Url

xiatian 4 years ago
parent
commit
607861de39

+ 12 - 0
themis-business/src/main/java/com/qmth/themis/business/cache/bean/ExamPaperCacheBean.java

@@ -33,6 +33,10 @@ public class ExamPaperCacheBean implements Serializable {
 
 	// 题干路径
 	private String paperPath;
+	
+	// 阅卷用试卷json路径
+	private String paperViewPath;
+	
 	// 结构文件路径
 	private String structPath;
 	// 标答路径
@@ -190,6 +194,14 @@ public class ExamPaperCacheBean implements Serializable {
 		this.audioPlayCount = audioPlayCount;
 	}
 
+	public String getPaperViewPath() {
+		return paperViewPath;
+	}
+
+	public void setPaperViewPath(String paperViewPath) {
+		this.paperViewPath = paperViewPath;
+	}
+
 	
 
 }

+ 14 - 0
themis-business/src/main/java/com/qmth/themis/business/entity/TEExamPaper.java

@@ -29,6 +29,10 @@ public class TEExamPaper extends BaseEntity {
     @ApiModelProperty(value = "题干路径")
     @TableField(value = "paper_path")
     private String paperPath;
+    
+    @ApiModelProperty(value = "阅卷用试卷json路径")
+    @TableField(value = "paper_view_path")
+    private String paperViewPath;
 
     @ApiModelProperty(value = "解密密钥")
     @TableField(value = "decrypt_secret")
@@ -198,4 +202,14 @@ public class TEExamPaper extends BaseEntity {
     public void setAudioPlayCount(Integer audioPlayCount) {
         this.audioPlayCount = audioPlayCount;
     }
+
+	public String getPaperViewPath() {
+		return paperViewPath;
+	}
+
+	public void setPaperViewPath(String paperViewPath) {
+		this.paperViewPath = paperViewPath;
+	}
+    
+    
 }

+ 1 - 0
themis-business/src/main/java/com/qmth/themis/business/service/impl/TEExamPaperServiceImpl.java

@@ -117,6 +117,7 @@ public class TEExamPaperServiceImpl extends ServiceImpl<TEExamPaperMapper, TEExa
         ret.setHasAudio(ep.getHasAudio());
         ret.setWeight(ep.getWeight());
         ret.setAudioPlayCount(ep.getAudioPlayCount());
+        ret.setPaperViewPath(ep.getPaperViewPath());
         return ret;
     }
     /**

+ 191 - 5
themis-business/src/main/java/com/qmth/themis/business/templete/impl/TaskExamPaperImportTemplete.java

@@ -16,6 +16,7 @@ import java.util.UUID;
 
 import javax.annotation.Resource;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -49,6 +50,7 @@ import com.qmth.themis.common.util.Result;
 import com.qmth.themis.common.util.ResultUtil;
 
 import cn.hutool.core.date.DateUtil;
+import sun.misc.BASE64Decoder;
 
 /**
  * @Description: 试卷导入
@@ -244,15 +246,109 @@ public class TaskExamPaperImportTemplete implements TaskImportTemplete {
 		if (processPaper) {
 			disposePaper(rootDir, paper, paperFile, attachmentDir);
 			disposeStruct(rootDir, paper, paperFile);
+			disposeViewPaper(rootDir, paper, paperFile, attachmentDir);
 		}
 		if (processAnswer) {
-			disposeAnswer(rootDir, paper, answerFile);
+			File structFile;
+			if(processPaper) {
+				structFile=paperFile;
+			}else {
+				String structPath = paper.getStructPath();
+				SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
+				String url = OssUtil.getUrlForPrivateBucket(systemConfig.getOssEnv(3), structPath);
+				structFile = new File(rootDir + uuid() + ".json");
+				FileUtil.saveUrlAsFile(url, structFile);
+			}
+			disposeAnswer(rootDir, paper, answerFile,structFile,attachmentDir);
 		}
 		Integer audioPlayCount = (Integer) map.get("audioPlayCount");
 		paper.setAudioPlayCount(audioPlayCount);
 		teExamPaperService.saveOrUpdate(paper);
 	}
 
+	private void disposeViewPaper(String rootDir, TEExamPaper paper, File paperFile, File attachmentDir) {
+		File urlPaperFile = new File(rootDir + uuid() + ".json");
+		JSONObject structJson = JSONObject.parseObject(FileUtil.readFileContent(paperFile));
+		JSONArray structdetails = structJson.getJSONArray("details");
+		if(structdetails==null||structdetails.size()==0) {
+			return;
+		}
+		for (int i = 0; i < structdetails.size(); i++) {
+			JSONArray structdetailquestions = structdetails.getJSONObject(i).getJSONArray("questions");
+			if(structdetailquestions!=null&&structdetailquestions.size()>=0) {
+				for (int j = 0; j < structdetailquestions.size(); j++) {
+					JSONObject structquestion = structdetailquestions.getJSONObject(j);
+					JSONObject body = structquestion.getJSONObject("body");
+					disposeQuestionBodyUrl(rootDir, body, paper.getId(), attachmentDir);
+					if (structquestion.getInteger("structType").intValue() == 6) {
+						JSONArray structsubQuestions = structquestion.getJSONArray("subQuestions");
+						for (int k = 0; k < structsubQuestions.size(); k++) {
+							JSONObject structsubquestion = structsubQuestions.getJSONObject(k);
+								JSONArray options = structsubquestion.getJSONArray("options");
+								disposeQuestionOptionsUrl(rootDir, options, paper.getId(), attachmentDir);
+						}
+					}
+				}
+			}
+		}
+		FileUtil.saveAsFile(urlPaperFile.getAbsolutePath(), structJson.toJSONString());
+		String filePath = sdf.format(new Date()) + "/" + uuid() + ".json";
+		paper.setPaperViewPath(filePath);
+		SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
+		OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, urlPaperFile);
+	}
+	
+	private void disposeQuestionBodyUrl(String rootDir,JSONObject body,Long paperId,File attachmentDir) {
+		if(body==null) {
+			return;
+		}
+		JSONArray sections = body.getJSONArray("sections");
+		if(sections==null||sections.size()==0) {
+			return;
+		}
+		for(int j=0;j<sections.size();j++) {
+			JSONObject block = sections.getJSONObject(j);
+			if(block!=null) {
+				JSONArray blocks = block.getJSONArray("blocks");
+				if(blocks!=null&&blocks.size()>0) {
+					for(int m=0;m<blocks.size();m++) {
+						JSONObject blockInfo = blocks.getJSONObject(m);
+						uploadImageAndAudio(rootDir,blockInfo, paperId, attachmentDir);
+					}
+				}
+			}
+		}
+	}
+	
+	private void disposeQuestionOptionsUrl(String rootDir,JSONArray options,Long paperId,File attachmentDir) {
+		if(options==null||options.size()==0) {
+			return;
+		}
+		for(int i=0;i<options.size();i++) {
+			JSONObject option = options.getJSONObject(i);
+			if(option!=null) {
+				JSONObject body = option.getJSONObject("body");
+				if(body!=null) {
+					JSONArray sections = body.getJSONArray("sections");
+					if(sections!=null&&sections.size()>0) {
+						for(int j=0;j<sections.size();j++) {
+							JSONObject block = sections.getJSONObject(j);
+							if(block!=null) {
+								JSONArray blocks = block.getJSONArray("blocks");
+								if(blocks!=null&&blocks.size()>0) {
+									for(int m=0;m<blocks.size();m++) {
+										JSONObject blockInfo = blocks.getJSONObject(m);
+										uploadImageAndAudio(rootDir,blockInfo, paperId, attachmentDir);
+									}
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+	
 	private void disposePaper(String rootDir, TEExamPaper paper, File paperFile, File attachmentDir) {
 		if (paperFile == null) {
 			return;
@@ -303,18 +399,108 @@ public class TaskExamPaperImportTemplete implements TaskImportTemplete {
 		OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, file);
 	}
 
-	private void disposeAnswer(String rootDir, TEExamPaper paper, File answerFile) {
+	private void disposeAnswer(String rootDir, TEExamPaper paper, File answerFile,File structFile,File attachmentDir) {
 		if (answerFile == null) {
 			return;
 		}
-//        File encryptFile = new File(rootDir + uuid() + ".json");
-//        FileUtil.encryptFile(answerFile, encryptFile, paper.getDecryptSecret(), paper.getDecryptVector());
+		File urlAnswerFile = new File(rootDir + uuid() + ".json");
+		JSONObject answerJson = JSONObject.parseObject(FileUtil.readFileContent(answerFile));
+		JSONArray answerdetails = answerJson.getJSONArray("details");
+		JSONObject structJson = JSONObject.parseObject(FileUtil.readFileContent(structFile));
+		JSONArray structdetails = structJson.getJSONArray("details");
+		for (int i = 0; i < answerdetails.size(); i++) {
+			JSONArray answerdetailquestions = answerdetails.getJSONObject(i).getJSONArray("questions");
+			JSONArray structdetailquestions = structdetails.getJSONObject(i).getJSONArray("questions");
+			for (int j = 0; j < answerdetailquestions.size(); j++) {
+				JSONObject answerquestion = answerdetailquestions.getJSONObject(j);
+				JSONObject structquestion = structdetailquestions.getJSONObject(j);
+				if (structquestion.getInteger("structType").intValue() != 1
+						&& structquestion.getInteger("structType").intValue() != 2
+							&& structquestion.getInteger("structType").intValue() != 3
+								&& structquestion.getInteger("structType").intValue() != 6) {
+					JSONArray answer = answerquestion.getJSONArray("answer");
+					disposeAnswerUrl(rootDir,answer, paper.getId(), attachmentDir);
+				}
+				if (structquestion.getInteger("structType").intValue() == 6) {
+					JSONArray answersubQuestions = answerquestion.getJSONArray("subQuestions");
+					JSONArray structsubQuestions = structquestion.getJSONArray("subQuestions");
+					for (int k = 0; k < answersubQuestions.size(); k++) {
+						JSONObject answersubquestion = answersubQuestions.getJSONObject(k);
+						JSONObject structsubquestion = structsubQuestions.getJSONObject(k);
+						if (structsubquestion.getInteger("structType").intValue() != 1
+								&& structsubquestion.getInteger("structType").intValue() != 2
+									&& structsubquestion.getInteger("structType").intValue() != 3) {
+							JSONArray answer = answersubquestion.getJSONArray("answer");
+							disposeAnswerUrl(rootDir,answer, paper.getId(), attachmentDir);
+						}
+					}
+				}
+			}
+		}
+		FileUtil.saveAsFile(urlAnswerFile.getAbsolutePath(), answerJson.toJSONString());
 		String filePath = sdf.format(new Date()) + "/" + uuid() + ".json";
 		paper.setAnswerPath(filePath);
 		SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
-		OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, answerFile);
+		OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, urlAnswerFile);
 	}
 
+	private void disposeAnswerUrl(String rootDir,JSONArray answer,Long paperId,File attachmentDir) {
+		if(answer!=null&&answer.size()>0) {
+			for(int i=0;i<answer.size();i++) {
+				JSONObject section = answer.getJSONObject(i);
+				if(section!=null) {
+					JSONArray sections = section.getJSONArray("sections");
+					if(sections!=null&&sections.size()>0) {
+						for(int j=0;j<sections.size();j++) {
+							JSONObject block = sections.getJSONObject(j);
+							if(block!=null) {
+								JSONArray blocks = block.getJSONArray("blocks");
+								if(blocks!=null&&blocks.size()>0) {
+									for(int m=0;m<blocks.size();m++) {
+										JSONObject blockInfo = blocks.getJSONObject(m);
+										uploadImageAndAudio(rootDir,blockInfo, paperId, attachmentDir);
+									}
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+	
+	private void uploadImageAndAudio(String rootDir,JSONObject blockInfo,Long paperId,File attachmentDir) {
+		String type=blockInfo.getString("type");
+		String value=blockInfo.getString("value");
+		if(("image".equals(type)||"audio".equals(type))
+				&&StringUtils.isNotBlank(value)
+				&&!value.toLowerCase().startsWith("https://")
+				&&!value.toLowerCase().startsWith("http://")) {
+			if("image".equals(type)&&value.contains("data:image")) {
+            	String suff=value.substring(11, value.indexOf(";"));
+            	File iamgeFile = new File(rootDir+uuid()+"."+suff);
+                BASE64Decoder decoder = new BASE64Decoder();
+                try {
+					byte[] bytes = decoder.decodeBuffer(value.substring(value.indexOf(",") + 1));
+					FileUtils.writeByteArrayToFile(iamgeFile, bytes);
+				} catch (IOException e) {
+					throw new BusinessException("媒体文件上传处理出错:"+blockInfo.toJSONString()+" errmsg:"+e.getMessage() );
+				}
+                String filePath = "upload/paper_file/"+paperId+"/" + uuid() +"."+ suff;
+                SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
+				OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, iamgeFile);
+				blockInfo.put("value", systemConfig.getProperty("aliyun.oss.url") + "/" +filePath);
+			}else{
+				String suff=value.substring(value.indexOf("."));
+				String filePath = "upload/paper_file/"+paperId+"/" + uuid() + suff;
+				File audioFile=new File(attachmentDir.getAbsolutePath()+"/"+value);
+				SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
+				OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, audioFile);
+				blockInfo.put("value", systemConfig.getProperty("aliyun.oss.url") + "/" +filePath);
+			}
+		}
+	}
+	
 	private void addResult(StringBuilder result, String msg) {
 		result.append(msg).append("\r\n");
 	}