|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|