|
@@ -0,0 +1,160 @@
|
|
|
|
+package cn.com.qmth.examcloud.tool.task.updateanswer;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.tool.task.Task;
|
|
|
|
+import cn.com.qmth.examcloud.tool.task.updateanswer.entity.ExamRecordEntity;
|
|
|
|
+import cn.com.qmth.examcloud.tool.task.updateanswer.entity.UpdateAnswerProperties;
|
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
|
+import com.mongodb.client.result.UpdateResult;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+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.data.mongodb.core.query.Update;
|
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.StringJoiner;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * description UpdateAnswerTask
|
|
|
|
+ *
|
|
|
|
+ * @author qinchao
|
|
|
|
+ * @date 2021/3/17 15:15
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class UpdateAnswerTask implements Task {
|
|
|
|
+
|
|
|
|
+ private static final String SEPARATOR = "/";
|
|
|
|
+
|
|
|
|
+ private static final String UNDERLINE = "_";
|
|
|
|
+
|
|
|
|
+ // 小程序答案上传目录
|
|
|
|
+ private static final String OE_ANSWER_FILE_PATH = "oe-answer-file";
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private OSS oss;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private UpdateAnswerProperties answerProperties;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void start(String params) {
|
|
|
|
+ log.info("UpdateAnswerTask start");
|
|
|
|
+ String identityNumber = answerProperties.getIdentityNumber();
|
|
|
|
+
|
|
|
|
+ List<ExamRecordEntity> list = getExamRecordList(answerProperties.getExamId(),
|
|
|
|
+ answerProperties.getCourseCode(), identityNumber);
|
|
|
|
+ for (ExamRecordEntity entity : list) {
|
|
|
|
+ File file = new File(answerProperties.getDir());
|
|
|
|
+ String[] paths = file.list();
|
|
|
|
+ if (paths != null && paths.length > 0) {
|
|
|
|
+ String delimiter = "";
|
|
|
|
+ String prefix = "<div class='photo-answers-block'>";
|
|
|
|
+ String suffix = "</div>";
|
|
|
|
+ StringJoiner sj = new StringJoiner(delimiter, prefix, suffix);
|
|
|
|
+ for (String temp : paths) {
|
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
|
+ sb.append(OE_ANSWER_FILE_PATH).append(SEPARATOR)
|
|
|
|
+ .append(entity.getExamStudentId()).append(SEPARATOR).append(entity.getExamRecordDataId())
|
|
|
|
+ .append(SEPARATOR).append(identityNumber).append(UNDERLINE).append(System.currentTimeMillis())
|
|
|
|
+ .append(temp.substring(temp.lastIndexOf(".")));
|
|
|
|
+ String filePath = sb.toString();
|
|
|
|
+ oss.putObject(answerProperties.getBucket(), filePath,
|
|
|
|
+ new File(answerProperties.getDir() + SEPARATOR + temp));
|
|
|
|
+ String url = getUrl(answerProperties.getDomain(), filePath);
|
|
|
|
+ sj.add(buildFileAnswer(url));
|
|
|
|
+ }
|
|
|
|
+ // 第二步,更新答案
|
|
|
|
+ long res = updateAnswer(entity.getExamRecordDataId(), answerProperties.getOrder(), sj.toString());
|
|
|
|
+ if (res < 1) {
|
|
|
|
+ log.error("更新答案失败,身份证号[{}],考试记录id[{}]", identityNumber, entity.getExamId());
|
|
|
|
+ } else {
|
|
|
|
+ log.info("更新答案成功,身份证号[{}],考试记录id[{}]", identityNumber, entity.getExamId());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ log.info("UpdateAnswerTask end");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<ExamRecordEntity> getExamRecordList(Long examId, String courseCode,
|
|
|
|
+ String identityNumber) {
|
|
|
|
+ String strSql = "select rd.id examRecordDataId,rd.exam_id examId, "
|
|
|
|
+ + "rd.identity_number identityNumber,c.`code` courseCode,rd.exam_student_id examStudentId "
|
|
|
|
+ + "from ec_oe_exam_record_data rd "
|
|
|
|
+ + "inner join ec_b_course c on rd.course_id=c.id " + "where rd.exam_id=" + examId
|
|
|
|
+ + " and c.`code`='" + courseCode + "'" + " and rd.identity_number='"
|
|
|
|
+ + identityNumber + "'";
|
|
|
|
+ return jdbcTemplate.query(strSql, (resultSet, i) -> {
|
|
|
|
+ ExamRecordEntity entity = new ExamRecordEntity();
|
|
|
|
+ entity.setExamRecordDataId(resultSet.getLong("examRecordDataId"));
|
|
|
|
+ entity.setExamStudentId(resultSet.getLong("examStudentId"));
|
|
|
|
+ entity.setExamId(examId);
|
|
|
|
+ entity.setCourseCode(courseCode);
|
|
|
|
+ entity.setIdentityNumber(identityNumber);
|
|
|
|
+ return entity;
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getUrl(String domain, String path) {
|
|
|
|
+ if (StringUtils.isBlank(domain)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isBlank(path)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ if (path.startsWith("/")) {
|
|
|
|
+ path = path.substring(1);
|
|
|
|
+ }
|
|
|
|
+ return domain + "/" + path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ File file = new File("C:\\Users\\qinchao\\Desktop\\temp");
|
|
|
|
+ String[] list = file.list();
|
|
|
|
+ for (String s : list) {
|
|
|
|
+ System.out.println(s.substring(s.lastIndexOf(".")));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 构建文件格式
|
|
|
|
+ *
|
|
|
|
+ * @param fileUrl
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private String buildFileAnswer(String fileUrl) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ sb.append("<a href='" + fileUrl + "' target='_blank' >");
|
|
|
|
+ sb.append("<img class='photo-answer' src='" + fileUrl + "?x-oss-process=image/resize,m_lfit,h_200,w_200' />");
|
|
|
|
+ sb.append("</a>");
|
|
|
|
+ return sb.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新指定试题的答案
|
|
|
|
+ *
|
|
|
|
+ * @param examRecordDataId
|
|
|
|
+ * @param order
|
|
|
|
+ * @param newAnswer
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public long updateAnswer(Long examRecordDataId, Integer order, String newAnswer) {
|
|
|
|
+ // 查询相应的题目
|
|
|
|
+ Query query = Query.query(Criteria.where("examRecordDataId").is(examRecordDataId)
|
|
|
|
+ .and("examQuestionEntities.order").is(order));
|
|
|
|
+ Update update = Update.update("examQuestionEntities.$.studentAnswer", newAnswer);
|
|
|
|
+ UpdateResult upResult = mongoTemplate.updateFirst(query, update, "examRecordQuestions");
|
|
|
|
+ return upResult.getMatchedCount();
|
|
|
|
+ }
|
|
|
|
+}
|