package com.qmth.jkserver.core.kw.plugin;

import com.qmth.jkserver.common.CsvUtils;
import com.qmth.jkserver.constant.SpringContextHolder;
import com.qmth.jkserver.core.kw.IKwTempDataGenerator;
import com.qmth.jkserver.enums.ExamType;
import com.qmth.jkserver.model.Attachment;
import com.qmth.jkserver.service.AttachmentService;
import com.qmth.jkserver.service.JkExamPlanService;
import com.qmth.jkserver.util.FileStoreUtil;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringJoiner;
import java.util.zip.ZipException;

/**
 * 上传考生附加信息 时候  生成临时数据
 *
 * @author tianjun
 */
public class StudentAdditionalTempDataGenerator implements IKwTempDataGenerator {

    @Override
    public void execute(String examCode, ExamType examType, Long attachmentId) throws Exception {
        JkExamPlanService examPlanService = SpringContextHolder.getBean(JkExamPlanService.class);
        AttachmentService attachmentService = SpringContextHolder.getBean(AttachmentService.class);
        //2020-12-19日加入阿里云下载
        FileStoreUtil fileStoreConfig = SpringContextHolder.getBean(FileStoreUtil.class);
        Attachment attachment = attachmentService.findByIdService(attachmentId);
        File file = null;
        if (fileStoreConfig.workIsOss()) {
            StringJoiner stringJoiner = new StringJoiner("").add(fileStoreConfig.getWorkRoot())
                    .add(File.separator).add(attachment.getAccessUri());
            file = fileStoreConfig.ossDownload(stringJoiner.toString().replaceAll("\\\\", "/"), fileStoreConfig.getWorkRoot() + File.separator + attachment.getAccessUri(), fileStoreConfig.getWorkRoot());
        } else {
            file = new File(new File(fileStoreConfig.getWorkRoot()), attachment.getAccessUri());
        }
//        MongoTemplate mongoTemplate = BeanFactory.getBean(MongoTemplate.class);
        switch (examType) {
            case NORMAL:
                break;
            case UTEST:
                processUtest(examCode, file, attachmentId, examPlanService);
                break;
            case SOLFEGE:
                break;
            default:
                break;
        }
    }


    //优特机考考场数据校验
    protected void processUtest(String examCode, File saveFile, Long attachmentId,
                                JkExamPlanService examPlanService) throws ZipException, IOException, ParseException, EncryptedDocumentException, InvalidFormatException {
        //存储考生附加信息临时数据
        CsvUtils.saveStudentAdditionalInfoFromCsv(examCode, attachmentId, saveFile.getAbsolutePath(), examPlanService);
    }
}
