Ver Fonte

add:
1.同时支持xlsx zip模式导入考生

caozixuan há 1 ano atrás
pai
commit
4d2476f548

+ 4 - 0
src/main/java/cn/com/qmth/print/manage/config/PmConstants.java

@@ -18,6 +18,10 @@ public interface PmConstants {
 
     public static final String DEFAULT_ORG_CODE = "qmth";
 
+    public static final String XLSX = ".xlsx";
+
+    public static final String ZIP = ".zip";
+
     /**
      * 默认密码
      */

+ 23 - 5
src/main/java/cn/com/qmth/print/manage/controller/ExamStudentController.java

@@ -1,10 +1,13 @@
 package cn.com.qmth.print.manage.controller;
 
+import cn.com.qmth.print.manage.config.PmConstants;
 import cn.com.qmth.print.manage.service.ExamStudentService;
 import cn.com.qmth.print.manage.service.query.ExamStudentQuery;
 import cn.com.qmth.print.manage.utils.PathUtil;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.core.exception.StatusException;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -13,6 +16,7 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.IOException;
+import java.util.Objects;
 
 /**
  * @Date: 2021/11/16.
@@ -31,8 +35,19 @@ public class ExamStudentController extends BaseController {
      * @return
      */
     @RequestMapping(value = "/import", method = RequestMethod.POST)
+    @Transactional(rollbackFor = Exception.class)
     public Object importFile(@RequestParam Long examId, @RequestParam MultipartFile file) throws IOException {
-        return examStudentService.analyzeZipAndImportStudents(examId, file);
+        String fileName = file.getOriginalFilename();
+        if (fileName == null || fileName.length() == 0) {
+            throw new StatusException("文件名不存在");
+        }
+        if (fileName.endsWith(PmConstants.XLSX)) {
+            return examStudentService.importStudents(examId, file);
+        } else if (fileName.endsWith(PmConstants.ZIP)) {
+            return examStudentService.analyzeZipAndImportStudents(examId, file);
+        } else {
+            throw new StatusException(String.format("只允许导入后缀为[%s,%s]的文件", PmConstants.XLSX, PmConstants.ZIP));
+        }
     }
 
     /**
@@ -57,9 +72,12 @@ public class ExamStudentController extends BaseController {
         return examStudentService.pageStudent(query);
     }
 
-    @GetMapping("/template")
+    @RequestMapping(value = "/template", method = RequestMethod.POST)
     public void getImportTemplate() {
-        String resoucePath = PathUtil.getResoucePath("importtemplates/studentImportTemplate.xlsx");
-        exportFile("考生导入模板.xlsx", new File(resoucePath));
+        String resourcePath = PathUtil.getResoucePath("importtemplates/studentImportTemplate.xlsx");
+        if (Objects.isNull(resourcePath)) {
+            throw new StatusException("未找到模板路径");
+        }
+        exportFile("考生导入模板.xlsx", new File(resourcePath));
     }
-}
+}

+ 17 - 0
src/main/java/cn/com/qmth/print/manage/dto/StudentDTO.java

@@ -2,6 +2,7 @@ package cn.com.qmth.print.manage.dto;
 
 import cn.com.qmth.print.manage.utils.excel.ExcelProperty;
 
+import javax.validation.constraints.NotNull;
 import java.io.Serializable;
 
 public class StudentDTO implements Serializable {
@@ -9,18 +10,23 @@ public class StudentDTO implements Serializable {
     private static final long serialVersionUID = -4556126416794102992L;
 
     @ExcelProperty(index = 1, name = "准考证号", type = 2)
+    @NotNull
     private String examNumber;
 
     @ExcelProperty(index = 2, name = "学号", type = 2)
+    @NotNull
     private String studentCode;
 
     @ExcelProperty(index = 3, name = "姓名", type = 2)
+    @NotNull
     private String name;
 
     @ExcelProperty(index = 4, name = "科目代码", type = 2)
+    @NotNull
     private String courseCode;
 
     @ExcelProperty(index = 5, name = "科目名称", type = 2)
+    @NotNull
     private String courseName;
 
     @ExcelProperty(index = 6, name = "考点", type = 2)
@@ -29,6 +35,9 @@ public class StudentDTO implements Serializable {
     @ExcelProperty(index = 7, name = "考场", type = 2)
     private String examRoom;
 
+    @ExcelProperty(index = 8, name = "考试单元", type = 2)
+    private String examUnit;
+
     public String getExamNumber() {
         return examNumber;
     }
@@ -84,4 +93,12 @@ public class StudentDTO implements Serializable {
     public void setExamRoom(String examRoom) {
         this.examRoom = examRoom;
     }
+
+    public String getExamUnit() {
+        return examUnit;
+    }
+
+    public void setExamUnit(String examUnit) {
+        this.examUnit = examUnit;
+    }
 }

+ 2 - 0
src/main/java/cn/com/qmth/print/manage/entity/ExamStudentEntity.java

@@ -20,8 +20,10 @@ public class ExamStudentEntity extends AuditingWithoutIdEntity {
 
     private String name;
 
+    @ApiModelProperty("学号")
     private String studentCode;
 
+    @ApiModelProperty("准考证号")
     private String examNumber;
 
     /**

+ 62 - 8
src/main/java/cn/com/qmth/print/manage/service/impl/ExamStudentServiceImpl.java

@@ -154,16 +154,44 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
         List<ExcelError> excelErrors = excelReader.reader(file.getInputStream(), obj -> {
             try {
                 StudentDTO dto = (StudentDTO) obj;
+                String nullError = "";
+
+                String examNumber = dto.getExamNumber();
+                String studentCode = dto.getStudentCode();
+                String name = dto.getName();
+                String subjectCode = dto.getCourseCode();
+                String subjectName = dto.getCourseName();
+
+                if (examNumber == null || examNumber.length() == 0) {
+                    nullError = nullError + "[准考证号]";
+                }
+                if (studentCode == null || studentCode.length() == 0) {
+                    nullError = nullError + "[学号]";
+                }
+                if (name == null || name.length() == 0) {
+                    nullError = nullError + "[姓名]";
+                }
+                if (subjectCode == null || subjectCode.length() == 0) {
+                    nullError = nullError + "[科目代码]";
+                }
+                if (subjectName == null || subjectName.length() == 0) {
+                    nullError = nullError + "[科目名称]";
+                }
+                if (nullError.length() > 0) {
+                    throw new StatusException(nullError + "不能为空");
+                }
+
                 ExamStudentEntity examStudentEntity = new ExamStudentEntity();
                 examStudentEntity.setExamId(examId);
                 examStudentEntity.setOrgId(examEntity.getOrgId());
-                examStudentEntity.setExamNumber(dto.getExamNumber());
-                examStudentEntity.setStudentCode(dto.getStudentCode());
-                examStudentEntity.setName(dto.getName());
-                examStudentEntity.setCourseCode(dto.getCourseCode().concat("_").concat(dto.getCourseName()));
+                examStudentEntity.setExamNumber(examNumber);
+                examStudentEntity.setStudentCode(studentCode);
+                examStudentEntity.setName(name);
+                examStudentEntity.setCourseCode(subjectCode.concat(PmConstants.LING_SIGN).concat(subjectName));
                 examStudentEntity.setExamSite(dto.getExamSite());
                 examStudentEntity.setExamRoom(dto.getExamRoom());
                 examStudentEntity.setSortNo(aLong.getAndIncrement());
+                examStudentEntity.setExamUnit(dto.getExamUnit());
                 examStudentEntity.setCreateTime(new Date());
                 examStudentEntity.setUpdateTime(new Date());
                 studentList.add(examStudentEntity);
@@ -227,14 +255,40 @@ public class ExamStudentServiceImpl extends ServiceImpl<ExamStudentDao, ExamStud
             AtomicLong sort = new AtomicLong(1L);
             List<ExcelError> excelErrors = excelReader.reader(FileUtil.getInputStream(studentExcel), obj -> {
                 try {
+                    String nullError = "";
                     ExamStudentImportDTO dto = (ExamStudentImportDTO) obj;
+                    String examNumber = dto.getExamNumber();
+                    String studentCode = dto.getStudentCode();
+                    String name = dto.getStudentName();
+                    String subjectCode = dto.getCourseCode();
+                    String subjectName = dto.getCourseName();
+
+                    if (examNumber == null || examNumber.length() == 0) {
+                        nullError = nullError + "[准考证号]";
+                    }
+                    if (studentCode == null || studentCode.length() == 0) {
+                        nullError = nullError + "[学号]";
+                    }
+                    if (name == null || name.length() == 0) {
+                        nullError = nullError + "[姓名]";
+                    }
+                    if (subjectCode == null || subjectCode.length() == 0) {
+                        nullError = nullError + "[科目代码]";
+                    }
+                    if (subjectName == null || subjectName.length() == 0) {
+                        nullError = nullError + "[科目名称]";
+                    }
+                    if (nullError.length() > 0) {
+                        throw new StatusException(nullError + "不能为空");
+                    }
+
                     ExamStudentEntity examStudentEntity = new ExamStudentEntity();
                     examStudentEntity.setExamId(examId);
                     examStudentEntity.setOrgId(examEntity.getOrgId());
-                    examStudentEntity.setExamNumber(dto.getExamNumber());
-                    examStudentEntity.setStudentCode(dto.getStudentCode());
-                    examStudentEntity.setName(dto.getStudentName());
-                    examStudentEntity.setCourseCode(dto.getCourseCode() + PmConstants.LING_SIGN + dto.getCourseName());
+                    examStudentEntity.setExamNumber(examNumber);
+                    examStudentEntity.setStudentCode(studentCode);
+                    examStudentEntity.setName(name);
+                    examStudentEntity.setCourseCode(subjectCode.concat(PmConstants.LING_SIGN).concat(subjectName));
                     examStudentEntity.setExamSite(dto.getExamPlaceName());
                     examStudentEntity.setSortNo(sort.getAndIncrement());
                     examStudentEntity.setExamUnit(dto.getExamUnit());

+ 21 - 24
src/main/java/cn/com/qmth/print/manage/utils/excel/ExcelReader.java

@@ -23,19 +23,16 @@ import java.util.Objects;
 public class ExcelReader extends ExcelUtils {
 
 	public ExcelReader(Class<?> dataClass) {
-		super(dataClass,true);
+		super(dataClass, true);
 	}
 
 	/**
-	 * 
-	 * @param inputStream
-	 *            输入流
-	 * @param handle
-	 *            单个对象处理器
+	 * @param inputStream 输入流
+	 * @param handle      单个对象处理器
 	 * @return 错误信息集合
 	 */
 	public List<ExcelError> reader(InputStream inputStream,
-			ExcelReaderHandle handle) {
+								   ExcelReaderHandle handle) {
 		List<ExcelError> excelErrors = new ArrayList<ExcelError>();
 		try {
 			Workbook wb = WorkbookFactory.create(inputStream);
@@ -46,7 +43,7 @@ public class ExcelReader extends ExcelUtils {
 				ColumnSetting columnSetting = this.getColumnSettings().get(j);
 				Cell cell = row0.getCell(columnSetting.getIndex() - 1);
 				String obj = String.valueOf(convert(cell));
-				if(!Objects.equals(columnSetting.getHeader(), obj)){
+				if (!Objects.equals(columnSetting.getHeader(), obj)) {
 					ExcelError error = new ExcelError();
 					error.setRow(1);
 					error.setExcelErrorType("表头格式有误,请检查");
@@ -65,7 +62,7 @@ public class ExcelReader extends ExcelUtils {
 					for (int j = 0; j < this.getColumnSettings().size(); j++) {
 						ColumnSetting columnSetting = this.getColumnSettings().get(j);
 						Cell cell = row.getCell(columnSetting.getIndex() - 1);
-						if (Objects.isNull(cell)){
+						if (Objects.isNull(cell)) {
 							// TODO: 2023/10/19 这个地方可以植入@NotNull判断必填报错 
 							continue;
 						}
@@ -80,7 +77,7 @@ public class ExcelReader extends ExcelUtils {
 					e.printStackTrace();
 				} catch (IllegalArgumentException e) {
 					ExcelError error = new ExcelError();
-					error.setRow(i+1);
+					error.setRow(i + 1);
 					error.setExcelErrorType("数据格式有误,请转成文本格式");
 					excelErrors.add(error);
 					return excelErrors;
@@ -89,12 +86,12 @@ public class ExcelReader extends ExcelUtils {
 				}
 				ExcelError error = handle.handle(dto);
 				if (error != null) {
-					error.setRow(i+1);
+					error.setRow(i + 1);
 					excelErrors.add(error);
 				}
 			}
-		}catch (InvalidFormatException e) {
-				e.printStackTrace();
+		} catch (InvalidFormatException e) {
+			e.printStackTrace();
 		} catch (IOException e) {
 			e.printStackTrace();
 		} catch (Exception e) {
@@ -110,17 +107,17 @@ public class ExcelReader extends ExcelUtils {
 	}
 
 	private Object convert(Cell cell) {
-		if(cell!=null){
+		if (cell != null) {
 			switch (cell.getCellType()) {
-			case Cell.CELL_TYPE_STRING:
-				return cell.getStringCellValue().toString();
-			case Cell.CELL_TYPE_BOOLEAN:
-				return String.valueOf(cell.getBooleanCellValue());
-			case Cell.CELL_TYPE_NUMERIC:
-				return Math.round(cell.getNumericCellValue());
+				case Cell.CELL_TYPE_STRING:
+					return cell.getStringCellValue().toString();
+				case Cell.CELL_TYPE_BOOLEAN:
+					return String.valueOf(cell.getBooleanCellValue());
+				case Cell.CELL_TYPE_NUMERIC:
+					return Math.round(cell.getNumericCellValue());
 			}
 		}
-		
+
 		return null;
 	}
 
@@ -157,13 +154,13 @@ public class ExcelReader extends ExcelUtils {
 			case HSSFCell.CELL_TYPE_STRING:
 				cellValue = String.valueOf(cell.getStringCellValue());
 				break;
-			case HSSFCell.CELL_TYPE_FORMULA:
+			case HSSFCell.CELL_TYPE_BOOLEAN:
 				cellValue = String.valueOf(cell.getBooleanCellValue());
 				break;
-			case HSSFCell.CELL_TYPE_BLANK:
+			case HSSFCell.CELL_TYPE_FORMULA:
 				cellValue = String.valueOf(cell.getCellFormula());
 				break;
-			case HSSFCell.CELL_TYPE_BOOLEAN:
+			case HSSFCell.CELL_TYPE_BLANK:
 				cellValue = null;
 				break;
 			case HSSFCell.CELL_TYPE_ERROR:

BIN
src/main/resources/importtemplates/studentImportTemplate.xlsx