|
@@ -238,6 +238,97 @@ public class PaperServiceImpl extends ServiceImpl<PaperDao, PaperEntity> impleme
|
|
|
return vo;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public List<String> importSubjectStruct(Long examId, User user, MultipartFile file) {
|
|
|
+ ExamEntity exam=examService.getById(examId);
|
|
|
+ if(exam==null) {
|
|
|
+ throw new StatusException("未找到考试批次");
|
|
|
+ }
|
|
|
+ if (!user.getRole().equals(Role.SUPER_ADMIN) && !user.getSchoolId().equals(exam.getSchoolId())) {
|
|
|
+ throw new StatusException("没有权限");
|
|
|
+ }
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream=file.getInputStream();
|
|
|
+ List<DataMap> lineList = ExcelReader.create(ExcelType.XLSX, inputStream, 0).getDataMapList();
|
|
|
+ if (CollectionUtils.isEmpty(lineList)) {
|
|
|
+ throw new StatusException("Excel无内容");
|
|
|
+ }
|
|
|
+ if (1001 < lineList.size()) {
|
|
|
+ throw new StatusException("数据行数不能超过1000");
|
|
|
+ }
|
|
|
+ List<String> failRecords = new ArrayList<>();
|
|
|
+ List<PaperEntity> ret = new ArrayList<>();
|
|
|
+ for (int i = 0; i < lineList.size(); i++) {
|
|
|
+ DataMap line = lineList.get(i);
|
|
|
+
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
+
|
|
|
+ PaperEntity imp = new PaperEntity();
|
|
|
+ imp.setTotalScore(0.0);
|
|
|
+ imp.setObjectiveScore(0.0);
|
|
|
+ imp.setSubjectiveScore(0.0);
|
|
|
+ imp.setSchoolId(exam.getSchoolId());
|
|
|
+ imp.setGroupFinish(false);
|
|
|
+ imp.setExamId(examId);
|
|
|
+ String code = trimAndNullIfBlank(line.getValue(0));
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ msg.append(" 科目代码不能为空");
|
|
|
+ } else if (code.length() > 20) {
|
|
|
+ msg.append(" 科目代码不能超过20个字符");
|
|
|
+ }
|
|
|
+
|
|
|
+ String name = trimAndNullIfBlank(line.getValue(1));
|
|
|
+ if (StringUtils.isBlank(name)) {
|
|
|
+ msg.append(" 科目名称不能为空");
|
|
|
+ } else if (name.length() > 20) {
|
|
|
+ msg.append(" 科目名称不能超过20个字符");
|
|
|
+ }
|
|
|
+ if (msg.length() == 0) {
|
|
|
+ CourseEntity course=courseService.saveOrGet(exam.getSchoolId(), code, name);
|
|
|
+ imp.setCourseId(course.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (msg.length() > 0) {
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
+ } else {
|
|
|
+ ret.add(imp);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ return failRecords;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < ret.size(); i++) {
|
|
|
+ PaperEntity cur = ret.get(i);
|
|
|
+ try {
|
|
|
+ this.save(cur);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ failRecords.add(newError(i + 1, "科目已存在"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ failRecords.add(newError(i + 1, "系统异常"));
|
|
|
+ log.error("科目导入系统异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isNotEmpty(failRecords)) {
|
|
|
+ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ }
|
|
|
+ return failRecords;
|
|
|
+ } catch (StatusException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("系统错误",e);
|
|
|
+ } finally {
|
|
|
+ if(inputStream!=null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|