|
@@ -1,402 +1,402 @@
|
|
|
-package com.qmth.cqb.paper.service.impl;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Map.Entry;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
-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.beans.factory.annotation.Value;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.common.dto.core.ExamCourseDto;
|
|
|
-import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
|
|
|
-import cn.com.qmth.examcloud.common.util.excel.ExcelWriter;
|
|
|
-
|
|
|
-import com.qmth.cqb.paper.dao.ExportServiceManageRepo;
|
|
|
-import com.qmth.cqb.paper.dao.ExtractConfigRepo;
|
|
|
-import com.qmth.cqb.paper.dao.PaperDetailUnitRepo;
|
|
|
-import com.qmth.cqb.paper.dao.PaperRepo;
|
|
|
-import com.qmth.cqb.paper.dto.ExportPaperInfoModel;
|
|
|
-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.ExportServiceManage;
|
|
|
-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.PaperDetailUnit;
|
|
|
-import com.qmth.cqb.paper.model.QuestionTypeNum;
|
|
|
-import com.qmth.cqb.paper.rpc.ExamCourseClient;
|
|
|
-import com.qmth.cqb.paper.service.ExamFileService;
|
|
|
-import com.qmth.cqb.paper.service.ExportStructureService;
|
|
|
-import com.qmth.cqb.paper.service.ExtractConfigFileService;
|
|
|
-import com.qmth.cqb.paper.service.ExtractConfigService;
|
|
|
-import com.qmth.cqb.paper.service.export.ExportPaperAbstractService;
|
|
|
-import com.qmth.cqb.question.model.QuesOption;
|
|
|
-import com.qmth.cqb.question.model.Question;
|
|
|
-import com.qmth.cqb.question.model.QuestionAudio;
|
|
|
-import com.qmth.cqb.question.service.QuestionAudioService;
|
|
|
-import com.qmth.cqb.utils.CommonUtils;
|
|
|
-import com.qmth.cqb.utils.FileDisposeUtil;
|
|
|
-import com.qmth.cqb.utils.SpringContextUtils;
|
|
|
-import com.qmth.cqb.utils.enums.ExamFileType;
|
|
|
-import com.qmth.cqb.utils.enums.ExportType;
|
|
|
-import com.qmth.cqb.utils.enums.ExportWay;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author chenken
|
|
|
- * @date 2017年7月31日 下午6:03:46
|
|
|
- * @company QMTH
|
|
|
- * @description ExtractConfigFileServiceImpl.java
|
|
|
- */
|
|
|
-@Service("extractConfigFileService")
|
|
|
-public class ExtractConfigFileServiceImpl implements ExtractConfigFileService {
|
|
|
-
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(ExtractConfigFileServiceImpl.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExamCourseClient examCourseClient;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExportStructureService exportStructureService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExtractConfigService extractConfigService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExamFileService examFileService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExportServiceManageRepo exportServiceManageRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ExtractConfigRepo extractConfigRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private QuestionAudioService questionAudioService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PaperRepo paperRepo;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private PaperDetailUnitRepo paperDetailUnitRepo;
|
|
|
-
|
|
|
- @Value("${upyun.downloadUrl}")
|
|
|
- protected String downloadUrl;
|
|
|
-
|
|
|
- @Value("${upyun.downloadDirectory}")
|
|
|
- private String downloadDirectory;
|
|
|
-
|
|
|
- @Value("${upyun.zipDirectory}")
|
|
|
- private String zipDirectory;
|
|
|
-
|
|
|
- @Value("${upyun.bucketName}")
|
|
|
- protected String bucketName;
|
|
|
-
|
|
|
- @Value("${upyun.userName}")
|
|
|
- protected String userName;
|
|
|
-
|
|
|
- @Value("${upyun.password}")
|
|
|
- protected String password;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void saveExtractConfigAndBuildPaperFile(ExtractConfig extractConfig,Integer isbuildFile,AccessUser accessUser) throws Exception {
|
|
|
- //查询试卷导出设置
|
|
|
- ExportStructure exportStructure = null;
|
|
|
- if(isbuildFile==1){
|
|
|
- exportStructure = exportStructureService.findStructureByExamId(extractConfig.getExamId()+"");
|
|
|
- }
|
|
|
- //生成试卷
|
|
|
- Map<String, String> finishedPaperIdMap = extractConfigService.saveExtractConfig(extractConfig,accessUser);
|
|
|
- if(isbuildFile==1){
|
|
|
- Set<Entry<String,String>> entrySet = finishedPaperIdMap.entrySet();
|
|
|
- Iterator<Entry<String,String>> iterator = entrySet.iterator();
|
|
|
- while(iterator.hasNext()){
|
|
|
- Entry<String,String> entry = iterator.next();
|
|
|
- String paperId = entry.getValue();
|
|
|
- uploadPaperFile(extractConfig,paperId,exportStructure,accessUser);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 生成并上传试卷文件
|
|
|
- * @param extractConfig
|
|
|
- * @param paperId
|
|
|
- * @param exportStructure
|
|
|
- * @param accessUser
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private void uploadPaperFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception {
|
|
|
- ExportServiceManage esm = exportServiceManageRepo.findByOrgName(accessUser.getRootOrgName());
|
|
|
- if(esm==null){
|
|
|
- esm = exportServiceManageRepo.findByOrgName("陕西师范大学");
|
|
|
- }
|
|
|
- ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(esm.getExportServiceName());
|
|
|
- exportPaperAbstractService.uploadFile(extractConfig,paperId,exportStructure,accessUser);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exportExamPaperInfoCheck(ExportPaperInfoModel exportModel,HttpServletResponse response) throws Exception {
|
|
|
- ExportStructure exportStructure = exportStructureService.findStructureByExamId(exportModel.getExamId()+"");
|
|
|
- //如果是批量导出
|
|
|
- if(exportModel.getExportWay()==ExportWay.BATCH){
|
|
|
- if(exportStructure==null){
|
|
|
- exportStructure = new ExportStructure();
|
|
|
- exportStructure.setExportType(ExportType.NORMAL);
|
|
|
- }
|
|
|
- //查询该考试下是否所有课程都制定了调卷规则和考试文件
|
|
|
- checkAllCourseByExamId(exportModel.getExamId(),exportStructure.getExportType());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void exportExamPaperInfo(ExportPaperInfoModel exportModel,HttpServletResponse response) throws Exception {
|
|
|
- //创建试卷和压缩文件 文件夹
|
|
|
- FileDisposeUtil.createDirectory(downloadDirectory);
|
|
|
- //创建压缩文件的文件夹
|
|
|
- FileDisposeUtil.createDirectory(zipDirectory);
|
|
|
- ExportStructure exportStructure = exportStructureService.findStructureByExamId(exportModel.getExamId()+"");
|
|
|
- if(exportStructure==null){
|
|
|
- exportStructure = new ExportStructure();
|
|
|
- exportStructure.setExportType(ExportType.NORMAL);
|
|
|
- }
|
|
|
- //如果是普通类型的批量导出
|
|
|
- if(exportModel.getExportWay()==ExportWay.BATCH){
|
|
|
- List<String> paperIds = checkAllCourseByExamId(exportModel.getExamId(),exportStructure.getExportType());
|
|
|
- if(exportStructure.getExportType()==ExportType.NORMAL){
|
|
|
- if(paperIds.size()>0&&exportModel.getExportContentList().contains(ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name())){
|
|
|
- makePaperStructure(exportStructure.getExamName(),paperIds,exportStructure);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //根据条件获取到文件下载路径,下载文件到服务器的downloadDirectory文件夹
|
|
|
- List<ExamFile> examFiles = examFileService.findExamFileListByExportPaperInfoModel(exportModel);
|
|
|
- if(examFiles!=null&&examFiles.size()>0){
|
|
|
- for(int i = 0;i<examFiles.size();i++){
|
|
|
- ExamFile examFile = examFiles.get(i);
|
|
|
- UpYun upyun = new UpYun(bucketName,userName,password);
|
|
|
- File file = new File(downloadDirectory+File.separator+examFile.getFileName());
|
|
|
- upyun.readFile(examFile.getFilePath(), file);
|
|
|
-
|
|
|
- if(examFile.getExamFileType()==ExamFileType.PAPER){
|
|
|
- Long examId = Long.parseLong(exportModel.getExamId());
|
|
|
- ExtractConfig extractConfig = extractConfigService.findConfig(new ExtractConfig(examId,exportModel.getCourseId()));
|
|
|
- Map<String,String> finishedPaperIdMap = extractConfig.getFinishedPaperIdMap();
|
|
|
- Set<Entry<String,String>> entrySet = finishedPaperIdMap.entrySet();
|
|
|
- Iterator<Entry<String,String>> iterator = entrySet.iterator();
|
|
|
- while(iterator.hasNext()){
|
|
|
- Entry<String,String> entry = iterator.next();
|
|
|
- String groupCode = entry.getKey();
|
|
|
- String paperId = entry.getValue();
|
|
|
- if(groupCode.equals(examFile.getGroupCode())){
|
|
|
- downloadAudio(paperId,examFile);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //创建压缩文件名称
|
|
|
- String zipFileName = exportModel.getExamId();
|
|
|
- //将downloadDirectory文件夹压缩成zip文件,存放到zipDirectory文件夹中
|
|
|
- FileDisposeUtil.fileToZip(downloadDirectory,zipDirectory,zipFileName);
|
|
|
- //下载zip文件到客户端
|
|
|
- FileDisposeUtil.downloadFile(zipFileName+".zip",zipDirectory+File.separator+zipFileName+".zip",response);
|
|
|
- //删除文件夹
|
|
|
- FileUtils.deleteQuietly(new File(downloadDirectory));
|
|
|
- FileUtils.deleteQuietly(new File(zipDirectory));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载试卷音频文件
|
|
|
- */
|
|
|
- private void downloadAudio(String paperId,ExamFile examFile){
|
|
|
- Paper paper = paperRepo.findOne(paperId);
|
|
|
- List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaperOrderByNumber(paper);
|
|
|
- for(PaperDetailUnit unit:paperDetailUnits){
|
|
|
- if(unit.getQuestion().getHasAudio()!=null&&unit.getQuestion().getHasAudio()){
|
|
|
- List<QuestionAudio> questionAudios = questionAudioService.findQuestionAudiosByQuestionId(unit.getQuestion().getId());
|
|
|
- for(QuestionAudio audio:questionAudios){
|
|
|
- String audioFileName = examFile.getCourseName()
|
|
|
- +"_"+examFile.getCourseId()
|
|
|
- +"_试卷_"+examFile.getGroupCode()
|
|
|
- +"_"+getAudioFileName(audio,unit);
|
|
|
- UpYun upyun = new UpYun(bucketName,userName,password);
|
|
|
- File file = new File(downloadDirectory+File.separator+audioFileName);
|
|
|
- upyun.readFile(audio.getFileUrl(), file);
|
|
|
- logger.info(audioFileName+"音频下载完成后返回");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 计算取得音频文件名称
|
|
|
- * @param audio
|
|
|
- * @param unit
|
|
|
- * @return
|
|
|
- */
|
|
|
- private String getAudioFileName(QuestionAudio audio,PaperDetailUnit unit){
|
|
|
- String questionAudioId = audio.getId();
|
|
|
- StringBuffer audioFileName = new StringBuffer(unit.getNumber()+"_");
|
|
|
- Question question = unit.getQuestion();
|
|
|
- List<String> idvaluesBody = CommonUtils.getAttrValueFromString(question.getQuesBody(),"a","id");
|
|
|
- if(idvaluesBody.contains(questionAudioId)){
|
|
|
- audioFileName.append("1");//题干
|
|
|
- audioFileName.append("_");
|
|
|
- int index = idvaluesBody.indexOf(questionAudioId);
|
|
|
- audioFileName.append(index+1);
|
|
|
- }else{
|
|
|
- List<QuesOption> options = question.getQuesOptions();
|
|
|
- if(options!=null&&options.size()>0){
|
|
|
- for(QuesOption option:options){
|
|
|
- List<String> idvaluesOption = CommonUtils.getAttrValueFromString(option.getOptionBody(),"a","id");
|
|
|
- if(idvaluesOption.contains(questionAudioId)){
|
|
|
- audioFileName.append("2");//选项
|
|
|
- audioFileName.append("_");
|
|
|
- audioFileName.append(CommonUtils.getOptionNum(Integer.parseInt(option.getNumber())-1));
|
|
|
- audioFileName.append("_");
|
|
|
- int index = idvaluesOption.indexOf(questionAudioId);
|
|
|
- audioFileName.append(index+1);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- audioFileName.append(".");
|
|
|
- audioFileName.append(audio.getFileSuffixes());
|
|
|
- return audioFileName.toString();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询该考试下是否所有课程都制定了调卷规则
|
|
|
- * @param examId
|
|
|
- */
|
|
|
- private List<String> checkAllCourseByExamId(String examId,ExportType exportType) {
|
|
|
- List<String> paperIdList = new ArrayList<String>();
|
|
|
- List<ExamCourseDto> examCourseDtoList = examCourseClient.findExamCourseByExamId(examId);
|
|
|
- for(ExamCourseDto examCourseDto:examCourseDtoList){
|
|
|
- ExtractConfig condition = new ExtractConfig();
|
|
|
- condition.setExamId(examCourseDto.getExamId());
|
|
|
- condition.setCourseCode(examCourseDto.getCourseCode());
|
|
|
- ExtractConfig extractConfig = extractConfigService.findConfig(condition);
|
|
|
- if(extractConfig==null){
|
|
|
- throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”没有制定调卷规则,不能批量导出,请保存调卷规则生成文件");
|
|
|
- }
|
|
|
- if(extractConfig.getIfFinish()==null||extractConfig.getIfFinish()==0){
|
|
|
- throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”考试文件没有生成,不能批量导出,请保存调卷规则生成文件");
|
|
|
- }
|
|
|
- checkExamFileExists(examId,examCourseDto,exportType);
|
|
|
- Map<String,String> finishedPaperIdMap = extractConfig.getFinishedPaperIdMap();
|
|
|
- Set<Entry<String,String>> entry = finishedPaperIdMap.entrySet();
|
|
|
- Iterator<Entry<String,String>> iterator = entry.iterator();
|
|
|
- while(iterator.hasNext()){
|
|
|
- String paperId = iterator.next().getValue();
|
|
|
- paperIdList.add(paperId);
|
|
|
- }
|
|
|
- }
|
|
|
- return paperIdList;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查试卷文件是否存在
|
|
|
- * @param examId
|
|
|
- * @param examCourseDto
|
|
|
- */
|
|
|
- private void checkExamFileExists(String examId,ExamCourseDto examCourseDto,ExportType exportType){
|
|
|
- ExamFile examFileCondition = new ExamFile();
|
|
|
- examFileCondition.setExamId(examId);
|
|
|
- examFileCondition.setCourseId(examCourseDto.getCourseCode());
|
|
|
-
|
|
|
- List<ExamFile> examfiles = examFileService.findExamFileListByExamFile(examFileCondition);
|
|
|
-
|
|
|
- //检查是否都生成了试卷文件
|
|
|
- boolean paperFlag = false;
|
|
|
- if(exportType == ExportType.NORMAL){
|
|
|
- for(ExamFile examFile:examfiles){
|
|
|
- if(examFile.getExamFileType()==ExamFileType.PAPER){
|
|
|
- paperFlag = true;
|
|
|
- }
|
|
|
- }
|
|
|
- }else{
|
|
|
- for(ExamFile examFile:examfiles){
|
|
|
- if(examFile.getExamFileType()==ExamFileType.COMPUTERTEST_PACKAGE){
|
|
|
- paperFlag = true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if(!paperFlag){
|
|
|
- String message = exportType == ExportType.NORMAL?"考试文件":"机考数据包";
|
|
|
- throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”"+message+"没有生成,不能批量导出,请重新保存调卷规则生成");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将该考试下的所有课程试卷的结构生成到一张Excel表中
|
|
|
- * @param examName
|
|
|
- * @param paperIds
|
|
|
- * @param exportStructure
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- private void makePaperStructure(String examName,List<String> paperIds,ExportStructure exportStructure) throws Exception{
|
|
|
- logger.info("正在批量生成试卷结构...");
|
|
|
- List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
|
|
|
-
|
|
|
- List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<ObjectiveQuestionStructure>();
|
|
|
-
|
|
|
- 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 = exportPaperAbstractService.initExportPaper(paperId);
|
|
|
- List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
|
|
|
- //根据设置补齐客观题
|
|
|
- exportPaperAbstractService.fillObjectiveQuestions(objectiveDetails, questionTypeNums);
|
|
|
- List<ObjectiveQuestionStructure> objectiveList = new ArrayList<ObjectiveQuestionStructure>();
|
|
|
- for(PaperDetailExp paperDetailExp:objectiveDetails){
|
|
|
- for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
|
|
|
- objectiveList.add(new ObjectiveQuestionStructure(paperExp,paperDetailExp,unit));
|
|
|
- }
|
|
|
- }
|
|
|
- objectiveQuestionStructureList.addAll(objectiveList);
|
|
|
-
|
|
|
- List<PaperDetailExp> subjectiveDetails = paperExp.getSubjectiveDetails();
|
|
|
- List<SubjectiveQuestionStructure> subjectiveList = new ArrayList<SubjectiveQuestionStructure>();
|
|
|
- for(PaperDetailExp paperDetailExp:subjectiveDetails){
|
|
|
- for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
|
|
|
- subjectiveList.add(new SubjectiveQuestionStructure(paperExp,paperDetailExp,unit));
|
|
|
- }
|
|
|
- }
|
|
|
- subjectiveQuestionStructureList.addAll(subjectiveList);
|
|
|
- }
|
|
|
-
|
|
|
- ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class);
|
|
|
- FileOutputStream out1 = new FileOutputStream(downloadDirectory+File.separator+examName+"_客观题.xlsx");
|
|
|
- objectiveExcelExporter.write(examName+"_客观题.xlsx",objectiveQuestionStructureList,out1);
|
|
|
-
|
|
|
- ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class);
|
|
|
- FileOutputStream out2 = new FileOutputStream(downloadDirectory+File.separator+examName+"_主观题.xlsx");
|
|
|
- subjectiveExcelExporter.write(examName+"_主观题.xlsx",subjectiveQuestionStructureList,out2);
|
|
|
-
|
|
|
- logger.info("批量生成试卷结构完成");
|
|
|
- }
|
|
|
-
|
|
|
+package com.qmth.cqb.paper.service.impl;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+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.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.common.dto.core.ExamCourseDto;
|
|
|
+import cn.com.qmth.examcloud.common.uac.entity.AccessUser;
|
|
|
+import cn.com.qmth.examcloud.common.util.excel.ExcelWriter;
|
|
|
+
|
|
|
+import com.qmth.cqb.paper.dao.ExportServiceManageRepo;
|
|
|
+import com.qmth.cqb.paper.dao.ExtractConfigRepo;
|
|
|
+import com.qmth.cqb.paper.dao.PaperDetailUnitRepo;
|
|
|
+import com.qmth.cqb.paper.dao.PaperRepo;
|
|
|
+import com.qmth.cqb.paper.dto.ExportPaperInfoModel;
|
|
|
+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.ExportServiceManage;
|
|
|
+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.PaperDetailUnit;
|
|
|
+import com.qmth.cqb.paper.model.QuestionTypeNum;
|
|
|
+import com.qmth.cqb.paper.rpc.ExamCourseClient;
|
|
|
+import com.qmth.cqb.paper.service.ExamFileService;
|
|
|
+import com.qmth.cqb.paper.service.ExportStructureService;
|
|
|
+import com.qmth.cqb.paper.service.ExtractConfigFileService;
|
|
|
+import com.qmth.cqb.paper.service.ExtractConfigService;
|
|
|
+import com.qmth.cqb.paper.service.export.ExportPaperAbstractService;
|
|
|
+import com.qmth.cqb.question.model.QuesOption;
|
|
|
+import com.qmth.cqb.question.model.Question;
|
|
|
+import com.qmth.cqb.question.model.QuestionAudio;
|
|
|
+import com.qmth.cqb.question.service.QuestionAudioService;
|
|
|
+import com.qmth.cqb.utils.CommonUtils;
|
|
|
+import com.qmth.cqb.utils.FileDisposeUtil;
|
|
|
+import com.qmth.cqb.utils.SpringContextUtils;
|
|
|
+import com.qmth.cqb.utils.enums.ExamFileType;
|
|
|
+import com.qmth.cqb.utils.enums.ExportType;
|
|
|
+import com.qmth.cqb.utils.enums.ExportWay;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author chenken
|
|
|
+ * @date 2017年7月31日 下午6:03:46
|
|
|
+ * @company QMTH
|
|
|
+ * @description ExtractConfigFileServiceImpl.java
|
|
|
+ */
|
|
|
+@Service("extractConfigFileService")
|
|
|
+public class ExtractConfigFileServiceImpl implements ExtractConfigFileService {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(ExtractConfigFileServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExamCourseClient examCourseClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExportStructureService exportStructureService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExtractConfigService extractConfigService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExamFileService examFileService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExportServiceManageRepo exportServiceManageRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExtractConfigRepo extractConfigRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QuestionAudioService questionAudioService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperRepo paperRepo;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PaperDetailUnitRepo paperDetailUnitRepo;
|
|
|
+
|
|
|
+ @Value("${upyun.downloadUrl}")
|
|
|
+ protected String downloadUrl;
|
|
|
+
|
|
|
+ @Value("${upyun.downloadDirectory}")
|
|
|
+ private String downloadDirectory;
|
|
|
+
|
|
|
+ @Value("${upyun.zipDirectory}")
|
|
|
+ private String zipDirectory;
|
|
|
+
|
|
|
+ @Value("${upyun.bucketName}")
|
|
|
+ protected String bucketName;
|
|
|
+
|
|
|
+ @Value("${upyun.userName}")
|
|
|
+ protected String userName;
|
|
|
+
|
|
|
+ @Value("${upyun.password}")
|
|
|
+ protected String password;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveExtractConfigAndBuildPaperFile(ExtractConfig extractConfig,Integer isbuildFile,AccessUser accessUser) throws Exception {
|
|
|
+ //查询试卷导出设置
|
|
|
+ ExportStructure exportStructure = null;
|
|
|
+ if(isbuildFile==1){
|
|
|
+ exportStructure = exportStructureService.findStructureByExamId(extractConfig.getExamId()+"");
|
|
|
+ }
|
|
|
+ //生成试卷
|
|
|
+ Map<String, String> finishedPaperIdMap = extractConfigService.saveExtractConfig(extractConfig,accessUser);
|
|
|
+ if(isbuildFile==1){
|
|
|
+ Set<Entry<String,String>> entrySet = finishedPaperIdMap.entrySet();
|
|
|
+ Iterator<Entry<String,String>> iterator = entrySet.iterator();
|
|
|
+ while(iterator.hasNext()){
|
|
|
+ Entry<String,String> entry = iterator.next();
|
|
|
+ String paperId = entry.getValue();
|
|
|
+ uploadPaperFile(extractConfig,paperId,exportStructure,accessUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成并上传试卷文件
|
|
|
+ * @param extractConfig
|
|
|
+ * @param paperId
|
|
|
+ * @param exportStructure
|
|
|
+ * @param accessUser
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void uploadPaperFile(ExtractConfig extractConfig,String paperId,ExportStructure exportStructure,AccessUser accessUser) throws Exception {
|
|
|
+ ExportServiceManage esm = exportServiceManageRepo.findByOrgName(accessUser.getRootOrgName());
|
|
|
+ if(esm==null){
|
|
|
+ esm = exportServiceManageRepo.findByOrgName("陕西师范大学");
|
|
|
+ }
|
|
|
+ ExportPaperAbstractService exportPaperAbstractService = (ExportPaperAbstractService) SpringContextUtils.getBeanById(esm.getExportServiceName());
|
|
|
+ exportPaperAbstractService.uploadFile(extractConfig,paperId,exportStructure,accessUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportExamPaperInfoCheck(ExportPaperInfoModel exportModel,HttpServletResponse response) throws Exception {
|
|
|
+ ExportStructure exportStructure = exportStructureService.findStructureByExamId(exportModel.getExamId()+"");
|
|
|
+ //如果是批量导出
|
|
|
+ if(exportModel.getExportWay()==ExportWay.BATCH){
|
|
|
+ if(exportStructure==null){
|
|
|
+ exportStructure = new ExportStructure();
|
|
|
+ exportStructure.setExportType(ExportType.NORMAL);
|
|
|
+ }
|
|
|
+ //查询该考试下是否所有课程都制定了调卷规则和考试文件
|
|
|
+ checkAllCourseByExamId(exportModel.getExamId(),exportStructure.getExportType());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exportExamPaperInfo(ExportPaperInfoModel exportModel,HttpServletResponse response) throws Exception {
|
|
|
+ //创建试卷和压缩文件 文件夹
|
|
|
+ FileDisposeUtil.createDirectory(downloadDirectory);
|
|
|
+ //创建压缩文件的文件夹
|
|
|
+ FileDisposeUtil.createDirectory(zipDirectory);
|
|
|
+ ExportStructure exportStructure = exportStructureService.findStructureByExamId(exportModel.getExamId()+"");
|
|
|
+ if(exportStructure==null){
|
|
|
+ exportStructure = new ExportStructure();
|
|
|
+ exportStructure.setExportType(ExportType.NORMAL);
|
|
|
+ }
|
|
|
+ //如果是普通类型的批量导出
|
|
|
+ if(exportModel.getExportWay()==ExportWay.BATCH){
|
|
|
+ List<String> paperIds = checkAllCourseByExamId(exportModel.getExamId(),exportStructure.getExportType());
|
|
|
+ if(exportStructure.getExportType()==ExportType.NORMAL){
|
|
|
+ if(paperIds.size()>0&&exportModel.getExportContentList().contains(ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name())){
|
|
|
+ makePaperStructure(exportStructure.getExamName(),paperIds,exportStructure);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //根据条件获取到文件下载路径,下载文件到服务器的downloadDirectory文件夹
|
|
|
+ List<ExamFile> examFiles = examFileService.findExamFileListByExportPaperInfoModel(exportModel);
|
|
|
+ if(examFiles!=null&&examFiles.size()>0){
|
|
|
+ for(int i = 0;i<examFiles.size();i++){
|
|
|
+ ExamFile examFile = examFiles.get(i);
|
|
|
+ UpYun upyun = new UpYun(bucketName,userName,password);
|
|
|
+ File file = new File(downloadDirectory+File.separator+examFile.getFileName());
|
|
|
+ upyun.readFile(examFile.getFilePath(), file);
|
|
|
+
|
|
|
+ if(examFile.getExamFileType()==ExamFileType.PAPER){
|
|
|
+ Long examId = Long.parseLong(exportModel.getExamId());
|
|
|
+ ExtractConfig extractConfig = extractConfigService.findConfig(new ExtractConfig(examId,examFile.getCourseId()));
|
|
|
+ Map<String,String> finishedPaperIdMap = extractConfig.getFinishedPaperIdMap();
|
|
|
+ Set<Entry<String,String>> entrySet = finishedPaperIdMap.entrySet();
|
|
|
+ Iterator<Entry<String,String>> iterator = entrySet.iterator();
|
|
|
+ while(iterator.hasNext()){
|
|
|
+ Entry<String,String> entry = iterator.next();
|
|
|
+ String groupCode = entry.getKey();
|
|
|
+ String paperId = entry.getValue();
|
|
|
+ if(groupCode.equals(examFile.getGroupCode())){
|
|
|
+ downloadAudio(paperId,examFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //创建压缩文件名称
|
|
|
+ String zipFileName = exportModel.getExamId();
|
|
|
+ //将downloadDirectory文件夹压缩成zip文件,存放到zipDirectory文件夹中
|
|
|
+ FileDisposeUtil.fileToZip(downloadDirectory,zipDirectory,zipFileName);
|
|
|
+ //下载zip文件到客户端
|
|
|
+ FileDisposeUtil.downloadFile(zipFileName+".zip",zipDirectory+File.separator+zipFileName+".zip",response);
|
|
|
+ //删除文件夹
|
|
|
+ FileUtils.deleteQuietly(new File(downloadDirectory));
|
|
|
+ FileUtils.deleteQuietly(new File(zipDirectory));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载试卷音频文件
|
|
|
+ */
|
|
|
+ private void downloadAudio(String paperId,ExamFile examFile){
|
|
|
+ Paper paper = paperRepo.findOne(paperId);
|
|
|
+ List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo.findByPaperOrderByNumber(paper);
|
|
|
+ for(PaperDetailUnit unit:paperDetailUnits){
|
|
|
+ if(unit.getQuestion().getHasAudio()!=null&&unit.getQuestion().getHasAudio()){
|
|
|
+ List<QuestionAudio> questionAudios = questionAudioService.findQuestionAudiosByQuestionId(unit.getQuestion().getId());
|
|
|
+ for(QuestionAudio audio:questionAudios){
|
|
|
+ String audioFileName = examFile.getCourseName()
|
|
|
+ +"_"+examFile.getCourseId()
|
|
|
+ +"_试卷_"+examFile.getGroupCode()
|
|
|
+ +"_"+getAudioFileName(audio,unit);
|
|
|
+ UpYun upyun = new UpYun(bucketName,userName,password);
|
|
|
+ File file = new File(downloadDirectory+File.separator+audioFileName);
|
|
|
+ upyun.readFile(audio.getFileUrl(), file);
|
|
|
+ logger.info(audioFileName+"音频下载完成后返回");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算取得音频文件名称
|
|
|
+ * @param audio
|
|
|
+ * @param unit
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getAudioFileName(QuestionAudio audio,PaperDetailUnit unit){
|
|
|
+ String questionAudioId = audio.getId();
|
|
|
+ StringBuffer audioFileName = new StringBuffer(unit.getNumber()+"_");
|
|
|
+ Question question = unit.getQuestion();
|
|
|
+ List<String> idvaluesBody = CommonUtils.getAttrValueFromString(question.getQuesBody(),"a","id");
|
|
|
+ if(idvaluesBody.contains(questionAudioId)){
|
|
|
+ audioFileName.append("1");//题干
|
|
|
+ audioFileName.append("_");
|
|
|
+ int index = idvaluesBody.indexOf(questionAudioId);
|
|
|
+ audioFileName.append(index+1);
|
|
|
+ }else{
|
|
|
+ List<QuesOption> options = question.getQuesOptions();
|
|
|
+ if(options!=null&&options.size()>0){
|
|
|
+ for(QuesOption option:options){
|
|
|
+ List<String> idvaluesOption = CommonUtils.getAttrValueFromString(option.getOptionBody(),"a","id");
|
|
|
+ if(idvaluesOption.contains(questionAudioId)){
|
|
|
+ audioFileName.append("2");//选项
|
|
|
+ audioFileName.append("_");
|
|
|
+ audioFileName.append(CommonUtils.getOptionNum(Integer.parseInt(option.getNumber())-1));
|
|
|
+ audioFileName.append("_");
|
|
|
+ int index = idvaluesOption.indexOf(questionAudioId);
|
|
|
+ audioFileName.append(index+1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ audioFileName.append(".");
|
|
|
+ audioFileName.append(audio.getFileSuffixes());
|
|
|
+ return audioFileName.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询该考试下是否所有课程都制定了调卷规则
|
|
|
+ * @param examId
|
|
|
+ */
|
|
|
+ private List<String> checkAllCourseByExamId(String examId,ExportType exportType) {
|
|
|
+ List<String> paperIdList = new ArrayList<String>();
|
|
|
+ List<ExamCourseDto> examCourseDtoList = examCourseClient.findExamCourseByExamId(examId);
|
|
|
+ for(ExamCourseDto examCourseDto:examCourseDtoList){
|
|
|
+ ExtractConfig condition = new ExtractConfig();
|
|
|
+ condition.setExamId(examCourseDto.getExamId());
|
|
|
+ condition.setCourseCode(examCourseDto.getCourseCode());
|
|
|
+ ExtractConfig extractConfig = extractConfigService.findConfig(condition);
|
|
|
+ if(extractConfig==null){
|
|
|
+ throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”没有制定调卷规则,不能批量导出,请保存调卷规则生成文件");
|
|
|
+ }
|
|
|
+ if(extractConfig.getIfFinish()==null||extractConfig.getIfFinish()==0){
|
|
|
+ throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”考试文件没有生成,不能批量导出,请保存调卷规则生成文件");
|
|
|
+ }
|
|
|
+ checkExamFileExists(examId,examCourseDto,exportType);
|
|
|
+ Map<String,String> finishedPaperIdMap = extractConfig.getFinishedPaperIdMap();
|
|
|
+ Set<Entry<String,String>> entry = finishedPaperIdMap.entrySet();
|
|
|
+ Iterator<Entry<String,String>> iterator = entry.iterator();
|
|
|
+ while(iterator.hasNext()){
|
|
|
+ String paperId = iterator.next().getValue();
|
|
|
+ paperIdList.add(paperId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return paperIdList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查试卷文件是否存在
|
|
|
+ * @param examId
|
|
|
+ * @param examCourseDto
|
|
|
+ */
|
|
|
+ private void checkExamFileExists(String examId,ExamCourseDto examCourseDto,ExportType exportType){
|
|
|
+ ExamFile examFileCondition = new ExamFile();
|
|
|
+ examFileCondition.setExamId(examId);
|
|
|
+ examFileCondition.setCourseId(examCourseDto.getCourseCode());
|
|
|
+
|
|
|
+ List<ExamFile> examfiles = examFileService.findExamFileListByExamFile(examFileCondition);
|
|
|
+
|
|
|
+ //检查是否都生成了试卷文件
|
|
|
+ boolean paperFlag = false;
|
|
|
+ if(exportType == ExportType.NORMAL){
|
|
|
+ for(ExamFile examFile:examfiles){
|
|
|
+ if(examFile.getExamFileType()==ExamFileType.PAPER){
|
|
|
+ paperFlag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ for(ExamFile examFile:examfiles){
|
|
|
+ if(examFile.getExamFileType()==ExamFileType.COMPUTERTEST_PACKAGE){
|
|
|
+ paperFlag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!paperFlag){
|
|
|
+ String message = exportType == ExportType.NORMAL?"考试文件":"机考数据包";
|
|
|
+ throw new RuntimeException("该考试下的课程“"+examCourseDto.getCourseName()+"”"+message+"没有生成,不能批量导出,请重新保存调卷规则生成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将该考试下的所有课程试卷的结构生成到一张Excel表中
|
|
|
+ * @param examName
|
|
|
+ * @param paperIds
|
|
|
+ * @param exportStructure
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void makePaperStructure(String examName,List<String> paperIds,ExportStructure exportStructure) throws Exception{
|
|
|
+ logger.info("正在批量生成试卷结构...");
|
|
|
+ List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
|
|
|
+
|
|
|
+ List<ObjectiveQuestionStructure> objectiveQuestionStructureList = new ArrayList<ObjectiveQuestionStructure>();
|
|
|
+
|
|
|
+ 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 = exportPaperAbstractService.initExportPaper(paperId);
|
|
|
+ List<PaperDetailExp> objectiveDetails = paperExp.getObjectiveDetails();
|
|
|
+ //根据设置补齐客观题
|
|
|
+ exportPaperAbstractService.fillObjectiveQuestions(objectiveDetails, questionTypeNums);
|
|
|
+ List<ObjectiveQuestionStructure> objectiveList = new ArrayList<ObjectiveQuestionStructure>();
|
|
|
+ for(PaperDetailExp paperDetailExp:objectiveDetails){
|
|
|
+ for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
|
|
|
+ objectiveList.add(new ObjectiveQuestionStructure(paperExp,paperDetailExp,unit));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ objectiveQuestionStructureList.addAll(objectiveList);
|
|
|
+
|
|
|
+ List<PaperDetailExp> subjectiveDetails = paperExp.getSubjectiveDetails();
|
|
|
+ List<SubjectiveQuestionStructure> subjectiveList = new ArrayList<SubjectiveQuestionStructure>();
|
|
|
+ for(PaperDetailExp paperDetailExp:subjectiveDetails){
|
|
|
+ for(PaperDetailUnitExp unit:paperDetailExp.getPaperDetailUnits()){
|
|
|
+ subjectiveList.add(new SubjectiveQuestionStructure(paperExp,paperDetailExp,unit));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ subjectiveQuestionStructureList.addAll(subjectiveList);
|
|
|
+ }
|
|
|
+
|
|
|
+ ExcelWriter objectiveExcelExporter = new ExcelWriter(ObjectiveQuestionStructure.class);
|
|
|
+ FileOutputStream out1 = new FileOutputStream(downloadDirectory+File.separator+examName+"_客观题.xlsx");
|
|
|
+ objectiveExcelExporter.write(examName+"_客观题.xlsx",objectiveQuestionStructureList,out1);
|
|
|
+
|
|
|
+ ExcelWriter subjectiveExcelExporter = new ExcelWriter(SubjectiveQuestionStructure.class);
|
|
|
+ FileOutputStream out2 = new FileOutputStream(downloadDirectory+File.separator+examName+"_主观题.xlsx");
|
|
|
+ subjectiveExcelExporter.write(examName+"_主观题.xlsx",subjectiveQuestionStructureList,out2);
|
|
|
+
|
|
|
+ logger.info("批量生成试卷结构完成");
|
|
|
+ }
|
|
|
+
|
|
|
}
|