瀏覽代碼

添加计算最终分数的代码

lideyin 5 年之前
父節點
當前提交
b686b0b43d

+ 9 - 12
src/main/java/cn/com/qmth/dp/examcloud/oe/Task.java

@@ -6,8 +6,10 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.ExportExamStudentScore;
 import cn.com.qmth.dp.examcloud.oe.modules.get_student_answer_detail.GetStduentAnswerDetailService;
 import cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.FixCorrectAnswerAndResetScoreService;
+import cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.ResetScoreService;
 import cn.com.qmth.examcloud.commons.util.FileUtil;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.ClassPathUtils;
@@ -51,20 +53,15 @@ public class Task {
     public void start() {
         try {
 
+            ResetScoreService bean1 = SpringContextHolder.getBean(ResetScoreService.class);
+            bean1.start();
 
-            FixCorrectAnswerAndResetScoreService bean = SpringContextHolder.getBean(FixCorrectAnswerAndResetScoreService.class);
-            bean.start();
 
-/*//			ExportData bean = SpringContextHolder.getBean(ExportData.class);
-            GetStduentAnswerDetailService bean = SpringContextHolder.getBean(GetStduentAnswerDetailService.class);
-//			bean.start(1173L,"9999");
-            bean.start(1191L, "100", "200", "101", "201");
-			bean.start(1193L, "302", "303", "304", "305","306");
-			bean.start(1192L, "300", "301", "102", "202",
-					"103","203","104","204","105","205","106","206","401");
-//			bean.start(764L, "A149","A150","A154");
-//			bean.start(765L, "C149","C150","C154");
-//			bean.start(766L, "B149","B150","B154");*/
+//            FixCorrectAnswerAndResetScoreService bean = SpringContextHolder.getBean(FixCorrectAnswerAndResetScoreService.class);
+//            bean.start();
+
+//            GetStduentAnswerDetailService bean = SpringContextHolder.getBean(GetStduentAnswerDetailService.class);
+//            bean.start(1360L, "303", "305");
 
         } catch (Exception e) {
             log.error("unexpected", e);

+ 20 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/export_exam_student_score/vo/ScoreVO.java

@@ -21,6 +21,26 @@ public class ScoreVO implements Serializable {
 
     private Double totalScore;
 
+    private Double objectiveAccuracy;
+
+    private Double succPercent;
+
+    public Double getObjectiveAccuracy() {
+        return objectiveAccuracy;
+    }
+
+    public void setObjectiveAccuracy(Double objectiveAccuracy) {
+        this.objectiveAccuracy = objectiveAccuracy;
+    }
+
+    public Double getSuccPercent() {
+        return succPercent;
+    }
+
+    public void setSuccPercent(Double succPercent) {
+        this.succPercent = succPercent;
+    }
+
     public Long getId() {
         return id;
     }

+ 344 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/ResetScoreService.java

@@ -0,0 +1,344 @@
+package cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer;
+
+import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.vo.ExamStudentVO;
+import cn.com.qmth.dp.examcloud.oe.modules.export_exam_student_score.vo.ScoreVO;
+import cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.entity.ExamRecordDataEntity;
+import cn.com.qmth.examcloud.commons.util.PropertiesUtil;
+import org.apache.commons.collections.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @Description 重新计算分数
+ * @Author lideyin
+ * @Date 2020/4/25 12:52
+ * @Version 1.0
+ */
+@Service
+public class ResetScoreService {
+
+    @Autowired
+    JdbcTemplate jdbcTemplate;
+
+    private static final Long MARKING_TYPE_KEY = 24L;// 阅卷方式KEY
+
+    @Async
+    public void start() {
+        String absolutePath = PropertiesUtil.getString("fixAnswer.data.path");
+//        ClassPathResource classPathResource = new ClassPathResource("temp1.txt");
+        FileInputStream fis = null;
+        InputStreamReader isr = null;
+        BufferedReader br = null;
+
+        try {
+            fis = new FileInputStream(new File(absolutePath));
+            isr = new InputStreamReader(fis);
+            br = new BufferedReader(isr);
+            String strExamIdAndCourseCode = "";
+            while ((strExamIdAndCourseCode = br.readLine()) != null) {
+                Long examId = Long.valueOf(strExamIdAndCourseCode.split(",")[0]);
+                Long courseId = Long.valueOf(strExamIdAndCourseCode.split(",")[1]);
+
+                run(examId, courseId);
+
+                Thread.sleep(10);
+                //19,"03013750"
+            }
+
+            System.out.println("999.all is over.....");
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            if (isr != null) {
+                try {
+                    isr.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+
+    }
+
+    public void run(long examId, Long courseId) {
+        //考试下所有考试记录
+        List<ExamRecordDataEntity> allExamRecordDataList = queryExamRecordDataList(examId, courseId);
+
+        //取正式有效的考试记录数据
+        List<ExamRecordDataEntity> effectiveExamRecordDataList =
+                allExamRecordDataList.stream()
+                        .filter(p -> !p.getIllegality() && (!p.getWarn() || (p.getWarn() && p.getAudit())))
+                        .collect(Collectors.toList());
+
+        //当前考试课程下的所有的课程id
+        List<ExamStudentVO> examStudentList = queryExamStudents(examId, courseId);
+
+        // 获取考试的阅卷方式
+        String markingType = this.queryExamMarkingType(examId, MARKING_TYPE_KEY);
+        double objectiveScore = 0;
+        double subjectiveScore = 0;
+        double objectiveAccuracy = 0;
+        double successPercent = 0;
+        double totalScore = 0;
+
+        int esTotalNum = examStudentList.size();
+        int esIndex = 0;
+        for (ExamStudentVO esVo : examStudentList) {
+            esIndex++;
+            
+            Long esId = esVo.getExamStudentId();
+            System.out.println(String.format("[examId_stuId_total]:%s_%s_total---start---共%s个考生,当前正在执行第%s条...",
+                    examId, esId,esTotalNum, esIndex));
+            //如果当前考生的考试成绩已存在则直接跳过
+            if (existsFinalScore(esId)) {
+                System.out.println(String.format("[examId_stuId_total]:%s_%s_total---exist---共%s个考生,当前正在执行第%s条...",
+                        examId, esId,esTotalNum, esIndex));
+                continue;
+            }
+
+            //当前考生的所有考试记录
+            List<ExamRecordDataEntity> curEstRecordList = effectiveExamRecordDataList.stream().filter(p -> p.getExamStudentId().equals(esId)).collect(Collectors.toList());
+
+            if (curEstRecordList == null || curEstRecordList.isEmpty()) {
+                System.out.println(String.format("[examId_stuId_total]:%s_%s_total---warn---共%s个考生,当前正在执行第%s条...",
+                        examId, esId,esTotalNum, esIndex));
+                continue;//缺考考生数据不处理
+            }
+
+            //最终有效的分数
+            ScoreVO effectiveScore;
+            if (curEstRecordList.size() == 1) {
+                // 只有一条考试记录情况
+                Long okExamRecordDataId = curEstRecordList.get(0).getId();
+
+                // 获取考试记录对应的成绩
+                effectiveScore = this.queryExamScore(okExamRecordDataId);
+
+            } else{
+                // 多条考试记录情况
+                List<ScoreVO> scores = new ArrayList<>();
+                for (ExamRecordDataEntity examRecordData : effectiveExamRecordDataList) {
+                    Long examRecordDataId = examRecordData.getId();
+                    // 分别获取考试记录对应的成绩
+                    ScoreVO score = this.queryExamScore(examRecordDataId);
+                    scores.add(score);
+                }
+
+                if (markingType.equals("ALL") || markingType.equals("OBJECT_SCORE_MAX")) {
+                    // 全部评阅规则 或 客观分最高规则:取总分最高
+                    effectiveScore = scores.stream()
+                            .sorted((obj1, obj2) -> obj2.getTotalScore().compareTo(obj1.getTotalScore()))
+                            .collect(Collectors.toList())
+                            .get(0);
+                } else {
+                    // 否则:取最后一次的成绩
+                    effectiveScore = scores.stream()
+                            .sorted((obj1, obj2) -> obj2.getId().compareTo(obj1.getId()))
+                            .collect(Collectors.toList())
+                            .get(0);
+                }
+            }
+
+            objectiveScore = effectiveScore.getObjectiveScore();
+            subjectiveScore = effectiveScore.getSubjectiveScore();
+            objectiveAccuracy = effectiveScore.getObjectiveAccuracy();
+            successPercent = effectiveScore.getSuccPercent();
+            totalScore = effectiveScore.getTotalScore();
+
+            //再次判断数据是否已存在
+            if (!existsFinalScore(esId)) {
+                insertFinalScore(effectiveScore.getExamRecordDataId(), esId, objectiveScore, objectiveAccuracy, subjectiveScore, successPercent, totalScore);
+
+                System.out.println(String.format("[examId_stuId_total]:%s_%s_total---success---共%s个考生,当前正在执行第%s条...",
+                        examId, esId,esTotalNum, esIndex));
+            }
+        }
+
+    }
+
+    /**
+     * 插入最终分数
+     * @param examRecordDataId
+     * @param esId
+     * @param objectiveScore
+     * @param objectiveAccuracy
+     * @param subjectiveScore
+     * @param successPercent
+     * @param totalScore
+     */
+    private void insertFinalScore(Long examRecordDataId, Long esId, double objectiveScore, double objectiveAccuracy,
+                                  double subjectiveScore, double successPercent, double totalScore) {
+        String strSql = String.format("INSERT INTO `ec_oe_exam_student_final_score` " +
+                "( `creation_time`, `update_time`, `exam_record_data_id`, `exam_student_id`, " +
+                "`objective_accuracy`, `objective_score`, `subjective_score`, `succ_percent`, `total_score` ) " +
+                "VALUES " +
+                " ( NOW(), NOW(), %d, %d, " +
+                "%s, %s, %s, %s, %s ) ", examRecordDataId, esId, objectiveAccuracy, objectiveScore, subjectiveScore, successPercent, totalScore);
+
+        jdbcTemplate.execute(strSql);
+    }
+
+    /**
+     * 是否存在最终分数
+      * @param estId
+     * @return
+     */
+    private boolean existsFinalScore(Long estId) {
+        String strSql = String.format("select 1 from ec_oe_exam_student_final_score where exam_student_id=%d ", estId);
+
+        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(strSql);
+
+        return !(mapList == null || mapList.isEmpty());
+    }
+
+    /**
+     * 查询考试记录
+     * @param examId
+     * @param courseId
+     * @return
+     */
+    private List<ExamRecordDataEntity> queryExamRecordDataList(Long examId, Long courseId) {
+        String strSql = String.format("SELECT " +
+                "t1.`id`, " +
+                "t1.`exam_student_id`, " +
+                "t1.`course_id`, " +
+                "t1.`is_illegality`, " +
+                "t1.`is_warn`, " +
+                "t1.`is_audit`, " +
+                "t1.exam_record_questions_id " +
+                "FROM " +
+                "ec_oe_exam_record_data t1 " +
+                "WHERE " +
+                "t1.exam_id = %d " +
+                "AND t1.`course_id` =%d ", examId, courseId);
+
+        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(strSql);
+        List<ExamRecordDataEntity> resultList = new ArrayList<>();
+        for (Map<String, Object> map : mapList) {
+            ExamRecordDataEntity entity = new ExamRecordDataEntity();
+            if (map.get("id") != null) {
+                entity.setId(Long.valueOf(map.get("id").toString()));
+            }
+
+            if (map.get("exam_student_id") != null) {
+                entity.setExamStudentId(Long.valueOf(map.get("exam_student_id").toString()));
+            }
+
+            if (map.get("exam_record_questions_id") != null) {
+                entity.setExamRecordQuestionsId(map.get("exam_record_questions_id").toString());
+            }
+
+            if (map.get("course_id") != null) {
+                entity.setCourseId(Long.valueOf(map.get("course_id").toString()));
+            }
+
+            if (map.get("is_illegality") != null) {
+                entity.setIllegality(Boolean.valueOf(map.get("is_illegality").toString()));
+            } else {
+                entity.setIllegality(false);
+            }
+
+            if (map.get("is_warn") != null) {
+                entity.setWarn(Boolean.valueOf(map.get("is_warn").toString()));
+            } else {
+                entity.setWarn(false);
+            }
+
+            if (map.get("is_audit") != null) {
+                entity.setAudit(Boolean.valueOf(map.get("is_audit").toString()));
+            } else {
+                entity.setAudit(false);
+            }
+
+            resultList.add(entity);
+        }
+        return resultList;
+    }
+
+
+    /**
+     * 查询考试下所有考生
+     * @param examId
+     * @param courseId
+     * @return
+     */
+    private List<ExamStudentVO> queryExamStudents(Long examId, Long courseId) {
+        final String columns = "exam_student_id,student_code,student_name,identity_number,org_id,course_id,course_code,course_level,grade,specialty_name,finished";
+        final String querySql = String.format("select %s from ec_oe_exam_student where exam_id = %s and course_id = %s and enable = 1", columns, examId, courseId);
+        return jdbcTemplate.query(querySql, new BeanPropertyRowMapper(ExamStudentVO.class));
+    }
+
+    /**
+     * 查询考试的阅卷类型
+     * @param examId
+     * @param markingTypeKey
+     * @return
+     */
+    private String queryExamMarkingType(Long examId, Long markingTypeKey) {
+        final String querySql = String.format("select value from ec_e_exam_prop where exam_id = %s and key_id = %s", examId, markingTypeKey);
+        try {
+            String result = jdbcTemplate.queryForObject(querySql, String.class);
+            return org.apache.commons.lang3.StringUtils.isNoneBlank(result) ? result.trim() : "ALL";
+        } catch (EmptyResultDataAccessException e) {
+            return "ALL";// 默认值
+        }
+    }
+
+    /**
+     * 查询考试分数
+     * @param examRecordDataId
+     * @return
+     */
+    private ScoreVO queryExamScore(Long examRecordDataId) {
+        final String querySql = String.format("select id,objective_score,subjective_score,total_score,objective_accuracy,succ_percent,total_score from ec_oe_exam_score where exam_record_data_id = %s", examRecordDataId);
+        List<ScoreVO> result = jdbcTemplate.query(querySql, new BeanPropertyRowMapper(ScoreVO.class));
+
+        ScoreVO score;
+        if (CollectionUtils.isNotEmpty(result)) {
+            score = result.get(0);
+        } else {
+            score = new ScoreVO();
+        }
+
+        // 默认值
+        score.setExamRecordDataId(examRecordDataId);
+        score.setObjectiveScore(score.getObjectiveScore() != null ? score.getObjectiveScore() : 0d);
+        score.setSubjectiveScore(score.getSubjectiveScore() != null ? score.getSubjectiveScore() : 0d);
+        score.setTotalScore(score.getTotalScore() != null ? score.getTotalScore() : 0d);
+        score.setObjectiveAccuracy(score.getObjectiveAccuracy() != null ? score.getObjectiveAccuracy() : 0d);
+        score.setSuccPercent(score.getSuccPercent() != null ? score.getSuccPercent() : 0d);
+
+        return score;
+    }
+
+}

+ 51 - 0
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/entity/ExamRecordDataEntity.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.entity;
 
+import javax.persistence.Column;
 import java.io.Serializable;
 
 /**
@@ -25,6 +26,56 @@ public class ExamRecordDataEntity implements Serializable {
      */
     private Long examStudentId;
 
+    /**
+     * 课程id
+     */
+    private Long courseId;
+
+    /**
+     * 是否异常数据
+     */
+    private Boolean isWarn;
+    /**
+     * 是否被审核过
+     */
+    private Boolean isAudit;
+    /**
+     * 是否违纪
+     */
+    private Boolean isIllegality;
+
+    public Boolean getWarn() {
+        return isWarn;
+    }
+
+    public void setWarn(Boolean warn) {
+        isWarn = warn;
+    }
+
+    public Boolean getAudit() {
+        return isAudit;
+    }
+
+    public void setAudit(Boolean audit) {
+        isAudit = audit;
+    }
+
+    public Boolean getIllegality() {
+        return isIllegality;
+    }
+
+    public void setIllegality(Boolean illegality) {
+        isIllegality = illegality;
+    }
+
+    public Long getCourseId() {
+        return courseId;
+    }
+
+    public void setCourseId(Long courseId) {
+        this.courseId = courseId;
+    }
+
     public Long getId() {
         return id;
     }