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

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.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.test.context.junit4.SpringRunner;

import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
import cn.com.qmth.examcloud.core.questions.dao.ExportServiceManageRepo;
import cn.com.qmth.examcloud.core.questions.dao.PaperDetailUnitRepo;
import cn.com.qmth.examcloud.core.questions.dao.PaperRepo;
import cn.com.qmth.examcloud.core.questions.dao.QuesRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExportServiceManage;
import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import cn.com.qmth.examcloud.core.questions.service.PaperService;
import cn.com.qmth.examcloud.core.questions.service.temp.DdExcelService;

/**
 * @author chenken
 * @date 2017年5月12日 下午1:35:39
 * @company QMTH
 * @description PaperServiceTest.java
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class PaperServiceTest {
    @Autowired
    PaperService paperService;
    @Autowired
    ExportServiceManageRepo esmRepo;
    @Autowired
    PaperRepo paperRepo;
    @Autowired
    PaperDetailUnitRepo unitRepo;
    @Autowired
    QuesRepo quesRepo;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private DdExcelService dService;

    @Test
    public void testUseBasePaper() {
        String selectedPaperIds = "58f7294f6bf08e3453d9836c";
        String userId = "chenken";
        paperService.useBasePaper(selectedPaperIds, userId);
    }

    public static void main(String[] args) {
        String answers = "正确";
        System.out.println(!answers.contains("正确"));
    }


    @Test
    public void testExportServiceManage() {
        ExportServiceManage esm = new ExportServiceManage();
        esm.setOrgName("石油大学");
        esm.setExportServiceName("sydxExportPaperService");
        esmRepo.save(esm);
    }

    @Test
    public void updatePaperDetailUnit() {
        List<Paper> papers = paperRepo.findByPaperType(PaperType.GENERATE);
        int i = 0;
        for (Paper paper : papers) {
            List<PaperDetailUnit> units = unitRepo.findByPaper(paper);
            for (PaperDetailUnit unit : units) {
                unit.setPaperType(PaperType.GENERATE);
            }
            i++;
            unitRepo.saveAll(units);
            System.out.println("已经处理完第" + i + "试卷");
        }
    }

    @Test
    public void updateQuestions() {
        List<Question> questions = new ArrayList<Question>();
        List<Question> updateQues = new ArrayList<Question>();
        String orgId = "1";
        QuesStructType questionType = QuesStructType.BOOL_ANSWER_QUESTION;
        Query query = new Query();
        query.addCriteria(Criteria.where("orgId").is(orgId));
        query.addCriteria(Criteria.where("questionType").is(questionType));
        long count = this.mongoTemplate.count(query, Question.class);
        System.out.println("总体量：" + count);//18761
        for (int i = 0; i < 190; i++) {
            query.limit(100);
            query.skip(i * 100);
            List<Question> questionList = this.mongoTemplate.find(query, Question.class);
            questions.addAll(questionList);
            System.out.println("第" + i + "次查询");
        }
        System.out.println("试题集合总体量：" + questions.size());//18761
        for (Question question : questions) {
            String answer = question.getQuesAnswer();
            if (!answer.contains("正确") && !answer.contains("错误")) {
                if (!StringUtils.isBlank(answer)) {
                    updateQues.add(question);
                }
            }
        }
        System.out.println("需要更新的试题量" + updateQues.size());
        for (Question question : updateQues) {
            String answer = question.getQuesAnswer();
            if (answer.contains("对")) {
                question.setQuesAnswer("正确");
            }
            if (answer.contains("错")) {
                question.setQuesAnswer("错误");
            }
        }
        System.out.println("实际更新的试题量" + updateQues.size());
        quesRepo.saveAll(updateQues);
    }

    @Test
    public void exportPaper() {
        List<String> codes = new ArrayList<String>();
        codes.add("Q001002");
        codes.add("Q001008");
        codes.add("Q010072");
        codes.add("Q011028");
        codes.add("Q011089");

        codes.add("Q011101");
        codes.add("Z012040");
        try {
            dService.exportSubQues("1", 105l, codes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

