wangwei il y a 6 ans
Parent
commit
e941a208b0

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

@@ -16,6 +16,7 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -44,6 +45,7 @@ import com.google.common.collect.Maps;
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnum;
 import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnumManager;
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
 import cn.com.qmth.examcloud.commons.base.util.PropertiesUtil;
 import cn.com.qmth.examcloud.commons.base.util.RegExpUtil;
 import cn.com.qmth.examcloud.commons.web.helpers.page.PageInfo;
@@ -739,4 +741,24 @@ public class OrgController extends ControllerSupport {
 		orgService.deleteSubOrg(orgId, false);
 	}
 
+	@ApiOperation(value = "下载导入模板", notes = "下载导入模板")
+	@GetMapping("importTemplate")
+	public void getDownloadTemplate(HttpServletResponse response) {
+		String resoucePath = PathUtil.getResoucePath("templates/subOrgImportTemplate.xlsx");
+		exportFile("课程导入模板.xlsx", new File(resoucePath));
+	}
+
+	@ApiOperation(value = "导入子机构", notes = "")
+	@PostMapping("importSubOrg")
+	public Map<String, Object> importSubOrg(@RequestParam CommonsMultipartFile file) {
+		DiskFileItem item = (DiskFileItem) file.getFileItem();
+		File storeLocation = item.getStoreLocation();
+		List<Map<String, Object>> failRecords = orgService.importSubOrg(getRootOrgId(),
+				storeLocation);
+		Map<String, Object> map = Maps.newHashMap();
+		map.put("hasError", CollectionUtils.isNotEmpty(failRecords));
+		map.put("failRecords", failRecords);
+		return map;
+	}
+
 }

+ 14 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/OrgService.java

@@ -1,5 +1,9 @@
 package cn.com.qmth.examcloud.core.basic.service;
 
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
 import cn.com.qmth.examcloud.core.basic.dao.entity.OrgEntity;
 import cn.com.qmth.examcloud.core.basic.service.bean.OrgInfo;
 
@@ -39,4 +43,14 @@ public interface OrgService {
 	 */
 	void deleteSubOrg(Long subOrgId, boolean cascade);
 
+	/**
+	 * 导入子机构
+	 *
+	 * @author WANGWEI
+	 * @param rootOrgId
+	 * @param file
+	 * @return
+	 */
+	public List<Map<String, Object>> importSubOrg(Long rootOrgId, File file);
+
 }

+ 127 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/OrgServiceImpl.java

@@ -1,17 +1,25 @@
 package cn.com.qmth.examcloud.core.basic.service.impl;
 
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnum;
 import cn.com.qmth.examcloud.commons.base.helpers.DynamicEnumManager;
+import cn.com.qmth.examcloud.commons.base.helpers.poi.ExcelReader;
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
 import cn.com.qmth.examcloud.core.basic.dao.ExamSiteRepo;
 import cn.com.qmth.examcloud.core.basic.dao.OrgPropertyRepo;
 import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
