/*
 * *************************************************
 * Copyright (c) 2019 QMTH. All Rights Reserved.
 * Created by Deason on 2019-05-15 16:06:29.
 * *************************************************
 */

package cn.com.qmth.examcloud.core.questions.service.export;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.ExportType;
import cn.com.qmth.examcloud.core.questions.base.word.DocxProcessUtil;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExamPaper;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExportStructure;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExtractConfig;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuestionTypeNum;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperExp;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 华东师范大学
 * 题库导出、上传服务类
 */
@Service("fdsfExportPaperService")
public class FdsfExportPaperService extends ExportPaperAbstractService {

    protected ExamCloudLog log = ExamCloudLogFactory.getLog(this.getClass());

    @Override
    public void downloadPaper(String paperId, String zipFileName, String examType) throws Exception {
        log.debug("题库开始下载试卷...");
        long startTime = System.currentTimeMillis();
        PaperExp paperExp = super.initPaperExp(paperId);
        long paperExpTime = System.currentTimeMillis();
        log.debug("拼装导出对象耗时:" + (paperExpTime - startTime) + "ms");
        if (paperExp != null) {
            String paperfileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_" + ExamFileType.PAPER.getName() + DOCX_SUFFIX;
            File file = new File(TEMP_FILE_EXP + File.separator + zipFileName + File.separator + paperfileName);
            if (examType.equals("offLine")) {
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
                paperExp.setExamRemarkWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(paperExp.getExamRemark())));
                DocxProcessUtil.exportWordNew(paperExp, file, FDSF_TMPLATE_PAPER);
            } else {
                long docxTime = System.currentTimeMillis();
                DocxProcessUtil.exportWordNew(paperExp, file, FDSF_TMPLATE_PAPER);
                long docxEndTime = System.currentTimeMillis();
                log.debug("处理word文字耗时:" + (docxEndTime - docxTime) + "ms");
            }
            long docxImgTime = System.currentTimeMillis();
            DocxProcessUtil.processImage(zipFileName + File.separator + paperfileName, getPkgList(paperId));
            long docxImgEndTime = System.currentTimeMillis();
            log.debug("处理word文字+图片耗时:" + (docxImgEndTime - docxImgTime) + "ms");
            //下载音频
            super.downloadAudio(paperExp, zipFileName);
            long aduioTime = System.currentTimeMillis();
            log.debug("处理音频耗时:" + (aduioTime - docxImgEndTime) + "ms");
        }
    }

    @Override
    public void downloadPaperAnswer(String paperId, String zipFileName) throws Exception {
        log.debug("题库开始下载答案...");
        long startTime = System.currentTimeMillis();
        PaperExp paperExp = initPaperExp(paperId);
        long paperExpTime = System.currentTimeMillis();
        log.debug("拼装导出对象耗时:" + (paperExpTime - startTime) + "ms");
        if (paperExp != null) {
            String answerFileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_" + ExamFileType.ANSWER.getName() + DOCX_SUFFIX;
            File file = new File(TEMP_FILE_EXP + File.separator + zipFileName + File.separator + answerFileName);
            long docxTime = System.currentTimeMillis();
            DocxProcessUtil.exportWordNew(paperExp, file, FDSF_TMPLATE_ANSWER);
            DocxProcessUtil.processImage(zipFileName + File.separator + answerFileName, getPkgList(paperId));
            long docxEndTime = System.currentTimeMillis();
            log.debug("处理word文字+图片耗时:" + (docxEndTime - docxTime) + "ms");
        }
    }

    @Override
    public void uploadFile(ExtractConfig extractConfig, String paperId, ExportStructure exportStructure, User accessUser, ExamPaper examPaper) throws Exception {
        PaperExp paperExp = initPaperExp(paperId);
        if (paperExp != null) {
            String currNum = CommonUtils.getCurNum();
            //删除原有试卷文件
        	/*ExamFile examFile = new ExamFile();
        	examFile.setExamId(extractConfig.getExamId()+"");
        	examFile.setCourseId(extractConfig.getCourseCode());
        	examFile.setOrgId(extractConfig.getOrgId());
        	examFileService.deleteExamFile(examFile);*/
            //没有试卷结构导出设置
            if (exportStructure == null) {
                //上传试卷
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, FDSF_TMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, FDSF_TMPLATE_ANSWER, ExamFileType.ANSWER, examPaper);
            } else if (exportStructure != null && exportStructure.getExportType() == ExportType.NORMAL) {
                List<QuestionTypeNum> questionTypeNums = exportStructure.getQuestionTypeNums();
                //检查试卷中客观题的数量是否大于试卷导出设置中的设置的数量
                checkObjectiveDetailsNum(paperExp, questionTypeNums);
                //上传试卷
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, FDSF_TMPLATE_PAPER, ExamFileType.PAPER, examPaper);
                //上传答案
                uploadPaperOrAnswerFile(paperExp, extractConfig, accessUser, currNum, FDSF_TMPLATE_ANSWER, ExamFileType.ANSWER, examPaper);
                //上传试卷结构   不能在上传试卷和答案之前
                uploadPaperStructure(paperExp, extractConfig, accessUser, currNum, questionTypeNums, examPaper);
            } else if (exportStructure != null && exportStructure.getExportType() == ExportType.ONLINE) {
                //上传机考JSON文件
                uploadComputerTestFile(extractConfig, accessUser, examPaper);
            }
        }
    }

}
