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

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

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import cn.com.qmth.examcloud.core.questions.base.BeanCopierUtil;
import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
import cn.com.qmth.examcloud.core.questions.base.Model;
import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.ExportTemplateType;
import cn.com.qmth.examcloud.core.questions.base.exception.PaperException;
import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.DownloadPaperDto;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailExp;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperDetailUnitExp;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.PaperExp;
import cn.com.qmth.examcloud.core.questions.service.util.ExportPaperUtil;
import cn.com.qmth.examcloud.core.questions.service.util.PaperUtil;


/**
 * @author chenken
 * @date 2017年7月31日 上午9:04:53
 * @company QMTH
 * @description 石油大学导出、上传文件service
 */
@Service("sydxExportPaperService")
public class SydxExportPaperService extends ExportPaperAbstractService {

    
    @Override
    public void downloadPaper(DownloadPaperDto dto) throws Exception {
        PaperExp paperExp = initExportSydxPaper(dto);
        if (paperExp != null) {
        	setExportQuestionTitle(paperExp.getPaperDetails(), true);
            String paperfileName = paperExp.getName() + "_" + paperExp.getCourseNo() + "_"
                    + ExamFileType.PAPER.getName() + DOCX_SUFFIX;
            File directory = new File(TEMP_FILE_EXP + File.separator + dto.getZipFileName());
            ExportPaperUtil.createPaperDocFile(dto.getRootOrgId(),paperExp, directory,paperfileName, ExportTemplateType.PAPER_EXPORT);
        }
    }

    @Override
    public void downloadPaperAnswer(DownloadPaperDto dto) throws Exception {
        PaperExp paperExp = initExportSydxPaper(dto);
        if (paperExp != null) {
            String answerFileName = paperExp.getCourseNo() + "_" + ExamFileType.ANSWER.getName() + DOCX_SUFFIX;
            File directory = new File(TEMP_FILE_EXP + File.separator + dto.getZipFileName());
            ExportPaperUtil.createPaperDocFile(dto.getRootOrgId(),paperExp, directory,answerFileName, ExportTemplateType.ANWSER_EXPORT);
        }

    }


    /**
     * 初始化导出石油大学试卷DTO
     *
     * @param id
     * @return
     */
    private PaperExp initExportSydxPaper(DownloadPaperDto dto) throws Exception {
        // 获取paper
        Paper paper = Model.of(paperRepo.findById(dto.getPaperId()));
        paperService.formatPaper(paper, null);
        if (paper == null) {
            throw new PaperException("该试卷不存在");
        }
        // 创建paperDto
        PaperExp paperExp = BeanCopierUtil.copyProperties(paper, PaperExp.class);
        
        paperExp.setTitle(CommonUtils.PAPER_TITLE);
        paperExp.setSubTitle(CommonUtils.PAPER_SUB_TITLE);
        // 获取大题
        List<PaperDetail> paperDetails = paperDetailRepo.findByPaperOrderByNumber(paper);
        List<PaperDetailExp> paperDetailExps = new ArrayList<PaperDetailExp>();
        for (PaperDetail paperDetail : paperDetails) {
            PaperDetailExp paperDetailExp = new PaperDetailExp();
            BeanUtils.copyProperties(paperDetail, paperDetailExp);
            paperDetailExps.add(paperDetailExp);
        }
        paperExp.setPaperDetails(paperDetailExps);
        // 封装小题
        for (int i = 0; i < paperDetailExps.size(); i++) {
            PaperDetailExp paperDetailExp = paperDetailExps.get(i);
            paperDetailExp.setTitle(getDetailTitle(paperDetailExp));
            List<PaperDetailUnit> paperDetailUnits = paperDetailUnitRepo
                    .findByPaperDetailOrderByNumber(paperDetails.get(i));
            List<PaperDetailUnitExp> paperDetailUnitExps = new ArrayList<PaperDetailUnitExp>();
            for (PaperDetailUnit paperDetailUnit : paperDetailUnits) {
                PaperDetailUnitExp paperDetailUnitExp = new PaperDetailUnitExp();
                BeanUtils.copyProperties(paperDetailUnit, paperDetailUnitExp);
                paperDetailUnitExps.add(paperDetailUnitExp);
            }
            // 选择题，套题下选择题 选项顺序重新排列
            paperService.reorderChoicequestionOption(paperDetailUnitExps);
            paperDetailExps.get(i).setPaperDetailUnits(paperDetailUnitExps);
        }
        // 初始化试卷序号
        initPaperNum(paperExp);
        
        PaperUtil.setQuestionSeq(paperDetailExps, dto.getSeqMode());
        return paperExp;
    }




    public String getDetailTitle(PaperDetailExp paperDetailExp) {
        int totalScore = paperDetailExp.getScore().intValue();
        int count = paperDetailExp.getUnitCount();
        return "本大题共" + count + "小题,共" + totalScore + "分";
    }

    /**
     * 初始化序号
     *
     * @param paperExp
     */
    public void initPaperNum(PaperExp paperExp) throws Exception {
        if (paperExp.getPaperDetails() == null || paperExp.getPaperDetails().size() == 0) {
            return;
        }
        int mainNum = 0;
        Collections.sort(paperExp.getPaperDetails());
        for (PaperDetailExp paperDetail : paperExp.getPaperDetails()) {
            // 大题序号
            paperDetail.setNumber(++mainNum);
            paperDetail.setCnNum(CommonUtils.toCHNum(paperDetail.getNumber()));
            if (paperDetail.getPaperDetailUnits() == null || paperDetail.getPaperDetailUnits().size() == 0) {
                continue;
            }
        }
    }

}
