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

import com.qmth.jkserver.common.Constants;
import com.qmth.jkserver.common.FileData;
import com.qmth.jkserver.common.UnZipUtils;
import com.qmth.jkserver.constant.SpringContextHolder;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.kw.IKwTempDataGenerator;
import com.qmth.jkserver.enums.ExamType;
import com.qmth.jkserver.model.Attachment;
import com.qmth.jkserver.model.ExamPaperTemp;
import com.qmth.jkserver.service.AttachmentService;
import com.qmth.jkserver.service.JkExamPlanService;
import com.qmth.jkserver.util.FileStoreUtil;
import org.apache.commons.lang3.StringUtils;

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

/**
 * 试卷文件上传时 临时数据的生成方式
 *
 * @author tianjun
 */
public class PaperTempDataGenerator 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());
        }
        switch (examType) {
            case NORMAL:
                processNormal(examCode, file, attachmentId, examPlanService, attachment.getPath());
                break;
            case UTEST:
                processUtest(examCode, file, attachmentId, examPlanService, attachment.getPath());
                break;
            case SOLFEGE:
                processSolfege(examCode, file, attachmentId, examPlanService, attachment.getPath());
                break;
            default:
                break;
        }
    }

    protected void processNormal(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService, String path) throws Exception {
        List<File> paperFiles = UnZipUtils.doUnZip(saveFile, saveFile.getParentFile(), path);
        File paperFile = null;
        for (File f : paperFiles) {
            if (f.getName().equals(Constants.KW_PAPER_FILE_NAME)) {
                paperFile = f;
                break;
            }
        }
        examPlanService.deleteKwCoursePaperTemp(examCode);
        FileData paperData = new FileData(paperFile.getAbsolutePath(), ",");
        // 一个科目可能对应多套试卷
        List<ExamPaperTemp> examPaperTemps = new LinkedList<>();
        while (paperData.nextLine()) {
            String[] datas = paperData.getDatas();
            String paperId = StringUtils.trimToNull(datas[0]);
            String courseCode = StringUtils.trimToNull(datas[3]);
            ExamPaperTemp examPaperTemp = new ExamPaperTemp();
            examPaperTemp.setId(SystemConstant.getDbUuid());
            examPaperTemp.setExamCode(examCode);
            examPaperTemp.setCourseCode(courseCode);
            examPaperTemp.setPaperName(datas[1]);
            examPaperTemp.setAttachmentId(attachmentId);
            examPaperTemp.setPaperId(paperId);
            examPaperTemps.add(examPaperTemp);
        }
        paperData.close();
        examPlanService.saveKwCoursePaperTemp(examPaperTemps);
    }

    protected void processUtest(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService, String path) throws Exception {
        processNormal(examCode, saveFile, attachmentId, examPlanService, path);
    }


    protected void processSolfege(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService, String path) throws Exception {
        processNormal(examCode, saveFile, attachmentId, examPlanService, path);
    }


}
