package cn.com.qmth.examcloud.core.questions.starter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;

import cn.com.qmth.examcloud.core.questions.base.question.QuestionDto;
import cn.com.qmth.examcloud.core.questions.dao.ExportServiceManageRepo;
import cn.com.qmth.examcloud.core.questions.dao.PaperRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExamFile;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExamPaper;
import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
import cn.com.qmth.examcloud.core.questions.service.ExamFileService;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigService;
import cn.com.qmth.examcloud.core.questions.service.export.SddxExportPaperService;
import cn.com.qmth.examcloud.core.questions.service.export.SxsfExportPaperService;
import cn.com.qmth.examcloud.core.questions.base.enums.ExamFileType;
import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;

/**
 * @author chenken
 * @date 2017年4月18日 下午3:32:25
 * @company QMTH
 * @description PaperServiceTest.java
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExtractConfigServiceTest {

    @Autowired
    private PaperRepo paperRepo;
    @Autowired
    private ExtractConfigService extractConfigService;
    @Autowired
    private SxsfExportPaperService sxsfExportPaperService;

    @Autowired
    private ExamFileService examFileService;
    @Autowired
    private ExportServiceManageRepo exportServiceManageRepo;
    @Autowired
    private SddxExportPaperService sddxExportPaperService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @SuppressWarnings("unused")
    private MockHttpServletRequest request;

    @SuppressWarnings("unused")
    private MockHttpServletResponse response;

    @Test
    public void downloadPaper() throws Exception {
        Query query = new Query();
        query.addCriteria(Criteria.where("orgId").is(135 + ""));
        query.addCriteria(Criteria.where("paperType").is(PaperType.GENERATE.name()));
        List<Paper> paperList = this.mongoTemplate.find(query, Paper.class);
        for (Paper paper : paperList) {
            try {
                StringBuffer sb = new StringBuffer();
                List<String> paperIds = readFromFile();
                for (String paperId : paperIds) {
                    sb.append(paperId);
                    sb.append("\r\n");
                }
                if (!paperIds.contains(paper.getId())) {
                    System.out.println("当前正在导出ID:" + paper.getId());
                    sddxExportPaperService.downloadPaper(paper.getId(), "shandong", "onLine");
                    writeToFile(sb.toString() + "\r\n" + paper.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * DOC 从文件里读内容.
     *
     * @throws IOException
     */
    private static List<String> readFromFile() throws IOException {
        BufferedReader bReader = new BufferedReader(new FileReader("E:\\paperIds.txt"));
        List<String> paperIds = new ArrayList<String>();
        String temp = "";
        while ((temp = bReader.readLine()) != null) {
            if (StringUtils.isNotBlank(temp)) {
                paperIds.add(temp);
            }
        }
        bReader.close();
        return paperIds;
    }

    /**
     * DOC 往文件里写入数据.
     *
     * @throws IOException
     */
    private static void writeToFile(String writerContent) throws IOException {
        File file = new File("E:\\paperIds.txt");// 要写入的文本文件  
        if (!file.exists()) {// 如果文件不存在，则创建该文件  
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);// 获取该文件的输出流  
        writer.write(writerContent);// 写内容  
        writer.flush();// 清空缓冲区，立即将输出流里的内容写到文件里  
        writer.close();// 关闭输出流，施放资源  
    }

    @Test
    public void test1() throws Exception {
        ExamFile examFile = new ExamFile();
        examFile.setExamId(70 + "");
        examFile.setOrgId(137 + "");
        examFile.setExamFileType(ExamFileType.PAPER_STRUCTURE_SUBJECTIVE);
        List<ExamFile> examFiles = examFileService.findExamFileListByExamFile(examFile);
        for (ExamFile examFile2 : examFiles) {
            System.out.println(examFile2.getFilePath());
        }
    }


    /**
     * 测试保存调卷规则
     *
     * @throws Exception
     */
    @Test
    public void testSaveExtractConfig() throws Exception {
		/*ExtractConfig extractConfig = new ExtractConfig();
		extractConfig.setExamId(2L);
		extractConfig.setExamName("测试考试0419");
		extractConfig.setExamType("TRANDITION");
		extractConfig.setCourseCode("03013750");
		extractConfig.setCourseName("测试课程0419");
		extractConfig.setCallType("WHOLE_SET");
		extractConfig.setScrambling_the_question_order((short)1);
		extractConfig.setScrambling_the_option_order((short)1);
		//List<ExamPaper> examPaperList = buildOnlineExamPaper();
		List<ExamPaper> examPaperList = buildTraditionExamPaper();
		extractConfig.setExamPaperList(examPaperList);
		extractConfigService.saveExtractConfig(extractConfig,"机构名称");*/
    }

    @SuppressWarnings("unused")
    private List<ExamPaper> buildOnlineExamPaper() {
        List<ExamPaper> examPaperList = new ArrayList<ExamPaper>();
        /*******************A类型********************/
        ExamPaper examPaper1 = new ExamPaper();
        examPaper1.setExamId(1L);
        examPaper1.setCourseCode("03013750");
        examPaper1.setGroupCode("A");
        examPaper1.setWeight(80);
        Paper paper1 = new Paper();
        paper1.setId("58f0a15258e31a0d7448d58c");
        examPaper1.setPaper(paper1);
        examPaperList.add(examPaper1);
        ExamPaper examPaper2 = new ExamPaper();
        examPaper2.setExamId(1L);
        examPaper2.setCourseCode("03013750");
        examPaper2.setGroupCode("A");
        examPaper2.setWeight(20);
        Paper paper2 = new Paper();
        paper2.setId("58f42fcd58e31a025844bfff");
        examPaper2.setPaper(paper2);
        examPaperList.add(examPaper2);
        /*******************B类型********************/
        ExamPaper examPaper3 = new ExamPaper();
        examPaper3.setExamId(1L);
        examPaper3.setCourseCode("03013750");
        examPaper3.setGroupCode("B");
        examPaper3.setWeight(60);
        Paper paper3 = new Paper();
        paper3.setId("58f4300c58e31a025844c02f");
        examPaper3.setPaper(paper3);
        examPaperList.add(examPaper3);
        ExamPaper examPaper4 = new ExamPaper();
        examPaper4.setExamId(1L);
        examPaper4.setCourseCode("03013750");
        examPaper4.setGroupCode("B");
        examPaper4.setWeight(40);
        Paper paper4 = new Paper();
        paper4.setId("e483c730-921e-4212-a67e-42235a0aec00");
        examPaper4.setPaper(paper4);
        examPaperList.add(examPaper4);
        return examPaperList;
    }

    @SuppressWarnings("unused")
    private List<ExamPaper> buildTraditionExamPaper() {
        List<ExamPaper> examPaperList = new ArrayList<ExamPaper>();
        /*******************A类型********************/
        ExamPaper examPaper1 = new ExamPaper();
        examPaper1.setExamId(1L);
        examPaper1.setCourseCode("03013750");
        examPaper1.setGroupCode("A");
        examPaper1.setWeight(100);
        Paper paper1 = new Paper();
        paper1.setId("58f0a15258e31a0d7448d58c");
        examPaper1.setPaper(paper1);
        examPaperList.add(examPaper1);
        /*******************B类型********************/
        ExamPaper examPaper2 = new ExamPaper();
        examPaper2.setExamId(1L);
        examPaper2.setCourseCode("03013750");
        examPaper2.setGroupCode("B");
        examPaper2.setWeight(100);
        Paper paper2 = new Paper();
        paper1.setId("58f0a15258e31a0d7448d58c");
        examPaper1.setPaper(paper2);
        examPaperList.add(examPaper2);
        return examPaperList;
    }

    @Test
    public void testGetExamPaperId() {
        String courseCode = "0003";
        String orgId = "1";
        List<String> paperIds = extractConfigService.getExamPaperId(courseCode, orgId);
        System.out.println(paperIds);
    }

    @Test
    public void testExtract() {
        Long exam_id = 59L;
        String course_code = "012027";
        String group_code = "A";
        Map<String, Object> returnMap = extractConfigService.extractExamPaper(exam_id, course_code, group_code);
        System.out.println(returnMap);
    }

    @Test
    public void testcheckIsAllQbjectiveQuestion() {
        System.out.println(extractConfigService.checkIsAllQbjectiveQuestion("5935658e90994d16dacf9fba"));
    }

    @Test
    public void testGetQuestionById() {
        String examId = "189";
        String paperDetailUnitId = "59b8d7200fe2d22f92f48bdc";
        String courseCode = "000001";
        String groupCode = "A";
        QuestionDto dto = extractConfigService.extractExamQuestion(examId, courseCode, groupCode, paperDetailUnitId);
        System.out.println(dto.getQuesAnswer());
    }


    public static void main(String[] args) {
        List<String> newList = new ArrayList<String>();
        newList.add("a");
        newList.add("b");
        newList.add("c");
        newList.add("d");
        newList.add("e");
        Collections.shuffle(newList);
        System.out.println(newList);
    }
}

