wangwei před 7 roky
rodič
revize
6b2bd471d4

+ 19 - 10
examcloud-core-examwork-api-provider/src/main/java/cn/com/qmth/examcloud/core/examwork/api/controller/ExamStudentController.java

@@ -1,8 +1,5 @@
 package cn.com.qmth.examcloud.core.examwork.api.controller;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -15,6 +12,7 @@ import javax.persistence.criteria.Predicate;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -46,20 +44,19 @@ import cn.com.qmth.examcloud.common.dto.examwork.CommonExamStudent;
 import cn.com.qmth.examcloud.commons.base.util.ErrorMsg;
 import cn.com.qmth.examcloud.commons.base.util.excel.ExcelError;
 import cn.com.qmth.examcloud.commons.web.helpers.page.PageInfo;
-import cn.com.qmth.examcloud.commons.web.security.bean.User;
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.core.basic.api.StudentCloudService;
 import cn.com.qmth.examcloud.core.basic.api.request.InsertOrUpdateStudentReq;
 import cn.com.qmth.examcloud.core.basic.api.response.InsertOrUpdateStudentResp;
 import cn.com.qmth.examcloud.core.examwork.api.controller.bean.ExamStudentDomain;
 import cn.com.qmth.examcloud.core.examwork.base.util.ExportUtils;
-import cn.com.qmth.examcloud.core.examwork.base.util.ImportUtils;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamStudentRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.bean.ExamStudentAssembler;
 import cn.com.qmth.examcloud.core.examwork.dao.bean.ExamStudentDTO;
 import cn.com.qmth.examcloud.core.examwork.dao.bean.OrgExamInfoDTO;
 import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamStudent;
+import cn.com.qmth.examcloud.core.examwork.service.ExamStudentImportService;
 import cn.com.qmth.examcloud.core.examwork.service.impl.ExamStudentService;
 import cn.com.qmth.examcloud.examwork.api.ExamStudentCloudService;
 import cn.com.qmth.examcloud.examwork.api.bean.ExamStudentBean;
