//package cn.com.qmth.examcloud.core.questions.service.temp;
//
//import cn.com.qmth.examcloud.commons.util.JsonUtil;
//import cn.com.qmth.examcloud.core.questions.base.CommonUtils;
//import cn.com.qmth.examcloud.core.questions.base.IdUtils;
//import cn.com.qmth.examcloud.core.questions.base.enums.PaperStatus;
//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.base.word.DocxProcessUtil;
//import cn.com.qmth.examcloud.core.questions.dao.*;
//import cn.com.qmth.examcloud.core.questions.dao.entity.*;
//import cn.com.qmth.examcloud.core.questions.service.temp.vo.*;
//import org.apache.commons.lang3.StringUtils;
//import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.jdbc.core.BeanPropertyRowMapper;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.stereotype.Service;
//
//import java.io.UnsupportedEncodingException;
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//@Service("ddCollegeUtilService")
//public class DdCollegeUtilService {
//    private static final Logger log = LoggerFactory.getLogger(DdCollegeUtilService.class);
//    @Autowired
//    @Qualifier("oracleJdbcTemplate")
//    private JdbcTemplate jdbcTemplate;
//
//    @Autowired
//    private CourseRepo courseRepo;
//
//    @Autowired
//    private QuesPkgPathRepo quesPkgPathRepo;
//
//    @Autowired
//    private QuesRepo quesRepo;
//
//    @Autowired
//    private PaperRepo paperRepo;
//
//    @Autowired
//    private PaperDetailRepo paperDetailRepo;
//
//    @Autowired
//    private PaperDetailUnitRepo paperDetailUnitRepo;
//
//    //地大题库数据导入 根据课程代码查询所有试卷
//    public void importPaper(String orgId, String courseCode, int type) throws Exception {
//        //查询课程
//        Course course = courseRepo.findFirstByCodeAndOrgId(courseCode, orgId);
//        List<TestPaper> papers = new ArrayList<>();
//        if (type == 1) {
//
//            TestPaper testPaper2 = new TestPaper();
//            testPaper2.setId("511D162BFE15663366207EA110D286A5");
//            testPaper2.setName("高层建筑结构设计(专升本)阶段性作业3");
//            testPaper2.setTestPaperContentId("1E61C33D53A1D51C278F3859EB60FA0F");
//            testPaper2.setScore(100d);
//            papers.add(testPaper2);
//
//        } else {
//            //查询试卷集合
//            papers = queryByCourse(courseCode);
//        }
//        if (papers != null && papers.size() > 0) {
//            for (TestPaper testPaper : papers) {
//                //查询试题详情
//                List<TestQuestion> questions = queryByTestPaper(testPaper.getTestPaperContentId());
//                if (questions != null && questions.size() > 0) {
//                    //构建大题和小题对应关系
//                    Map<Integer, List<TestQuestion>> map = sortPaperDetail(questions);
//                    //初始化试卷
//                    Paper paper = initPaper(testPaper, orgId, course, map);
//                    //定义大题集合
//                    List<PaperDetail> paperDetails = new ArrayList<>();
//                    //定义小题集合
//                    List<PaperDetailUnit> paperDetailUnits = new ArrayList<>();
//                    //试题---资源 对应关系
//                    Map<Question, QuestionPkgPath> map2 = new HashMap<>();
//                    //定义试卷难度
//                    double difficulty = 0;
//                    int error = 1;
//                    for (Integer key : map.keySet()) {
//                        List<TestQuestion> dQuestions = map.get(key);
//                        //初始化大题对象
//                        PaperDetail paperDetail = initPaperDetail(dQuestions, paper);
//                        if (dQuestions != null && dQuestions.size() > 0) {
//                            for (TestQuestion testQuestion : dQuestions) {
//                                TestQuestionInfo testQuestionInfo = JsonUtil.fromJson(testQuestion.getJson(), TestQuestionInfo.class);
//                                //初始化试题对象
//                                Question question = null;
//                                try {
//                                    question = initQuestion(testQuestionInfo, testQuestion, course, map2, difficulty);
//                                } catch (Exception e) {
//                                    log.error(e.getMessage(), e);
//                                    log.debug("错误题号：" + error);
//                                }
//                                //初始化小题对象
//                                PaperDetailUnit paperDetailUnit = initPaperDetailUnit(paper, paperDetail, question, testQuestion);
//                                paperDetailUnits.add(paperDetailUnit);
//                                error++;
//                            }
//                        }
//                        paperDetails.add(paperDetail);
//                    }
//                    //保存试题资源
//                    quesPkgPathRepo.saveAll(map2.values());
//                    for (Map.Entry<Question, QuestionPkgPath> entry : map2.entrySet()) {
//                        entry.getKey().setQuesPkgPathId(entry.getValue().getId());
//                    }
//                    //计算试卷难度
//                    BigDecimal b = BigDecimal.valueOf(difficulty / paper.getTotalScore());
//                    Double difficultyDegree = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                    paper.setDifficultyDegree(0.5);
//                    //重新计算试题序号
//                    Map<PaperDetail, List<PaperDetailUnit>> map3 = new LinkedHashMap<>();
//                    List<PaperDetailUnit> newList = null;
//                    for (PaperDetail paperDetail : paperDetails) {
//                        for (PaperDetailUnit paperDetailUnit : paperDetailUnits) {
//                            if (paperDetail.getName().equals(paperDetailUnit.getPaperDetail().getName())) {
//                                if (map3.get(paperDetail) == null) {
//                                    newList = new ArrayList<>();
//                                    newList.add(paperDetailUnit);
//                                } else {
//                                    newList = map3.get(paperDetail);
//                                    newList.add(paperDetailUnit);
//                                }
//                                map3.put(paperDetail, newList);
//                            }
//                        }
//                    }
//                    int number = 1;
//                    for (Map.Entry<PaperDetail, List<PaperDetailUnit>> entry : map3.entrySet()) {
//                        for (PaperDetailUnit paperDetailUnit : entry.getValue()) {
//                            paperDetailUnit.setNumber(number);
//                            number++;
//                        }
//                    }
//                    quesRepo.saveAll(map2.keySet());
//                    paperRepo.save(paper);
//                    paperDetailRepo.saveAll(paperDetails);
//                    paperDetailUnitRepo.saveAll(paperDetailUnits);
//                }
//            }
//        }
//    }
//
//    //根据课程代码查询所有试卷
//    @SuppressWarnings({"unchecked", "rawtypes"})
//    public List<TestPaper> queryByCourse(String courseCode) throws UnsupportedEncodingException {
//        String sql = "SELECT * from TEST_PAPER_INFO p WHERE p.COURSE_ID in "
//                + "(SELECT c.ID FROM PE_TCH_COURSE c WHERE c.code = '" + courseCode + "') and (p.INVALID_REASON is null or p.INVALID_REASON='') and p.PAPER_STATUS = 0 "
//                + "and p.NAME not like '%考试'";
//        List<TestPaper> papers = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(TestPaper.class));
//        return papers;
//    }
//
//    //根据考试id查询所有试题
//    @SuppressWarnings({"unchecked", "rawtypes"})
//    public List<TestQuestion> queryByTestPaper(String testPaperContentId) {
//        List<TestQuestion> dquestions = new ArrayList<TestQuestion>();
//        String sql = "SELECT q.ID id,d.dNumber,d.NAME dName,d.number1 number1,d.score score,q.PARENT_ID parent_id,q.QUESTION_TYPE_ENUM type,q.JSON "
//                + "FROM QUESTION_CONTENT q ,(SELECT b.QUESTION_CONTENT_ID question_id, b.SCORE score, b.SEQUENCE number1, c.NAME name, c.dNumber dNumber "
//                + "FROM TEST_PAPER_QUESTION b ,(SELECT a.ID id, a.NAME name,a.SEQUENCE dNumber,a.TEST_PAPER_CONTENT_ID "
//                + "FROM TEST_PAPER_QUESTION_GROUP a WHERE a.TEST_PAPER_CONTENT_ID = '" + testPaperContentId + "' ORDER BY a.SEQUENCE) c "
//                + "WHERE b.TEST_PAPER_QUESTION_GROUP_ID = c.id ORDER BY c.dNumber,b.SEQUENCE) d WHERE q.ID = d.question_id ORDER BY d.dNumber,d.number1";
//        List<TestQuestion> questions = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(TestQuestion.class));
//        if (questions != null && questions.size() > 0) {
//            for (TestQuestion testQuestion : questions) {
//                if (testQuestion.getParentId().equals("00000000000000000000000000000000")) {
//                    dquestions.add(testQuestion);
//                }
//            }
//        }
//        return dquestions;
//    }
//
//    //测试oracle的连接
//    public int testCount() {
//        String sql = "select count(*) from QUESTION_CONTENT";
//        int count = this.jdbcTemplate.queryForObject(sql, Integer.class);
//        String key = "{\n" +
//                "  \"Answer\": \"（1）3m处的自重应力<br /><img width=\\\"164\\\" height=\\\"20\\\" alt=\\\"\\\" src=\\\"/UploadFiles/Import/Image/6c1ff809-f378-4b17-b6b6-ee2f699b9833_.png\\\" /><br />6m处的自重应力<br /><img width=\\\"279\\\" height=\\\"20\\\" alt=\\\"\\\" src=\\\"/UploadFiles/Import/Image/4f475944-d381-42c5-8946-85f3d4bdc0af_.png\\\" /><br />（2）受无限均布荷载，地下附加应力处处相等，因此3m和6m处的附加应力均为<br /><img width=\\\"95\\\" height=\\\"20\\\" alt=\\\"\\\" src=\\\"/UploadFiles/Import/Image/97d5d878-b602-4f04-b940-12c3d08a3e25_.png\\\" /><br /><table width=\\\"100%\\\" class=\\\"MsoNormalTable\\\" border=\\\"1\\\" cellspacing=\\\"0\\\" cellpadding=\\\"0\\\" style=\\\"border: currentColor; border-collapse: collapse;\\\">    <tbody>        <tr style=\\\"height: 15pt;\\\">            <td width=\\\"100%\\\" colspan=\\\"7\\\" style=\\\"padding: 0cm 5.4pt; border: 1pt solid windowtext; width: 100%; height: 15pt;\\\">            <span style=\\\"font-family: 宋体;\\\">小于某粒径（</span><span lang=\\\"EN-US\\\">mm</span><span style=\\\"font-family: 宋体;\\\">）的累积百分含量</span>            </td>        </tr>        <tr style=\\\"height: 15pt;\\\">            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentColor windowtext windowtext; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">P20</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q20</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q2</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.18%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q0.5</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.2%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q0.25</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.96%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q0.075</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.18%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">Q0.005</span>            </td>        </tr>        <tr style=\\\"height: 15pt;\\\">            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt; border-style: none solid solid; border-color: currentColor windowtext windowtext; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">6</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">94</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.16%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">76</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.18%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">57</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.2%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">34</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.96%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">15</span>            </td>            <td width=\\\"14%\\\" style=\\\"border-width: medium 1pt 1pt medium; border-style: none solid solid none; border-color: currentColor windowtext windowtext currentColor; padding: 0cm 5.4pt; width: 14.18%; height: 15pt;\\\">            <span lang=\\\"EN-US\\\">3</span>            </td>        </tr>    </tbody></table>\",\n" +
//                "  \"ID\": \"360e1c0a-499a-4897-8f66-ebd2eb5512a2\",\n" +
//                "  \"CreateTime\": \"2017-07-12 22:12:53\",\n" +
//                "  \"QuestionType\": 60,\n" +
//                "  \"ParentId\": \"00000000-0000-0000-0000-000000000000\",\n" +
//                "  \"QuestionInfoId\": \"bcc11589-b4d5-45b0-8f06-12b12eda6e6e\",\n" +
//                "  \"QuestionHTML\": \"地表作用有无限均布荷载P=100 kPa，地表以下为细砂土层，地下水埋深3m，地下水位以上重度为18 kN/m<sup>3</sup>，水位以下重度为20 kN/m<sup>3</sup>。<br />（1）分别求地下3m和6m处的自重应力；<br />（2）分别求地下3m和6m处的附加应力。\",\n" +
//                "  \"QuestionTXT\": \"地表作用有无限均布荷载P=100 kPa，地表以下为细砂土层，地下水埋深3m，地下水位以上重度为18 kN/m3，水位以下重度为20kN/m3。（1）分别求地下3m和6m处的自重应力；（2）分别求地下3m和6m处的附加应力。\",\n" +
//                "  \"AnswerTips\": null\n" +
//                "}";
//        String sql2 = "update QUESTION_CONTENT set JSON = ? where ID = ?";
//        this.jdbcTemplate.update(sql2, key, "0A1C0E369A4997488F66EBD2EB5512A2");
//        return 1;
//    }
//
//    //根据父ID查询子题
//    @SuppressWarnings({"unchecked", "rawtypes"})
//    public List<TestQuestion> querySubQuestions(String parentId) {
//        String sql = "SELECT q.question_type_enum type, q.json json from QUESTION_CONTENT q WHERE q.parent_id = '" + parentId + "' order by q.sequence";
//        List<TestQuestion> questions = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper(TestQuestion.class));
//        return questions;
//    }
//
//    //初始化试卷对象
//    public Paper initPaper(TestPaper testPaper, String orgId, Course course, Map<Integer, List<TestQuestion>> map) {
//        int unitCount = 0;
//        Set<Integer> paperDetails = map.keySet();
//        for (Map.Entry<Integer, List<TestQuestion>> entry : map.entrySet()) {
//            unitCount = unitCount + entry.getValue().size();
//        }
//        Paper paper = new Paper();
//        paper.setName(testPaper.getName());
//        paper.setTitle(testPaper.getName());
//        paper.setPaperType(PaperType.IMPORT);
//        paper.setPaperStatus(PaperStatus.DRAFT);
//        paper.setOrgId(orgId);
//        paper.setCreator("wei");
//        paper.setTotalScore(testPaper.getScore());
//        paper.setCourse(course);
//        paper.setCourseName(course.getName());
//        paper.setCourseNo(course.getCode());
//        paper.setCreateTime(CommonUtils.getCurDateTime());
//        paper.setPaperDetailCount(paperDetails.size());
//        paper.setUnitCount(unitCount);
//        return paper;
//    }
//
//    //初始化大题对象
//    public PaperDetail initPaperDetail(List<TestQuestion> dQuestions, Paper paper) {
//        //定义大题分数
//        double dTotal = 0;
//        PaperDetail paperDetail = new PaperDetail();
//        if (dQuestions != null && dQuestions.size() > 0) {
//            for (TestQuestion question : dQuestions) {
//                dTotal = dTotal + question.getScore();
//            }
//            paperDetail.setPaper(paper);
//            paperDetail.setNumber(dQuestions.get(0).getdNumber());
//            paperDetail.setName(dQuestions.get(0).getdName());
//            paperDetail.setScore(dTotal);
//            paperDetail.setUnitCount(dQuestions.size());
//            paperDetail.setCreator("wei");
//            paperDetail.setCreateTime(CommonUtils.getCurDateTime());
//        }
//        return paperDetail;
//    }
//
//    //初始化试题对象
//    public Question initQuestion(TestQuestionInfo testQuestionInfo, TestQuestion testQuestion, Course course, Map<Question, QuestionPkgPath> map, double difficulty) throws Exception {
//        Question question = new Question();
//        question.setCreateTime(CommonUtils.getCurDateTime());
//        question.setScore(testQuestion.getScore());
//        question.setCourse(course);
//        question.setOrgId(course.getOrgId());
//        question.setHasAudio(false);
//        question.setDifficulty("中");
//        question.setDifficultyDegree(0.5);
//        question.setPublicity(true);
//        //按试题分类初始化题干，答案，选项
//        initQuestionInfo(question, testQuestionInfo, map, testQuestion);
//        difficulty = question.getScore() * 0.5 + difficulty;
//        return question;
//    }
//
//    //初始化题干，答案，选项,资源
//    public void initQuestionInfo(Question question, TestQuestionInfo testQuestionInfo, Map<Question, QuestionPkgPath> map, TestQuestion testQuestion) throws Exception {
//        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
//        //选词填空
//        if (testQuestionInfo.getQuestionType() == 14) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            if (bodyImgList != null && bodyImgList.size() > 0) {
//                int number = 1;
//                for (Map.Entry<String, String> entry : bodyImgList.entrySet()) {
//                    if (StringUtils.isBlank(entry.getValue())) {
//                        quesBody = quesBody.replace(entry.getKey(), "##" + number + "##");
//                        number++;
//                    } else {
//                        quesBody = quesBody.replace(entry.getKey(), entry.getValue());
//                    }
//                }
//            }
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.NESTED_ANSWER_QUESTION);
//            List<Question> subQuestions = initSubQuestionsByOther(testQuestionInfo, wordMLPackage);
//            question.setSubQuestions(subQuestions);
//        }
//        //阅读理解题
//        if (testQuestionInfo.getQuestionType() == 42) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            //替换、补全Img标签
//            quesBody = replaceImg(bodyImgList, quesBody);
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.NESTED_ANSWER_QUESTION);
//            List<Question> subQues = new ArrayList<Question>();
//            //查询所有子题
//            List<TestQuestion> subQuestions = querySubQuestions(testQuestion.getId());
//            if (subQuestions != null && subQuestions.size() > 0) {
//                for (TestQuestion testQuestion2 : subQuestions) {
//                    TestQuestionInfo testQuestionInfo2 = JsonUtil.fromJson(testQuestion2.getJson(), TestQuestionInfo.class);
//                    Question subQuestion = new Question();
//                    subQuestion.setDifficulty("中");
//                    subQuestion.setDifficultyDegree(0.5);
//                    subQuestion.setPublicity(true);
//                    //按试题分类初始化题干，答案，选项
//                    initQuestionInfo(subQuestion, testQuestionInfo2, map, testQuestion2);
//                    subQues.add(subQuestion);
//                }
//                question.setSubQuestions(subQues);
//            }
//        }
//        //完形填空题
//        if (testQuestionInfo.getQuestionType() == 15) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            if (bodyImgList != null && bodyImgList.size() > 0) {
//                int number = 1;
//                for (Map.Entry<String, String> entry : bodyImgList.entrySet()) {
//                    if (StringUtils.isBlank(entry.getValue())) {
//                        quesBody = quesBody.replace(entry.getKey(), "##" + number + "##");
//                        number++;
//                    } else {
//                        quesBody = quesBody.replace(entry.getKey(), entry.getValue());
//                    }
//                }
//            }
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.NESTED_ANSWER_QUESTION);
//            //获取所有选项
//            List<TestFillAnswer> blanks = testQuestionInfo.getBlanks();
//            List<Question> subQuestions = initSubQuestions(blanks, wordMLPackage);
//            question.setSubQuestions(subQuestions);
//        }
//        //问答题  、 名词解释
//        if (testQuestionInfo.getQuestionType() == 60 || testQuestionInfo.getQuestionType() == 69) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            //替换、补全Img标签
//            quesBody = replaceImg(bodyImgList, quesBody);
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.TEXT_ANSWER_QUESTION);
//            String answer = testQuestionInfo.getAnswer();
//            //去掉<span>标签中的lang
//            answer = replaceLang(answer);
//            //获取img标签
//            LinkedHashMap<String, String> answerImgList = imgList(answer);
//            //替换、补全Img标签
//            answer = replaceImg(answerImgList, answer);
//            question.setQuesAnswer("<p>" + answer + "</p>");
//            question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesAnswer())));
//        }
//        //单选题
//        if (testQuestionInfo.getQuestionType() == 11) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            //替换、补全Img标签
//            quesBody = replaceImg(bodyImgList, quesBody);
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.SINGLE_ANSWER_QUESTION);
//            List<QuesOption> quesOptions = new ArrayList<QuesOption>();
//            List<TestOption> options = testQuestionInfo.getOptions();
//            Integer answerNumber = null;
//            if (options != null && options.size() > 0) {
//                int number = 1;
//                short isCorrect = 0;
//                for (TestOption testOption : options) {
//                    if (testOption.getID().equals(testQuestionInfo.getAnswerId())) {
//                        isCorrect = 1;
//                        answerNumber = number;
//                    } else {
//                        isCorrect = 0;
//                    }
//                    QuesOption quesOption = initQuesOption(testOption, number, isCorrect, wordMLPackage);
//                    quesOptions.add(quesOption);
//                    number++;
//                }
//            }
//            question.setQuesOptions(quesOptions);
//            if (answerNumber != null) {
//                char c1 = (char) (answerNumber + 64);
//                question.setQuesAnswer(String.valueOf(c1));
//                question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + question.getQuesAnswer() + "</p>")));
//            }
//        }
//        //多选题
//        if (testQuestionInfo.getQuestionType() == 12) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            //替换、补全Img标签
//            quesBody = replaceImg(bodyImgList, quesBody);
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.MULTIPLE_ANSWER_QUESTION);
//            List<QuesOption> quesOptions = new ArrayList<QuesOption>();
//            List<TestOption> options = testQuestionInfo.getOptions();
//            List<Integer> answerNumbers = new ArrayList<Integer>();
//            if (options != null && options.size() > 0) {
//                int number = 1;
//                short isCorrect = 0;
//                for (TestOption testOption : options) {
//                    if (testQuestionInfo.getAnswers().contains(testOption.getID())) {
//                        isCorrect = 1;
//                        answerNumbers.add(number);
//                    } else {
//                        isCorrect = 0;
//                    }
//                    QuesOption quesOption = initQuesOption(testOption, number, isCorrect, wordMLPackage);
//                    quesOptions.add(quesOption);
//                    number++;
//                }
//            }
//            question.setQuesOptions(quesOptions);
//            if (answerNumbers != null && answerNumbers.size() > 0) {
//                String answers = "";
//                for (Integer number : answerNumbers) {
//                    char c1 = (char) (number + 64);
//                    if (StringUtils.isBlank(answers)) {
//                        answers = String.valueOf(c1);
//                    } else {
//                        answers = answers + "," + String.valueOf(c1);
//                    }
//                }
//                question.setQuesAnswer(answers);
//                question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + question.getQuesAnswer() + "</p>")));
//            }
//        }
//        //填空题 ###
//        if (testQuestionInfo.getQuestionType() == 13) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> imgList = imgList(quesBody);
//            if (imgList != null && imgList.size() > 0) {
//                for (Map.Entry<String, String> entry : imgList.entrySet()) {
//                    if (StringUtils.isBlank(entry.getValue())) {
//                        quesBody = quesBody.replace(entry.getKey(), "(###)");
//                    } else {
//                        quesBody = quesBody.replace(entry.getKey(), entry.getValue());
//                    }
//                }
//            }
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.FILL_BLANK_QUESTION);
//            String answer = "";
//            List<TestFillAnswer> blanks = testQuestionInfo.getBlanks();
//            if (blanks != null && blanks.size() > 0) {
//                for (TestFillAnswer testFillAnswer : blanks) {
//                    List<TestFillContent> answers = testFillAnswer.getAnswers();
//                    if (answers != null && answers.size() > 0) {
//                        for (TestFillContent testFillContent : answers) {
//                            if (StringUtils.isBlank(answer)) {
//                                answer = testFillContent.getContent();
//                            } else {
//                                answer = answer + "##" + testFillContent.getContent();
//                            }
//                        }
//                    }
//                }
//                //去掉<span>标签中的lang
//                answer = replaceLang(answer);
//                //获取img标签
//                LinkedHashMap<String, String> answerImgList = imgList(answer);
//                //替换、补全Img标签
//                answer = replaceImg(answerImgList, answer);
//                question.setQuesAnswer("<p>" + answer + "</p>");
//                question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesAnswer())));
//            }
//        }
//        //判断题
//        if (testQuestionInfo.getQuestionType() == 20) {
//            String quesBody = testQuestionInfo.getQuestionHTML();
//            //去掉<span>标签中的lang
//            quesBody = replaceLang(quesBody);
//            //获取img标签
//            LinkedHashMap<String, String> bodyImgList = imgList(quesBody);
//            //替换、补全Img标签
//            quesBody = replaceImg(bodyImgList, quesBody);
//            question.setQuesBody("<p>" + quesBody + "</p>");
//            question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(question.getQuesBody())));
//            question.setQuestionType(QuesStructType.BOOL_ANSWER_QUESTION);
//            String answer = testQuestionInfo.getAnswer();
//            if (answer.equals("false")) {
//                answer = "错误";
//            } else {
//                answer = "正确";
//            }
//            question.setQuesAnswer(answer);
//            question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + question.getQuesAnswer() + "</p>")));
//        }
//        byte[] pkgByte = DocxProcessUtil.getPkgByte(wordMLPackage);
//        QuestionPkgPath quesPkgPath = new QuestionPkgPath(pkgByte);
//        map.put(question, quesPkgPath);
//    }
//
//    //初始化选项
//    public QuesOption initQuesOption(TestOption testOption, int number, short isCorrect, WordprocessingMLPackage wordMLPackage) throws Exception {
//        QuesOption quesOption = new QuesOption();
//        quesOption.setNumber(String.valueOf(number));
//        quesOption.setIsCorrect(isCorrect);
//        String optionBody = testOption.getOptionHTML();
//        //去掉<span>标签中的lang
//        optionBody = replaceLang(optionBody);
//        //获取img标签
//        LinkedHashMap<String, String> optionBodyImgList = imgList(optionBody);
//        //替换、补全Img标签
//        optionBody = replaceImg(optionBodyImgList, optionBody);
//        quesOption.setOptionBody("<p>" + optionBody + "</p>");
//        quesOption.setOptionBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml(quesOption.getOptionBody())));
//        return quesOption;
//    }
//
//    //完形填空初始化所有子题
//    public List<Question> initSubQuestions(List<TestFillAnswer> blanks, WordprocessingMLPackage wordMLPackage) throws Exception {
//        List<Question> subQuestions = new ArrayList<Question>();
//        if (blanks != null && blanks.size() > 0) {
//            //int number = 1;
//            for (TestFillAnswer testFillAnswer : blanks) {
//                Question question = new Question();
//                question.setId(IdUtils.uuid());
//                question.setQuestionType(QuesStructType.SINGLE_ANSWER_QUESTION);
//                question.setDifficulty("中");
//                question.setDifficultyDegree(0.5);
//                question.setPublicity(true);
//                question.setQuesBody("______");
//                question.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + question.getQuesBody() + "</p>")));
//                TestOptionGroup optionGroup = testFillAnswer.getOptionGroup();
//                List<QuesOption> quesOptions = new ArrayList<QuesOption>();
//                List<TestOption> subOptions = optionGroup.getOptions();
//                Integer answerNumber = null;
//                if (subOptions != null && subOptions.size() > 0) {
//                    int subNumber = 1;
//                    short isCorrect = 0;
//                    for (TestOption subOption : subOptions) {
//                        if (testFillAnswer.getAnswerId().equals(subOption.getID())) {
//                            isCorrect = 1;
//                            answerNumber = subNumber;
//                        } else {
//                            isCorrect = 0;
//                        }
//                        QuesOption quesOption = initQuesOption(subOption, subNumber, isCorrect, wordMLPackage);
//                        quesOptions.add(quesOption);
//                        subNumber++;
//                    }
//                }
//                question.setQuesOptions(quesOptions);
//                if (answerNumber != null) {
//                    char c1 = (char) (answerNumber + 64);
//                    question.setQuesAnswer(String.valueOf(c1));
//                    question.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + question.getQuesAnswer() + "</p>")));
//                }
//                subQuestions.add(question);
//                //number++;
//            }
//        }
//        return subQuestions;
//    }
//
//    public List<Question> initSubQuestionsByOther(TestQuestionInfo testQuestionInfo, WordprocessingMLPackage wordMLPackage) throws Exception {
//        List<Question> subQuestions = new ArrayList<Question>();
//        if (testQuestionInfo.getBlanks() != null && testQuestionInfo.getBlanks().size() > 0) {
//            List<TestFillAnswer> blanks = testQuestionInfo.getBlanks();
//            for (TestFillAnswer testFillAnswer : blanks) {
//                Question subQuestion = new Question();
//                subQuestion.setId(IdUtils.uuid());
//                subQuestion.setQuestionType(QuesStructType.SINGLE_ANSWER_QUESTION);
//                subQuestion.setDifficulty("中");
//                subQuestion.setDifficultyDegree(0.5);
//                subQuestion.setPublicity(true);
//                subQuestion.setQuesBody("______");
//                subQuestion.setQuesBodyWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + subQuestion.getQuesBody() + "</p>")));
//                List<QuesOption> quesOptions = new ArrayList<QuesOption>();
//                TestOptionGroup optionGroup = testQuestionInfo.getOptionGroup();
//                List<TestOption> options = optionGroup.getOptions();
//                Integer answerNumber = null;
//                if (options != null && options.size() > 0) {
//                    for (TestOption subOption : options) {
//                        int subNumber = 1;
//                        short isCorrect = 0;
//                        if (testFillAnswer.getAnswerId().equals(subOption.getID())) {
//                            isCorrect = 1;
//                            answerNumber = subNumber;
//                        } else {
//                            isCorrect = 0;
//                        }
//                        QuesOption quesOption = initQuesOption(subOption, subNumber, isCorrect, wordMLPackage);
//                        quesOptions.add(quesOption);
//                        subNumber++;
//                    }
//                }
//                subQuestion.setQuesOptions(quesOptions);
//                if (answerNumber != null) {
//                    char c1 = (char) (answerNumber + 64);
//                    subQuestion.setQuesAnswer(String.valueOf(c1));
//                    subQuestion.setQuesAnswerWord(DocxProcessUtil.html2Docx(wordMLPackage, CommonUtils.formatHtml("<p>" + subQuestion.getQuesAnswer() + "</p>")));
//                }
//                subQuestions.add(subQuestion);
//            }
//        }
//        return subQuestions;
//    }
//
//    //初始化小题对象
//    public PaperDetailUnit initPaperDetailUnit(Paper paper, PaperDetail paperDetail, Question question, TestQuestion testQuestion) {
//        PaperDetailUnit paperDetailUnit = new PaperDetailUnit();
//        paperDetailUnit.setPaper(paper);
//        paperDetailUnit.setNumber(testQuestion.getNumber());
//        paperDetailUnit.setScore(testQuestion.getScore());
//        paperDetailUnit.setPaperDetail(paperDetail);
//        paperDetailUnit.setQuestionType(question.getQuestionType());
//        paperDetailUnit.setCreator("wei");
//        paperDetailUnit.setCreateTime(CommonUtils.getCurDateTime());
//        paperDetailUnit.setQuestion(question);
//        return paperDetailUnit;
//    }
//
//    //将大题和小题分类
//    public Map<Integer, List<TestQuestion>> sortPaperDetail(List<TestQuestion> questions) {
//        if (questions != null && questions.size() > 0) {
//            Map<Integer, List<TestQuestion>> map = new HashMap<>();
//            List<TestQuestion> sortQuestions = null;
//            for (TestQuestion testQuestion : questions) {
//                if (map.get(testQuestion.getdNumber()) == null) {
//                    sortQuestions = new ArrayList<TestQuestion>();
//                    sortQuestions.add(testQuestion);
//                } else {
//                    sortQuestions = map.get(testQuestion.getdNumber());
//                    sortQuestions.add(testQuestion);
//                }
//                map.put(testQuestion.getdNumber(), sortQuestions);
//            }
//            return map;
//        }
//        return null;
//    }
//
//    //去除图片中有下划线的图片，并补全图片下载地址
//    public LinkedHashMap<String, String> imgList(String str) throws Exception {
//        LinkedHashMap<String, String> map = new LinkedHashMap<>();
//        //获取img标签
//        List<String> srcList = ImgDataUtils.parseImages(str);
//        if (srcList != null && srcList.size() > 0) {
//            for (String img : srcList) {
//                if (img.contains("/Shared/UnderLine")) {
//                    map.put(img, "");
//                } else {
//                    List<String> parseImageSrc = ImgDataUtils.parseImageSrc(img);
//                    if (parseImageSrc != null && parseImageSrc.size() > 0) {
//                        String src = parseImageSrc.get(0);
//                        String url = regexCH(src);
//                        String picture = "http://ncexam.cug.edu.cn" + url;
//                        String base64 = ImgDataUtils.loadImageToBase64(picture);
//                        if (base64 == null) {
//                            throw new Exception("图片下载失败" + picture);
//                        } else {
//                            if (src.contains("jpg")) {
//                                map.put(img, img.replace(src, "data:image/jpg;base64," + base64));
//                            } else {
//                                map.put(img, img.replace(src, "data:image/png;base64," + base64));
//                            }
//                        }
//                    }
//                }
//            }
//            return map;
//        }
//        return null;
//    }
//
//    //去除<span> 标签中lang = EN - US
//    public String replaceLang(String str) {
//        if (str.contains("lang=EN-US")) {
//            str = str.replace("lang=EN-US", "");
//        }
//        if (str.contains("alt=\"\u0012\"")) {
//            str = str.replace("alt=\"\u0012\"", "alt=\"\"");
//        }
//        if (str.contains("alt=\"&#18;\"")) {
//            str = str.replace("alt=\"&#18;\"", "alt=\"\"");
//        }
//        if (str.contains("<img width=\"12\" height=\"13\" src=\"管理会计%20专升本.files/image001.png\">")) {
//            str = str.replace("<img width=\"12\" height=\"13\" src=\"管理会计%20专升本.files/image001.png\">", "");
//        }
//        if (str.contains("<img width=\"14\" height=\"2\" src=\"管理会计%20专升本.files/image002.png\">")) {
//            str = str.replace("<img width=\"14\" height=\"2\" src=\"管理会计%20专升本.files/image002.png\">", "");
//        }
//        if (str.contains("<img alt=\"\" src=\"/UploadFiles/李波/Images/OnlineExam20140211123953.bmp\" />")) {
//            str = str.replace("<img alt=\"\" src=\"/UploadFiles/李波/Images/OnlineExam20140211123953.bmp\" />", "图片缺失");
//        }
//        if (str.contains("<img width=\"52\" height=\"45\" src=\"机械制造工艺学%20专升本%20综合测试2.files/image002.png\">")) {
//            str = str.replace("<img width=\"52\" height=\"45\" src=\"机械制造工艺学%20专升本%20综合测试2.files/image002.png\">", "图片缺失");
//        }
//        if (str.contains("&lt;")) {
//            str = str.replace("&lt;", "<");
//        }
//        if (str.contains("&gt;")) {
//            str = str.replace("&gt;", ">");
//        }
//        return str;
//    }
//
//    //替换img标签，补全图片
//    public String replaceImg(LinkedHashMap<String, String> imgList, String str) {
//        if (imgList != null && imgList.size() > 0) {
//            for (Map.Entry<String, String> entry : imgList.entrySet()) {
//                if (StringUtils.isBlank(entry.getValue())) {
//                    str = str.replace(entry.getKey(), "___");
//                } else {
//                    str = str.replace(entry.getKey(), entry.getValue());
//                }
//            }
//        }
//        return str;
//    }
//
//    //將url路徑中的中文转码
//    public String regexCH(String str) throws UnsupportedEncodingException {
//        StringBuffer s = new StringBuffer();
//        Pattern pat = Pattern.compile("[\u4E00-\u9FA5]");
//        Matcher mat = pat.matcher(str);
//        while (mat.find()) {
//            s.append(mat.group());
//        }
//        String url = java.net.URLEncoder.encode(s.toString(), "utf-8");
//        return str.replace(s.toString(), url);
//    }
//
//}
