|
@@ -0,0 +1,171 @@
|
|
|
|
+package cn.com.qmth.dp.examcloud.oe.modules.findcheatanswer;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.locks.Lock;
|
|
|
|
+import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.bson.types.ObjectId;
|
|
|
|
+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 cn.com.qmth.dp.examcloud.oe.enums.question.QuesStructType;
|
|
|
|
+import cn.com.qmth.dp.examcloud.oe.excel.ExportUtils;
|
|
|
|
+import cn.com.qmth.dp.examcloud.oe.multithread.Consumer;
|
|
|
|
+import cn.com.qmth.dp.examcloud.oe.util.PaperUtil;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 主观题作答筛查
|
|
|
|
+ *
|
|
|
|
+ * @author chenken
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class FindCheatAnswerService {
|
|
|
|
+ private Long examId = 5860L;
|
|
|
|
+ private int threadCount = 40;
|
|
|
|
+ private static Lock lock = new ReentrantLock();
|
|
|
|
+ private static Map<String, String> questionTexts = new ConcurrentHashMap<>();
|
|
|
|
+ @Autowired
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
+ @Resource(name = "mongoTemplate")
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
+ @Autowired
|
|
|
|
+ private FindCheatAnswerProducer pr;
|
|
|
|
+
|
|
|
|
+ public void start() {
|
|
|
|
+ File file = new File("d:/ret.xlsx");
|
|
|
|
+ if (file.exists()) {
|
|
|
|
+ file.delete();
|
|
|
|
+ }
|
|
|
|
+ Date s = new Date();
|
|
|
|
+ try {
|
|
|
|
+ Map<String, Object> param = new HashMap<>();
|
|
|
|
+ param.put("examId", examId);
|
|
|
|
+ pr.startDispose(FindCheatAnswerConsumer.class, threadCount, param);
|
|
|
|
+ List<Long> ret = new ArrayList<>();
|
|
|
|
+ for (Consumer c : pr.getConsumers()) {
|
|
|
|
+ ret.addAll(c.getRet());
|
|
|
|
+ }
|
|
|
|
+ System.out.println("ret count:" + ret.size());
|
|
|
|
+ if (ret.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ List<FindCheatAnswerRetDto> dtos = findRet(ret);
|
|
|
|
+ FileOutputStream fos = null;
|
|
|
|
+ try {
|
|
|
|
+ file.createNewFile();
|
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
|
+ ExportUtils.makeExcel(FindCheatAnswerRetDto.class, dtos, fos);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ if (fos != null) {
|
|
|
|
+ try {
|
|
|
|
+ fos.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ Date e = new Date();
|
|
|
|
+ System.out.println("time:" + (e.getTime() - s.getTime()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<FindCheatAnswerRetDto> findRet(List<Long> ids) {
|
|
|
|
+ StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
+ sqlBuilder.append(" select t.id examRecordDataId,f.course_code courseCode,f.student_name studentName "
|
|
|
|
+ + " ,f.identity_number identityNumber,f.student_code studentCode " + " from ec_oe_exam_record_data t ");
|
|
|
|
+ sqlBuilder.append(" left join ec_oe_exam_student f on t.exam_student_id=f.exam_student_id ");
|
|
|
|
+ sqlBuilder.append(" where t.id in(" + StringUtils.join(ids, ",") + ") ");
|
|
|
|
+
|
|
|
|
+ RowMapper<FindCheatAnswerRetDto> rowMapper = new BeanPropertyRowMapper<>(FindCheatAnswerRetDto.class);
|
|
|
|
+ List<FindCheatAnswerRetDto> ret = jdbcTemplate.query(sqlBuilder.toString(), rowMapper);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getQuestionAnswerText(String questionId, Integer subIndex) {
|
|
|
|
+ if (StringUtils.isBlank(questionId)) {
|
|
|
|
+ throw new RuntimeException("题ID不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (subIndex == null) {
|
|
|
|
+ throw new RuntimeException("subIndex不能为空");
|
|
|
|
+ }
|
|
|
|
+ String key = questionId + "-" + subIndex;
|
|
|
|
+
|
|
|
|
+ String text = questionTexts.get(key);
|
|
|
|
+ if (text != null) {
|
|
|
|
+ return text;
|
|
|
|
+ }
|
|
|
|
+ lock.lock();
|
|
|
|
+ try {
|
|
|
|
+ text = questionTexts.get(key);
|
|
|
|
+ if (text != null) {
|
|
|
|
+ return text;
|
|
|
|
+ }
|
|
|
|
+ text = getQuestionAnswer(questionId, subIndex);
|
|
|
|
+ questionTexts.put(text, text);
|
|
|
|
+ return text;
|
|
|
|
+ } finally {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getQuestionAnswer(String questionId, Integer subIndex) {
|
|
|
|
+ Query query = new Query();
|
|
|
|
+ query.addCriteria(Criteria.where("_id").is(new ObjectId(questionId)));
|
|
|
|
+ List<QuestionDto> ret = mongoTemplate.find(query, QuestionDto.class, "question");
|
|
|
|
+ if (CollectionUtils.isEmpty(ret)) {
|
|
|
|
+ throw new RuntimeException("未找到试题:"+questionId);
|
|
|
|
+ }
|
|
|
|
+ QuestionDto que=ret.get(0);
|
|
|
|
+ String an = "";
|
|
|
|
+ if (QuesStructType.NESTED_ANSWER_QUESTION.equals(que.getQuestionType())) {
|
|
|
|
+ an = que.getSubQuestions().get(subIndex).getQuesAnswer();
|
|
|
|
+ if (StringUtils.isBlank(an)) {
|
|
|
|
+ an = "";
|
|
|
|
+ return an;
|
|
|
|
+ }
|
|
|
|
+ if (QuesStructType.FILL_BLANK_QUESTION.equals(que.getQuestionType())) {
|
|
|
|
+ an = an.replaceAll("##", "");
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ an = que.getQuesAnswer();
|
|
|
|
+ if (StringUtils.isBlank(an)) {
|
|
|
|
+ an = "";
|
|
|
|
+ return an;
|
|
|
|
+ }
|
|
|
|
+ if (QuesStructType.FILL_BLANK_QUESTION.equals(que.getQuestionType())) {
|
|
|
|
+ an = an.replaceAll("##", "");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isBlank(an)) {
|
|
|
|
+ an = "";
|
|
|
|
+ } else {
|
|
|
|
+ an = PaperUtil.getExtractText(an);
|
|
|
|
+ }
|
|
|
|
+ return an;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|