deason il y a 1 an
Parent
commit
d67ecfc40f

+ 59 - 56
src/main/java/cn/com/qmth/examcloud/tool/service/batch_import_exam_student/BatchImportExamStudentTask.java

@@ -79,7 +79,7 @@ public class BatchImportExamStudentTask implements TaskService {
         }
 
         if (total > 100000) {
-            throw new StatusException("Excel数据条数限制10万条以内!");
+            throw new StatusException("Excel数据条数限制最大10万条!");
         }
 
         // 检验数据
@@ -146,73 +146,49 @@ public class BatchImportExamStudentTask implements TaskService {
         if (!errMessages.isEmpty()) {
             throw new StatusException(StringUtils.join(errMessages, "\n"));
         }
-        long start = System.currentTimeMillis();
 
-        int batchNum = 20;
-        int runCount = 0;
-        AtomicInteger failCount = new AtomicInteger();
-        List<ExamStudentInfo> batchList = new ArrayList<>();
-        for (int i = 0; i < total; i++) {
-            batchList.add(dataList.get(i));
-            runCount++;
-
-            if (i % batchNum == 0) {
-                this.concurrentRun(loginUser, batchList, failCount);
-                batchList.clear();
-
-                long cost = (System.currentTimeMillis() - start) / 1000L;
-                log.info("总数:{} 成功数:{} 失败数:{} 平均{}条每秒 已耗时:{}秒 当前第{}条 进度:{}%"
-                        , total, runCount - failCount.get(), failCount.get(), runCount / (cost + 1)
-                        , cost, runCount, runCount * 100f / total);
+        // 开始执行导入
+        long startTime = System.currentTimeMillis();
+        AtomicInteger successCount = new AtomicInteger(), failCount = new AtomicInteger();
+        if (total <= 1) {
+            this.singleRun(loginUser, dataList, successCount, failCount);
+        } else {
+            final int batchSize = 20;// 分批数量
+            for (int start = 0; start < total; start += batchSize) {
+                int end = Math.min(start + batchSize, total);
+                List<ExamStudentInfo> batchList = dataList.subList(start, end);
+                this.concurrentRun(loginUser, batchList, successCount, failCount);
+
+                long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
+                int runCount = successCount.get() + failCount.get();
+                log.info("总数:{} 成功数:{} 失败数:{} 已执行:{}条 已耗时:{}秒 平均每秒{}条 进度:{}%"
+                        , total, successCount.get(), failCount.get(), runCount, cost
+                        , runCount / cost, runCount * 100f / total);
             }
         }
 
-        if (!batchList.isEmpty()) {
-            this.concurrentRun(loginUser, batchList, failCount);
-            batchList.clear();
-
-            long cost = (System.currentTimeMillis() - start) / 1000L;
-            log.info("总数:{} 成功数:{} 失败数:{} 平均{}条每秒 已耗时:{}秒 当前第{}条 进度:{}%"
-                    , total, runCount - failCount.get(), failCount.get(), runCount / (cost + 1)
-                    , cost, runCount, runCount * 100f / total);
-        }
-
-        long cost = (System.currentTimeMillis() - start) / 1000;
-        String msg = String.format("总数:%s 成功数:%s 失败数:%s 平均%s条每秒 总耗时:%s秒"
-                , total, total - failCount.get(), failCount.get(), total / (cost + 1), cost);
+        long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
+        String msg = String.format("总数:%s 成功数:%s 失败数:%s 平均每秒%s条 总耗时:%s秒"
+                , total, successCount.get(), failCount.get(), total / cost, cost);
         log.info(msg);
         task.setDescription(msg);
 
         dataList.clear();
     }
 
-    private void saveExamStudent(User loginUser, ExamStudentInfo examStudent) {
-        Map<String, String> headers = new HashMap<>();
-        headers.put("key", loginUser.getKey());
-        headers.put("token", loginUser.getToken());
-
-        Map<String, Object> params = new HashMap<>();
-        params.put("examId", examStudent.getExamId());
-        params.put("courseId", examStudent.getCourseId());
-        params.put("orgId", examStudent.getOrgId());
-        params.put("studentName", examStudent.getStudentName());
-        params.put("studentCode", examStudent.getStudentCode());
-        params.put("identityNumber", examStudent.getIdentityNumber());
-        params.put("specialtyName", examStudent.getSpecialtyName());
-        // 其它字段 暂略...
-
-        String url = loginUser.getServerUrl() + "/api/ecs_exam_work/exam_student";
-        String result = HttpHelper.put(url, headers, params);
-        // log.info(result);
-
-        JsonNode value = new JsonMapper().getNode(result);
-        if (value == null || !value.has("id")) {
-            // 创建成功会返回ID,否则失败
-            throw new StatusException(result);
+    private void singleRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount, AtomicInteger failCount) {
+        for (ExamStudentInfo examStudent : batchList) {
+            try {
+                this.saveExamStudent(loginUser, examStudent);
+                successCount.incrementAndGet();
+            } catch (Exception e) {
+                failCount.incrementAndGet();
+                log.error("学号:{} 执行失败!{}", examStudent.getStudentCode(), e.getMessage());
+            }
         }
     }
 
-    private void concurrentRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger failCount) {
+    private void concurrentRun(User loginUser, List<ExamStudentInfo> batchList, AtomicInteger successCount, AtomicInteger failCount) {
         if (batchList.isEmpty()) {
             return;
         }
@@ -226,6 +202,7 @@ public class BatchImportExamStudentTask implements TaskService {
             Runnable runnable = () -> {
                 try {
                     this.saveExamStudent(loginUser, examStudent);
+                    successCount.incrementAndGet();
                 } catch (Exception e) {
                     failCount.incrementAndGet();
                     log.error("学号:{} 执行失败!{}", examStudent.getStudentCode(), e.getMessage());
@@ -241,7 +218,33 @@ public class BatchImportExamStudentTask implements TaskService {
             commander.countDown();
             worker.await();
         } catch (Exception e) {
-            log.error(e.getMessage());
+            log.error("线程异常!{}", e.getMessage());
+        }
+    }
+
+    private void saveExamStudent(User loginUser, ExamStudentInfo examStudent) {
+        Map<String, String> headers = new HashMap<>();
+        headers.put("key", loginUser.getKey());
+        headers.put("token", loginUser.getToken());
+
+        Map<String, Object> params = new HashMap<>();
+        params.put("examId", examStudent.getExamId());
+        params.put("courseId", examStudent.getCourseId());
+        params.put("orgId", examStudent.getOrgId());
+        params.put("studentName", examStudent.getStudentName());
+        params.put("studentCode", examStudent.getStudentCode());
+        params.put("identityNumber", examStudent.getIdentityNumber());
+        params.put("specialtyName", examStudent.getSpecialtyName());
+        // 其它字段 暂略...
+
+        String url = loginUser.getServerUrl() + "/api/ecs_exam_work/exam_student";
+        String result = HttpHelper.put(url, headers, params);
+        // log.info(result);
+
+        JsonNode value = new JsonMapper().getNode(result);
+        if (value == null || !value.has("id")) {
+            // 创建成功会返回ID,否则失败
+            throw new StatusException(result);
         }
     }