|
@@ -0,0 +1,415 @@
|
|
|
+package cn.com.qmth.dp.examcloud.oe.modules.import_paper_gkd;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+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.jdbc.core.BeanPropertyRowMapper;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.core.RowMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.Course;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.Paper;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.PaperDetail;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.PaperDetailUnit;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.QuesOption;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.QuesTypeName;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.entity.question.Question;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.enums.question.PaperStatus;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.enums.question.PaperType;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.enums.question.QuesStructType;
|
|
|
+import cn.com.qmth.dp.examcloud.oe.util.FileUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 广开学试卷导入
|
|
|
+ *
|
|
|
+ * @author chenken
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ImportPaperGkdService {
|
|
|
+
|
|
|
+ private static String paperSuff = "";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关键参数
|
|
|
+ */
|
|
|
+ private static String rootOrgId = "19371";
|
|
|
+
|
|
|
+ private static String userName = "qmthtiku (qmthtiku)";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关键参数
|
|
|
+ */
|
|
|
+ private static Long userId = 634161L;
|
|
|
+
|
|
|
+ private static int totalcount = 0;
|
|
|
+
|
|
|
+ private static int count = 0;
|
|
|
+
|
|
|
+ private static String filePath = "";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ MongoTemplate mongoTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ public void start() {
|
|
|
+ String basePath = PropertiesUtil.getString("file.export.path");
|
|
|
+ File zfile = new File(basePath + "/kd_export.zip");
|
|
|
+
|
|
|
+ String tempDir = basePath + "/tempDir";
|
|
|
+ String dir = tempDir + File.separator + UUID.randomUUID() + File.separator;
|
|
|
+ try {
|
|
|
+ File dfile = new File(dir);
|
|
|
+ dfile.mkdirs();
|
|
|
+ FileUtil.unZip(dfile, zfile);
|
|
|
+ List<String> courseCodes = readCourseCode(dir);
|
|
|
+ for (String code : courseCodes) {
|
|
|
+ resolvingFile(dfile, code);
|
|
|
+ }
|
|
|
+ } catch (StatusException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("100001", "导入试卷失败:" + e.getMessage(), e);
|
|
|
+ } finally {
|
|
|
+ System.out.println("试卷总数:" + totalcount);
|
|
|
+ System.out.println("导入试卷总数:" + count);
|
|
|
+ FileUtil.deleteFolder(dir);
|
|
|
+ System.out.println("导入完毕!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> readCourseCode(String dir) throws Exception {
|
|
|
+ List<String> list = new ArrayList<String>();
|
|
|
+ XSSFWorkbook wb = null;
|
|
|
+ try {
|
|
|
+ wb = new XSSFWorkbook(dir + "/course.xlsx");
|
|
|
+ XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
+ int rows = sheet.getLastRowNum();
|
|
|
+ for (int i = 1; i <= rows; i++) {
|
|
|
+ XSSFRow row = sheet.getRow(i);
|
|
|
+ String tem = row.getCell(0).getStringCellValue().trim();
|
|
|
+ list.add(tem);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (wb != null) {
|
|
|
+ wb.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void resolvingFile(File file, String code) {
|
|
|
+ String cousre = code.substring(0, code.length() - 2);
|
|
|
+ File cousreDir = new File(file.getAbsoluteFile() + "/" + cousre + "/");
|
|
|
+ if(!cousreDir.exists()||cousreDir.listFiles().length==0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for(File paperDir:cousreDir.listFiles()) {
|
|
|
+ if(paperDir.isDirectory()&&paperDir.getName().startsWith("paper")) {
|
|
|
+ File paperFile=new File(paperDir.getAbsoluteFile() + "/paper.json");
|
|
|
+ if(paperFile.exists()&& paperFile.isFile()) {
|
|
|
+ totalcount++;
|
|
|
+ KdPaper kpaper = JSONObject.parseObject(FileUtil.readFileContent(paperFile), KdPaper.class);
|
|
|
+ kpaper.setCourseCode(code);
|
|
|
+ filePath = paperFile.getAbsolutePath();
|
|
|
+ savePaper(kpaper);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void savePaper(KdPaper kpaper) {
|
|
|
+ int questionNum = 0;
|
|
|
+ Course course = getCourse(kpaper.getCourseCode());
|
|
|
+ if (course == null) {
|
|
|
+ throw new RuntimeException("课程不存在:" + kpaper.getCourseCode());
|
|
|
+ }
|
|
|
+ Paper paper = initPaper(kpaper, course);
|
|
|
+ paper.setDifficultyDegree(0.5);
|
|
|
+ // 定义大题集合
|
|
|
+ List<PaperDetail> paperDetails = new ArrayList<>();
|
|
|
+ // 定义小题集合
|
|
|
+ List<PaperDetailUnit> paperDetailUnits = new ArrayList<>();
|
|
|
+ List<Question> questions = new ArrayList<Question>();
|
|
|
+ for (int i = 0; i < kpaper.getDetails().size(); i++) {
|
|
|
+ KdDetail de = kpaper.getDetails().get(i);
|
|
|
+ de.setNumber(i + 1);
|
|
|
+ PaperDetail paperDetail = initPaperDetail(de, paper);
|
|
|
+ paperDetails.add(paperDetail);
|
|
|
+ for (KdQuestion que : de.getQuestions()) {
|
|
|
+ questionNum++;
|
|
|
+ Question question = initQuestion(que, course);
|
|
|
+ questions.add(question);
|
|
|
+ PaperDetailUnit paperDetailUnit = initPaperDetailUnit(paper, paperDetail, question, questionNum);
|
|
|
+ paperDetailUnits.add(paperDetailUnit);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mongoTemplate.insert(questions, "question");
|
|
|
+ mongoTemplate.insert(paper, "paper");
|
|
|
+ mongoTemplate.insert(paperDetails, "paperDetail");
|
|
|
+ mongoTemplate.insert(paperDetailUnits, "paperDetailUnit");
|
|
|
+ if (paperDetailUnits.size() > 0) {
|
|
|
+ saveQuesTypeName(paperDetailUnits);
|
|
|
+ }
|
|
|
+ count++;
|
|
|
+ System.out.println("已处理:" + count);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveQuesTypeName(List<PaperDetailUnit> detailUnitList) {
|
|
|
+
|
|
|
+ for (PaperDetailUnit paperDetailUnit : detailUnitList) {
|
|
|
+ String orgId = paperDetailUnit.getPaper().getOrgId();
|
|
|
+ String courseNo = paperDetailUnit.getPaper().getCourseNo();
|
|
|
+ QuesStructType questionType = paperDetailUnit.getQuestionType();
|
|
|
+ String detailName = paperDetailUnit.getPaperDetail().getName();
|
|
|
+ List<QuesTypeName> quesTypeNames = findQuesName(orgId, courseNo, questionType);
|
|
|
+ if (quesTypeNames != null && quesTypeNames.size() > 0) {
|
|
|
+ QuesTypeName quesTypeName = quesTypeNames.get(0);
|
|
|
+ List<String> quesNames = quesTypeName.getQuesNames();
|
|
|
+ if (quesNames != null && quesNames.size() > 0) {
|
|
|
+ if (quesNames.contains(detailName)) {
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ quesNames.add(detailName);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ quesNames = new ArrayList<>();
|
|
|
+ quesNames.add(detailName);
|
|
|
+ }
|
|
|
+ quesTypeName.setQuesNames(quesNames);
|
|
|
+ mongoTemplate.save(quesTypeName, "quesTypeName");
|
|
|
+ } else {
|
|
|
+ QuesTypeName quesTypeName = new QuesTypeName();
|
|
|
+ List<String> quesNames = new ArrayList<>();
|
|
|
+ quesNames.add(detailName);
|
|
|
+ quesTypeName.setOrgId(orgId);
|
|
|
+ quesTypeName.setCourseNo(courseNo);
|
|
|
+ quesTypeName.setQuestionType(questionType);
|
|
|
+ quesTypeName.setQuesNames(quesNames);
|
|
|
+ mongoTemplate.save(quesTypeName, "quesTypeName");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<QuesTypeName> findQuesName(String orgId, String courseNo, QuesStructType questionType) {
|
|
|
+ // 查询相应的题目
|
|
|
+ Query query = Query.query(
|
|
|
+ Criteria.where("orgId").is(orgId).and("courseNo").is(courseNo).and("questionType").is(questionType));
|
|
|
+ List<QuesTypeName> ds = mongoTemplate.find(query, QuesTypeName.class, "quesTypeName");
|
|
|
+ return ds;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Paper initPaper(KdPaper kpaper, Course course) {
|
|
|
+ int unitCount = 0;
|
|
|
+ for (KdDetail de : kpaper.getDetails()) {
|
|
|
+ unitCount = unitCount + de.getQuestions().size();
|
|
|
+ }
|
|
|
+ Paper paper = new Paper();
|
|
|
+ paper.setName(kpaper.getName() + paperSuff);
|
|
|
+ paper.setTitle(kpaper.getName() + paperSuff);
|
|
|
+ paper.setPaperType(PaperType.IMPORT);
|
|
|
+ paper.setPaperStatus(PaperStatus.DRAFT);
|
|
|
+ paper.setOrgId(rootOrgId);
|
|
|
+ paper.setCreator(userName);
|
|
|
+ paper.setTotalScore(unitCount + 0.0);
|
|
|
+ paper.setCourse(course);
|
|
|
+ paper.setCourseName(course.getName());
|
|
|
+ paper.setCourseNo(course.getCode());
|
|
|
+ paper.setCreateTime(getCurDateTime());
|
|
|
+ paper.setPaperDetailCount(kpaper.getDetails().size());
|
|
|
+ paper.setCreationBy(userId);
|
|
|
+ paper.setCreationDate(new Date());
|
|
|
+ paper.setUnitCount(unitCount);
|
|
|
+ return paper;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getCurDateTime() {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Course getCourse(String code) {
|
|
|
+ String sql = "select * from EC_B_COURSE where root_org_id=" + rootOrgId + " and code='" + code + "'";
|
|
|
+ RowMapper<CourseBean> rowMapper = new BeanPropertyRowMapper<CourseBean>(CourseBean.class);
|
|
|
+ List<CourseBean> ret = jdbcTemplate.query(sql, rowMapper);
|
|
|
+ if (ret != null && ret.size() > 0) {
|
|
|
+ CourseBean courseBean = ret.get(0);
|
|
|
+ Course course = new Course();
|
|
|
+ BeanUtils.copyProperties(courseBean, course);
|
|
|
+ if (courseBean.getEnable()) {
|
|
|
+ course.setEnable("true");
|
|
|
+ } else {
|
|
|
+ course.setEnable("false");
|
|
|
+ }
|
|
|
+ course.setId(courseBean.getId() + "");
|
|
|
+ course.setOrgId(courseBean.getRootOrgId() + "");
|
|
|
+ course.setCreateTime(getCurDateTime());
|
|
|
+ return course;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PaperDetail initPaperDetail(KdDetail de, Paper paper) {
|
|
|
+ PaperDetail paperDetail = new PaperDetail();
|
|
|
+ if (de.getQuestions() != null && de.getQuestions().size() > 0) {
|
|
|
+ paperDetail.setPaper(paper);
|
|
|
+ paperDetail.setNumber(de.getNumber());
|
|
|
+ paperDetail.setName(de.getName());
|
|
|
+ paperDetail.setScore(de.getQuestions().size() + 0.0);
|
|
|
+ paperDetail.setUnitCount(de.getQuestions().size());
|
|
|
+ paperDetail.setCreator(userName);
|
|
|
+ paperDetail.setCreateTime(getCurDateTime());
|
|
|
+ paperDetail.setCreationBy(userId);
|
|
|
+ paperDetail.setCreationDate(new Date());
|
|
|
+ }
|
|
|
+ return paperDetail;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Question initQuestion(KdQuestion que, Course course) {
|
|
|
+ Question question = new Question();
|
|
|
+ question.setCreateTime(getCurDateTime());
|
|
|
+ question.setScore(1.0);
|
|
|
+ question.setCourse(course);
|
|
|
+ question.setOrgId(course.getOrgId());
|
|
|
+ question.setHasAudio(false);
|
|
|
+ question.setDifficulty("中");
|
|
|
+ question.setDifficultyDegree(0.5);
|
|
|
+ question.setPublicity(true);
|
|
|
+ question.setCreationBy(userId);
|
|
|
+ question.setCreationDate(new Date());
|
|
|
+ question.setCreateTime(getCurDateTime());
|
|
|
+ // 按试题分类初始化题干,答案,选项
|
|
|
+ initQuestionInfo(question, que);
|
|
|
+ return question;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initQuestionInfo(Question question, KdQuestion que) {
|
|
|
+ // 单选题
|
|
|
+ if (que.getStructType() == 1) {
|
|
|
+ String quesBody = que.getBody();
|
|
|
+ question.setQuesBody(quesBody);
|
|
|
+ question.setQuestionType(QuesStructType.SINGLE_ANSWER_QUESTION);
|
|
|
+ List<QuesOption> quesOptions = new ArrayList<QuesOption>();
|
|
|
+ List<KdQuesOption> options = que.getOptions();
|
|
|
+ Integer answerNumber = null;
|
|
|
+ if (options != null && options.size() > 0) {
|
|
|
+ int number = 1;
|
|
|
+ short isCorrect = 0;
|
|
|
+ for (KdQuesOption testOption : options) {
|
|
|
+ if (testOption.getNumber().toString().equals(que.getAnswer())) {
|
|
|
+ isCorrect = 1;
|
|
|
+ answerNumber = number;
|
|
|
+ } else {
|
|
|
+ isCorrect = 0;
|
|
|
+ }
|
|
|
+ QuesOption quesOption = initQuesOption(testOption, isCorrect);
|
|
|
+ quesOptions.add(quesOption);
|
|
|
+ number++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ question.setQuesOptions(quesOptions);
|
|
|
+ if (answerNumber != null) {
|
|
|
+ char c1 = (char) (answerNumber + 64);
|
|
|
+ question.setQuesAnswer(String.valueOf(c1));
|
|
|
+ }
|
|
|
+ } else
|
|
|
+ // 多选题
|
|
|
+ if (que.getStructType() == 2) {
|
|
|
+ String quesBody = que.getBody();
|
|
|
+ question.setQuesBody(quesBody);
|
|
|
+ question.setQuestionType(QuesStructType.MULTIPLE_ANSWER_QUESTION);
|
|
|
+ List<QuesOption> quesOptions = new ArrayList<QuesOption>();
|
|
|
+ List<KdQuesOption> options = que.getOptions();
|
|
|
+ List<Integer> answerNumbers = new ArrayList<Integer>();
|
|
|
+ if (options != null && options.size() > 0) {
|
|
|
+ int number = 1;
|
|
|
+ short isCorrect = 0;
|
|
|
+ for (KdQuesOption testOption : options) {
|
|
|
+ if (que.getAnswer().contains(testOption.getNumber().toString())) {
|
|
|
+ isCorrect = 1;
|
|
|
+ answerNumbers.add(number);
|
|
|
+ } else {
|
|
|
+ isCorrect = 0;
|
|
|
+ }
|
|
|
+ QuesOption quesOption = initQuesOption(testOption, isCorrect);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } else
|
|
|
+ // 判断题
|
|
|
+ if (que.getStructType() == 3) {
|
|
|
+ String quesBody = que.getBody();
|
|
|
+ question.setQuesBody(quesBody);
|
|
|
+ question.setQuestionType(QuesStructType.BOOL_ANSWER_QUESTION);
|
|
|
+ String answer = que.getAnswer();
|
|
|
+ if (answer.equals("false")) {
|
|
|
+ answer = "错误";
|
|
|
+ } else {
|
|
|
+ answer = "正确";
|
|
|
+ }
|
|
|
+ question.setQuesAnswer(answer);
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("主观题题型需加代码:" + filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private QuesOption initQuesOption(KdQuesOption testOption, short isCorrect) {
|
|
|
+ QuesOption quesOption = new QuesOption();
|
|
|
+ quesOption.setNumber(testOption.getNumber().toString());
|
|
|
+ quesOption.setIsCorrect(isCorrect);
|
|
|
+ String optionBody = testOption.getBody();
|
|
|
+ quesOption.setOptionBody(optionBody);
|
|
|
+ return quesOption;
|
|
|
+ }
|
|
|
+
|
|
|
+ private PaperDetailUnit initPaperDetailUnit(Paper paper, PaperDetail paperDetail, Question question,
|
|
|
+ int questionNum) {
|
|
|
+ PaperDetailUnit paperDetailUnit = new PaperDetailUnit();
|
|
|
+ paperDetailUnit.setPaper(paper);
|
|
|
+ paperDetailUnit.setNumber(questionNum);
|
|
|
+ paperDetailUnit.setScore(1.0);
|
|
|
+ paperDetailUnit.setPaperDetail(paperDetail);
|
|
|
+ paperDetailUnit.setQuestionType(question.getQuestionType());
|
|
|
+ paperDetailUnit.setCreator(userName);
|
|
|
+ paperDetailUnit.setCreateTime(getCurDateTime());
|
|
|
+ paperDetailUnit.setQuestion(question);
|
|
|
+ paperDetailUnit.setPaperType(PaperType.IMPORT);
|
|
|
+ paperDetailUnit.setCreationBy(userId);
|
|
|
+ paperDetailUnit.setCreationDate(new Date());
|
|
|
+ return paperDetailUnit;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|