deason há 9 meses atrás
pai
commit
cf019a8c9f

+ 12 - 2
src/main/java/cn/com/qmth/scancentral/controller/admin/StudentImportController.java

@@ -10,12 +10,14 @@ import cn.com.qmth.scancentral.vo.studentimport.StudentCountVo;
 import cn.com.qmth.scancentral.vo.studentimport.StudentImportConfigVo;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.tools.uuid.FastUUID;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -37,8 +39,9 @@ public class StudentImportController extends BaseController {
 
     @ApiOperation(value = "考生导入")
     @PostMapping("/import")
-    public Map<String, Object> studentImport(@RequestParam Long examId, @RequestParam MultipartFile file) {
-        String taskId = studentImportService.studentImport(examId, file);
+    public Map<String, Object> studentImport(@RequestParam Long examId, @RequestParam MultipartFile file) throws Exception {
+        final String taskId = FastUUID.get();
+        studentImportService.studentImport(taskId, examId, file.getOriginalFilename(), file.getBytes());
 
         Map<String, Object> result = new HashMap<>();
         result.put("taskId", taskId);
@@ -51,6 +54,13 @@ public class StudentImportController extends BaseController {
         return studentImportService.studentImportProgress(taskId);
     }
 
+    @ApiOperation(value = "考生导入报告下载", hidden = true)
+    @GetMapping("/import/report/{taskId}")
+    public void importReport(@PathVariable String taskId) {
+        File logFile = studentImportService.studentImportReport(taskId);
+        super.exportFile("考生导入报告.txt", logFile);
+    }
+
     @ApiOperation(value = "查询科目考生数")
     @PostMapping("/count")
     public List<StudentCountVo> countStudent(@RequestParam Long examId) {

+ 4 - 3
src/main/java/cn/com/qmth/scancentral/service/StudentImportService.java

@@ -1,13 +1,14 @@
 package cn.com.qmth.scancentral.service;
 
-import org.springframework.web.multipart.MultipartFile;
-
+import java.io.File;
 import java.util.Map;
 
 public interface StudentImportService {
 
-    String studentImport(Long examId, MultipartFile file);
+    void studentImport(String taskId, Long examId, String fileName, byte[] fileBytes);
 
     Map<String, Object> studentImportProgress(String taskId);
 
+    File studentImportReport(String taskId);
+
 }

+ 67 - 20
src/main/java/cn/com/qmth/scancentral/service/impl/StudentImportServiceImpl.java

@@ -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) {

+ 35 - 0
src/main/java/cn/com/qmth/scancentral/vo/studentimport/ImportTaskVo.java

@@ -0,0 +1,35 @@
+package cn.com.qmth.scancentral.vo.studentimport;
+
+public class ImportTaskVo {
+
+    private String taskId;
+
+    private Double progress;
+
+    private String logFilePath;
+
+    public String getTaskId() {
+        return taskId;
+    }
+
+    public void setTaskId(String taskId) {
+        this.taskId = taskId;
+    }
+
+    public Double getProgress() {
+        return progress;
+    }
+
+    public void setProgress(Double progress) {
+        this.progress = progress;
+    }
+
+    public String getLogFilePath() {
+        return logFilePath;
+    }
+
+    public void setLogFilePath(String logFilePath) {
+        this.logFilePath = logFilePath;
+    }
+
+}