@@ -95,6 +92,9 @@ public class ExamStudentController extends ControllerSupport {
 	@Autowired
 	ExamStudentCloudService examStudentCloudService;
 
+	@Autowired
+	ExamStudentImportService examStudentImportService;
+
 	@GetMapping("/query")
 	public List<ExamStudent> find(
 			@RequestParam(value = "student_id", required = false) Long studentId) {
@@ -396,16 +396,24 @@ public class ExamStudentController extends ControllerSupport {
 		return examId;
 	}
 
+	/**
+	 * 重构
+	 *
+	 * @author WANGWEI
+	 * @param examId
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
 	@ApiOperation(value = "导入考试学生", notes = "导入")
 	@PostMapping("/import")
 	public List<ExcelError> importExamStudent(@RequestParam Long examId,
 			@RequestParam CommonsMultipartFile file) throws Exception {
-		User accessUser = getAccessUser();
 
-		File tempFile = ImportUtils.getUploadFile(file);
-		List<ExcelError> excelErrors = examStudentService.importExamStudent(
-				accessUser.getUserToken(), examId, new FileInputStream(tempFile));
-		return excelErrors;
+		DiskFileItem item = (DiskFileItem) file.getFileItem();
+		examStudentImportService.importExamStudent(item.getStoreLocation(),
+				file.getOriginalFilename());
+		return null;
 	}
 
 	@ApiOperation(value = "下载导入模板", notes = "下载导入模板")
@@ -436,6 +444,7 @@ public class ExamStudentController extends ControllerSupport {
 			list.add(examStudentAssembler.toDTO(c));
 		});
 		ExportUtils.exportEXCEL("课程列表", ExamStudentDTO.class, list, response);
+
 	}
 
 	@ApiOperation(value = "复制考试学生", notes = "复制")

+ 15 - 0
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/ExamStudentTempRepo.java

@@ -0,0 +1,15 @@
+package cn.com.qmth.examcloud.core.examwork.dao;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.repository.query.QueryByExampleExecutor;
+
+import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamStudentTempEntity;
+
+public interface ExamStudentTempRepo
+		extends
+			JpaRepository<ExamStudentTempEntity, Long>,
+			QueryByExampleExecutor<ExamStudentTempEntity>,
+			JpaSpecificationExecutor<ExamStudentTempEntity> {
+
+}

+ 284 - 0
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/entity/ExamStudentTempEntity.java

@@ -0,0 +1,284 @@
+package cn.com.qmth.examcloud.core.examwork.dao.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+import cn.com.qmth.examcloud.commons.web.jpa.JpaEntity;
+
+/**
+ * 考生临时表
+ *
+ * @author WANGWEI
+ * @date 2018年7月13日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@Entity
+@Table(name = "EC_E_EXAM_STUDENT_TMP")
+public class ExamStudentTempEntity extends JpaEntity {
+
+	private static final long serialVersionUID = -6030503185547639031L;
+
+	@Id
+	@GeneratedValue
+	private Long id;
+
+	/**
+	 * 批次ID
+	 */
+	private Long batchId;
+
+	/**
+	 * 姓名
+	 */
+	private String name;
+
+	private Long examId;
+
+	/**
+	 * 学校id
+	 */
+	private Long rootOrgId;
+
+	/**
+	 * 学习中心id
+	 */
+	private Long orgId;
+
+	/**
+	 * 学习中心code
+	 */
+	private String orgCode;
+
+	/**
+	 * 学习中心名称
+	 */
+	private String orgName;
+
+	/**
+	 * 学号
+	 */
+	private String studentCode;
+
+	/**
+	 * 身份证号
+	 */
+	private String identityNumber;
+
+	/**
+	 * 课程code
+	 */
+	private String courseCode;
+
+	/**
+	 * 课程名称
+	 */
+	private String courseName;
+
+	/**
+	 * 课程等级
+	 */
+	private String courseLevel;
+
+	/**
+	 * 试卷类型
+	 */
+	private String paperType;
+
+	/**
+	 * 专业名称
+	 */
+	private String specialtyName;
+
+	/**
+	 * 专业code
+	 */
+	private String specialtyCode;
+
+	/**
+	 * 年级
+	 */
+	private String grade;
+
+	/**
+	 * 学生用户id
+	 */
+	private Long studentId;
+
+	/**
+	 * 考点
+	 */
+	private String examSite;
+
+	/**
+	 * 信息采集人
+	 */
+	private String infoCollector;
+
+	/**
+	 * 学生电话
+	 */
+	private String phone;
+
+	public Long getBatchId() {
+		return batchId;
+	}
+
+	public void setBatchId(Long batchId) {
+		this.batchId = batchId;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Long getExamId() {
+		return examId;
+	}
+
+	public void setExamId(Long examId) {
+		this.examId = examId;
+	}
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+	public Long getOrgId() {
+		return orgId;
+	}
+
+	public void setOrgId(Long orgId) {
+		this.orgId = orgId;
+	}
+
+	public String getOrgCode() {
+		return orgCode;
+	}
+
+	public void setOrgCode(String orgCode) {
+		this.orgCode = orgCode;
+	}
+
+	public String getOrgName() {
+		return orgName;
+	}
+
+	public void setOrgName(String orgName) {
+		this.orgName = orgName;
+	}
+
+	public String getStudentCode() {
+		return studentCode;
+	}
+
+	public void setStudentCode(String studentCode) {
+		this.studentCode = studentCode;
+	}
+
+	public String getIdentityNumber() {
+		return identityNumber;
+	}
+
+	public void setIdentityNumber(String identityNumber) {
+		this.identityNumber = identityNumber;
+	}
+
+	public String getCourseCode() {
+		return courseCode;
+	}
+
+	public void setCourseCode(String courseCode) {
+		this.courseCode = courseCode;
+	}
+
+	public String getCourseName() {
+		return courseName;
+	}
+
+	public void setCourseName(String courseName) {
+		this.courseName = courseName;
+	}
+
+	public String getCourseLevel() {
+		return courseLevel;
+	}
+
+	public void setCourseLevel(String courseLevel) {
+		this.courseLevel = courseLevel;
+	}
+
+	public String getPaperType() {
+		return paperType;
+	}
+
+	public void setPaperType(String paperType) {
+		this.paperType = paperType;
+	}
+
+	public String getSpecialtyName() {
+		return specialtyName;
+	}
+
+	public void setSpecialtyName(String specialtyName) {
+		this.specialtyName = specialtyName;
+	}
+
+	public String getSpecialtyCode() {
+		return specialtyCode;
+	}
+
+	public void setSpecialtyCode(String specialtyCode) {
+		this.specialtyCode = specialtyCode;
+	}
+
+	public String getGrade() {
+		return grade;
+	}
+
+	public void setGrade(String grade) {
+		this.grade = grade;
+	}
+
+	public Long getStudentId() {
+		return studentId;
+	}
+
+	public void setStudentId(Long studentId) {
+		this.studentId = studentId;
+	}
+
+	public String getExamSite() {
+		return examSite;
+	}
+
+	public void setExamSite(String examSite) {
+		this.examSite = examSite;
+	}
+
+	public String getInfoCollector() {
+		return infoCollector;
+	}
+
+	public void setInfoCollector(String infoCollector) {
+		this.infoCollector = infoCollector;
+	}
+
+	public String getPhone() {
+		return phone;
+	}
+
+	public void setPhone(String phone) {
+		this.phone = phone;
+	}
+
+}

+ 16 - 0
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/ExamStudentImportService.java

@@ -0,0 +1,16 @@
+package cn.com.qmth.examcloud.core.examwork.service;
+
+import java.io.File;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年7月13日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public interface ExamStudentImportService {
+	
+	void importExamStudent(File file,String fileName);
+
+}

+ 99 - 0
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/ExamStudentImportServiceImpl.java

@@ -0,0 +1,99 @@
+package cn.com.qmth.examcloud.core.examwork.service.impl;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.google.common.collect.Lists;
+
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.commons.base.helpers.poi.ExcelParser;
+import cn.com.qmth.examcloud.core.examwork.dao.ExamStudentTempRepo;
+import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamStudentTempEntity;
+import cn.com.qmth.examcloud.core.examwork.service.ExamStudentImportService;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年7月13日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+@Service
+public class ExamStudentImportServiceImpl implements ExamStudentImportService {
+
+	private static final String[] EXCEL_HEADER = new String[]{"姓名", "学号", "身份证号", "学习中心代码",
+			"学习中心名称", "课程代码", "课程名称", "试卷类型", "专业", "考点", "信息采集人", "学生电话", "年级"};
+
+	@Autowired
+	ExamStudentTempRepo ExamStudentTempRepo;
+
+	@Override
+	public void importExamStudent(File file, String fileName) {
+		Workbook workBook = ExcelParser.getWorkBook(file, fileName);
+
+		Sheet sheet = workBook.getSheetAt(0);
+
+		List<String[]> lineList = ExcelParser.readSheet(sheet, 13);
+		ExcelParser.close(workBook);
+
+		List<ExamStudentTempEntity> list = Lists.newArrayList();
+		long currentTimeMillis = System.currentTimeMillis();
+
+		for (int i = 0; i < lineList.size(); i++) {
+			String[] line = lineList.get(i);
+			if (0 == i) {
+				checkExcelHeader(line);
+			}
+
+			ExamStudentTempEntity es = new ExamStudentTempEntity();
+			es.setBatchId(currentTimeMillis);
+			es.setName(trimAndNullIfBlank(line[0]));
+			es.setStudentCode(trimAndNullIfBlank(line[1]));
+			es.setIdentityNumber(trimAndNullIfBlank(line[2]));
+			es.setOrgCode(trimAndNullIfBlank(line[3]));
+			es.setOrgName(trimAndNullIfBlank(line[4]));
+			es.setCourseCode(trimAndNullIfBlank(line[5]));
+			es.setCourseName(trimAndNullIfBlank(line[6]));
+			es.setPaperType(trimAndNullIfBlank(line[7]));
+			es.setSpecialtyName(trimAndNullIfBlank(line[8]));
+			es.setExamSite(trimAndNullIfBlank(line[9]));
+			es.setInfoCollector(trimAndNullIfBlank(line[10]));
+			es.setPhone(trimAndNullIfBlank(line[11]));
+			es.setGrade(trimAndNullIfBlank(line[12]));
+
+			list.add(es);
+		}
+
+		ExamStudentTempRepo.save(list);
+
+	}
+
+	/**
+	 * 方法注释
+	 *
+	 * @author WANGWEI
+	 * @param s
+	 * @return
+	 */
+	private String trimAndNullIfBlank(String s) {
+		if (StringUtils.isBlank(s)) {
+			return null;
+		}
+		return s.trim();
+	}
+
+	private void checkExcelHeader(String[] header) {
+		for (int i = 0; i < EXCEL_HEADER.length; i++) {
+			if (!EXCEL_HEADER[i].equals(header[i].trim())) {
+				throw new StatusException("E-651001", "Excel 表头错误");
+			}
+		}
+	}
+
+}