|
@@ -26,429 +26,433 @@ import cn.com.qmth.ex.config.Param;
|
|
import cn.com.qmth.ex.multithread.Consumer;
|
|
import cn.com.qmth.ex.multithread.Consumer;
|
|
|
|
|
|
public class MyConsumer extends Consumer<PaperExportDto> {
|
|
public class MyConsumer extends Consumer<PaperExportDto> {
|
|
- private static String[] sucStr = new String[] { "对", "正确", "√", "是", "True" };
|
|
|
|
- private static String[] errStr = new String[] { "错", "错误", "×", "不正确", "否", "False" };
|
|
|
|
-// private static String paperSuff = "(231205)";
|
|
|
|
- private int maxqc = 200;
|
|
|
|
-
|
|
|
|
- private Pattern imgPat = Pattern.compile("<img[^<]+src=\"([^<\"]+)\"[^<]*>");
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void consume(PaperExportDto dto) {
|
|
|
|
- Connection connect = null;
|
|
|
|
- File sub = new File(Param.dataDir+"/paper/" + dto.getCourseCode() + "/");
|
|
|
|
- sub.mkdir();
|
|
|
|
- try {
|
|
|
|
- Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
|
-
|
|
|
|
- String url = "jdbc:mysql://localhost:3306/" + Param.dbName + "?serverTimezone=GMT%2B8";
|
|
|
|
-
|
|
|
|
- String user = Param.dbUser;
|
|
|
|
-
|
|
|
|
- String password = Param.dbPass;
|
|
|
|
- connect = DriverManager.getConnection(url, user, password);
|
|
|
|
- exportPaper(connect, dto);
|
|
|
|
- ExportPaperByCourseCode.addDisposeCount();
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- } finally {
|
|
|
|
- if (connect != null) {
|
|
|
|
- try {
|
|
|
|
- connect.close();
|
|
|
|
- } catch (SQLException e) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private List<KdQuestion> getQuestion(Connection connect, String courseCode) throws SQLException, IOException {
|
|
|
|
- List<KdQuestion> qs = new ArrayList<>();
|
|
|
|
- PreparedStatement preState = null;
|
|
|
|
- ResultSet resultSet = null;
|
|
|
|
- try {
|
|
|
|
- String sql = " select q.id,q.question_type,q.topic,q.difficulty,q.answer "
|
|
|
|
- + " from wq_question_bank_subject t " + " left join wq_subject sub on t.subject_id=sub.id"
|
|
|
|
- + " left join wq_question_bank b on t.question_bank_id=b.id "
|
|
|
|
- + " left join wq_question_question_bank f on t.question_bank_id=f.question_bank_id "
|
|
|
|
- + " left join wq_question q on f.question_id=q.id " + " where sub.subject_code='" + courseCode
|
|
|
|
- + "' and b.rent_id=811 and b.is_deleted=0 " + " and q.is_deleted=0 and q.rent_id=811 ";
|
|
|
|
-
|
|
|
|
- preState = connect.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
- resultSet = preState.executeQuery();
|
|
|
|
-
|
|
|
|
- while (resultSet.next()) {
|
|
|
|
- KdQuestion q = new KdQuestion();
|
|
|
|
- q.setScore(1.0);
|
|
|
|
- q.setHaveAudio(false);
|
|
|
|
- q.setId(resultSet.getLong("id"));
|
|
|
|
- q.setBody(disposeImg(resultSet.getString("topic"), courseCode));
|
|
|
|
- q.setQst(YunkaiQuesStructType.getByYunKaiType(resultSet.getInt("question_type")));
|
|
|
|
- q.setQuesStructType(QuesStructType.getQuesStructTypeById(q.getQst().getType()));
|
|
|
|
- YunkaiDifficulty yd = YunkaiDifficulty.getByYunKaiType(resultSet.getInt("difficulty"));
|
|
|
|
- q.setDifficultyDegree(yd == null ? 0.5 : yd.getType());
|
|
|
|
-// if (!q.getQst().isObjective()) {
|
|
|
|
-// continue;
|
|
|
|
-// }
|
|
|
|
-// q.setDifficulty(YunkaiDifficulty.ZHONGDENG);
|
|
|
|
- q.setAnswer(disposeImg(resultSet.getString("answer"), courseCode));
|
|
|
|
- q.setObjective(q.getQst().isObjective());
|
|
|
|
- qs.add(q);
|
|
|
|
- }
|
|
|
|
- return qs;
|
|
|
|
- } finally {
|
|
|
|
- if (resultSet != null) {
|
|
|
|
- resultSet.close();
|
|
|
|
- }
|
|
|
|
- if (preState != null) {
|
|
|
|
- preState.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private String disposeImg(String str, String courseCode) {
|
|
|
|
- if (StringUtils.isBlank(str)) {
|
|
|
|
- return str;
|
|
|
|
- }
|
|
|
|
- Matcher matcher = imgPat.matcher(str);
|
|
|
|
- Map<String, String> srcMap = new HashMap<>();
|
|
|
|
- while (matcher.find()) {
|
|
|
|
- String imgSrc = matcher.group(1).trim();
|
|
|
|
- if (imgSrc.toLowerCase().trim().startsWith("http")) {
|
|
|
|
- if (srcMap.get(imgSrc) == null) {
|
|
|
|
- String suff = imgSrc.substring(imgSrc.lastIndexOf(".") + 1).toLowerCase();
|
|
|
|
- File img = new File(Param.dataDir+"/paper/" + courseCode + "/" + UUID.randomUUID() + "." + suff);
|
|
|
|
- FileUtil.saveUrlAs(imgSrc, img.getAbsolutePath());
|
|
|
|
- String base64 = FileUtil.fileToBase64Src(img);
|
|
|
|
- img.delete();
|
|
|
|
- srcMap.put(imgSrc, base64);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- for (String imgSrc : srcMap.keySet()) {
|
|
|
|
- str = str.replaceAll(imgSrc, srcMap.get(imgSrc));
|
|
|
|
- }
|
|
|
|
- return str;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private List<QuestionProp> getQuestionProp(Connection connect, List<KdQuestion> qs) {
|
|
|
|
- List<QuestionProp> as = new ArrayList<>();
|
|
|
|
- List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
|
|
|
|
- String idsstr = StringUtils.join(ids, ",");
|
|
|
|
- PreparedStatement preState = null;
|
|
|
|
- ResultSet resultSet = null;
|
|
|
|
- try {
|
|
|
|
- String sql = " select t.knowledge_system_id,t.question_id " + " from wq_question_knowledge_system t "
|
|
|
|
- + " where t.question_id in (" + idsstr + ") ";
|
|
|
|
- preState = connect.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
- resultSet = preState.executeQuery();
|
|
|
|
-
|
|
|
|
- while (resultSet.next()) {
|
|
|
|
- QuestionProp a = new QuestionProp();
|
|
|
|
- a.setKnowledgeId(resultSet.getLong("knowledge_system_id"));
|
|
|
|
- a.setQuestionId(resultSet.getLong("question_id"));
|
|
|
|
- as.add(a);
|
|
|
|
- }
|
|
|
|
- return as;
|
|
|
|
- } catch (Exception e1) {
|
|
|
|
- throw new RuntimeException(e1);
|
|
|
|
- } finally {
|
|
|
|
- if (resultSet != null) {
|
|
|
|
- try {
|
|
|
|
- resultSet.close();
|
|
|
|
- } catch (SQLException e1) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (preState != null) {
|
|
|
|
- try {
|
|
|
|
- preState.close();
|
|
|
|
- } catch (SQLException e1) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void fillProp(Connection connect, List<KdQuestion> qs) {
|
|
|
|
- List<QuestionProp> qps = new BatchGetDataUtil<QuestionProp, KdQuestion>() {
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- protected List<QuestionProp> getData(List<KdQuestion> paramList) {
|
|
|
|
- return getQuestionProp(connect, paramList);
|
|
|
|
- }
|
|
|
|
- }.getDataForBatch(qs, 200);
|
|
|
|
- if (CollectionUtils.isEmpty(qps)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- Map<Long, KdQuestion> map = new HashMap<>();
|
|
|
|
- for (KdQuestion q : qs) {
|
|
|
|
- map.put(q.getId(), q);
|
|
|
|
- }
|
|
|
|
- for (QuestionProp qp : qps) {
|
|
|
|
- KdQuestion q = map.get(qp.getQuestionId());
|
|
|
|
- List<Long> qpids = q.getPropIds();
|
|
|
|
- if (qpids == null) {
|
|
|
|
- qpids = new ArrayList<>();
|
|
|
|
- q.setPropIds(qpids);
|
|
|
|
- }
|
|
|
|
- qpids.add(qp.getKnowledgeId());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void fillAnswer(Connection connect, List<KdQuestion> qs, String courseCode) {
|
|
|
|
- List<Answer> qps = new BatchGetDataUtil<Answer, KdQuestion>() {
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- protected List<Answer> getData(List<KdQuestion> paramList) {
|
|
|
|
- return getAnswerBatch(connect, paramList, courseCode);
|
|
|
|
- }
|
|
|
|
- }.getDataForBatch(qs, 200);
|
|
|
|
- if (CollectionUtils.isEmpty(qps)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Map<Long, List<Answer>> map = new HashMap<>();
|
|
|
|
- for (Answer qp : qps) {
|
|
|
|
- List<Answer> list = map.get(qp.getQuestionId());
|
|
|
|
- if (list == null) {
|
|
|
|
- list = new ArrayList<>();
|
|
|
|
- map.put(qp.getQuestionId(), list);
|
|
|
|
- }
|
|
|
|
- list.add(qp);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for (KdQuestion q : qs) {
|
|
|
|
- q.setQuesStructType(QuesStructType.getQuesStructTypeById(q.getQst().getType()));
|
|
|
|
- if (YunkaiQuesStructType.DANXUAN.equals(q.getQst())) {
|
|
|
|
- List<KdQuesOption> ops = new ArrayList<>();
|
|
|
|
- q.setOptions(ops);
|
|
|
|
- int num = 0;
|
|
|
|
- for (Answer a : map.get(q.getId())) {
|
|
|
|
- num++;
|
|
|
|
- KdQuesOption op = new KdQuesOption();
|
|
|
|
- ops.add(op);
|
|
|
|
- op.setNumber(num);
|
|
|
|
- op.setBody(a.getBody());
|
|
|
|
- op.setSelect(a.getRight() == 1);
|
|
|
|
- if (op.getSelect()) {
|
|
|
|
- q.setAnswer(getOptionNum(num));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if (YunkaiQuesStructType.DUOXUAN.equals(q.getQst())
|
|
|
|
- || YunkaiQuesStructType.BUDINGXIANG.equals(q.getQst())) {
|
|
|
|
- List<String> as = new ArrayList<>();
|
|
|
|
- List<KdQuesOption> ops = new ArrayList<>();
|
|
|
|
- q.setOptions(ops);
|
|
|
|
- int num = 0;
|
|
|
|
- for (Answer a : map.get(q.getId())) {
|
|
|
|
- num++;
|
|
|
|
- KdQuesOption op = new KdQuesOption();
|
|
|
|
- ops.add(op);
|
|
|
|
- op.setNumber(num);
|
|
|
|
- op.setBody(a.getBody());
|
|
|
|
- op.setSelect(a.getRight() == 1);
|
|
|
|
- if (op.getSelect()) {
|
|
|
|
- as.add(getOptionNum(num));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- q.setAnswer(StringUtils.join(as, ","));
|
|
|
|
- } else if (YunkaiQuesStructType.PANDUAN.equals(q.getQst())) {
|
|
|
|
- for (Answer a : map.get(q.getId())) {
|
|
|
|
- if (a.getRight() == 1) {
|
|
|
|
- q.setAnswer(getBool(a.getBody()));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } else if (YunkaiQuesStructType.TIANKONGTI.equals(q.getQst())) {
|
|
|
|
- q.setBody(relaceQuestionIdx(q.getBody()));
|
|
|
|
- if (map.get(q.getId()) != null) {
|
|
|
|
- List<String> as = new ArrayList<>();
|
|
|
|
- try {
|
|
|
|
- for (Answer a : map.get(q.getId())) {
|
|
|
|
- if (a.getRight() == 1) {
|
|
|
|
- as.add(a.getBody());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw e;
|
|
|
|
- }
|
|
|
|
- q.setAnswer(StringUtils.join(as, "##"));
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- // nothing
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private String relaceQuestionIdx(String str) {
|
|
|
|
- StringBuffer sb = new StringBuffer("");
|
|
|
|
- Pattern pattern = Pattern.compile("__(\\d+)__");
|
|
|
|
-
|
|
|
|
- Matcher matcher = pattern.matcher(str);
|
|
|
|
-
|
|
|
|
- while (matcher.find()) {
|
|
|
|
- matcher.appendReplacement(sb, "###");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (StringUtils.isEmpty(sb.toString())) {
|
|
|
|
- return str;
|
|
|
|
- } else {
|
|
|
|
- matcher.appendTail(sb);
|
|
|
|
- return sb.toString();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 将数字1,2,3,4转化成A,B,C,D
|
|
|
|
- *
|
|
|
|
- * @param number
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private String getOptionNum(int number) {
|
|
|
|
- char optionNum = (char) (64 + number);
|
|
|
|
- return String.valueOf(optionNum);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private String getBool(String val) {
|
|
|
|
- String valid = "错答案:正确";
|
|
|
|
- if (val.contains(valid)) {
|
|
|
|
- return "正确";
|
|
|
|
- }
|
|
|
|
- for (String suc : sucStr) {
|
|
|
|
- if (val.contains(suc)) {
|
|
|
|
- return "正确";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- for (String err : errStr) {
|
|
|
|
- if (val.contains(err)) {
|
|
|
|
- return "错误";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return "正确";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private List<Answer> getAnswerBatch(Connection connect, List<KdQuestion> qs, String courseCode) {
|
|
|
|
- List<Answer> as = new ArrayList<>();
|
|
|
|
- List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
|
|
|
|
- String idsstr = StringUtils.join(ids, ",");
|
|
|
|
- PreparedStatement preState = null;
|
|
|
|
- ResultSet resultSet = null;
|
|
|
|
- try {
|
|
|
|
- String sql = " select t.question_id,t.answer_text,t.is_right " + " from wq_question_answer_item t "
|
|
|
|
- + " where t.question_id in (" + idsstr + ") " + " order by t.question_id,t.sequence ";
|
|
|
|
- preState = connect.prepareStatement(sql);
|
|
|
|
-
|
|
|
|
- resultSet = preState.executeQuery();
|
|
|
|
-
|
|
|
|
- while (resultSet.next()) {
|
|
|
|
- Answer a = new Answer();
|
|
|
|
- a.setBody(disposeImg(resultSet.getString("answer_text"), courseCode));
|
|
|
|
- a.setRight(resultSet.getInt("is_right"));
|
|
|
|
- a.setQuestionId(resultSet.getLong("question_id"));
|
|
|
|
- as.add(a);
|
|
|
|
- }
|
|
|
|
- return as;
|
|
|
|
- } catch (Exception e1) {
|
|
|
|
- throw new RuntimeException(e1);
|
|
|
|
- } finally {
|
|
|
|
- if (resultSet != null) {
|
|
|
|
- try {
|
|
|
|
- resultSet.close();
|
|
|
|
- } catch (SQLException e1) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (preState != null) {
|
|
|
|
- try {
|
|
|
|
- preState.close();
|
|
|
|
- } catch (SQLException e1) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void exportPaper(Connection connect, PaperExportDto dto) throws Exception {
|
|
|
|
- List<KdQuestion> qs = getQuestion(connect, dto.getCourseCode());
|
|
|
|
- if (qs.size() == 0) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- fillProp(connect, qs);
|
|
|
|
- fillAnswer(connect, qs, dto.getCourseCode());
|
|
|
|
-
|
|
|
|
- Map<YunkaiQuesStructType, List<KdQuestion>> qmap = new HashMap<>();
|
|
|
|
- for (KdQuestion q : qs) {
|
|
|
|
- if (QuesStructType.SINGLE_ANSWER_QUESTION.equals(q.getQuesStructType())
|
|
|
|
- || QuesStructType.MULTIPLE_ANSWER_QUESTION.equals(q.getQuesStructType())) {
|
|
|
|
- if(CollectionUtils.isEmpty(q.getOptions())) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- if(q.getOptions().size()==1) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- List<KdQuestion> list = qmap.get(q.getQst());
|
|
|
|
- if (list == null) {
|
|
|
|
- list = new ArrayList<>();
|
|
|
|
- qmap.put(q.getQst(), list);
|
|
|
|
- }
|
|
|
|
- list.add(q);
|
|
|
|
- }
|
|
|
|
- for (YunkaiQuesStructType qt : qmap.keySet()) {
|
|
|
|
- createPapers(qmap.get(qt), qt, dto);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void createPapers(List<KdQuestion> qs, YunkaiQuesStructType qt, PaperExportDto dto) {
|
|
|
|
- if (qs == null || qs.size() == 0) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- if (qs.size() <= maxqc) {
|
|
|
|
- createPaper(qs, qt, dto, 1);
|
|
|
|
- } else {
|
|
|
|
- int size = qs.size();
|
|
|
|
- int len = maxqc;
|
|
|
|
- int count = (size + len - 1) / len;
|
|
|
|
-
|
|
|
|
- for (int i = 0; i < count; i++) {
|
|
|
|
- List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
|
|
|
|
- createPaper(subList, qt, dto, i + 1);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void createPaper(List<KdQuestion> qs, YunkaiQuesStructType qt, PaperExportDto dto, int indx) {
|
|
|
|
- if (qs.size() == 0) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- String detailName = qt.getYunKaiDesc();
|
|
|
|
- KdPaper paper = new KdPaper();
|
|
|
|
- paper.setDetailCount(1);
|
|
|
|
- paper.setUnitCount(qs.size());
|
|
|
|
- paper.setTotalScore((double) qs.size());
|
|
|
|
- paper.setName(Param.paperPrefix + detailName + "_" + indx);
|
|
|
|
- paper.setCourseCode(dto.getCourseCode());
|
|
|
|
- List<KdDetail> des = new ArrayList<>();
|
|
|
|
- KdDetail d = new KdDetail();
|
|
|
|
- d.setName(detailName);
|
|
|
|
- d.setNumber(1);
|
|
|
|
- d.setQuestionCount(qs.size());
|
|
|
|
- d.setTotalScore((double) qs.size());
|
|
|
|
- d.setQuestions(qs);
|
|
|
|
- des.add(d);
|
|
|
|
- paper.setDetails(des);
|
|
|
|
- paper.setDetailCount(1);
|
|
|
|
- paper.setUnitCount(qs.size());
|
|
|
|
- File paperdir = new File(Param.dataDir+"/paper/" + dto.getCourseCode() + "/" + qt.getYunKaiType() + "/");
|
|
|
|
- paperdir.mkdirs();
|
|
|
|
- try {
|
|
|
|
- FileUtil.writeFile(paperdir.getAbsolutePath(), "/paper_" + indx + ".json", JSONObject.toJSONString(paper));
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void initResult() {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ private static String[] sucStr = new String[] { "对", "正确", "√", "是", "True" };
|
|
|
|
+
|
|
|
|
+ private static String[] errStr = new String[] { "错", "错误", "×", "不正确", "否", "False" };
|
|
|
|
+
|
|
|
|
+ // private static String paperSuff = "(231205)";
|
|
|
|
+ private int maxqc = 200;
|
|
|
|
+
|
|
|
|
+ private Pattern imgPat = Pattern.compile("<img[^<]+src=\"([^<\"]+)\"[^<]*>");
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void consume(PaperExportDto dto) {
|
|
|
|
+ Connection connect = null;
|
|
|
|
+ File sub = new File(Param.dataDir + "/paper/" + dto.getCourseCode() + "/");
|
|
|
|
+ sub.mkdir();
|
|
|
|
+ try {
|
|
|
|
+ Class.forName("com.mysql.cj.jdbc.Driver");
|
|
|
|
+
|
|
|
|
+ String url = "jdbc:mysql://localhost:3306/" + Param.dbName + "?serverTimezone=GMT%2B8";
|
|
|
|
+
|
|
|
|
+ String user = Param.dbUser;
|
|
|
|
+
|
|
|
|
+ String password = Param.dbPass;
|
|
|
|
+ connect = DriverManager.getConnection(url, user, password);
|
|
|
|
+ exportPaper(connect, dto);
|
|
|
|
+ ExportPaperByCourseCode.addDisposeCount();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (connect != null) {
|
|
|
|
+ try {
|
|
|
|
+ connect.close();
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<KdQuestion> getQuestion(Connection connect, String courseCode) throws SQLException, IOException {
|
|
|
|
+ List<KdQuestion> qs = new ArrayList<>();
|
|
|
|
+ PreparedStatement preState = null;
|
|
|
|
+ ResultSet resultSet = null;
|
|
|
|
+ try {
|
|
|
|
+ String sql = " select q.id,q.question_type,q.topic,q.difficulty,q.answer "
|
|
|
|
+ + " from wq_question_bank_subject t " + " left join wq_subject sub on t.subject_id=sub.id"
|
|
|
|
+ + " left join wq_question_bank b on t.question_bank_id=b.id "
|
|
|
|
+ + " left join wq_question_question_bank f on t.question_bank_id=f.question_bank_id "
|
|
|
|
+ + " left join wq_question q on f.question_id=q.id " + " where sub.subject_code='" + courseCode
|
|
|
|
+ + "' and b.rent_id=811 and b.is_deleted=0 " + " and q.is_deleted=0 and q.rent_id=811 ";
|
|
|
|
+
|
|
|
|
+ preState = connect.prepareStatement(sql);
|
|
|
|
+
|
|
|
|
+ resultSet = preState.executeQuery();
|
|
|
|
+
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
+ KdQuestion q = new KdQuestion();
|
|
|
|
+ q.setScore(1.0);
|
|
|
|
+ q.setHaveAudio(false);
|
|
|
|
+ q.setId(resultSet.getLong("id"));
|
|
|
|
+ q.setBody(disposeImg(resultSet.getString("topic"), courseCode));
|
|
|
|
+ q.setQst(YunkaiQuesStructType.getByYunKaiType(resultSet.getInt("question_type")));
|
|
|
|
+ q.setQuesStructType(QuesStructType.getQuesStructTypeById(q.getQst().getType()));
|
|
|
|
+ YunkaiDifficulty yd = YunkaiDifficulty.getByYunKaiType(resultSet.getInt("difficulty"));
|
|
|
|
+ q.setDifficultyDegree(yd == null ? 0.5 : yd.getType());
|
|
|
|
+ // if (!q.getQst().isObjective()) {
|
|
|
|
+ // continue;
|
|
|
|
+ // }
|
|
|
|
+ // q.setDifficulty(YunkaiDifficulty.ZHONGDENG);
|
|
|
|
+ q.setAnswer(disposeImg(resultSet.getString("answer"), courseCode));
|
|
|
|
+ q.setObjective(q.getQst().isObjective());
|
|
|
|
+ qs.add(q);
|
|
|
|
+ }
|
|
|
|
+ return qs;
|
|
|
|
+ } finally {
|
|
|
|
+ if (resultSet != null) {
|
|
|
|
+ resultSet.close();
|
|
|
|
+ }
|
|
|
|
+ if (preState != null) {
|
|
|
|
+ preState.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String disposeImg(String str, String courseCode) {
|
|
|
|
+ if (StringUtils.isBlank(str)) {
|
|
|
|
+ return str;
|
|
|
|
+ }
|
|
|
|
+ Matcher matcher = imgPat.matcher(str);
|
|
|
|
+ Map<String, String> srcMap = new HashMap<>();
|
|
|
|
+ while (matcher.find()) {
|
|
|
|
+ String imgSrc = matcher.group(1).trim();
|
|
|
|
+ if (imgSrc.toLowerCase().trim().startsWith("http")) {
|
|
|
|
+ if (srcMap.get(imgSrc) == null) {
|
|
|
|
+ String suff = imgSrc.substring(imgSrc.lastIndexOf(".") + 1).toLowerCase();
|
|
|
|
+ File img = new File(Param.dataDir + "/paper/" + courseCode + "/" + UUID.randomUUID() + "." + suff);
|
|
|
|
+ FileUtil.saveUrlAs(imgSrc, img.getAbsolutePath());
|
|
|
|
+ String base64 = FileUtil.fileToBase64Src(img);
|
|
|
|
+ img.delete();
|
|
|
|
+ srcMap.put(imgSrc, base64);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (String imgSrc : srcMap.keySet()) {
|
|
|
|
+ str = str.replaceAll(imgSrc, srcMap.get(imgSrc));
|
|
|
|
+ }
|
|
|
|
+ return str;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<QuestionProp> getQuestionProp(Connection connect, List<KdQuestion> qs) {
|
|
|
|
+ List<QuestionProp> as = new ArrayList<>();
|
|
|
|
+ List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
|
|
|
|
+ String idsstr = StringUtils.join(ids, ",");
|
|
|
|
+ PreparedStatement preState = null;
|
|
|
|
+ ResultSet resultSet = null;
|
|
|
|
+ try {
|
|
|
|
+ String sql = " select t.knowledge_system_id,t.question_id " + " from wq_question_knowledge_system t "
|
|
|
|
+ + " where t.question_id in (" + idsstr + ") ";
|
|
|
|
+ preState = connect.prepareStatement(sql);
|
|
|
|
+
|
|
|
|
+ resultSet = preState.executeQuery();
|
|
|
|
+
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
+ QuestionProp a = new QuestionProp();
|
|
|
|
+ a.setKnowledgeId(resultSet.getLong("knowledge_system_id"));
|
|
|
|
+ a.setQuestionId(resultSet.getLong("question_id"));
|
|
|
|
+ as.add(a);
|
|
|
|
+ }
|
|
|
|
+ return as;
|
|
|
|
+ } catch (Exception e1) {
|
|
|
|
+ throw new RuntimeException(e1);
|
|
|
|
+ } finally {
|
|
|
|
+ if (resultSet != null) {
|
|
|
|
+ try {
|
|
|
|
+ resultSet.close();
|
|
|
|
+ } catch (SQLException e1) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (preState != null) {
|
|
|
|
+ try {
|
|
|
|
+ preState.close();
|
|
|
|
+ } catch (SQLException e1) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void fillProp(Connection connect, List<KdQuestion> qs) {
|
|
|
|
+ List<QuestionProp> qps = new BatchGetDataUtil<QuestionProp, KdQuestion>() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected List<QuestionProp> getData(List<KdQuestion> paramList) {
|
|
|
|
+ return getQuestionProp(connect, paramList);
|
|
|
|
+ }
|
|
|
|
+ }.getDataForBatch(qs, 200);
|
|
|
|
+ if (CollectionUtils.isEmpty(qps)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Map<Long, KdQuestion> map = new HashMap<>();
|
|
|
|
+ for (KdQuestion q : qs) {
|
|
|
|
+ map.put(q.getId(), q);
|
|
|
|
+ }
|
|
|
|
+ for (QuestionProp qp : qps) {
|
|
|
|
+ KdQuestion q = map.get(qp.getQuestionId());
|
|
|
|
+ List<Long> qpids = q.getPropIds();
|
|
|
|
+ if (qpids == null) {
|
|
|
|
+ qpids = new ArrayList<>();
|
|
|
|
+ q.setPropIds(qpids);
|
|
|
|
+ }
|
|
|
|
+ qpids.add(qp.getKnowledgeId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void fillAnswer(Connection connect, List<KdQuestion> qs, String courseCode) {
|
|
|
|
+ List<Answer> qps = new BatchGetDataUtil<Answer, KdQuestion>() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected List<Answer> getData(List<KdQuestion> paramList) {
|
|
|
|
+ return getAnswerBatch(connect, paramList, courseCode);
|
|
|
|
+ }
|
|
|
|
+ }.getDataForBatch(qs, 200);
|
|
|
|
+ if (CollectionUtils.isEmpty(qps)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Map<Long, List<Answer>> map = new HashMap<>();
|
|
|
|
+ for (Answer qp : qps) {
|
|
|
|
+ List<Answer> list = map.get(qp.getQuestionId());
|
|
|
|
+ if (list == null) {
|
|
|
|
+ list = new ArrayList<>();
|
|
|
|
+ map.put(qp.getQuestionId(), list);
|
|
|
|
+ }
|
|
|
|
+ list.add(qp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (KdQuestion q : qs) {
|
|
|
|
+ q.setQuesStructType(QuesStructType.getQuesStructTypeById(q.getQst().getType()));
|
|
|
|
+ if (YunkaiQuesStructType.DANXUAN.equals(q.getQst())) {
|
|
|
|
+ List<KdQuesOption> ops = new ArrayList<>();
|
|
|
|
+ q.setOptions(ops);
|
|
|
|
+ int num = 0;
|
|
|
|
+ for (Answer a : map.get(q.getId())) {
|
|
|
|
+ num++;
|
|
|
|
+ KdQuesOption op = new KdQuesOption();
|
|
|
|
+ ops.add(op);
|
|
|
|
+ op.setNumber(num);
|
|
|
|
+ op.setBody(a.getBody());
|
|
|
|
+ op.setSelect(a.getRight() == 1);
|
|
|
|
+ if (op.getSelect()) {
|
|
|
|
+ q.setAnswer(getOptionNum(num));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (YunkaiQuesStructType.DUOXUAN.equals(q.getQst())
|
|
|
|
+ || YunkaiQuesStructType.BUDINGXIANG.equals(q.getQst())) {
|
|
|
|
+ List<String> as = new ArrayList<>();
|
|
|
|
+ List<KdQuesOption> ops = new ArrayList<>();
|
|
|
|
+ q.setOptions(ops);
|
|
|
|
+ int num = 0;
|
|
|
|
+ for (Answer a : map.get(q.getId())) {
|
|
|
|
+ num++;
|
|
|
|
+ KdQuesOption op = new KdQuesOption();
|
|
|
|
+ ops.add(op);
|
|
|
|
+ op.setNumber(num);
|
|
|
|
+ op.setBody(a.getBody());
|
|
|
|
+ op.setSelect(a.getRight() == 1);
|
|
|
|
+ if (op.getSelect()) {
|
|
|
|
+ as.add(getOptionNum(num));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ q.setAnswer(StringUtils.join(as, ","));
|
|
|
|
+ } else if (YunkaiQuesStructType.PANDUAN.equals(q.getQst())) {
|
|
|
|
+ for (Answer a : map.get(q.getId())) {
|
|
|
|
+ if (a.getRight() == 1) {
|
|
|
|
+ q.setAnswer(getBool(a.getBody()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else if (YunkaiQuesStructType.TIANKONGTI.equals(q.getQst())) {
|
|
|
|
+ q.setBody(relaceQuestionIdx(q.getBody()));
|
|
|
|
+ if (map.get(q.getId()) != null) {
|
|
|
|
+ List<String> as = new ArrayList<>();
|
|
|
|
+ try {
|
|
|
|
+ for (Answer a : map.get(q.getId())) {
|
|
|
|
+ if (a.getRight() == 1) {
|
|
|
|
+ as.add(a.getBody());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw e;
|
|
|
|
+ }
|
|
|
|
+ q.setAnswer(StringUtils.join(as, "##"));
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // nothing
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String relaceQuestionIdx(String str) {
|
|
|
|
+ StringBuffer sb = new StringBuffer("");
|
|
|
|
+ Pattern pattern = Pattern.compile("__(\\d+)__");
|
|
|
|
+
|
|
|
|
+ Matcher matcher = pattern.matcher(str);
|
|
|
|
+
|
|
|
|
+ while (matcher.find()) {
|
|
|
|
+ matcher.appendReplacement(sb, "###");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isEmpty(sb.toString())) {
|
|
|
|
+ return str;
|
|
|
|
+ } else {
|
|
|
|
+ matcher.appendTail(sb);
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将数字1,2,3,4转化成A,B,C,D
|
|
|
|
+ *
|
|
|
|
+ * @param number
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String getOptionNum(int number) {
|
|
|
|
+ char optionNum = (char) (64 + number);
|
|
|
|
+ return String.valueOf(optionNum);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getBool(String val) {
|
|
|
|
+ String valid = "错答案:正确";
|
|
|
|
+ if (val.contains(valid)) {
|
|
|
|
+ return "正确";
|
|
|
|
+ }
|
|
|
|
+ for (String suc : sucStr) {
|
|
|
|
+ if (val.contains(suc)) {
|
|
|
|
+ return "正确";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (String err : errStr) {
|
|
|
|
+ if (val.contains(err)) {
|
|
|
|
+ return "错误";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "正确";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<Answer> getAnswerBatch(Connection connect, List<KdQuestion> qs, String courseCode) {
|
|
|
|
+ List<Answer> as = new ArrayList<>();
|
|
|
|
+ List<Long> ids = qs.stream().map(e -> e.getId()).collect(Collectors.toList());
|
|
|
|
+ String idsstr = StringUtils.join(ids, ",");
|
|
|
|
+ PreparedStatement preState = null;
|
|
|
|
+ ResultSet resultSet = null;
|
|
|
|
+ try {
|
|
|
|
+ String sql = " select t.question_id,t.answer_text,t.is_right " + " from wq_question_answer_item t "
|
|
|
|
+ + " where t.question_id in (" + idsstr + ") " + " order by t.question_id,t.sequence ";
|
|
|
|
+ preState = connect.prepareStatement(sql);
|
|
|
|
+
|
|
|
|
+ resultSet = preState.executeQuery();
|
|
|
|
+
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
+ Answer a = new Answer();
|
|
|
|
+ a.setBody(disposeImg(resultSet.getString("answer_text"), courseCode));
|
|
|
|
+ a.setRight(resultSet.getInt("is_right"));
|
|
|
|
+ a.setQuestionId(resultSet.getLong("question_id"));
|
|
|
|
+ as.add(a);
|
|
|
|
+ }
|
|
|
|
+ return as;
|
|
|
|
+ } catch (Exception e1) {
|
|
|
|
+ throw new RuntimeException(e1);
|
|
|
|
+ } finally {
|
|
|
|
+ if (resultSet != null) {
|
|
|
|
+ try {
|
|
|
|
+ resultSet.close();
|
|
|
|
+ } catch (SQLException e1) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (preState != null) {
|
|
|
|
+ try {
|
|
|
|
+ preState.close();
|
|
|
|
+ } catch (SQLException e1) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void exportPaper(Connection connect, PaperExportDto dto) throws Exception {
|
|
|
|
+ List<KdQuestion> qs = getQuestion(connect, dto.getCourseCode());
|
|
|
|
+ if (qs.size() == 0) {
|
|
|
|
+ ExportPaperByCourseCode.addNoQues(dto.getCourseCode());
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ fillProp(connect, qs);
|
|
|
|
+ fillAnswer(connect, qs, dto.getCourseCode());
|
|
|
|
+
|
|
|
|
+ Map<YunkaiQuesStructType, List<KdQuestion>> qmap = new HashMap<>();
|
|
|
|
+ for (KdQuestion q : qs) {
|
|
|
|
+ if (QuesStructType.SINGLE_ANSWER_QUESTION.equals(q.getQuesStructType())
|
|
|
|
+ || QuesStructType.MULTIPLE_ANSWER_QUESTION.equals(q.getQuesStructType())) {
|
|
|
|
+ if (CollectionUtils.isEmpty(q.getOptions())) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (q.getOptions().size() == 1) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ List<KdQuestion> list = qmap.get(q.getQst());
|
|
|
|
+ if (list == null) {
|
|
|
|
+ list = new ArrayList<>();
|
|
|
|
+ qmap.put(q.getQst(), list);
|
|
|
|
+ }
|
|
|
|
+ list.add(q);
|
|
|
|
+ }
|
|
|
|
+ for (YunkaiQuesStructType qt : qmap.keySet()) {
|
|
|
|
+ createPapers(qmap.get(qt), qt, dto);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createPapers(List<KdQuestion> qs, YunkaiQuesStructType qt, PaperExportDto dto) {
|
|
|
|
+ if (qs == null || qs.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (qs.size() <= maxqc) {
|
|
|
|
+ createPaper(qs, qt, dto, 1);
|
|
|
|
+ } else {
|
|
|
|
+ int size = qs.size();
|
|
|
|
+ int len = maxqc;
|
|
|
|
+ int count = (size + len - 1) / len;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
|
+ List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
|
|
|
|
+ createPaper(subList, qt, dto, i + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void createPaper(List<KdQuestion> qs, YunkaiQuesStructType qt, PaperExportDto dto, int indx) {
|
|
|
|
+ if (qs.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String detailName = qt.getYunKaiDesc();
|
|
|
|
+ KdPaper paper = new KdPaper();
|
|
|
|
+ paper.setDetailCount(1);
|
|
|
|
+ paper.setUnitCount(qs.size());
|
|
|
|
+ paper.setTotalScore((double) qs.size());
|
|
|
|
+ paper.setName(Param.paperPrefix + detailName + "_" + indx);
|
|
|
|
+ paper.setCourseCode(dto.getCourseCode());
|
|
|
|
+ List<KdDetail> des = new ArrayList<>();
|
|
|
|
+ KdDetail d = new KdDetail();
|
|
|
|
+ d.setName(detailName);
|
|
|
|
+ d.setNumber(1);
|
|
|
|
+ d.setQuestionCount(qs.size());
|
|
|
|
+ d.setTotalScore((double) qs.size());
|
|
|
|
+ d.setQuestions(qs);
|
|
|
|
+ des.add(d);
|
|
|
|
+ paper.setDetails(des);
|
|
|
|
+ paper.setDetailCount(1);
|
|
|
|
+ paper.setUnitCount(qs.size());
|
|
|
|
+ File paperdir = new File(Param.dataDir + "/paper/" + dto.getCourseCode() + "/" + qt.getYunKaiType() + "/");
|
|
|
|
+ paperdir.mkdirs();
|
|
|
|
+ try {
|
|
|
|
+ FileUtil.writeFile(paperdir.getAbsolutePath(), "/paper_" + indx + ".json", JSONObject.toJSONString(paper));
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void initResult() {
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|