|
@@ -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.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.ExamStudentInfo;
|
|
import cn.com.qmth.examcloud.tool.service.batch_import_exam_student.vo.ExamStudentInfoListener;
|
|
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.HttpHelper;
|
|
import cn.com.qmth.examcloud.tool.utils.JsonMapper;
|
|
import cn.com.qmth.examcloud.tool.utils.JsonMapper;
|
|
import cn.com.qmth.examcloud.tool.utils.StatusException;
|
|
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.alibaba.excel.EasyExcel;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
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.CountDownLatch;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.Executors;
|
|
@@ -54,6 +55,21 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
}
|
|
}
|
|
|
|
|
|
private void execute(User loginUser, TaskEntity task) {
|
|
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());
|
|
JsonNode jsonParams = new JsonMapper().getNode(task.getParams());
|
|
if (jsonParams == null) {
|
|
if (jsonParams == null) {
|
|
throw new StatusException("任务参数解析错误!");
|
|
throw new StatusException("任务参数解析错误!");
|
|
@@ -114,46 +130,46 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
errMessages.add("第" + (i + 1) + "条 机构代码不能为空!");
|
|
errMessages.add("第" + (i + 1) + "条 机构代码不能为空!");
|
|
} else {
|
|
} else {
|
|
examStudent.setOrgCode(examStudent.getOrgCode().trim());
|
|
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())) {
|
|
if (StringUtils.isBlank(examStudent.getCourseCode())) {
|
|
errMessages.add("第" + (i + 1) + "条 课程代码不能为空!");
|
|
errMessages.add("第" + (i + 1) + "条 课程代码不能为空!");
|
|
} else {
|
|
} else {
|
|
examStudent.setCourseCode(examStudent.getCourseCode().trim());
|
|
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) {
|
|
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()) {
|
|
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();
|
|
long startTime = System.currentTimeMillis();
|
|
AtomicInteger successCount = new AtomicInteger(), failCount = new AtomicInteger();
|
|
AtomicInteger successCount = new AtomicInteger(), failCount = new AtomicInteger();
|
|
if (total <= 1000) {
|
|
if (total <= 1000) {
|
|
- this.singleRun(loginUser, dataList, successCount, failCount, task.getId());
|
|
|
|
|
|
+ this.singleRun(loginUser, dataList, successCount, failCount, task.getId(), failRecords);
|
|
} else {
|
|
} else {
|
|
final int batchSize = 20;// 分批数量
|
|
final int batchSize = 20;// 分批数量
|
|
|
|
|
|
@@ -164,7 +180,7 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
for (int start = 0; start < cutTotal; start += batchSize) {
|
|
for (int start = 0; start < cutTotal; start += batchSize) {
|
|
int end = Math.min(start + batchSize, cutTotal);
|
|
int end = Math.min(start + batchSize, cutTotal);
|
|
List<ExamStudentInfo> batchList = list.subList(start, end);
|
|
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);
|
|
long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
|
|
int runCount = successCount.get() + failCount.get();
|
|
int runCount = successCount.get() + failCount.get();
|
|
@@ -180,11 +196,13 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
, total, successCount.get(), failCount.get(), total / cost, cost);
|
|
, total, successCount.get(), failCount.get(), total / cost, cost);
|
|
log.info(msg);
|
|
log.info(msg);
|
|
task.setDescription(msg);
|
|
task.setDescription(msg);
|
|
|
|
+ FileHelper.writeLines(logFile, failRecords, true);
|
|
|
|
|
|
dataList.clear();
|
|
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();
|
|
long startTime = System.currentTimeMillis();
|
|
for (int n = 0; n < batchList.size(); n++) {
|
|
for (int n = 0; n < batchList.size(); n++) {
|
|
ExamStudentInfo examStudent = batchList.get(n);
|
|
ExamStudentInfo examStudent = batchList.get(n);
|
|
@@ -193,9 +211,11 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
successCount.incrementAndGet();
|
|
successCount.incrementAndGet();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
failCount.incrementAndGet();
|
|
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.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
|
|
examStudent.getStudentCode(), e.getMessage());
|
|
examStudent.getStudentCode(), e.getMessage());
|
|
|
|
+ failRecords.add(msg);
|
|
|
|
+ log.error("taskId:{} {}", taskId, msg);
|
|
}
|
|
}
|
|
|
|
|
|
long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
|
|
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()) {
|
|
if (batchList.isEmpty()) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -225,9 +246,11 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
successCount.incrementAndGet();
|
|
successCount.incrementAndGet();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
failCount.incrementAndGet();
|
|
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.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
|
|
examStudent.getStudentCode(), e.getMessage());
|
|
examStudent.getStudentCode(), e.getMessage());
|
|
|
|
+ failRecords.add(msg);
|
|
|
|
+ log.error("taskId:{} {}", taskId, msg);
|
|
} finally {
|
|
} finally {
|
|
worker.countDown();
|
|
worker.countDown();
|
|
}
|
|
}
|