Procházet zdrojové kódy

优化试卷导出代码

chenken před 8 roky
rodič
revize
041f755a04

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

@@ -57,6 +57,8 @@ public class ExportStructure implements Serializable{
     private Date updateTime;
     
     private String orgId;
+    
+    private String orgName;
 
     public ExportStructure(){}
     
@@ -163,6 +165,14 @@ public class ExportStructure implements Serializable{
 	public void setOrgId(String orgId) {
 		this.orgId = orgId;
 	}
+
+	public String getOrgName() {
+		return orgName;
+	}
+
+	public void setOrgName(String orgName) {
+		this.orgName = orgName;
+	}
 	
 }
 

+ 40 - 2
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/DzkdExportPaperService.java

@@ -1,9 +1,7 @@
 package com.qmth.cqb.paper.service.export;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -16,19 +14,30 @@ import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
 import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperDetailUnitExp;
 import com.qmth.cqb.paper.dto.PaperExp;
+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.question.model.Question;
 import com.qmth.cqb.utils.BeanCopierUtil;
 import com.qmth.cqb.utils.CommonUtils;
 import com.qmth.cqb.utils.enums.ExamFileType;
+import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.exception.PaperException;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
+/**
+ * 
+ * @author  	chenken
+ * @date    	2017年7月31日 上午9:21:51
+ * @company 	QMTH
+ * @description 电子科技大学导出、上传文件service
+ */
 @Service("dzkdExportPaperService")
 public class DzkdExportPaperService extends ExportPaperAbstractService{
+	
     /**
      * 下载试卷
      * @param id
@@ -129,4 +138,33 @@ public class DzkdExportPaperService extends ExportPaperAbstractService{
 		return paperExp;
 	}
 
+	@Override
+    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception{
+    	PaperExp paperExp = initExportPaper(paperId);
+        if (paperExp!=null) {
+        	String currNum = CommonUtils.getCurNum();
+        	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
+        	//没有试卷结构导出设置
+        	if(exportStructure==null){
+        		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,DZKD_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,DZKD_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
+        		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
+        		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
+        		checkObjectiveDetailsNum(paperExp,objectiveDetails,questionTypeNums);
+	    		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,DZKD_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,DZKD_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+				//上传试卷结构   不能在上传试卷和答案之前
+        		uploadPaperStructure(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
+        		//上传机考JSON文件
+        		uploadComputerTestFile(extractConfig,accessUser);
+        	}
+        }
+    }
+
 }

+ 387 - 9
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/ExportPaperAbstractService.java

@@ -1,5 +1,9 @@
 package com.qmth.cqb.paper.service.export;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -8,11 +12,16 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.xml.bind.JAXBElement;
 
+import main.java.com.UpYun;
+
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.docx4j.XmlUtils;
 import org.docx4j.jaxb.Context;
@@ -27,25 +36,42 @@ import org.springframework.beans.factory.annotation.Value;
 import cn.com.qmth.examcloud.common.dto.core.enums.CourseLevel;
 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.PaperDetailRepo;
 import com.qmth.cqb.paper.dao.PaperDetailUnitRepo;
 import com.qmth.cqb.paper.dao.PaperRepo;
+import com.qmth.cqb.paper.dto.ObjectiveQuestionStructure;
 import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperDetailUnitExp;
 import com.qmth.cqb.paper.dto.PaperExp;
+import com.qmth.cqb.paper.dto.SubjectiveQuestionStructure;
+import com.qmth.cqb.paper.model.ExamFile;
+import com.qmth.cqb.paper.model.ExamPaper;
 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.ExamFileService;
+import com.qmth.cqb.paper.service.PaperDetailService;
 import com.qmth.cqb.paper.service.PaperService;
 import com.qmth.cqb.question.dao.QuesRepo;
 import com.qmth.cqb.question.model.QuesOption;
 import com.qmth.cqb.question.model.Question;
 import com.qmth.cqb.utils.BeanCopierUtil;
 import com.qmth.cqb.utils.CommonUtils;
+import com.qmth.cqb.utils.FileDisposeUtil;
+import com.qmth.cqb.utils.enums.ExamFileType;
 import com.qmth.cqb.utils.exception.PaperException;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
@@ -57,7 +83,7 @@ import freemarker.template.Template;
  * @author  	chenken
  * @date    	2017年7月15日 下午4:00:24
  * @company 	QMTH
- * @description 上传下载文件父类
+ * @description 导出、上传文件service 父类
  */
 public abstract class ExportPaperAbstractService {
 	
@@ -76,6 +102,9 @@ public abstract class ExportPaperAbstractService {
 	@Autowired
 	ExamFileService examFileService;
 
+	@Autowired
+	private PaperDetailService paperDetailService;
+	
 	@Autowired
 	QuesRepo quesRepo;
 
@@ -120,7 +149,7 @@ public abstract class ExportPaperAbstractService {
 	protected static Template TJDX_TEMPLATE_ANSWER;
 	
 	//石油大学模板
-	//protected static Template SYDX_TEMPLATE_NORMAL;
+	protected static Template SYDX_TEMPLATE_PAPER;
 
 	@Value("${upyun.bucketName}")
 	protected String bucketName;
@@ -160,7 +189,7 @@ public abstract class ExportPaperAbstractService {
 			TJDX_TEMPLATE_PAPER = CONFIGURATION.getTemplate("tjdx_paper_template.ftl", ENCODING);
 			TJDX_TEMPLATE_ANSWER = CONFIGURATION.getTemplate("tjdx_answer_template.ftl", ENCODING);
 			
-			//SYDX_TEMPLATE_NORMAL = CONFIGURATION.getTemplate("paper_template.ftl", ENCODING);
+			SYDX_TEMPLATE_PAPER = CONFIGURATION.getTemplate("sydx_paper_template.ftl", ENCODING);
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
@@ -181,13 +210,11 @@ public abstract class ExportPaperAbstractService {
 	 * @param paperId
 	 * @throws Exception
 	 */
-    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser)  throws Exception{
-    	
-    }
+    public abstract void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser)  throws Exception;
 	
 	/**
+	 * 公共方法
 	 * 初始化导出试卷DTO
-	 * 
 	 * @param id
 	 * @return
 	 */
@@ -424,7 +451,7 @@ public abstract class ExportPaperAbstractService {
         return pWordMl.toString();
     }
 	
-    public String getOptionNum(int number){
+    protected String getOptionNum(int number){
         char optionNum = (char)(65 + number);
         return String.valueOf(optionNum);
     }
@@ -434,7 +461,7 @@ public abstract class ExportPaperAbstractService {
      * @param id
      * @return
      */
-    public List<WordprocessingMLPackage> getPkgList(String id){
+    protected List<WordprocessingMLPackage> getPkgList(String id){
         Paper paper = paperRepo.findOne(id);
         List<WordprocessingMLPackage> wordMLPackages = paperDetailUnitRepo.findByPaper(paper)
                 .stream().map(PaperDetailUnit::getQuestion).collect(Collectors.toList())
@@ -559,4 +586,355 @@ public abstract class ExportPaperAbstractService {
         return pWordMl.toString();
     }
     
+    
+    /**
+     * 创建机考文件,并打包上传至又拍云
+     * @param paperId
+     * @param currNum
+     * ObjectId("591ead1290994d57cd453464")
+     * @throws IOException 
+     */
+    protected 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_SUFFIX);
+			String zipUpyunFilePath = uploadUrl+jsonDirectoryName+ZIP_SUFFIX;
+			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);
+    	}
+    }
+    
+    /**
+     * 构建机考数据包实体类
+     * @param extractConfig
+     * @return
+     */
+    private 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
+     */
+    private 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;
+    }
+
+	/**
+     * 生成试卷或答案Word,上传至又拍云
+     * @param orgName
+     * @param dataMap
+     * @param extractConfig
+     * @param paperId
+     * @param accessUser
+     */
+    protected void uploadPaperOrAnswerFile(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum,Template template,ExamFileType examFileType){
+    	String paperfileName = currNum+examFileType.name()+DOCX_SUFFIX;
+    	try {
+			DocxProcessUtil.exportWord(paperExp,paperfileName,template);
+			DocxProcessUtil.processImage(paperfileName,getPkgList(paperExp.getId()));
+			File file  = new File(TEMP_FILE_EXP+paperfileName);
+			String paperFilePath = uploadUrl+paperfileName;
+            UpYun upyun = new UpYun(bucketName,userName,password);
+			upyun.writeFile(paperFilePath,file,true);
+			examFileService.saveExamFile(new ExamFile(extractConfig,paperFilePath,examFileType,DOCX_SUFFIX),accessUser);
+			file.delete();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+    }
+    
+    /**
+     * 生成试卷结构,上传至又拍云
+     */
+    protected void uploadPaperStructure(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum,List<QuestionTypeNum> questionTypeNums) {
+    	exportObjectiveQuestionStructures(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
+    	exportSubjectiveQuestionStructures(paperExp,extractConfig,accessUser,currNum);
+	}
+    
+    /**
+     * 获得客观题导出结构,并上传至又拍云
+     * @param paperExp
+     * @return
+     */
+    protected void exportObjectiveQuestionStructures(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum,List<QuestionTypeNum> questionTypeNums){
+    	String objectiveFilename = currNum+ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name()+EXCEL_SUFFIX;
+    	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
+    	//根据试卷结构导出设置中的数量补齐客观题
+    	fillObjectiveQuestions(objectiveDetails,questionTypeNums);
+    	List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<ObjectiveQuestionStructure>();
+    	for(PaperDetailExp paperDetailExp:objectiveDetails){
+    		for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
+    			objectiveQuestionStructureList.add(new ObjectiveQuestionStructure(paperExp,paperDetailExp,unit));
+    		}
+    	}
+    	ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class); 
+		try {
+			File file = new File(TEMP_FILE_EXP+objectiveFilename);
+			FileOutputStream out = new FileOutputStream(file);
+			objectiveExcelExporter.write(objectiveFilename,objectiveQuestionStructureList,out);
+			String objectiveFilePath = uploadUrl+objectiveFilename;
+            UpYun upyun = new UpYun(bucketName,userName,password);
+			upyun.writeFile(objectiveFilePath,file,true);
+			examFileService.saveExamFile(new ExamFile(extractConfig,objectiveFilePath,ExamFileType.PAPER_STRUCTURE_OBJECTIVE,EXCEL_SUFFIX),accessUser);
+			file.delete();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+    }
+    
+    /**
+     * 根据试卷结构导出设置中的数量补齐客观题
+     * @param objectiveDetails
+     * @param questionTypeNums
+     */
+    public void fillObjectiveQuestions(List<PaperDetailExp> objectiveDetails,List<QuestionTypeNum> questionTypeNums) {
+    	for(PaperDetailExp paperDetailExp:objectiveDetails){
+    		List<PaperDetailUnitExp> paperDetailUnits = paperDetailExp.getPaperDetailUnits();
+    		//1.将大题中的小题排序
+    		Collections.sort(paperDetailUnits);
+    		//2.将小题的number重新设置
+    		for(int i = 0;i<paperDetailUnits.size();i++){
+    			PaperDetailUnitExp paperDetailUnitExp = paperDetailUnits.get(i);
+    			paperDetailUnitExp.setNumber(i+1);
+    		}
+    		//3.根据试卷结构导出设置中的数量补齐客观题
+    		QuesStructType type = paperDetailUnits.get(0).getQuestionType();
+    		int unitSize = paperDetailUnits.size();
+    		for(QuestionTypeNum questionTypeNum:questionTypeNums){
+    			if(type==questionTypeNum.getQuestionType()){
+    				int length = questionTypeNum.getQuantity()-unitSize;
+    				for(int i = 0;i<length;i++){
+    					PaperDetailUnitExp paperDetailUnitExp = new PaperDetailUnitExp();
+    					paperDetailUnitExp.setQuestionType(type);
+    					paperDetailUnitExp.setScore((double) 0);
+    					paperDetailUnitExp.setNumber(unitSize+(i+1));
+    					Question question = new Question();
+    					question.setQuesAnswer("#");
+    					paperDetailUnitExp.setQuestion(question);
+    					paperDetailUnits.add(paperDetailUnitExp);
+    				}
+    			}
+    		}
+    	}
+	}
+
+	/**
+     * 检查客观题数量是否小于试卷结构导出设置的数量
+     * @param paperExp
+     * @param objectiveDetails
+     * @param questionTypeNums
+     * @return
+     */
+    protected void checkObjectiveDetailsNum(PaperExp paperExp,List<PaperDetailExp> objectiveDetails,List<QuestionTypeNum> questionTypeNums) {
+	   for(PaperDetailExp paperDetailExp:objectiveDetails){
+			for(QuestionTypeNum typeNum:questionTypeNums){
+				QuesStructType quesStructType = paperDetailExp.getPaperDetailUnits().get(0).getQuestionType();
+				if(typeNum.getQuestionType()==quesStructType){
+					if(paperDetailExp.getUnitCount()>typeNum.getQuantity()){
+						throw new RuntimeException("试卷:"+paperExp.getName()
+								+"中"+quesStructType.getName()+"的数量:"+paperDetailExp.getUnitCount()
+								+ ",大于试卷结构导出设置的数量:"+typeNum.getQuantity()+",不符合试卷结构导出规则");
+					}
+				}
+			}
+		}
+    }
+
+   	/**
+     * 获得主观题导出结构,并上传至又拍云
+     * @param paperExp
+     * @return
+     */
+    protected void exportSubjectiveQuestionStructures(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum){
+    	String subjectiveFileName = currNum+ExamFileType.PAPER_STRUCTURE_SUBJECTIVE.name()+EXCEL_SUFFIX;
+    	List<PaperDetailExp> subjectiveDetails = paperExp.getSubjectiveDetails();
+    	List<SubjectiveQuestionStructure> subjectiveQuestionStructureList = new ArrayList<SubjectiveQuestionStructure>();
+    	for(PaperDetailExp paperDetailExp:subjectiveDetails){
+    		for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
+    			subjectiveQuestionStructureList.add(new SubjectiveQuestionStructure(paperExp,paperDetailExp,unit));
+    		}
+    	}
+    	ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class); 
+		try {
+			File file = new File(TEMP_FILE_EXP+subjectiveFileName);
+			FileOutputStream out = new FileOutputStream(file);
+			subjectiveExcelExporter.write(subjectiveFileName,subjectiveQuestionStructureList,out);
+			String subjectiveFilePath = uploadUrl+subjectiveFileName;
+            UpYun upyun = new UpYun(bucketName,userName,password);
+			upyun.writeFile(subjectiveFilePath,file,true);
+			examFileService.saveExamFile(new ExamFile(extractConfig,subjectiveFilePath,ExamFileType.PAPER_STRUCTURE_SUBJECTIVE,EXCEL_SUFFIX),accessUser);
+			file.delete();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+    }
+    
 }

+ 42 - 8
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/HzkjExportPaperService.java

@@ -7,28 +7,33 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.springframework.stereotype.Service;
 
+import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
+
+import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperExp;
+import com.qmth.cqb.paper.model.ExportStructure;
+import com.qmth.cqb.paper.model.ExtractConfig;
+import com.qmth.cqb.paper.model.QuestionTypeNum;
+import com.qmth.cqb.utils.CommonUtils;
 import com.qmth.cqb.utils.enums.ExamFileType;
+import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
 /**
  * @author  	chenken
  * @date    	2017年7月7日 上午11:29:49
  * @company 	QMTH
- * @description 华中科技大学导出service
+ * @description 华中科技大学导出、上传文件service
  */
 @Service("hzkjExportPaperService")
 public class HzkjExportPaperService extends ExportPaperAbstractService {
-    
-	public static final String TEMP_FILE_EXP = "docxExport/";
-	public static final String DOCX_SUFFIX = ".docx";
 	
-    @Override
+	@Override
     public void downloadPaper(String paperId, HttpServletResponse response) throws Exception {
     	PaperExp paperExp = initExportPaper(paperId);
-        List<String> fileList = new ArrayList<String>();
-        if (paperExp != null) {
-        	String paperfileName = paperExp.getName()+DOCX_SUFFIX;
+        if (paperExp!=null) {
+        	List<String> fileList = new ArrayList<String>();
+            String paperfileName = paperExp.getName()+DOCX_SUFFIX;
             String answerFileName = paperExp.getName()+"_"+ExamFileType.ANSWER.getName()+DOCX_SUFFIX;
             DocxProcessUtil.exportWord(paperExp, paperfileName,HZKJ_TEMPLATE_PAPER);
             DocxProcessUtil.exportWord(paperExp,answerFileName,HZKJ_TEMPLATE_ANSWER);
@@ -40,5 +45,34 @@ public class HzkjExportPaperService extends ExportPaperAbstractService {
         }
     }
 
+	@Override
+    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception{
+    	PaperExp paperExp = initExportPaper(paperId);
+        if (paperExp!=null) {
+        	String currNum = CommonUtils.getCurNum();
+        	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
+        	//没有试卷结构导出设置
+        	if(exportStructure==null){
+        		//上传试卷
+	        	uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,HZKJ_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+	        	uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,HZKJ_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
+        		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
+        		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
+        		checkObjectiveDetailsNum(paperExp,objectiveDetails,questionTypeNums);
+	    		//上传试卷
+	        	uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,HZKJ_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+	        	uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,HZKJ_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+				//上传试卷结构   不能在上传试卷和答案之前
+        		uploadPaperStructure(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
+        		//上传机考JSON文件
+        		uploadComputerTestFile(extractConfig,accessUser);
+        	}
+        }
+    }
+
 }
 

+ 38 - 13
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/SddxExportPaperService.java

@@ -1,43 +1,39 @@
 package com.qmth.cqb.paper.service.export;
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
 
-import main.java.com.UpYun;
-
 import org.springframework.stereotype.Service;
 
 import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
 
+import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperExp;
-import com.qmth.cqb.paper.model.ExamFile;
+import com.qmth.cqb.paper.model.ExportStructure;
 import com.qmth.cqb.paper.model.ExtractConfig;
+import com.qmth.cqb.paper.model.QuestionTypeNum;
 import com.qmth.cqb.utils.CommonUtils;
 import com.qmth.cqb.utils.enums.ExamFileType;
+import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
 /**
  * @author  	chenken
  * @date    	2017年7月7日 上午11:29:49
  * @company 	QMTH
- * @description 山东大学导出service
+ * @description 山东大学导出、上传文件service
  */
 @Service("sddxExportPaperService")
 public class SddxExportPaperService extends ExportPaperAbstractService {
     
-	public static final String TEMP_FILE_EXP = "docxExport/";
-	public static final String DOCX_SUFFIX = ".docx";
-	
-    @Override
+	@Override
     public void downloadPaper(String paperId, HttpServletResponse response) throws Exception {
     	PaperExp paperExp = initExportPaper(paperId);
-        List<String> fileList = new ArrayList<String>();
-        if (paperExp != null) {
-        	String paperfileName = paperExp.getName()+DOCX_SUFFIX;
+        if (paperExp!=null) {
+        	List<String> fileList = new ArrayList<String>();
+            String paperfileName = paperExp.getName()+DOCX_SUFFIX;
             String answerFileName = paperExp.getName()+"_"+ExamFileType.ANSWER.getName()+DOCX_SUFFIX;
             DocxProcessUtil.exportWord(paperExp, paperfileName,SDDX_TEMPLATE_PAPER);
             DocxProcessUtil.exportWord(paperExp,answerFileName,SDDX_TEMPLATE_ANSWER);
@@ -49,4 +45,33 @@ public class SddxExportPaperService extends ExportPaperAbstractService {
         }
     }
 
+	@Override
+    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception{
+    	PaperExp paperExp = initExportPaper(paperId);
+        if (paperExp!=null) {
+        	String currNum = CommonUtils.getCurNum();
+        	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
+        	//没有试卷结构导出设置
+        	if(exportStructure==null){
+        		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SDDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SDDX_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
+        		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
+        		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
+        		checkObjectiveDetailsNum(paperExp,objectiveDetails,questionTypeNums);
+	    		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SDDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SDDX_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+				//上传试卷结构   不能在上传试卷和答案之前
+        		uploadPaperStructure(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
+        		//上传机考JSON文件
+        		uploadComputerTestFile(extractConfig,accessUser);
+        	}
+        }
+    }
+    
 }

+ 6 - 426
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/SxsfExportPaperService.java

@@ -1,59 +1,20 @@
 package com.qmth.cqb.paper.service.export;
 
-import java.io.File;
-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 javax.servlet.http.HttpServletResponse;
 
-import main.java.com.UpYun;
-
-import org.apache.commons.io.FileUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-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;
-import com.qmth.cqb.paper.dto.PaperDetailUnitExp;
 import com.qmth.cqb.paper.dto.PaperExp;
-import com.qmth.cqb.paper.dto.SubjectiveQuestionStructure;
-import com.qmth.cqb.paper.model.ExamFile;
-import com.qmth.cqb.paper.model.ExamPaper;
 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;
@@ -62,22 +23,11 @@ import com.qmth.cqb.utils.word.DocxProcessUtil;
  * @author  	chenken
  * @date    	2017年7月7日 上午11:29:49
  * @company 	QMTH
- * @description 陕西师范试卷导出service
+ * @description 陕西师范大学导出、上传文件service
  */
 @Service("sxsfExportPaperService")
 public class SxsfExportPaperService extends ExportPaperAbstractService {
 	
-	private static final Logger logger = LoggerFactory.getLogger(SxsfExportPaperService.class);
-	
-	@Autowired
-	private PaperService paperService;
-	
-	@Autowired
-	private PaperDetailService paperDetailService;
-	
-	@Autowired
-    PaperRepo paperRepo;
-	
 	@Override
     public void downloadPaper(String paperId, HttpServletResponse response) throws Exception {
     	PaperExp paperExp = initExportPaper(paperId);
@@ -104,19 +54,19 @@ public class SxsfExportPaperService extends ExportPaperAbstractService {
         	//没有试卷结构导出设置
         	if(exportStructure==null){
         		//上传试卷
-	        	uploadPaperWord(paperExp,extractConfig,paperId,accessUser,currNum);
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SXSF_TEMPLATE_PAPER,ExamFileType.PAPER);
 	        	//上传答案
-	        	uploadAnswerWord(paperExp,extractConfig,paperId,accessUser,currNum);
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SXSF_TEMPLATE_ANSWER,ExamFileType.ANSWER);
         	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
         		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
         		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
         		checkObjectiveDetailsNum(paperExp,objectiveDetails,questionTypeNums);
 	    		//上传试卷
-	        	uploadPaperWord(paperExp,extractConfig,paperId,accessUser,currNum);
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SXSF_TEMPLATE_PAPER,ExamFileType.PAPER);
 	        	//上传答案
-	        	uploadAnswerWord(paperExp,extractConfig,paperId,accessUser,currNum);
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,SXSF_TEMPLATE_ANSWER,ExamFileType.ANSWER);
 				//上传试卷结构   不能在上传试卷和答案之前
-        		uploadPaperStructure(paperExp,extractConfig,paperId,accessUser,currNum,questionTypeNums);
+        		uploadPaperStructure(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
         	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
         		//上传机考JSON文件
         		uploadComputerTestFile(extractConfig,accessUser);
@@ -124,375 +74,5 @@ public class SxsfExportPaperService extends ExportPaperAbstractService {
         }
     }
     
-
-	/**
-     * 生成试卷Word,上传至又拍云
-     * @param orgName
-     * @param dataMap
-     * @param extractConfig
-     * @param paperId
-     * @param accessUser
-     */
-    private void uploadPaperWord(PaperExp paperExp,ExtractConfig extractConfig,String paperId,AccessUser accessUser,String currNum){
-    	String paperfileName = currNum+ExamFileType.PAPER.name()+DOCX_SUFFIX;
-    	try {
-			DocxProcessUtil.exportWord(paperExp,paperfileName,SXSF_TEMPLATE_PAPER);
-			DocxProcessUtil.processImage(paperfileName,getPkgList(paperId));
-			File paperFile  = new File(TEMP_FILE_EXP+paperfileName);
-			String paperFilePath = uploadUrl+paperfileName;
-            UpYun upyun = new UpYun(bucketName,userName,password);
-			upyun.writeFile(paperFilePath, paperFile,true);
-			examFileService.saveExamFile(new ExamFile(extractConfig,paperFilePath,ExamFileType.PAPER,DOCX_SUFFIX),accessUser);
-			paperFile.delete();
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-    }
-    
-    /**
-     * 生成答案Word,上传至又拍云
-     * @param orgName
-     * @param dataMap
-     * @param extractConfig
-     * @param paperId
-     * @param accessUser
-     */
-    private void uploadAnswerWord(PaperExp paperExp,ExtractConfig extractConfig,String paperId,AccessUser accessUser,String currNum){
-    	String answerFileName = currNum+ExamFileType.ANSWER.name()+DOCX_SUFFIX;
-    	try {
-    		DocxProcessUtil.exportWord(paperExp,answerFileName,SXSF_TEMPLATE_ANSWER);
-    		DocxProcessUtil.processImage(answerFileName,getPkgList(paperId));
-    		File answerFile  = new File(TEMP_FILE_EXP+answerFileName);
-    		String answerFilePath = uploadUrl+answerFileName;
-            UpYun upyun = new UpYun(bucketName,userName,password);
-			upyun.writeFile(answerFilePath, answerFile,true);
-			examFileService.saveExamFile(new ExamFile(extractConfig,answerFilePath,ExamFileType.ANSWER,DOCX_SUFFIX),accessUser);
-			answerFile.delete();
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-    }
-    
-    /**
-     * 生成试卷结构,上传至又拍云
-     */
-    private void uploadPaperStructure(PaperExp paperExp,ExtractConfig extractConfig,String paperId,AccessUser accessUser,String currNum,List<QuestionTypeNum> questionTypeNums) {
-    	exportObjectiveQuestionStructures(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
-    	exportSubjectiveQuestionStructures(paperExp,extractConfig,accessUser,currNum);
-	}
-    
-    /**
-     * 获得客观题导出结构,并上传至又拍云
-     * @param paperExp
-     * @return
-     */
-    private void exportObjectiveQuestionStructures(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum,List<QuestionTypeNum> questionTypeNums){
-    	String objectiveFilename = currNum+ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name()+EXCEL_SUFFIX;
-    	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
-    	//根据试卷结构导出设置中的数量补齐客观题
-    	fillObjectiveQuestions(objectiveDetails,questionTypeNums);
-    	List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<ObjectiveQuestionStructure>();
-    	for(PaperDetailExp paperDetailExp:objectiveDetails){
-    		for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
-    			objectiveQuestionStructureList.add(new ObjectiveQuestionStructure(paperExp,paperDetailExp,unit));
-    		}
-    	}
-    	ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class); 
-		try {
-			File file = new File(TEMP_FILE_EXP+objectiveFilename);
-			FileOutputStream out = new FileOutputStream(file);
-			objectiveExcelExporter.write(objectiveFilename,objectiveQuestionStructureList,out);
-			String objectiveFilePath = uploadUrl+objectiveFilename;
-            UpYun upyun = new UpYun(bucketName,userName,password);
-			upyun.writeFile(objectiveFilePath,file,true);
-			examFileService.saveExamFile(new ExamFile(extractConfig,objectiveFilePath,ExamFileType.PAPER_STRUCTURE_OBJECTIVE,EXCEL_SUFFIX),accessUser);
-			file.delete();
-		} catch (FileNotFoundException e) {
-			e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-    }
-    
-    /**
-     * 根据试卷结构导出设置中的数量补齐客观题
-     * @param objectiveDetails
-     * @param questionTypeNums
-     */
-    public void fillObjectiveQuestions(List<PaperDetailExp> objectiveDetails,List<QuestionTypeNum> questionTypeNums) {
-    	for(PaperDetailExp paperDetailExp:objectiveDetails){
-    		List<PaperDetailUnitExp> paperDetailUnits = paperDetailExp.getPaperDetailUnits();
-    		//1.将大题中的小题排序
-    		Collections.sort(paperDetailUnits);
-    		//2.将小题的number重新设置
-    		for(int i = 0;i<paperDetailUnits.size();i++){
-    			PaperDetailUnitExp paperDetailUnitExp = paperDetailUnits.get(i);
-    			paperDetailUnitExp.setNumber(i+1);
-    		}
-    		//3.根据试卷结构导出设置中的数量补齐客观题
-    		QuesStructType type = paperDetailUnits.get(0).getQuestionType();
-    		int unitSize = paperDetailUnits.size();
-    		for(QuestionTypeNum questionTypeNum:questionTypeNums){
-    			if(type==questionTypeNum.getQuestionType()){
-    				int length = questionTypeNum.getQuantity()-unitSize;
-    				for(int i = 0;i<length;i++){
-    					PaperDetailUnitExp paperDetailUnitExp = new PaperDetailUnitExp();
-    					paperDetailUnitExp.setQuestionType(type);
-    					paperDetailUnitExp.setScore((double) 0);
-    					paperDetailUnitExp.setNumber(unitSize+(i+1));
-    					Question question = new Question();
-    					question.setQuesAnswer("#");
-    					paperDetailUnitExp.setQuestion(question);
-    					paperDetailUnits.add(paperDetailUnitExp);
-    				}
-    			}
-    		}
-    	}
-	}
-
-	/**
-     * 检查客观题数量是否小于试卷结构导出设置的数量
-     * @param paperExp
-     * @param objectiveDetails
-     * @param questionTypeNums
-     * @return
-     */
-    private void checkObjectiveDetailsNum(PaperExp paperExp,List<PaperDetailExp> objectiveDetails,List<QuestionTypeNum> questionTypeNums) {
-	   for(PaperDetailExp paperDetailExp:objectiveDetails){
-			for(QuestionTypeNum typeNum:questionTypeNums){
-				QuesStructType quesStructType = paperDetailExp.getPaperDetailUnits().get(0).getQuestionType();
-				if(typeNum.getQuestionType()==quesStructType){
-					if(paperDetailExp.getUnitCount()>typeNum.getQuantity()){
-						throw new RuntimeException("试卷:"+paperExp.getName()
-								+"中"+quesStructType.getName()+"的数量:"+paperDetailExp.getUnitCount()
-								+ ",大于试卷结构导出设置的数量:"+typeNum.getQuantity()+",不符合试卷结构导出规则");
-					}
-				}
-			}
-		}
-    }
-
-   	/**
-     * 获得主观题导出结构,并上传至又拍云
-     * @param paperExp
-     * @return
-     */
-    private void exportSubjectiveQuestionStructures(PaperExp paperExp,ExtractConfig extractConfig,AccessUser accessUser,String currNum){
-    	String subjectiveFileName = currNum+ExamFileType.PAPER_STRUCTURE_SUBJECTIVE.name()+EXCEL_SUFFIX;
-    	List<PaperDetailExp> subjectiveDetails = paperExp.getSubjectiveDetails();
-    	List<SubjectiveQuestionStructure> subjectiveQuestionStructureList = new ArrayList<SubjectiveQuestionStructure>();
-    	for(PaperDetailExp paperDetailExp:subjectiveDetails){
-    		for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
-    			subjectiveQuestionStructureList.add(new SubjectiveQuestionStructure(paperExp,paperDetailExp,unit));
-    		}
-    	}
-    	ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class); 
-		try {
-			File file = new File(TEMP_FILE_EXP+subjectiveFileName);
-			FileOutputStream out = new FileOutputStream(file);
-			subjectiveExcelExporter.write(subjectiveFileName,subjectiveQuestionStructureList,out);
-			String subjectiveFilePath = uploadUrl+subjectiveFileName;
-            UpYun upyun = new UpYun(bucketName,userName,password);
-			upyun.writeFile(subjectiveFilePath,file,true);
-			examFileService.saveExamFile(new ExamFile(extractConfig,subjectiveFilePath,ExamFileType.PAPER_STRUCTURE_SUBJECTIVE,EXCEL_SUFFIX),accessUser);
-			file.delete();
-		} catch (FileNotFoundException e) {
-			e.printStackTrace();
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-    }
-    
-    /**
-     * 创建机考文件,并打包上传至又拍云
-     * @param paperId
-     * @param currNum
-     * ObjectId("591ead1290994d57cd453464")
-     * @throws IOException 
-     */
-    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;
-    }
-
 }
 

+ 63 - 136
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/SydxExportPaperService.java

@@ -3,14 +3,12 @@ package com.qmth.cqb.paper.service.export;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang3.StringUtils;
 import org.docx4j.XmlUtils;
 import org.docx4j.jaxb.Context;
-import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
 import org.docx4j.wml.Body;
 import org.docx4j.wml.P;
 import org.docx4j.wml.R;
@@ -19,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import cn.com.qmth.examcloud.common.dto.question.enums.QuesStructType;
+import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
 
 import com.google.gson.Gson;
 import com.qmth.cqb.paper.dao.PaperDetailRepo;
@@ -27,9 +26,13 @@ import com.qmth.cqb.paper.dao.PaperRepo;
 import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperDetailUnitExp;
 import com.qmth.cqb.paper.dto.PaperExp;
+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.service.PaperDetailService;
 import com.qmth.cqb.paper.service.PaperDetailUnitService;
 import com.qmth.cqb.paper.service.PaperService;
 import com.qmth.cqb.question.dao.QuesRepo;
@@ -37,14 +40,17 @@ import com.qmth.cqb.question.model.QuesOption;
 import com.qmth.cqb.question.model.Question;
 import com.qmth.cqb.utils.BeanCopierUtil;
 import com.qmth.cqb.utils.CommonUtils;
+import com.qmth.cqb.utils.enums.ExamFileType;
+import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.exception.PaperException;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
-import freemarker.template.Configuration;
-import freemarker.template.Template;
-
 /**
- * Created by songyue on 17/3/15.
+ * 
+ * @author  	chenken
+ * @date    	2017年7月31日 上午9:04:53
+ * @company 	QMTH
+ * @description 石油大学导出、上传文件service
  */
 @Service("sydxExportPaperService")
 public class SydxExportPaperService extends ExportPaperAbstractService{
@@ -69,33 +75,28 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
     @Autowired
     PaperService paperService;
 
-    private static Configuration CONFIGURATION;
-
-	private static final String ENCODING = "UTF-8";
+	@Autowired
+	private PaperDetailService paperDetailService;
+	
+    @Override
+    public void downloadPaper(String id, HttpServletResponse response) throws Exception {
+    	List<String> fileNames = new ArrayList<String>();
+    	PaperExp paperExp = initExportSydxPaper(id);
+        if (paperExp!=null) {
+            String fileName = paperExp.getName()+DOCX_SUFFIX;
+            DocxProcessUtil.exportWord(paperExp,fileName,SYDX_TEMPLATE_PAPER);
+            DocxProcessUtil.processImage(fileName, getPkgList(id));
+            fileNames.add(fileName);
+            DocxProcessUtil.processDownload(fileNames, response);
+        }
+    }
 	
-	//石油大学word模板
-  	private static Template SYDX_TEMPLATE_PAPER;
-
-  	static {
-  		try {
-  			CONFIGURATION = new Configuration(Configuration.VERSION_2_3_25);
-  			// 设置编码
-  			CONFIGURATION.setDefaultEncoding(ENCODING);
-  			// 设置ftl模板路径
-  			CONFIGURATION.setClassForTemplateLoading(DocxProcessUtil.class, "/");
-  			SYDX_TEMPLATE_PAPER = CONFIGURATION.getTemplate("sydx_paper_template.ftl", ENCODING);
-  		} catch (Exception e) {
-  			e.printStackTrace();
-  		}
-  	}
-    
     /**
-     * 初始化导出试卷DTO
+     * 初始化导出石油大学试卷DTO
      * @param id
      * @return
      */
-  	@Override
-    public PaperExp initExportPaper(String id) throws Exception{
+    public PaperExp initExportSydxPaper(String id) throws Exception{
         //获取paper
         Paper paper = paperRepo.findOne(id);
         paperService.formatPaper(paper,null);
@@ -116,7 +117,7 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
         for(int i = 0; i < paperDetailExps.size(); i++){
             PaperDetailExp paperDetailExp = paperDetailExps.get(i);
             paperDetailExp.setTitle(getDetailTitle(paperDetailExp));
-            List<PaperDetailUnit> paperDetailUnits =paperDetailUnitRepo.findByPaperDetail(paperDetails.get(i));
+            List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaperDetail(paperDetails.get(i));
 
             setAnswerWord(paperDetailUnits);
 
@@ -131,12 +132,12 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
     }
 
     /**
-     * 设置答案
+     * 设置客观题答案
      * @param answer
      * @return
      * @throws Exception
      */
-    public String getObjectAnswer(String answerWordMl,String answer) throws Exception {
+    private String getObjectAnswer(String answerWordMl,String answer) throws Exception {
         String tmpStr = DocxProcessUtil.BODY_HEADER + answerWordMl + DocxProcessUtil.BODY_TAIL;
         Body body = (Body)XmlUtils.unmarshalString(tmpStr,Context.jc,Body.class);
         List<Object> pList = body.getContent();
@@ -165,12 +166,12 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
     }
 
     /**
-     * 设置答案
+     * 设置主观题答案
      * @param answerWordMl
      * @return
      * @throws Exception
      */
-    public String getSubjectAnswer(String answerWordMl) throws Exception {
+    private String getSubjectAnswer(String answerWordMl) throws Exception {
         String tmpStr = DocxProcessUtil.BODY_HEADER + answerWordMl + DocxProcessUtil.BODY_TAIL;
         Body body = (Body)XmlUtils.unmarshalString(tmpStr,Context.jc,Body.class);
         List<Object> pList = body.getContent();
@@ -258,25 +259,7 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
     public String getDetailTitle(PaperDetailExp paperDetailExp){
         int totalScore = paperDetailExp.getScore().intValue();
         int count = paperDetailExp.getUnitCount();
-        String title = "本大题共"+count+"小题,共"+totalScore+"分";
-        return title;
-    }
-
-    /**
-     * 下载试卷
-     * @param id
-     */
-    @Override
-    public void downloadPaper(String id, HttpServletResponse response) throws Exception {
-    	List<String> fileNames = new ArrayList<String>();
-    	PaperExp paperExp = initExportPaper(id);
-        if (paperExp!=null) {
-            String fileName = paperExp.getName()+DOCX_SUFFIX;
-            DocxProcessUtil.exportWord(paperExp,fileName,SYDX_TEMPLATE_PAPER);
-            DocxProcessUtil.processImage(fileName, getPkgList(id));
-            fileNames.add(fileName);
-            DocxProcessUtil.processDownload(fileNames, response);
-        }
+        return "本大题共"+count+"小题,共"+totalScore+"分";
     }
 
     /**
@@ -334,91 +317,35 @@ public class SydxExportPaperService extends ExportPaperAbstractService{
             }
         }
     }
-
-    public String getOptionNum(int number){
-        char optionNum = (char)(65 + number);
-        return String.valueOf(optionNum);
-    }
-
-    /**
-     * 获取当前试卷下所有试题WordPkg
-     * @param id
-     * @return
-     */
-    public List<WordprocessingMLPackage> getPkgList(String id){
-        Paper paper = paperRepo.findOne(id);
-        List<WordprocessingMLPackage> wordMLPackages = paperDetailUnitRepo.findByPaper(paper)
-                .stream().map(PaperDetailUnit::getQuestion).collect(Collectors.toList())
-                .stream().map(Question::getPkgObj).collect(Collectors.toList());
-        return wordMLPackages;
-    }
-
-    /**
-     * 设置题号
-     * @param quesBodyWordMl
-     * @param num
-     * @return
-     * @throws Exception
-     */
-    public String setSubQuesNum(String quesBodyWordMl,int num) throws Exception {
-        String tmpStr = DocxProcessUtil.BODY_HEADER + quesBodyWordMl + DocxProcessUtil.BODY_TAIL;
-        Body body = (Body)XmlUtils.unmarshalString(tmpStr,Context.jc,Body.class);
-        List<Object> pList = body.getContent();
-        int index = 0;
-        for(Object pObj:pList){
-            if(index > 0){
-                break;
-            }
-            P p = (P) pObj;
-            List<Object> pContent = p.getContent();
-            R run = new R();
-            Text text = new Text();
-            text.setValue(num+". ");
-            run.getContent().add(text);
-            pContent.add(0,run);
-            index++;
-        }
-        StringBuffer pWordMl = new StringBuffer();
-        for(Object pObj:pList){
-            if(pObj instanceof P){
-                pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
-            }
-        }
-        return pWordMl.toString();
-    }
-
-    /**
-     * 设置选项号
-     * @param optionWordMl
-     * @param num
-     * @return
-     * @throws Exception
-     */
-    public String setOptionNum(String optionWordMl,String num) throws Exception {
-        String tmpStr = DocxProcessUtil.BODY_HEADER + optionWordMl + DocxProcessUtil.BODY_TAIL;
-        Body body = (Body)XmlUtils.unmarshalString(tmpStr,Context.jc,Body.class);
-        List<Object> pList = body.getContent();
-        int index = 0;
-        for(Object pObj:pList){
-            if(index > 0){
-                break;
-            }
-            P p = (P) pObj;
-            List<Object> pContent = p.getContent();
-            R run = new R();
-            Text text = new Text();
-            text.setValue(num+". ");
-            run.getContent().add(text);
-            pContent.add(0,run);
-            index++;
-        }
-        StringBuffer pWordMl = new StringBuffer();
-        for(Object pObj:pList){
-            if(pObj instanceof P){
-                pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
-            }
+    
+    @Override
+    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception{
+    	PaperExp exportPaperExp = initExportSydxPaper(paperId);
+    	PaperExp exportPaperStructureExp = initExportPaper(paperId);
+        if (exportPaperExp!=null&&exportPaperStructureExp!=null) {
+        	String currNum = CommonUtils.getCurNum();
+        	List<PaperDetailExp> objectiveDetails = exportPaperStructureExp.getObjectiveDetails();
+        	//没有试卷结构导出设置
+        	if(exportStructure==null){
+        		//上传试卷
+        		uploadPaperOrAnswerFile(exportPaperExp,extractConfig,accessUser,currNum,SYDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案----石油大学无答案模板
+	        	//uploadAnswerWord(paperExp,extractConfig,paperId,accessUser,currNum);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
+        		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
+        		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
+        		checkObjectiveDetailsNum(exportPaperStructureExp,objectiveDetails,questionTypeNums);
+	    		//上传试卷
+        		uploadPaperOrAnswerFile(exportPaperExp,extractConfig,accessUser,currNum,SYDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案----石油大学无答案模板
+	        	//uploadAnswerWord(paperExp,extractConfig,paperId,accessUser,currNum);
+				//上传试卷结构   不能在上传试卷和答案之前
+        		uploadPaperStructure(exportPaperStructureExp,extractConfig,accessUser,currNum,questionTypeNums);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
+        		//上传机考JSON文件
+        		uploadComputerTestFile(extractConfig,accessUser);
+        	}
         }
-        return pWordMl.toString();
     }
 
 }

+ 38 - 13
cqb-paper/src/main/java/com/qmth/cqb/paper/service/export/TjdxExportPaperService.java

@@ -1,43 +1,39 @@
 package com.qmth.cqb.paper.service.export;
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
 
-import main.java.com.UpYun;
-
 import org.springframework.stereotype.Service;
 
 import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
 
+import com.qmth.cqb.paper.dto.PaperDetailExp;
 import com.qmth.cqb.paper.dto.PaperExp;
-import com.qmth.cqb.paper.model.ExamFile;
+import com.qmth.cqb.paper.model.ExportStructure;
 import com.qmth.cqb.paper.model.ExtractConfig;
+import com.qmth.cqb.paper.model.QuestionTypeNum;
 import com.qmth.cqb.utils.CommonUtils;
 import com.qmth.cqb.utils.enums.ExamFileType;
+import com.qmth.cqb.utils.enums.ExportType;
 import com.qmth.cqb.utils.word.DocxProcessUtil;
 
 /**
  * @author  	chenken
  * @date    	2017年7月7日 上午11:29:49
  * @company 	QMTH
- * @description 天津大学导出service
+ * @description 天津大学导出、上传文件service
  */
 @Service("tjdxExportPaperService")
 public class TjdxExportPaperService extends ExportPaperAbstractService {
     
-	public static final String TEMP_FILE_EXP = "docxExport/";
-	public static final String DOCX_SUFFIX = ".docx";
-	
-    @Override
+	@Override
     public void downloadPaper(String paperId, HttpServletResponse response) throws Exception {
     	PaperExp paperExp = initExportPaper(paperId);
-        List<String> fileList = new ArrayList<String>();
-        if (paperExp!= null) {
-        	String paperfileName = paperExp.getName()+DOCX_SUFFIX;
+        if (paperExp!=null) {
+        	List<String> fileList = new ArrayList<String>();
+            String paperfileName = paperExp.getName()+DOCX_SUFFIX;
             String answerFileName = paperExp.getName()+"_"+ExamFileType.ANSWER.getName()+DOCX_SUFFIX;
             DocxProcessUtil.exportWord(paperExp, paperfileName,TJDX_TEMPLATE_PAPER);
             DocxProcessUtil.exportWord(paperExp,answerFileName,TJDX_TEMPLATE_ANSWER);
@@ -48,6 +44,35 @@ public class TjdxExportPaperService extends ExportPaperAbstractService {
             DocxProcessUtil.processDownload(fileList, response);
         }
     }
+
+	@Override
+    public void uploadFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception{
+    	PaperExp paperExp = initExportPaper(paperId);
+        if (paperExp!=null) {
+        	String currNum = CommonUtils.getCurNum();
+        	List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
+        	//没有试卷结构导出设置
+        	if(exportStructure==null){
+        		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,TJDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,TJDX_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.NORMAL){
+        		List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
+        		//检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
+        		checkObjectiveDetailsNum(paperExp,objectiveDetails,questionTypeNums);
+	    		//上传试卷
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,TJDX_TEMPLATE_PAPER,ExamFileType.PAPER);
+	        	//上传答案
+        		uploadPaperOrAnswerFile(paperExp,extractConfig,accessUser,currNum,TJDX_TEMPLATE_ANSWER,ExamFileType.ANSWER);
+				//上传试卷结构   不能在上传试卷和答案之前
+        		uploadPaperStructure(paperExp,extractConfig,accessUser,currNum,questionTypeNums);
+        	}else if(exportStructure!=null&&exportStructure.getExportType()==ExportType.ONLINE){
+        		//上传机考JSON文件
+        		uploadComputerTestFile(extractConfig,accessUser);
+        	}
+        }
+    }
     
 }
 

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

@@ -75,6 +75,7 @@ public class ExportStructureServiceImpl implements ExportStructureService {
 			exportStructure.setUpdateTime(new Date());
 		}
 		exportStructure.setOrgId(user.getRootOrgId()+"");
+		exportStructure.setOrgName(user.getRootOrgName());
 		exportStructureRepo.save(exportStructure);
 	}
 

+ 7 - 3
cqb-paper/src/main/java/com/qmth/cqb/paper/service/impl/ExtractConfigServiceImpl.java

@@ -184,12 +184,13 @@ public class ExtractConfigServiceImpl implements ExtractConfigService {
 		Iterator<Entry<String,String>> iterator = entry.iterator();
 		while(iterator.hasNext()){
 			String paperId = iterator.next().getValue();
+			accessUser.setRootOrgName(orgName);
 			uploadPaperFile(extractConfig,paperId,exportStructure,accessUser);
 		}
 	}
 
 	private void uploadPaperFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception {
-		ExportServiceManage esm = exportServiceManageRepo.findByOrgName("陕西师范大学");
+		ExportServiceManage esm = exportServiceManageRepo.findByOrgName(accessUser.getRootOrgName());
     	ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(esm.getExportServiceName());
     	exportPaperAbstractService.uploadFile(extractConfig,paperId,exportStructure,accessUser);
 	}
@@ -752,11 +753,14 @@ public class ExtractConfigServiceImpl implements ExtractConfigService {
 		
 		List<SubjectiveQuestionStructure> subjectiveQuestionStructureList = new ArrayList<SubjectiveQuestionStructure>();
 		
+		ExportServiceManage esm = exportServiceManageRepo.findByOrgName(exportStructure.getOrgName());
+    	ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(esm.getExportServiceName());
+		
 		for(String paperId:paperIds){
-			PaperExp paperExp = sxsfExportPaperService.initExportPaper(paperId);
+			PaperExp paperExp = exportPaperAbstractService.initExportPaper(paperId);
 			List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
 			//根据设置补齐客观题
-			sxsfExportPaperService.fillObjectiveQuestions(objectiveDetails, questionTypeNums);
+			exportPaperAbstractService.fillObjectiveQuestions(objectiveDetails, questionTypeNums);
 			List<ObjectiveQuestionStructure> objectiveList = new ArrayList<ObjectiveQuestionStructure>();
 	    	for(PaperDetailExp paperDetailExp:objectiveDetails){
 	    		for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){