|
@@ -5,19 +5,21 @@ import cn.com.qmth.scancentral.service.ExamService;
|
|
|
import cn.com.qmth.scancentral.service.StudentImportService;
|
|
|
import cn.com.qmth.scancentral.service.SubjectService;
|
|
|
import cn.com.qmth.scancentral.util.FileUtil;
|
|
|
+import cn.com.qmth.scancentral.vo.studentimport.ImportTaskVo;
|
|
|
import cn.com.qmth.scancentral.vo.studentimport.StudentImportInfo;
|
|
|
-import com.qmth.boot.tools.uuid.FastUUID;
|
|
|
+import com.qmth.boot.core.exception.ParameterException;
|
|
|
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.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
@Component
|
|
@@ -25,6 +27,8 @@ public class StudentImportServiceImpl implements StudentImportService {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(StudentImportServiceImpl.class);
|
|
|
|
|
|
+ private final static Map<String, ImportTaskVo> IMPORT_TASKS = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
@Autowired
|
|
|
private ExamService examService;
|
|
|
|
|
@@ -33,66 +37,109 @@ public class StudentImportServiceImpl implements StudentImportService {
|
|
|
|
|
|
@Override
|
|
|
public Map<String, Object> studentImportProgress(String taskId) {
|
|
|
+ ImportTaskVo task = IMPORT_TASKS.get(taskId);
|
|
|
+ if (task == null) {
|
|
|
+ throw new ParameterException("考生导入任务不存在");
|
|
|
+ }
|
|
|
+
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("progress", 100);
|
|
|
- result.put("errFileUrl", "");
|
|
|
+ result.put("progress", task.getProgress());
|
|
|
+ if (task.getProgress() == 100d) {
|
|
|
+ result.put("logFileUrl", "/api/admin/student/import/report/" + taskId);
|
|
|
+ }
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String studentImport(Long examId, MultipartFile file) {
|
|
|
- final String taskId = FastUUID.get();
|
|
|
+ public File studentImportReport(String taskId) {
|
|
|
+ ImportTaskVo task = IMPORT_TASKS.get(taskId);
|
|
|
+ if (task == null) {
|
|
|
+ throw new ParameterException("考生导入任务不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (task.getProgress() != 100d) {
|
|
|
+ throw new ParameterException("考生导入报告文件正在生成中");
|
|
|
+ }
|
|
|
+
|
|
|
+ File logFile = new File(task.getLogFilePath());
|
|
|
+ if (!logFile.exists()) {
|
|
|
+ throw new ParameterException("考生导入报告文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ return logFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ @Override
|
|
|
+ public void studentImport(String taskId, Long examId, String fileName, byte[] fileBytes) {
|
|
|
final String tempDir = "temp/import_data/";
|
|
|
FileUtil.makeDirs(tempDir);
|
|
|
File logFile = new File(tempDir + taskId + ".log");
|
|
|
|
|
|
- if (examId == null) {
|
|
|
- this.writeLogFile(logFile, "【错误】考试ID不能为空");
|
|
|
- return taskId;
|
|
|
- }
|
|
|
+ ImportTaskVo task = new ImportTaskVo();
|
|
|
+ task.setTaskId(taskId);
|
|
|
+ task.setProgress(0d);
|
|
|
+ task.setLogFilePath(logFile.getAbsolutePath());
|
|
|
+ IMPORT_TASKS.put(taskId, task);
|
|
|
|
|
|
ExamEntity exam = examService.getById(examId);
|
|
|
if (exam == null) {
|
|
|
this.writeLogFile(logFile, "【错误】当前考试不存在");
|
|
|
- return taskId;
|
|
|
+ task.setProgress(100d);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if (exam.getYear() == null || exam.getYearHalf() == null) {
|
|
|
this.writeLogFile(logFile, "【错误】未设置考试年度或考次");
|
|
|
- return taskId;
|
|
|
+ task.setProgress(100d);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- String fileSuffix = FileUtil.getFileSuffix(file.getOriginalFilename());
|
|
|
+ String fileSuffix = FileUtil.getFileSuffix(fileName);
|
|
|
if (!".txt".equals(fileSuffix)) {
|
|
|
this.writeLogFile(logFile, "【错误】导入模板目前仅支持后缀名为“.txt”的文件");
|
|
|
- return taskId;
|
|
|
+ task.setProgress(100d);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
List<String> lines;
|
|
|
try {
|
|
|
File dataFile = new File(tempDir + taskId + fileSuffix);
|
|
|
- FileUtils.writeByteArrayToFile(dataFile, file.getBytes());
|
|
|
+ FileUtils.writeByteArrayToFile(dataFile, fileBytes);
|
|
|
lines = FileUtil.readAllLines(dataFile);
|
|
|
log.warn("读取考生数据文件共{}行! taskId:{}", lines.size(), taskId);
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
|
this.writeLogFile(logFile, "【错误】读取导入数据文件失败");
|
|
|
- return taskId;
|
|
|
+ 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("本次执行导入0条,请先处理内容错误!");
|
|
|
this.writeLogFile(logFile, StringUtils.join(errs, "\n"));
|
|
|
- return taskId;
|
|
|
+ task.setProgress(100d);
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- // todo insert
|
|
|
- log.info("开始写入数据库...");
|
|
|
+ log.info("执行导入数据库开始... {}", taskId);
|
|
|
+ this.writeLogFile(logFile, "执行导入数据库开始...");
|
|
|
+
|
|
|
+ //to do
|
|
|
+ try {
|
|
|
+ for (int i = 1; i <= list.size(); i++) {
|
|
|
+ Thread.sleep(5000);
|
|
|
+ task.setProgress(i * 100d / list.size());
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
+ log.info("执行导入数据库结束... {}", taskId);
|
|
|
this.writeLogFile(logFile, "本次成功导入0条,失败0条");
|
|
|
- return taskId;
|
|
|
}
|
|
|
|
|
|
private List<String> parseValues(List<String> lines, List<StudentImportInfo> list, Long examId, Integer yearConfig, Integer yearHalfConfig) {
|