WANG 6 年 前
コミット
d38baa6074

+ 24 - 0
examcloud-core-examwork-api-provider/src/main/java/cn/com/qmth/examcloud/core/examwork/api/controller/ExamController.java

@@ -18,6 +18,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.fileupload.disk.DiskFileItem;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -38,6 +39,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -1157,6 +1159,28 @@ public class ExamController extends ControllerSupport {
 		FileUtils.deleteQuietly(file);
 	}
 
+	@ApiOperation(value = "导入学习中心设置", notes = "导入")
+	@PostMapping("importExamOrgSettings/{examId}")
+	public Map<String, Object> importExamOrgSettings(@PathVariable Long examId,
+			@RequestParam CommonsMultipartFile file) {
+
+		ExamEntity exam = examRepo.findOne(examId);
+		if (null == exam) {
+			throw new StatusException("E-001010", "考试不存在");
+		}
+
+		validateRootOrgIsolation(exam.getRootOrgId());
+
+		DiskFileItem item = (DiskFileItem) file.getFileItem();
+		File storeLocation = item.getStoreLocation();
+		List<Map<String, Object>> failRecords = examService.importExamOrgSettings(examId,
+				storeLocation);
+		Map<String, Object> map = Maps.newHashMap();
+		map.put("hasError", CollectionUtils.isNotEmpty(failRecords));
+		map.put("failRecords", failRecords);
+		return map;
+	}
+
 	/**
 	 * 方法注释
 	 *

+ 14 - 0
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/ExamService.java

@@ -1,5 +1,9 @@
 package cn.com.qmth.examcloud.core.examwork.service;
 
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
 import cn.com.qmth.examcloud.commons.web.enums.DataExecutionStatus;
 import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamEntity;
 import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamOrgSettingsEntity;
@@ -44,4 +48,14 @@ public interface ExamService {
 	 */
 	String getOrgProperty(Long examId, Long orgId, String key);
 
+	/**
+	 * 导入学习中心设置
+	 *
+	 * @author WANGWEI
+	 * @param examId
+	 * @param file
+	 * @return
+	 */
+	List<Map<String, Object>> importExamOrgSettings(Long examId, File file);
+
 }

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

@@ -1,18 +1,29 @@
 package cn.com.qmth.examcloud.core.examwork.service.impl;
 
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+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.data.domain.Example;
 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.DateUtil;
+import cn.com.qmth.examcloud.commons.base.util.DateUtil.DatePatterns;
+import cn.com.qmth.examcloud.commons.base.util.PathUtil;
 import cn.com.qmth.examcloud.commons.web.enums.DataExecutionStatus;
 import cn.com.qmth.examcloud.core.basic.api.OrgCloudService;
 import cn.com.qmth.examcloud.core.basic.api.bean.OrgBean;
