deason 6 ماه پیش
والد
کامیت
c8c3cfe862

+ 6 - 0
src/main/java/cn/com/qmth/examcloud/tool/service/CommonService.java

@@ -87,6 +87,9 @@ public class CommonService {
 
 
     public Long queryCourseIdByCode(User loginUser, String courseCode) {
+        if (StringUtils.isBlank(courseCode)) {
+            return null;
+        }
         Map<String, String> headers = new HashMap<>();
         headers.put("key", loginUser.getKey());
         headers.put("token", loginUser.getToken());
@@ -118,6 +121,9 @@ public class CommonService {
     }
 
     public Long queryOrgIdByCode(User loginUser, String orgCode) {
+        if (StringUtils.isBlank(orgCode)) {
+            return null;
+        }
         Map<String, String> headers = new HashMap<>();
         headers.put("key", loginUser.getKey());
         headers.put("token", loginUser.getToken());

+ 54 - 31
src/main/java/cn/com/qmth/examcloud/tool/service/batch_import_exam_student/BatchImportExamStudentTask.java

@@ -7,6 +7,7 @@ import cn.com.qmth.examcloud.tool.service.CommonService;
 import cn.com.qmth.examcloud.tool.service.TaskService;
 import cn.com.qmth.examcloud.tool.service.batch_import_exam_student.vo.ExamStudentInfo;
 import cn.com.qmth.examcloud.tool.service.batch_import_exam_student.vo.ExamStudentInfoListener;
+import cn.com.qmth.examcloud.tool.utils.FileHelper;
 import cn.com.qmth.examcloud.tool.utils.HttpHelper;
 import cn.com.qmth.examcloud.tool.utils.JsonMapper;
 import cn.com.qmth.examcloud.tool.utils.StatusException;
@@ -14,16 +15,16 @@ import cn.com.qmth.examcloud.tool.vo.user.User;
 import com.alibaba.excel.EasyExcel;
 import com.fasterxml.jackson.databind.JsonNode;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.File;
+import java.io.IOException;
+import java.util.*;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -54,6 +55,21 @@ public class BatchImportExamStudentTask implements TaskService {
     }
 
     private void execute(User loginUser, TaskEntity task) {
+        final String logFilePath = "log_task_" + task.getId() + ".txt";
+        task.setFilePath(logFilePath);
+
+        File logFile = new File(sysProperty.getDataDir() + "/" + logFilePath);
+        try {
+            FileHelper.makeDirs(sysProperty.getDataDir());
+            if (logFile.exists()) {
+                //若存在时则先清除旧内容
+                FileUtils.deleteQuietly(logFile);
+            }
+            logFile.createNewFile();
+        } catch (IOException e) {
+            //ignore
+        }
+
         JsonNode jsonParams = new JsonMapper().getNode(task.getParams());
         if (jsonParams == null) {
             throw new StatusException("任务参数解析错误!");
@@ -114,46 +130,46 @@ public class BatchImportExamStudentTask implements TaskService {
                 errMessages.add("第" + (i + 1) + "条 机构代码不能为空!");
             } else {
                 examStudent.setOrgCode(examStudent.getOrgCode().trim());
+                Long orgId = orgMaps.get(examStudent.getOrgCode());
+                if (orgId == null) {
+                    orgId = commonService.queryOrgIdByCode(loginUser, examStudent.getOrgCode());
+                    if (orgId == null) {
+                        errMessages.add("第" + (i + 1) + "条 机构代码不正确!" + examStudent.getOrgCode());
+                    } else {
+                        orgMaps.put(examStudent.getOrgCode(), orgId);
+                    }
+                }
+                examStudent.setOrgId(orgId);
             }
 
             if (StringUtils.isBlank(examStudent.getCourseCode())) {
                 errMessages.add("第" + (i + 1) + "条 课程代码不能为空!");
             } else {
                 examStudent.setCourseCode(examStudent.getCourseCode().trim());
-            }
-
-            Long orgId = orgMaps.get(examStudent.getOrgCode());
-            if (orgId == null) {
-                orgId = commonService.queryOrgIdByCode(loginUser, examStudent.getOrgCode());
-                if (orgId == null) {
-                    errMessages.add("第" + (i + 1) + "条 机构代码不正确!" + examStudent.getOrgCode());
-                } else {
-                    orgMaps.put(examStudent.getOrgCode(), orgId);
-                }
-            }
-            examStudent.setOrgId(orgId);
-
-            Long courseId = courseMaps.get(examStudent.getCourseCode());
-            if (courseId == null) {
-                courseId = commonService.queryCourseIdByCode(loginUser, examStudent.getCourseCode());
+                Long courseId = courseMaps.get(examStudent.getCourseCode());
                 if (courseId == null) {
-                    errMessages.add("第" + (i + 1) + "条 课程代码不正确!" + examStudent.getCourseCode());
-                } else {
-                    courseMaps.put(examStudent.getCourseCode(), courseId);
+                    courseId = commonService.queryCourseIdByCode(loginUser, examStudent.getCourseCode());
+                    if (courseId == null) {
+                        errMessages.add("第" + (i + 1) + "条 课程代码不正确!" + examStudent.getCourseCode());
+                    } else {
+                        courseMaps.put(examStudent.getCourseCode(), courseId);
+                    }
                 }
+                examStudent.setCourseId(courseId);
             }
-            examStudent.setCourseId(courseId);
         }
 
         if (!errMessages.isEmpty()) {
-            throw new StatusException(StringUtils.join(errMessages, "\n"));
+            FileHelper.writeLines(logFile, errMessages);
+            throw new StatusException("检验数据有误!");
         }
 
         // 开始执行导入
+        List<String> failRecords = Collections.synchronizedList(new ArrayList<>());
         long startTime = System.currentTimeMillis();
         AtomicInteger successCount = new AtomicInteger(), failCount = new AtomicInteger();
         if (total <= 1000) {
-            this.singleRun(loginUser, dataList, successCount, failCount, task.getId());
+            this.singleRun(loginUser, dataList, successCount, failCount, task.getId(), failRecords);
         } else {
             final int batchSize = 20;// 分批数量
 
@@ -164,7 +180,7 @@ public class BatchImportExamStudentTask implements TaskService {
                 for (int start = 0; start < cutTotal; start += batchSize) {
                     int end = Math.min(start + batchSize, cutTotal);
                     List<ExamStudentInfo> batchList = list.subList(start, end);
-                    this.concurrentRun(loginUser, batchList, successCount, failCount, task.getId());
+                    this.concurrentRun(loginUser, batchList, successCount, failCount, task.getId(), failRecords);
 
                     long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
                     int runCount = successCount.get() + failCount.get();
@@ -180,11 +196,13 @@ public class BatchImportExamStudentTask implements TaskService {
                 , total, successCount.get(), failCount.get(), total / cost, cost);
         log.info(msg);
         task.setDescription(msg);
+        FileHelper.writeLines(logFile, failRecords, true);
 
         dataList.clear();
     }
 
-    private void singleRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount, AtomicInteger failCount, Long taskId) {
+    private void singleRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount,
+                           AtomicInteger failCount, Long taskId, List<String> failRecords) {
         long startTime = System.currentTimeMillis();
         for (int n = 0; n < batchList.size(); n++) {
             ExamStudentInfo examStudent = batchList.get(n);
@@ -193,9 +211,11 @@ public class BatchImportExamStudentTask implements TaskService {
                 successCount.incrementAndGet();
             } catch (Exception e) {
                 failCount.incrementAndGet();
-                log.error("保存考生失败!taskId:{} examId:{} courseCode:{} identityNumber:{} studentCode:{} {}", taskId,
+                String msg = String.format("保存考生失败!examId:%s courseCode:%s identityNumber:%s studentCode:%s %s",
                         examStudent.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
                         examStudent.getStudentCode(), e.getMessage());
+                failRecords.add(msg);
+                log.error("taskId:{} {}", taskId, msg);
             }
 
             long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
@@ -208,7 +228,8 @@ public class BatchImportExamStudentTask implements TaskService {
         }
     }
 
-    private void concurrentRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount, AtomicInteger failCount, Long taskId) {
+    private void concurrentRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount,
+                               AtomicInteger failCount, Long taskId, List<String> failRecords) {
         if (batchList.isEmpty()) {
             return;
         }
@@ -225,9 +246,11 @@ public class BatchImportExamStudentTask implements TaskService {
                     successCount.incrementAndGet();
                 } catch (Exception e) {
                     failCount.incrementAndGet();
-                    log.error("保存考生失败!taskId:{} examId:{} courseCode:{} identityNumber:{} studentCode:{} {}", taskId,
+                    String msg = String.format("保存考生失败!examId:%s courseCode:%s identityNumber:%s studentCode:%s %s",
                             examStudent.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
                             examStudent.getStudentCode(), e.getMessage());
+                    failRecords.add(msg);
+                    log.error("taskId:{} {}", taskId, msg);
                 } finally {
                     worker.countDown();
                 }

+ 12 - 0
src/main/java/cn/com/qmth/examcloud/tool/utils/FileHelper.java

@@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
 import java.io.*;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
@@ -104,6 +105,17 @@ public class FileHelper {
         }
     }
 
+    public static void writeLine(File file, String content) {
+        if (StringUtils.isEmpty(content)) {
+            return;
+        }
+        try {
+            FileHelper.writeLines(file, Collections.singletonList(content), true);
+        } catch (Exception e) {
+            throw e;
+        }
+    }
+
     /**
      * 文件压缩
      *

+ 1 - 1
src/main/resources/templates/admin/taskList.ftlh

@@ -157,7 +157,7 @@
                 window.location.href = url;
             },
             disableDownload(row) {
-                if (row.status === "FINISHED" && row.filePath) {
+                if (row.filePath) {
                     return false;
                 }
                 return true;