WANG 6 éve
szülő
commit
e10d9a4898

+ 85 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/StudentController.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.core.basic.api.controller;
 
+import java.io.File;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -8,6 +9,7 @@ import java.util.stream.Stream;
 
 import javax.persistence.criteria.Predicate;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -27,7 +29,9 @@ import org.springframework.web.bind.annotation.RestController;
 import com.google.common.collect.Lists;
 
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.commons.base.helpers.poi.ExcelWriter;
 import cn.com.qmth.examcloud.commons.base.util.PropertiesUtil;
+import cn.com.qmth.examcloud.commons.web.config.SystemConfig;
 import cn.com.qmth.examcloud.commons.web.enums.BooleanSelect;
 import cn.com.qmth.examcloud.commons.web.helpers.page.PageInfo;
 import cn.com.qmth.examcloud.commons.web.security.bean.User;
@@ -69,6 +73,9 @@ public class StudentController extends ControllerSupport {
 	@Autowired
 	DataSyncCloudService dataSyncCloudService;
 
+	private static final String[] EXCEL_HEADER = new String[]{"ID", "姓名", "学号", "身份证", "学习中心代码",
+			"学习中心名称"};
+
 	/**
 	 * 方法注释
 	 *
@@ -175,6 +182,84 @@ public class StudentController extends ControllerSupport {
 
 	}
 
+	@ApiOperation(value = "导出学生", notes = "")
+	@GetMapping("export")
+	public void exportStudents(@RequestParam String name, @RequestParam String studentCode,
+			@RequestParam String identityNumber, @RequestParam(required = false) Long rootOrgId,
+			@RequestParam(required = false) BooleanSelect hasPhoto) {
+
+		User accessUser = getAccessUser();
+		if (null == rootOrgId) {
+			rootOrgId = accessUser.getRootOrgId();
+		}
+
+		validateRootOrgIsolation(rootOrgId);
+
+		final Long finalRootOrgId = rootOrgId;
+
+		Specification<StudentEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), finalRootOrgId));
+
+			if (StringUtils.isNotEmpty(name)) {
+				predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
+			}
+			if (StringUtils.isNotEmpty(studentCode)) {
+				predicates.add(cb.like(root.get("studentCode"), toSqlSearchPattern(studentCode)));
+			}
+			if (StringUtils.isNotEmpty(identityNumber)) {
+				predicates.add(
+						cb.like(root.get("identityNumber"), toSqlSearchPattern(identityNumber)));
+			}
+			if (null != hasPhoto) {
+				Boolean hasPhotoBoolean = hasPhoto.getBoolean();
+				if (null != hasPhotoBoolean) {
+					if (hasPhotoBoolean) {
+						predicates.add(cb.isNotNull(root.get("photoPath")));
+					} else {
+						predicates.add(cb.isNull(root.get("photoPath")));
+					}
+				}
+			}
+			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
+
+		long count = studentRepo.count(specification);
+		if (100000 < count) {
+			throw new StatusException("B-520200", "数据量超过100000,无法导出");
+		}
+
+		List<StudentEntity> studentList = studentRepo.findAll(specification,
+				new Sort(Direction.DESC, "updateTime"));
+
+		List<Object[]> datas = Lists.newArrayList();
+
+		for (StudentEntity cur : studentList) {
+			String orgCode = null;
+			String orgName = null;
+			if (null != cur.getOrgId()) {
+				OrgEntity org = orgRepo.findOne(Long.valueOf(cur.getOrgId()));
+				if (null != org) {
+					orgCode = org.getCode();
+					orgName = org.getName();
+				}
+			}
+			datas.add(new Object[]{cur.getId(), cur.getName(), cur.getStudentCode(),
+					cur.getIdentityNumber(), orgCode, orgName});
+		}
+
+		String filePath = SystemConfig.getTempDataDir() + File.separator
+				+ System.currentTimeMillis() + ".xlsx";
+		File file = new File(filePath);
+
+		ExcelWriter.write(EXCEL_HEADER, new Class[]{Long.class, String.class, String.class,
+				String.class, String.class, String.class}, datas, new File(filePath));
+
+		exportFile("学生-" + getRootOrgId() + ".xlsx", file);
+
+		FileUtils.deleteQuietly(file);
+	}
+
 	/**
 	 * 方法注释
 	 *