@@ -63,6 +74,9 @@ public class ExamServiceImpl implements ExamService {
 	@Autowired
 	DataSyncCloudService dataSyncCloudService;
 
+	private static final String[] EXAM_ORG_SETTINGS_EXCEL_HEADER = new String[]{"学习中心ID", "学习中心代码",
+			"学习中心名称", "是否可以考试", "开始考试时间", "结束考试时间"};
+
 	/**
 	 * 方法注释
 	 *
@@ -370,4 +384,165 @@ public class ExamServiceImpl implements ExamService {
 
 		return examPropertyEntity.getValue();
 	}
+
+	@Override
+	public List<Map<String, Object>> importExamOrgSettings(Long examId, File file) {
+
+		ExamEntity examEntity = examRepo.findOne(examId);
+		List<String[]> lineList = null;
+		try {
+			lineList = ExcelReader.readSheetBySax(PathUtil.getCanonicalPath(file), 1, 6);
+		} catch (Exception e) {
+			throw new StatusException("B-200110", "Excel 解析失败");
+		}
+
+		if (CollectionUtils.isEmpty(lineList)) {
+			throw new StatusException("B-200111", "Excel无内容");
+		}
+
+		if (10001 < lineList.size()) {
+			throw new StatusException("B-200112", "数据行数不能超过10000");
+		}
+
+		List<Map<String, Object>> failRecords = Collections
+				.synchronizedList(new ArrayList<Map<String, Object>>());
+		List<ExamOrgSettingsEntity> orgSettingsList = 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();
+
+			ExamOrgSettingsEntity orgSettings = new ExamOrgSettingsEntity();
+			orgSettings.setRootOrgId(examEntity.getRootOrgId());
+			orgSettings.setExamId(examId);
+
+			String orgId = trimAndNullIfBlank(line[0]);
+			if (StringUtils.isBlank(orgId)) {
+				msg.append("  学习中心ID不能为空");
+				hasError = true;
+			} else {
+				try {
+					long orgIdLong = Long.parseLong(orgId);
+
+					GetOrgReq getOrgReq = new GetOrgReq();
+					getOrgReq.setOrgId(orgIdLong);
+					GetOrgResp getOrgResp = orgCloudService.getOrg(getOrgReq);
+					OrgBean orgBean = getOrgResp.getOrg();
+
+					if (null != orgBean.getParentId()
+							|| !orgBean.getRootId().equals(examEntity.getRootOrgId())) {
+						msg.append("  学习中心ID非法");
+						hasError = true;
+					} else {
+						orgSettings.setOrgId(orgIdLong);
+					}
+				} catch (NumberFormatException e) {
+					msg.append("  学习中心ID必须为整数");
+					hasError = true;
+				}
+			}
+
+			String examLimit = trimAndNullIfBlank(line[3]);
+			if (StringUtils.isNotBlank(examLimit)) {
+				if (examLimit.equals("是")) {
+					orgSettings.setExamLimit(false);
+				} else if (examLimit.equals("否")) {
+					orgSettings.setExamLimit(true);
+				} else {
+					msg.append("  是否可以开始考试必须为['是','否',空]");
+					hasError = true;
+				}
+			}
+
+			String beginTime = trimAndNullIfBlank(line[4]);
+			if (StringUtils.isNotBlank(beginTime)) {
+				try {
+					Date beginDate = DateUtil.parse(beginTime, DatePatterns.ISO);
+					orgSettings.setBeginTime(beginDate);
+				} catch (Exception e) {
+					msg.append("  开始考试时间格式错误. 正确格式为 " + DatePatterns.ISO);
+					hasError = true;
+				}
+			}
+			String endTime = trimAndNullIfBlank(line[5]);
+			if (StringUtils.isNotBlank(endTime)) {
+				try {
+					Date endDate = DateUtil.parse(endTime, DatePatterns.ISO);
+					orgSettings.setBeginTime(endDate);
+				} catch (Exception e) {
+					msg.append("  结束考试时间格式错误. 正确格式为 " + DatePatterns.ISO);
+					hasError = true;
+				}
+			}
+
+			if (hasError) {
+				failRecords.add(newError(i + 1, msg.toString()));
+			} else {
+				orgSettingsList.add(orgSettings);
+			}
+
+		}
+
+		if (CollectionUtils.isNotEmpty(failRecords)) {
+			return failRecords;
+		}
+
+		for (ExamOrgSettingsEntity cur : orgSettingsList) {
+			ExamOrgSettingsEntity query = examOrgSettingsRepo.findByExamIdAndOrgId(examId,
+					cur.getOrgId());
+
+			if (null != query) {
+				query.setExamLimit(cur.getExamLimit());
+				query.setBeginTime(cur.getBeginTime());
+				query.setEndTime(cur.getEndTime());
+				examOrgSettingsRepo.save(query);
+			} else {
+				examOrgSettingsRepo.save(cur);
+			}
+		}
+
+		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 < EXAM_ORG_SETTINGS_EXCEL_HEADER.length; i++) {
+			if (null == header[i]) {
+				return true;
+			}
+			if (!EXAM_ORG_SETTINGS_EXCEL_HEADER[i].equals(header[i].trim())) {
+				return true;
+			}
+		}
+		return false;
+	}
+
 }