xiatian 7 月之前
父節點
當前提交
9418af8c71

+ 177 - 174
examcloud-task-api-provider/src/main/java/cn/com/qmth/examcloud/task/api/controller/ExamStudentImportController.java

@@ -29,6 +29,7 @@ import com.google.common.collect.Lists;
 
 import cn.com.qmth.examcloud.api.commons.exchange.PageInfo;
 import cn.com.qmth.examcloud.api.commons.security.bean.User;
+import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.commons.util.DateUtil;
 import cn.com.qmth.examcloud.commons.util.DateUtil.DatePatterns;
 import cn.com.qmth.examcloud.commons.util.PathUtil;
@@ -60,179 +61,181 @@ import io.swagger.annotations.ApiOperation;
 @RequestMapping("${$rmp.ctr.task}" + "examStudentImport")
 public class ExamStudentImportController extends ControllerSupport {
 
-	@Autowired
-	SystemProperties systemConfig;
-
-	private static final String EXAM_STUDENT_IMPORT_FILES = "exam_student_import_files";
-
-	@Autowired
-	ExamStudentImportRepo examStudentImportRepo;
-
-	@Autowired
-	OrgCloudService orgCloudService;
-
-	@Autowired
-	ExamCloudService examCloudService;
-
-	@ApiOperation(value = "导入考试学生", notes = "导入")
-	@PostMapping("/import")
-	public void importExamStudent(@RequestParam Long examId,
-			@RequestParam CommonsMultipartFile file) throws Exception {
-		User accessUser = getAccessUser();
-		Long rootOrgId = accessUser.getRootOrgId();
-
-		GetExamReq req = new GetExamReq();
-		req.setId(examId);
-		req.setRootOrgId(accessUser.getRootOrgId());
-		examCloudService.getExam(req);
-
-		DiskFileItem item = (DiskFileItem) file.getFileItem();
-		File storeLocation = item.getStoreLocation();
-		String originalFilename = file.getOriginalFilename();
-
-		String suffix = originalFilename.substring(originalFilename.indexOf("."));
-		long batchId = System.currentTimeMillis();
-		String name = new StringBuilder().append(rootOrgId).append("_").append(examId).append("_")
-				.append(DateUtil.now(DatePatterns.YYYYMMDDHHMMSSSSS)).toString();
-
-		String destFileName = name + suffix;
-		String resultFileName = name + ".txt";
-
-		String destFilePath = PathUtil.getCanonicalPath(
-				systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + destFileName);
-		File destFile = new File(destFilePath);
-
-		FileUtils.copyFile(storeLocation, destFile);
-
-		ExamStudentImportEntity examStudentImport = new ExamStudentImportEntity();
-		examStudentImport.setBatchId(batchId);
-		examStudentImport.setFailNum(0L);
-		examStudentImport.setSuccessNum(0L);
-		examStudentImport.setTotalNum(0L);
-		examStudentImport.setFilePath(destFileName);
-		examStudentImport.setResultFilePath(resultFileName);
-		examStudentImport.setStatus(ExamStudentImportStatus.NONE);
-		examStudentImport.setRootOrgId(rootOrgId);
-		examStudentImport.setExamId(examId);
-		examStudentImport.setFileName(originalFilename);
-
-		examStudentImportRepo.saveAndFlush(examStudentImport);
-	}
-
-	@ApiOperation(value = "查询导入记录", notes = "")
-	@GetMapping("/all/{curPage}/{pageSize}")
-	public PageInfo<ExamStudentImportDomain> getExamStudentImportList(@PathVariable Integer curPage,
-			@PathVariable Integer pageSize) {
-		User accessUser = getAccessUser();
-		Long rootOrgId = accessUser.getRootOrgId();
-
-		Specification<ExamStudentImportEntity> specification = (root, query, cb) -> {
-			List<Predicate> predicates = new ArrayList<>();
-			if (!isSuperAdmin()) {
-				predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
-			}
-
-			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
-		};
-
-		Pageable pageable = PageRequest.of(curPage, pageSize, Sort.Direction.DESC, "creationTime");
-		Page<ExamStudentImportEntity> list = examStudentImportRepo.findAll(specification, pageable);
-
-		List<ExamStudentImportDomain> ret = Lists.newArrayList();
-
-		for (ExamStudentImportEntity cur : list) {
-			ExamStudentImportDomain domain = new ExamStudentImportDomain();
-			domain.setBatchId(cur.getBatchId());
-			domain.setExamId(cur.getExamId());
-			domain.setFailNum(cur.getFailNum());
-			domain.setFileName(cur.getFileName());
-			domain.setFilePath(cur.getFilePath());
-			domain.setId(cur.getId());
-			domain.setResultFilePath(cur.getResultFilePath());
-			domain.setRootOrgId(cur.getRootOrgId());
-			domain.setStatus(cur.getStatus().name());
-			domain.setStatusDesc(cur.getStatus().getDesc());
-			domain.setSuccessNum(cur.getSuccessNum());
-			domain.setTotalNum(cur.getTotalNum());
-			domain.setErrorDesc(cur.getErrorDesc());
-			domain.setCreationTime(cur.getCreationTime());
-			domain.setUpdateTime(cur.getUpdateTime());
-
-			GetExamReq req = new GetExamReq();
-			req.setId(domain.getExamId());
-			req.setRootOrgId(domain.getRootOrgId());
-			GetExamResp examResp = examCloudService.getExam(req);
-			ExamBean examBean = examResp.getExamBean();
-			domain.setExamName(examBean.getName());
-
-			GetOrgReq getOrgReq = new GetOrgReq();
-			getOrgReq.setOrgId(domain.getRootOrgId());
-			getOrgReq.setRootOrgId(rootOrgId);
-			GetOrgResp getOrgResp = orgCloudService.getOrg(getOrgReq);
-			OrgBean org = getOrgResp.getOrg();
-			domain.setRootOrgName(org.getName());
-
-			ret.add(domain);
-		}
-
-		return PageUtils.toPageInfo(list, ret);
-	}
-
-	@ApiOperation(value = "下载报告")
-	@GetMapping("/getReports/{id}")
-	public void importFileTemplate(@PathVariable Long id) {
-		ExamStudentImportEntity entity = null;
-		Optional<ExamStudentImportEntity> opt = examStudentImportRepo.findById(id);
-		if (opt.isPresent()) {
-			entity = opt.get();
-		}
-		String resultFilePath = entity.getResultFilePath();
-
-		if (!isSuperAdmin()) {
-			if (!entity.getRootOrgId().equals(getRootOrgId())) {
-				exportFile(resultFilePath, "非法请求.".getBytes());
-			}
-		}
-
-		String path = PathUtil.getCanonicalPath(
-				systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + resultFilePath);
-
-		File file = new File(path);
-
-		if (!file.exists()) {
-			exportFile(resultFilePath, "报告未生成.".getBytes());
-		}
-
-		exportFile(resultFilePath, file);
-	}
-
-	@ApiOperation(value = "下载导入文件")
-	@GetMapping("/getUploadedFile/{id}")
-	public void getUploadedFile(@PathVariable Long id) {
-		ExamStudentImportEntity entity = null;
-		Optional<ExamStudentImportEntity> opt = examStudentImportRepo.findById(id);
-		if (opt.isPresent()) {
-			entity = opt.get();
-		}
-		String fileName = entity.getFileName();
-
-		if (!isSuperAdmin()) {
-			if (!entity.getRootOrgId().equals(getRootOrgId())) {
-				exportFile(fileName, "非法请求.".getBytes());
-			}
-		}
-
-		String filePath = entity.getFilePath();
-		String path = PathUtil.getCanonicalPath(
-				systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + filePath);
-
-		File file = new File(path);
-
-		if (!file.exists()) {
-			exportFile(fileName, "文件不存在.".getBytes());
-		}
-
-		exportFile(fileName, file);
-	}
+    @Autowired
+    SystemProperties systemConfig;
+
+    private static final String EXAM_STUDENT_IMPORT_FILES = "exam_student_import_files";
+
+    @Autowired
+    ExamStudentImportRepo examStudentImportRepo;
+
+    @Autowired
+    OrgCloudService orgCloudService;
+
+    @Autowired
+    ExamCloudService examCloudService;
+
+    @ApiOperation(value = "导入考试学生", notes = "导入")
+    @PostMapping("/import")
+    public void importExamStudent(@RequestParam Long examId, @RequestParam CommonsMultipartFile file) throws Exception {
+        User accessUser = getAccessUser();
+        Long rootOrgId = accessUser.getRootOrgId();
+
+        GetExamReq req = new GetExamReq();
+        req.setId(examId);
+        req.setRootOrgId(accessUser.getRootOrgId());
+        GetExamResp res = examCloudService.getExam(req);
+        if (res.getExamBean().getArchived()) {
+            throw new StatusException("考试已归档");
+        }
+
+        DiskFileItem item = (DiskFileItem) file.getFileItem();
+        File storeLocation = item.getStoreLocation();
+        String originalFilename = file.getOriginalFilename();
+
+        String suffix = originalFilename.substring(originalFilename.indexOf("."));
+        long batchId = System.currentTimeMillis();
+        String name = new StringBuilder().append(rootOrgId).append("_").append(examId).append("_")
+                .append(DateUtil.now(DatePatterns.YYYYMMDDHHMMSSSSS)).toString();
+
+        String destFileName = name + suffix;
+        String resultFileName = name + ".txt";
+
+        String destFilePath = PathUtil
+                .getCanonicalPath(systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + destFileName);
+        File destFile = new File(destFilePath);
+
+        FileUtils.copyFile(storeLocation, destFile);
+
+        ExamStudentImportEntity examStudentImport = new ExamStudentImportEntity();
+        examStudentImport.setBatchId(batchId);
+        examStudentImport.setFailNum(0L);
+        examStudentImport.setSuccessNum(0L);
+        examStudentImport.setTotalNum(0L);
+        examStudentImport.setFilePath(destFileName);
+        examStudentImport.setResultFilePath(resultFileName);
+        examStudentImport.setStatus(ExamStudentImportStatus.NONE);
+        examStudentImport.setRootOrgId(rootOrgId);
+        examStudentImport.setExamId(examId);
+        examStudentImport.setFileName(originalFilename);
+
+        examStudentImportRepo.saveAndFlush(examStudentImport);
+    }
+
+    @ApiOperation(value = "查询导入记录", notes = "")
+    @GetMapping("/all/{curPage}/{pageSize}")
+    public PageInfo<ExamStudentImportDomain> getExamStudentImportList(@PathVariable Integer curPage,
+            @PathVariable Integer pageSize) {
+        User accessUser = getAccessUser();
+        Long rootOrgId = accessUser.getRootOrgId();
+
+        Specification<ExamStudentImportEntity> specification = (root, query, cb) -> {
+            List<Predicate> predicates = new ArrayList<>();
+            if (!isSuperAdmin()) {
+                predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
+            }
+
+            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+        };
+
+        Pageable pageable = PageRequest.of(curPage, pageSize, Sort.Direction.DESC, "creationTime");
+        Page<ExamStudentImportEntity> list = examStudentImportRepo.findAll(specification, pageable);
+
+        List<ExamStudentImportDomain> ret = Lists.newArrayList();
+
+        for (ExamStudentImportEntity cur : list) {
+            ExamStudentImportDomain domain = new ExamStudentImportDomain();
+            domain.setBatchId(cur.getBatchId());
+            domain.setExamId(cur.getExamId());
+            domain.setFailNum(cur.getFailNum());
+            domain.setFileName(cur.getFileName());
+            domain.setFilePath(cur.getFilePath());
+            domain.setId(cur.getId());
+            domain.setResultFilePath(cur.getResultFilePath());
+            domain.setRootOrgId(cur.getRootOrgId());
+            domain.setStatus(cur.getStatus().name());
+            domain.setStatusDesc(cur.getStatus().getDesc());
+            domain.setSuccessNum(cur.getSuccessNum());
+            domain.setTotalNum(cur.getTotalNum());
+            domain.setErrorDesc(cur.getErrorDesc());
+            domain.setCreationTime(cur.getCreationTime());
+            domain.setUpdateTime(cur.getUpdateTime());
+
+            GetExamReq req = new GetExamReq();
+            req.setId(domain.getExamId());
+            req.setRootOrgId(domain.getRootOrgId());
+            GetExamResp examResp = examCloudService.getExam(req);
+            ExamBean examBean = examResp.getExamBean();
+            domain.setExamName(examBean.getName());
+
+            GetOrgReq getOrgReq = new GetOrgReq();
+            getOrgReq.setOrgId(domain.getRootOrgId());
+            getOrgReq.setRootOrgId(rootOrgId);
+            GetOrgResp getOrgResp = orgCloudService.getOrg(getOrgReq);
+            OrgBean org = getOrgResp.getOrg();
+            domain.setRootOrgName(org.getName());
+
+            ret.add(domain);
+        }
+
+        return PageUtils.toPageInfo(list, ret);
+    }
+
+    @ApiOperation(value = "下载报告")
+    @GetMapping("/getReports/{id}")
+    public void importFileTemplate(@PathVariable Long id) {
+        ExamStudentImportEntity entity = null;
+        Optional<ExamStudentImportEntity> opt = examStudentImportRepo.findById(id);
+        if (opt.isPresent()) {
+            entity = opt.get();
+        }
+        String resultFilePath = entity.getResultFilePath();
+
+        if (!isSuperAdmin()) {
+            if (!entity.getRootOrgId().equals(getRootOrgId())) {
+                exportFile(resultFilePath, "非法请求.".getBytes());
+            }
+        }
+
+        String path = PathUtil
+                .getCanonicalPath(systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + resultFilePath);
+
+        File file = new File(path);
+
+        if (!file.exists()) {
+            exportFile(resultFilePath, "报告未生成.".getBytes());
+        }
+
+        exportFile(resultFilePath, file);
+    }
+
+    @ApiOperation(value = "下载导入文件")
+    @GetMapping("/getUploadedFile/{id}")
+    public void getUploadedFile(@PathVariable Long id) {
+        ExamStudentImportEntity entity = null;
+        Optional<ExamStudentImportEntity> opt = examStudentImportRepo.findById(id);
+        if (opt.isPresent()) {
+            entity = opt.get();
+        }
+        String fileName = entity.getFileName();
+
+        if (!isSuperAdmin()) {
+            if (!entity.getRootOrgId().equals(getRootOrgId())) {
+                exportFile(fileName, "非法请求.".getBytes());
+            }
+        }
+
+        String filePath = entity.getFilePath();
+        String path = PathUtil
+                .getCanonicalPath(systemConfig.getDataDir() + "/" + EXAM_STUDENT_IMPORT_FILES + "/" + filePath);
+
+        File file = new File(path);
+
+        if (!file.exists()) {
+            exportFile(fileName, "文件不存在.".getBytes());
+        }
+
+        exportFile(fileName, file);
+    }
 
 }