deason 9 ヶ月 前
コミット
0fd8e51946

+ 54 - 55
src/main/java/cn/com/qmth/scancentral/service/impl/StudentImportServiceImpl.java

@@ -97,63 +97,64 @@ public class StudentImportServiceImpl implements StudentImportService {
         task.setLogFilePath(logFile.getAbsolutePath());
         IMPORT_TASKS.put(taskId, task);
 
-        ExamEntity exam = examService.getById(examId);
-        if (exam == null) {
-            this.writeLogFile(logFile, "【错误】当前考试不存在");
-            task.setProgress(100d);
-            return;
-        }
+        int total = 0, finishCount = 0;
 
-        if (exam.getYear() == null || exam.getYearHalf() == null) {
-            this.writeLogFile(logFile, "【错误】未设置考试年度或考次");
-            task.setProgress(100d);
-            return;
-        }
+        try {
+            ExamEntity exam = examService.getById(examId);
+            if (exam == null) {
+                this.writeLogFile(logFile, "【错误】当前考试不存在");
+                task.setProgress(100d);
+                return;
+            }
 
-        String fileSuffix = FileUtil.getFileSuffix(fileName);
-        if (!".txt".equals(fileSuffix)) {
-            this.writeLogFile(logFile, "【错误】导入模板目前仅支持后缀名为“.txt”的文件");
-            task.setProgress(100d);
-            return;
-        }
+            if (exam.getYear() == null || exam.getYearHalf() == null) {
+                this.writeLogFile(logFile, "【错误】未设置考试年度或考次");
+                task.setProgress(100d);
+                return;
+            }
 
-        List<String> lines;
-        try {
-            File dataFile = new File(tempDir + taskId + fileSuffix);
-            FileUtils.writeByteArrayToFile(dataFile, fileBytes);
-            lines = FileUtil.readAllLines(dataFile);
-            log.warn("读取考生数据文件共{}行! taskId:{}", lines.size(), taskId);
-        } catch (Exception e) {
-            log.error(e.getMessage());
-            this.writeLogFile(logFile, "【错误】读取导入数据文件失败");
-            task.setProgress(100d);
-            return;
-        }
+            String fileSuffix = FileUtil.getFileSuffix(fileName);
+            if (!".txt".equals(fileSuffix)) {
+                this.writeLogFile(logFile, "【错误】导入模板目前仅支持后缀名为“.txt”的文件");
+                task.setProgress(100d);
+                return;
+            }
 
-        List<StudentImportInfo> list = new ArrayList<>();
-        List<String> errs = this.parseValues(lines, list, examId, exam.getYear(), exam.getYearHalf());
-        log.warn("本次解析考生数据共{}条! errCount:{} taskId:{}", list.size(), errs.size(), taskId);
-        if (!errs.isEmpty()) {
-            errs.add(String.format("本次解析考生数据共%s条,执行导入0条,请先处理文件内容错误!", list.size()));
-            this.writeLogFile(logFile, StringUtils.join(errs, "\n"));
-            task.setProgress(100d);
-            return;
-        }
+            List<String> lines;
+            try {
+                File dataFile = new File(tempDir + taskId + fileSuffix);
+                FileUtils.writeByteArrayToFile(dataFile, fileBytes);
+                lines = FileUtil.readAllLines(dataFile);
+                log.warn("读取考生数据文件共{}行! taskId:{}", lines.size(), taskId);
+            } catch (Exception e) {
+                log.error(e.getMessage());
+                this.writeLogFile(logFile, "【错误】读取导入数据文件失败");
+                task.setProgress(100d);
+                return;
+            }
 
-        if (list.isEmpty()) {
-            this.writeLogFile(logFile, "本次解析考生数据共0条,请填写有效的数据内容!");
-            task.setProgress(100d);
-            return;
-        }
+            List<StudentImportInfo> list = new ArrayList<>();
+            List<String> errs = this.parseValues(lines, list, examId, exam.getYear(), exam.getYearHalf());
+            log.warn("本次解析考生数据共{}条! errCount:{} taskId:{}", list.size(), errs.size(), taskId);
+            if (!errs.isEmpty()) {
+                errs.add(String.format("本次解析考生数据共%s条,执行导入0条,请先处理文件内容错误!", list.size()));
+                this.writeLogFile(logFile, StringUtils.join(errs, "\n"));
+                task.setProgress(100d);
+                return;
+            }
 
-        log.info("执行导入数据库开始... {}", taskId);
-        this.writeLogFile(logFile, "执行导入数据库开始...");
+            if (list.isEmpty()) {
+                this.writeLogFile(logFile, "本次解析考生数据共0条,请填写有效的数据内容!");
+                task.setProgress(100d);
+                return;
+            }
 
-        int finishCount = 0;
-        int total = list.size();
-        long startTime = System.currentTimeMillis();
+            log.info("执行导入数据库开始... {}", taskId);
+            this.writeLogFile(logFile, "执行导入数据库开始...");
+
+            total = list.size();
+            long startTime = System.currentTimeMillis();
 
-        try {
             int batchNum = 1000;
             List<ImportStudentDomain> batchList = new ArrayList<>();
 
@@ -178,16 +179,14 @@ public class StudentImportServiceImpl implements StudentImportService {
             }
 
             long cost = (System.currentTimeMillis() - startTime) / 1000L;
-            log.info("已导入:{}条 已耗时:{}秒 进度:100%", finishCount, cost);
+            log.info("已导入:{}条 已耗时:{}秒 进度:100% taskId:{}", finishCount, cost, taskId);
+            this.writeLogFile(logFile, String.format("本次解析考生数据共%s条,已导入%s条,未导入%s条!", total, finishCount, total - finishCount));
         } catch (Exception e) {
-            log.error(e.getMessage(), e);
-            this.writeLogFile(logFile, "【错误】导入过程异常终止!");
+            log.error("导入过程异常终止,已导入{}条,未导入{}条!taskId:{} err:{}", finishCount, total - finishCount, taskId, e.getMessage(), e);
+            this.writeLogFile(logFile, String.format("【错误】导入过程异常终止,已导入%s条,未导入%s条!", finishCount, total - finishCount));
         } finally {
             task.setProgress(100d);
         }
-
-        log.info("执行导入数据库结束... {}", taskId);
-        this.writeLogFile(logFile, String.format("本次解析考生数据共%s条,成功导入%s条,失败导入%s条!", total, finishCount, total - finishCount));
     }
 
     private ImportStudentDomain ofData(StudentImportInfo info) {