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

import com.qmth.jkserver.common.FileData;
import com.qmth.jkserver.constant.SpringContextHolder;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.core.kw.IKwTempDataGenerator;
import com.qmth.jkserver.enums.ExamType;
import com.qmth.jkserver.model.Attachment;
import com.qmth.jkserver.model.ExamRoomTemp;
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.io.IOException;
import java.text.ParseException;
import java.util.*;
import java.util.zip.ZipException;

/**
 * 考场文件上传时 临时数据的生成方式
 *
 * @author tianjun
 */
public class KcTempDataGenerator 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);
                break;
            case UTEST:
                processUtest(examCode, file, attachmentId, examPlanService);
                break;
            case SOLFEGE:
                processSolfege(examCode, file, attachmentId, examPlanService);
                break;
            default:
                break;
        }
    }

    //通用机考考场数据校验
    protected void processNormal(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService) throws ZipException, IOException, ParseException {
        // 解析考场文件 临时性存储
        FileData fileData = new FileData(saveFile.getAbsolutePath(), ",");
        List<ExamRoomTemp> examRoomTemps = new LinkedList<ExamRoomTemp>();
        Set<String> roomCodes = new HashSet<>();
        while (fileData.nextLine()) {
            String[] datas = fileData.getDatas();
            ExamRoomTemp examRoomTemp = new ExamRoomTemp();
            examRoomTemp.setId(SystemConstant.getDbUuid());
            examRoomTemp.setExamCode(examCode);
            examRoomTemp.setCreateTime(System.currentTimeMillis());
            String placeCode = StringUtils.trimToNull(datas[0]);
            examRoomTemp.setPlaceCode(placeCode);
            examRoomTemp.setPlaceName(StringUtils.trimToNull(datas[1]));
            String roomCode = StringUtils.trimToNull(datas[2]);
            if (roomCodes.contains(roomCode)) {
                throw new JkServerException("考场代码已经存在");
            } else {
                roomCodes.add(roomCode);
            }
            examRoomTemp.setRoomCode(roomCode);
            examRoomTemp.setRoomName(StringUtils.trimToNull(datas[3]));
            if (datas.length > 4) {
                examRoomTemp.setJkTeacher(StringUtils.trimToNull(datas[4]));
            }
            if (datas.length > 5) {
                examRoomTemp.setJkPassword(StringUtils.trimToNull(datas[5]));
            }
            examRoomTemp.setAttachmentId(attachmentId);
            examRoomTemps.add(examRoomTemp);
        }
        examPlanService.saveKwRoomTemp(examRoomTemps);
        fileData.close();
    }

    //优特机考考场数据校验
    protected void processUtest(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService) throws ZipException, IOException, ParseException {
        processNormal(examCode, saveFile, attachmentId, examPlanService);
    }


    //视唱机考考场数据校验
    protected void processSolfege(String examCode, File saveFile, Long attachmentId, JkExamPlanService examPlanService) throws ZipException, IOException, ParseException {
        this.processNormal(examCode, saveFile, attachmentId, examPlanService);
    }


}
