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.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.ExamRoomAddressTemp;
import com.qmth.jkserver.model.StudentInfoTemp;
import com.qmth.jkserver.service.AttachmentService;
import com.qmth.jkserver.service.JkExamPlanService;
import com.qmth.jkserver.service.KWService;
import com.qmth.jkserver.util.FileStoreUtil;

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

/**
 * 考情监控数据
 *
 * @author tianjun
 */
public class RoomMonitorTempDataGenerator 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> roomMonitorPackage = UnZipUtils.doUnZip(saveFile, saveFile.getParentFile(), path);
        File roomAddress = null;
        File studentInfo = null;
        for (File f : roomMonitorPackage) {
            if (f.getName().equals(Constants.ROOM_ADDRESS_INFO)) {
                roomAddress = f;
            }
            if (f.getName().equals(Constants.STUDENT_INFO)) {
                studentInfo = f;
            }
        }
        if (roomAddress == null) {
            throw new JkServerException("zip包中没有room-address.csv文件");
        }
        if (studentInfo == null) {
            throw new JkServerException("zip包中没有student-info.csv文件");
        }
        FileData roomData = new FileData(roomAddress.getAbsolutePath(), ",");
        List<ExamRoomAddressTemp> addressTemps = new LinkedList<ExamRoomAddressTemp>();
        while (roomData.nextLine()) {
            String[] datas = roomData.getDatas();
            String placeCode = datas[0];
            String placeName = datas[1];
            String roomCode = datas[2];
            String roomName = datas[3];
            String province = datas[4];
            String city = datas[5];
            String area = datas[6];
            ExamRoomAddressTemp examRoomAddressTemp = new ExamRoomAddressTemp();
            examRoomAddressTemp.setId(SystemConstant.getDbUuid());
            examRoomAddressTemp.setExamCode(examCode);
            examRoomAddressTemp.setAttachmentId(attachmentId);
            examRoomAddressTemp.setPlaceCode(placeCode);
            examRoomAddressTemp.setPlaceName(placeName);
            examRoomAddressTemp.setRoomCode(roomCode);
            examRoomAddressTemp.setRoomName(roomName);
            examRoomAddressTemp.setProvince(province);
            examRoomAddressTemp.setCity(city);
            examRoomAddressTemp.setArea(area);
            addressTemps.add(examRoomAddressTemp);
        }
        roomData.close();
        FileData studentInfoData = new FileData(studentInfo.getAbsolutePath(), ",");
        List<StudentInfoTemp> studentInfos = new ArrayList<>();
        while (studentInfoData.nextLine()) {
            String[] datas = studentInfoData.getDatas();
            String ticketNumber = datas[0];
            String idno = datas[1];
            String name = datas[2];
            String category = datas[3];
            String studentId = datas[4];
            String phone = datas[5];
            String schoolName = datas[6];
            String province = datas[7];
            String city = datas[8];
            String area = datas[9];
            StudentInfoTemp studentInfoTemp = new StudentInfoTemp();
            studentInfoTemp.setId(SystemConstant.getDbUuid());
            studentInfoTemp.setExamCode(examCode);
            studentInfoTemp.setTicketNumber(ticketNumber);
            studentInfoTemp.setAttachmentId(attachmentId);
            studentInfoTemp.setIdno(idno);
            studentInfoTemp.setName(name);
            studentInfoTemp.setCategory(category);
            studentInfoTemp.setStudentId(Long.parseLong(studentId));
            studentInfoTemp.setPhone(phone);
            studentInfoTemp.setSchoolName(schoolName);
            studentInfoTemp.setProvince(province);
            studentInfoTemp.setCity(city);
            studentInfoTemp.setArea(area);
            studentInfos.add(studentInfoTemp);
        }
        studentInfoData.close();
        KWService kwService = SpringContextHolder.getBean(KWService.class);
        kwService.saveRoomMonitorTempInfo(addressTemps, studentInfos);
    }

    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);
    }

}
