|
@@ -7,13 +7,11 @@ 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;
|
|
|
+import cn.com.qmth.examcloud.tool.utils.*;
|
|
|
import cn.com.qmth.examcloud.tool.vo.user.User;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -52,16 +50,21 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
throw new StatusException("用户未登录!");
|
|
|
}
|
|
|
|
|
|
- this.execute(loginUser, task);
|
|
|
- }
|
|
|
+ final String tempDir = "task_" + task.getId();
|
|
|
+ FileHelper.makeDirs(sysProperty.getDataDir() + "/" + tempDir);
|
|
|
+
|
|
|
+ this.execute(loginUser, task, tempDir);
|
|
|
|
|
|
- private void execute(User loginUser, TaskEntity task) {
|
|
|
- final String logFilePath = "log_task_" + task.getId() + ".txt";
|
|
|
- task.setFilePath(logFilePath);
|
|
|
+ // 压缩文件
|
|
|
+ FileHelper.zip(new File(sysProperty.getDataDir() + "/" + tempDir),
|
|
|
+ new File(sysProperty.getDataDir() + "/" + tempDir + ".zip"));
|
|
|
+ task.setFilePath(tempDir + ".zip");
|
|
|
+ }
|
|
|
|
|
|
+ private void execute(User loginUser, TaskEntity task, String tempDir) {
|
|
|
+ final String logFilePath = tempDir + "/task_log_" + task.getId() + ".txt";
|
|
|
File logFile = new File(sysProperty.getDataDir() + "/" + logFilePath);
|
|
|
try {
|
|
|
- FileHelper.makeDirs(sysProperty.getDataDir());
|
|
|
if (logFile.exists()) {
|
|
|
//若存在时则先清除旧内容
|
|
|
FileUtils.deleteQuietly(logFile);
|
|
@@ -89,14 +92,13 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
log.error(e.getMessage());
|
|
|
throw new StatusException("Excel内容解析错误,请使用标准模板!");
|
|
|
}
|
|
|
- List<ExamStudentInfo> dataList = dataListener.getList();
|
|
|
|
|
|
+ List<ExamStudentInfo> dataList = dataListener.getList();
|
|
|
int total = dataList.size();
|
|
|
log.info("Excel共{}条数据!examId:{} dataFile:{}", total, examId, dataFilePath);
|
|
|
if (total == 0) {
|
|
|
- return;
|
|
|
+ throw new StatusException("Excel共0条数据!");
|
|
|
}
|
|
|
-
|
|
|
if (total > 300000) {
|
|
|
throw new StatusException("Excel的数据限制最大30万条!");
|
|
|
}
|
|
@@ -173,7 +175,9 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
|
|
|
if (!errMessages.isEmpty()) {
|
|
|
FileHelper.writeLines(logFile, errMessages);
|
|
|
- throw new StatusException("检验数据有误!");
|
|
|
+ log.warn("检验数据有误!Excel共{}条 examId:{} taskId:{}", total, examId, task.getId());
|
|
|
+ task.setDescription("检验数据有误!");
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
log.info("检验数据完成!Excel共{}条 【课程_学号_证件号:{}条】【课程_学号:{}条】【课程_证件号:{}条】 examId:{} taskId:{}",
|
|
@@ -222,13 +226,23 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
}
|
|
|
|
|
|
long cost = Math.max((System.currentTimeMillis() - startTime) / 1000L, 1);
|
|
|
- String msg = String.format("Excel共%s条 【课程_学号_证件号:%s条】【课程_学号:%s条】【课程_证件号:%s条】 成功数:%s 失败数:%s 平均每秒%s条 总耗时:%s秒",
|
|
|
+ String msg = String.format("导入数据完成!Excel共%s条 【课程_学号_证件号:%s条】【课程_学号:%s条】【课程_证件号:%s条】 成功数:%s 失败数:%s 平均每秒%s条 总耗时:%s秒",
|
|
|
total, courseStudentCodeIdentityNumbers.size(), courseStudentCodes.size(), courseIdentityNumbers.size(),
|
|
|
successCount.get(), failCount.get(), total / cost, cost);
|
|
|
log.info(msg);
|
|
|
task.setDescription(msg);
|
|
|
FileHelper.writeLines(logFile, failRecords, true);
|
|
|
|
|
|
+ // 汇总错误数据Excel
|
|
|
+ List<ExamStudentInfo> errDataList = dataList.stream().filter(ExamStudentInfo::isHasError).collect(Collectors.toList());
|
|
|
+ final String errDataPath = sysProperty.getDataDir() + "/" + tempDir + "/task_err_" + task.getId() + ".xlsx";
|
|
|
+ List<String> excelHeaders = Lists.newArrayList("姓名", "学号", "身份证号", "机构代码", "机构名称", "课程代码", "课程名称", "专业名称");
|
|
|
+ EasyExcel.write(errDataPath).sheet()
|
|
|
+ .head(ExcelHelper.buildHeaders(excelHeaders))
|
|
|
+ .registerWriteHandler(ExcelHelper.sheetStrategy())
|
|
|
+ .registerWriteHandler(ExcelHelper.styleStrategy())
|
|
|
+ .doWrite(errDataList);
|
|
|
+
|
|
|
dataList.clear();
|
|
|
}
|
|
|
|
|
@@ -252,6 +266,7 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
successCount.incrementAndGet();
|
|
|
} catch (Exception e) {
|
|
|
failCount.incrementAndGet();
|
|
|
+ examStudent.setHasError(true);
|
|
|
String msg = String.format("保存考生失败!examId:%s courseCode:%s identityNumber:%s studentCode:%s %s",
|
|
|
examStudent.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
|
|
|
examStudent.getStudentCode(), e.getMessage());
|
|
@@ -287,6 +302,7 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
successCount.incrementAndGet();
|
|
|
} catch (Exception e) {
|
|
|
failCount.incrementAndGet();
|
|
|
+ examStudent.setHasError(true);
|
|
|
String msg = String.format("保存考生失败!examId:%s courseCode:%s identityNumber:%s studentCode:%s %s",
|
|
|
examStudent.getExamId(), examStudent.getCourseCode(), examStudent.getIdentityNumber(),
|
|
|
examStudent.getStudentCode(), e.getMessage());
|