فهرست منبع

FixExamRecordQuestionsId

deason 4 سال پیش
والد
کامیت
005d5e5dc2

+ 116 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/fix_exam_record_questions_id/FixExamRecordQuestionsId.java

@@ -0,0 +1,116 @@
+package cn.com.qmth.dp.examcloud.oe.modules.fix_exam_record_questions_id;
+
+import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.vo.ExamRecordQuestionVO;
+import cn.com.qmth.dp.examcloud.oe.modules.fix_exam_record_questions_id.vo.ExamRecordDataVO;
+import org.apache.commons.collections.CollectionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+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.BatchPreparedStatementSetter;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Component;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 修正数据空值(可重复执行):
+ * 表 ec_oe_exam_record_data 字段 exam_record_questions_id
+ */
+@Component
+public class FixExamRecordQuestionsId {
+
+    private final static Logger log = LoggerFactory.getLogger(FixExamRecordQuestionsId.class);
+
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
+
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    public void start() {
+        StringBuilder querySql = new StringBuilder()
+                .append("select id,exam_type from ec_oe_exam_record_data ")
+                .append("where id > %s and exam_type != 'OFFLINE' and exam_record_questions_id is null ")
+                .append("order by id asc limit 0,100");
+
+        int total = 0;
+        Long nextMinId = 0L;
+        while (true) {
+            System.out.println();
+            log.info("query nextMinId:" + nextMinId + ", total " + total);
+
+            List<ExamRecordDataVO> list = jdbcTemplate.query(String.format(querySql.toString(), nextMinId),
+                    new BeanPropertyRowMapper(ExamRecordDataVO.class));
+
+            if (CollectionUtils.isEmpty(list)) {
+                break;
+            }
+
+            nextMinId = list.get(list.size() - 1).getId();
+            total += list.size();
+
+            this.fix(list);
+        }
+
+        log.info("task end...");
+    }
+
+    private void fix(List<ExamRecordDataVO> examRecordDataList) {
+        final String msg = "examRecordDataId = %s, examType = %s, examRecordQuestions = %s";
+
+        List<ExamRecordDataVO> todoExamRecordDataList = new ArrayList<>();
+
+        for (ExamRecordDataVO examRecordData : examRecordDataList) {
+            ExamRecordQuestionVO detail = this.queryExamRecordQuestions(examRecordData.getId());
+            if (detail == null) {
+                System.out.println(String.format(msg, examRecordData.getId(), examRecordData.getExamType(), "empty"));
+                continue;
+            }
+
+            System.out.println(String.format(msg, examRecordData.getId(), examRecordData.getExamType(), detail.getId()));
+            examRecordData.setExamRecordQuestionsId(detail.getId());
+            todoExamRecordDataList.add(examRecordData);
+        }
+
+        // this.updateExamRecordDataList(todoExamRecordDataList);
+    }
+
+    private ExamRecordQuestionVO queryExamRecordQuestions(Long examRecordDataId) {
+        Query query = new Query();
+        query.addCriteria(Criteria.where("examRecordDataId").is(examRecordDataId));
+        List<ExamRecordQuestionVO> result = mongoTemplate.find(query, ExamRecordQuestionVO.class, "examRecordQuestions");
+        if (CollectionUtils.isNotEmpty(result)) {
+            return result.get(0);
+        }
+        return null;
+    }
+
+    private void updateExamRecordDataList(List<ExamRecordDataVO> list) {
+        if (CollectionUtils.isEmpty(list)) {
+            return;
+        }
+
+        String updateSql = "update ec_oe_exam_record_data set exam_record_questions_id = ? where id = ?";
+        jdbcTemplate.batchUpdate(updateSql, new BatchPreparedStatementSetter() {
+            @Override
+            public void setValues(PreparedStatement ps, int n) throws SQLException {
+                ExamRecordDataVO row = list.get(n);
+                ps.setString(1, row.getExamRecordQuestionsId());
+                ps.setLong(2, row.getId());
+            }
+
+            @Override
+            public int getBatchSize() {
+                return list.size();
+            }
+        });
+    }
+
+}

+ 39 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/fix_exam_record_questions_id/vo/ExamRecordDataVO.java

@@ -0,0 +1,39 @@
+package cn.com.qmth.dp.examcloud.oe.modules.fix_exam_record_questions_id.vo;
+
+import java.io.Serializable;
+
+public class ExamRecordDataVO implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private Long id;
+
+    private String examType;
+
+    private String examRecordQuestionsId;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getExamType() {
+        return examType;
+    }
+
+    public void setExamType(String examType) {
+        this.examType = examType;
+    }
+
+    public String getExamRecordQuestionsId() {
+        return examRecordQuestionsId;
+    }
+
+    public void setExamRecordQuestionsId(String examRecordQuestionsId) {
+        this.examRecordQuestionsId = examRecordQuestionsId;
+    }
+
+}