@@ -49,6 +57,8 @@ public class OrgServiceImpl implements OrgService {
 	@Autowired
 	StudentRepo studentRepo;
 
+	private static final String[] EXCEL_HEADER = new String[]{"专业名称", "专业代码"};
+
 	/*
 	 * 保存顶级机构
 	 *
@@ -273,4 +283,121 @@ public class OrgServiceImpl implements OrgService {
 
 	}
 
+	@Override
+	public List<Map<String, Object>> importSubOrg(Long rootOrgId, File file) {
+
+		List<String[]> lineList = null;
+		try {
+			lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 3);
+		} catch (Exception e) {
+			throw new StatusException("B-100110", "Excel 解析失败");
+		}
+
+		if (CollectionUtils.isEmpty(lineList)) {
+			throw new StatusException("B-100111", "Excel无内容");
+		}
+
+		if (10001 < lineList.size()) {
+			throw new StatusException("B-100112", "数据行数不能超过10000");
+		}
+
+		List<Map<String, Object>> failRecords = Collections
+				.synchronizedList(new ArrayList<Map<String, Object>>());
+
+		List<OrgEntity> courseList = Lists.newArrayList();
+
+		for (int i = 0; i < lineList.size(); i++) {
+			String[] line = lineList.get(i);
+			if (0 == i) {
+				if (headerError(line)) {
+					throw new StatusException("B-100111", "Excel表头错误");
+				}
+				continue;
+			}
+
+			boolean hasError = false;
+			StringBuilder msg = new StringBuilder();
+
+			OrgEntity org = new OrgEntity();
+			org.setParentId(rootOrgId);
+			org.setEnable(true);
+
+			String name = trimAndNullIfBlank(line[0]);
+			if (StringUtils.isBlank(name)) {
+				msg.append("  课程名称不能为空");
+				hasError = true;
+			} else if (name.length() > 30) {
+				msg.append("  课程名称不能超过30个字符");
+				hasError = true;
+			}
+			org.setName(name);
+
+			String code = trimAndNullIfBlank(line[1]);
+			if (StringUtils.isBlank(code)) {
+				msg.append("  课程代码不能为空");
+				hasError = true;
+			} else if (code.length() > 30) {
+				msg.append("  课程代码不能超过30个字符");
+				hasError = true;
+			}
+
+			org.setCode(code);
+
+			if (hasError) {
+				failRecords.add(newError(i + 1, msg.toString()));
+			} else {
+				courseList.add(org);
+			}
+
+		}
+
+		if (CollectionUtils.isNotEmpty(failRecords)) {
+			return failRecords;
+		}
+
+		for (OrgEntity cur : courseList) {
+			OrgInfo info = new OrgInfo();
+			info.setCode(cur.getCode());
+			info.setParentId(cur.getParentId());
+			info.setRootId(cur.getRootId());
+
+			info.setName(cur.getName());
+			info.setContacts(cur.getContacts());
+			info.setTelephone(cur.getTelephone());
+			info.setRemark(cur.getRemark());
+			saveSubOrg(info);
+		}
+
+		return failRecords;
+	}
+
+	private Map<String, Object> newError(int lineNum, String msg) {
+		Map<String, Object> map = Maps.newHashMap();
+		map.put("lineNum", lineNum);
+		map.put("msg", msg);
+		return map;
+	}
+
+	private String trimAndNullIfBlank(String s) {
+		if (StringUtils.isBlank(s)) {
+			return null;
+		}
+		return s.trim();
+	}
+
+	/**
+	 * 方法注释
+	 *
+	 * @author WANGWEI
+	 * @param header
+	 */
+	private boolean headerError(String[] header) {
+		for (int i = 0; i < EXCEL_HEADER.length; i++) {
+			if (!EXCEL_HEADER[i].equals(header[i].trim())) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 }

+ 6 - 6
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/SpecialtyServiceImpl.java

@@ -113,9 +113,9 @@ public class SpecialtyServiceImpl implements SpecialtyService {
 			boolean hasError = false;
 			StringBuilder msg = new StringBuilder();
 
-			SpecialtyEntity couse = new SpecialtyEntity();
-			couse.setRootOrgId(rootOrgId);
-			couse.setEnable(true);
+			SpecialtyEntity specialty = new SpecialtyEntity();
+			specialty.setRootOrgId(rootOrgId);
+			specialty.setEnable(true);
 
 			String name = trimAndNullIfBlank(line[0]);
 			if (StringUtils.isBlank(name)) {
@@ -125,7 +125,7 @@ public class SpecialtyServiceImpl implements SpecialtyService {
 				msg.append("  课程名称不能超过30个字符");
 				hasError = true;
 			}
-			couse.setName(name);
+			specialty.setName(name);
 
 			String code = trimAndNullIfBlank(line[1]);
 			if (StringUtils.isBlank(code)) {
@@ -136,12 +136,12 @@ public class SpecialtyServiceImpl implements SpecialtyService {
 				hasError = true;
 			}
 
-			couse.setCode(code);
+			specialty.setCode(code);
 
 			if (hasError) {
 				failRecords.add(newError(i + 1, msg.toString()));
 			} else {
-				courseList.add(couse);
+				courseList.add(specialty);
 			}
 
 		}