|
@@ -1,164 +1,164 @@
|
|
-package cn.com.qmth.examcloud.core.questions.dao;
|
|
|
|
-
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
|
|
|
|
-import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
|
|
|
|
-import com.mongodb.DBRef;
|
|
|
|
-import com.mongodb.client.MongoCollection;
|
|
|
|
-import com.mongodb.client.MongoCursor;
|
|
|
|
-import org.bson.Document;
|
|
|
|
-import org.bson.types.ObjectId;
|
|
|
|
-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.data.mongodb.core.MongoTemplate;
|
|
|
|
-import org.springframework.stereotype.Repository;
|
|
|
|
-
|
|
|
|
-import java.util.*;
|
|
|
|
-
|
|
|
|
-import static com.mongodb.client.model.Filters.eq;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * Created by songyue on 18/1/22.
|
|
|
|
- */
|
|
|
|
-@Repository
|
|
|
|
-public class PaperDetailUnitNativeRepo {
|
|
|
|
-
|
|
|
|
- private static final Logger log = LoggerFactory.getLogger(PaperDetailUnitNativeRepo.class);
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- @Qualifier("pduCollection")
|
|
|
|
- private MongoCollection<Document> mongoCollection;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private MongoTemplate mongoTemplate;
|
|
|
|
-
|
|
|
|
- public List<PaperDetailUnit> findByPaperId(String paperId) {
|
|
|
|
-
|
|
|
|
- long beginTime = System.currentTimeMillis();
|
|
|
|
-
|
|
|
|
- //初始化试卷缓存
|
|
|
|
- Paper paperCache = mongoTemplate.findById(getObjectId(paperId), Paper.class);
|
|
|
|
-
|
|
|
|
- //初始化大题缓存
|
|
|
|
- Map<String, PaperDetail> pdCache = new HashMap<>();
|
|
|
|
-
|
|
|
|
- //初始化小题列表
|
|
|
|
- List<PaperDetailUnit> paperDetailUnits = new ArrayList<>();
|
|
|
|
-
|
|
|
|
- //获取小题原始文档
|
|
|
|
- MongoCursor<Document> mongoCursor = mongoCollection.find(eq("paper.$id", getObjectId(paperId))).iterator();
|
|
|
|
- long documentEndTime = System.currentTimeMillis();
|
|
|
|
- log.info("获取document共耗时:" + (documentEndTime - beginTime) + "ms");
|
|
|
|
-
|
|
|
|
- //转换小题文档
|
|
|
|
- while (mongoCursor.hasNext()) {
|
|
|
|
- Document document = mongoCursor.next();
|
|
|
|
- PaperDetailUnit pdu = toDomain(document, paperCache, pdCache);
|
|
|
|
- paperDetailUnits.add(pdu);
|
|
|
|
- }
|
|
|
|
- long pduEndTime = System.currentTimeMillis();
|
|
|
|
- log.info("转换document共耗时:" + (pduEndTime - documentEndTime) + "ms");
|
|
|
|
- return paperDetailUnits;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
|
- private PaperDetailUnit toDomain(Document document,
|
|
|
|
- Paper paperCache,
|
|
|
|
- Map<String, PaperDetail> pdCache) {
|
|
|
|
- PaperDetailUnit paperDetailUnit = new PaperDetailUnit();
|
|
|
|
- Set<String> pduProperty = document.keySet();
|
|
|
|
- for (String key : pduProperty) {
|
|
|
|
- if (key.equals("_id")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setId(String.valueOf(document.get(key)));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("number")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setNumber(document.getInteger(key));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("score")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setScore(document.getDouble(key));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("subScoreList")) {
|
|
|
|
-
|
|
|
|
- List<Double> subScoreList = document.get(key, List.class);
|
|
|
|
- paperDetailUnit.setSubScoreList(subScoreList);
|
|
|
|
-
|
|
|
|
- } else if (key.equals("questionType")) {
|
|
|
|
-
|
|
|
|
- String quesType = document.getString(key);
|
|
|
|
- paperDetailUnit.setQuestionType(QuesStructType.valueOf(quesType));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("optionOrder")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setOptionOrder(document.getString(key));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("creator")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setCreator(document.getString(key));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("createTime")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setCreateTime(document.getString(key));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("paperType")) {
|
|
|
|
-
|
|
|
|
- String paperType = document.getString(key);
|
|
|
|
- paperDetailUnit.setPaperType(PaperType.valueOf(paperType));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("paper")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setPaper(paperCache);
|
|
|
|
-
|
|
|
|
- } else if (key.equals("paperDetail")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setPaperDetail(getPaperDetail(document, pdCache));
|
|
|
|
-
|
|
|
|
- } else if (key.equals("question")) {
|
|
|
|
-
|
|
|
|
- paperDetailUnit.setQuestion(getQuestion(document));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return paperDetailUnit;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private Object getObjectId(String id) {
|
|
|
|
- if (id == null) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- return ObjectId.isValid(id) ? new ObjectId(id) : id;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private PaperDetail getPaperDetail(Document parentDocument, Map<String, PaperDetail> pdCache) {
|
|
|
|
- Object value = parentDocument.get("paperDetail");
|
|
|
|
- if (value == null || !(value instanceof DBRef)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- DBRef paperDetailDoc = (DBRef) value;
|
|
|
|
- String paperDetailId = String.valueOf(paperDetailDoc.getId());
|
|
|
|
- PaperDetail paperDetail = pdCache.get(paperDetailId);
|
|
|
|
- if (paperDetail == null) {
|
|
|
|
- paperDetail = mongoTemplate.findById(getObjectId(paperDetailId), PaperDetail.class);
|
|
|
|
- pdCache.put(paperDetailId, paperDetail);
|
|
|
|
- }
|
|
|
|
- return paperDetail;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private Question getQuestion(Document parentDocument) {
|
|
|
|
- Object value = parentDocument.get("question");
|
|
|
|
- if (value == null || !(value instanceof DBRef)) {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- DBRef quesDoc = (DBRef) value;
|
|
|
|
- String quesId = String.valueOf(quesDoc.getId());
|
|
|
|
- Question question = mongoTemplate.findById(getObjectId(quesId), Question.class);
|
|
|
|
- return question;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+// package cn.com.qmth.examcloud.core.questions.dao;
|
|
|
|
+//
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.dao.entity.Paper;
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
|
|
|
|
+// import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
|
|
|
|
+// import com.mongodb.DBRef;
|
|
|
|
+// import com.mongodb.client.MongoCollection;
|
|
|
|
+// import com.mongodb.client.MongoCursor;
|
|
|
|
+// import org.bson.Document;
|
|
|
|
+// import org.bson.types.ObjectId;
|
|
|
|
+// 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.data.mongodb.core.MongoTemplate;
|
|
|
|
+// import org.springframework.stereotype.Repository;
|
|
|
|
+//
|
|
|
|
+// import java.util.*;
|
|
|
|
+//
|
|
|
|
+// import static com.mongodb.client.model.Filters.eq;
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * Created by songyue on 18/1/22.
|
|
|
|
+// */
|
|
|
|
+// @Repository
|
|
|
|
+// public class PaperDetailUnitNativeRepo {
|
|
|
|
+//
|
|
|
|
+// private static final Logger log = LoggerFactory.getLogger(PaperDetailUnitNativeRepo.class);
|
|
|
|
+//
|
|
|
|
+// @Autowired
|
|
|
|
+// @Qualifier("pduCollection")
|
|
|
|
+// private MongoCollection<Document> mongoCollection;
|
|
|
|
+//
|
|
|
|
+// @Autowired
|
|
|
|
+// private MongoTemplate mongoTemplate;
|
|
|
|
+//
|
|
|
|
+// public List<PaperDetailUnit> findByPaperId(String paperId) {
|
|
|
|
+//
|
|
|
|
+// long beginTime = System.currentTimeMillis();
|
|
|
|
+//
|
|
|
|
+// //初始化试卷缓存
|
|
|
|
+// Paper paperCache = mongoTemplate.findById(getObjectId(paperId), Paper.class);
|
|
|
|
+//
|
|
|
|
+// //初始化大题缓存
|
|
|
|
+// Map<String, PaperDetail> pdCache = new HashMap<>();
|
|
|
|
+//
|
|
|
|
+// //初始化小题列表
|
|
|
|
+// List<PaperDetailUnit> paperDetailUnits = new ArrayList<>();
|
|
|
|
+//
|
|
|
|
+// //获取小题原始文档
|
|
|
|
+// MongoCursor<Document> mongoCursor = mongoCollection.find(eq("paper.$id", getObjectId(paperId))).iterator();
|
|
|
|
+// long documentEndTime = System.currentTimeMillis();
|
|
|
|
+// log.info("获取document共耗时:" + (documentEndTime - beginTime) + "ms");
|
|
|
|
+//
|
|
|
|
+// //转换小题文档
|
|
|
|
+// while (mongoCursor.hasNext()) {
|
|
|
|
+// Document document = mongoCursor.next();
|
|
|
|
+// PaperDetailUnit pdu = toDomain(document, paperCache, pdCache);
|
|
|
|
+// paperDetailUnits.add(pdu);
|
|
|
|
+// }
|
|
|
|
+// long pduEndTime = System.currentTimeMillis();
|
|
|
|
+// log.info("转换document共耗时:" + (pduEndTime - documentEndTime) + "ms");
|
|
|
|
+// return paperDetailUnits;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// @SuppressWarnings("unchecked")
|
|
|
|
+// private PaperDetailUnit toDomain(Document document,
|
|
|
|
+// Paper paperCache,
|
|
|
|
+// Map<String, PaperDetail> pdCache) {
|
|
|
|
+// PaperDetailUnit paperDetailUnit = new PaperDetailUnit();
|
|
|
|
+// Set<String> pduProperty = document.keySet();
|
|
|
|
+// for (String key : pduProperty) {
|
|
|
|
+// if (key.equals("_id")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setId(String.valueOf(document.get(key)));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("number")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setNumber(document.getInteger(key));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("score")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setScore(document.getDouble(key));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("subScoreList")) {
|
|
|
|
+//
|
|
|
|
+// List<Double> subScoreList = document.get(key, List.class);
|
|
|
|
+// paperDetailUnit.setSubScoreList(subScoreList);
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("questionType")) {
|
|
|
|
+//
|
|
|
|
+// String quesType = document.getString(key);
|
|
|
|
+// paperDetailUnit.setQuestionType(QuesStructType.valueOf(quesType));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("optionOrder")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setOptionOrder(document.getString(key));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("creator")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setCreator(document.getString(key));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("createTime")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setCreateTime(document.getString(key));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("paperType")) {
|
|
|
|
+//
|
|
|
|
+// String paperType = document.getString(key);
|
|
|
|
+// paperDetailUnit.setPaperType(PaperType.valueOf(paperType));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("paper")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setPaper(paperCache);
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("paperDetail")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setPaperDetail(getPaperDetail(document, pdCache));
|
|
|
|
+//
|
|
|
|
+// } else if (key.equals("question")) {
|
|
|
|
+//
|
|
|
|
+// paperDetailUnit.setQuestion(getQuestion(document));
|
|
|
|
+//
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// return paperDetailUnit;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private Object getObjectId(String id) {
|
|
|
|
+// if (id == null) {
|
|
|
|
+// return null;
|
|
|
|
+// }
|
|
|
|
+// return ObjectId.isValid(id) ? new ObjectId(id) : id;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private PaperDetail getPaperDetail(Document parentDocument, Map<String, PaperDetail> pdCache) {
|
|
|
|
+// Object value = parentDocument.get("paperDetail");
|
|
|
|
+// if (value == null || !(value instanceof DBRef)) {
|
|
|
|
+// return null;
|
|
|
|
+// }
|
|
|
|
+// DBRef paperDetailDoc = (DBRef) value;
|
|
|
|
+// String paperDetailId = String.valueOf(paperDetailDoc.getId());
|
|
|
|
+// PaperDetail paperDetail = pdCache.get(paperDetailId);
|
|
|
|
+// if (paperDetail == null) {
|
|
|
|
+// paperDetail = mongoTemplate.findById(getObjectId(paperDetailId), PaperDetail.class);
|
|
|
|
+// pdCache.put(paperDetailId, paperDetail);
|
|
|
|
+// }
|
|
|
|
+// return paperDetail;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// private Question getQuestion(Document parentDocument) {
|
|
|
|
+// Object value = parentDocument.get("question");
|
|
|
|
+// if (value == null || !(value instanceof DBRef)) {
|
|
|
|
+// return null;
|
|
|
|
+// }
|
|
|
|
+// DBRef quesDoc = (DBRef) value;
|
|
|
|
+// String quesId = String.valueOf(quesDoc.getId());
|
|
|
|
+// Question question = mongoTemplate.findById(getObjectId(quesId), Question.class);
|
|
|
|
+// return question;
|
|
|
|
+// }
|
|
|
|
+// }
|