|
@@ -1,10 +1,22 @@
|
|
package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
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 org.apache.commons.collections.CollectionUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
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.exception.StatusException;
|
|
|
|
+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.CourseRepo;
|
|
import cn.com.qmth.examcloud.core.basic.dao.CourseRepo;
|
|
import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
import cn.com.qmth.examcloud.core.basic.dao.entity.CourseEntity;
|
|
import cn.com.qmth.examcloud.core.basic.dao.entity.CourseEntity;
|
|
@@ -22,6 +34,8 @@ public class CourseServiceImpl implements CourseService {
|
|
@Autowired
|
|
@Autowired
|
|
OrgRepo orgRepo;
|
|
OrgRepo orgRepo;
|
|
|
|
|
|
|
|
+ private static final String[] EXCEL_HEADER = new String[]{"课程名称", "课程代码", "层次(ZSB,GQZ,ALL)"};
|
|
|
|
+
|
|
/*
|
|
/*
|
|
* 实现
|
|
* 实现
|
|
*
|
|
*
|
|
@@ -117,4 +131,128 @@ public class CourseServiceImpl implements CourseService {
|
|
return saved;
|
|
return saved;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public List<Map<String, Object>> importCourse(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<CourseEntity> 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表头错误");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ boolean hasError = false;
|
|
|
|
+ StringBuilder msg = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ CourseEntity couse = new CourseEntity();
|
|
|
|
+ couse.setRootOrgId(rootOrgId);
|
|
|
|
+ couse.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;
|
|
|
|
+ }
|
|
|
|
+ couse.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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ couse.setCode(code);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ CourseLevel cl = CourseLevel.valueOf(trimAndNullIfBlank(line[2]));
|
|
|
|
+ couse.setLevel(cl);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ msg.append(" 课程层次错误");
|
|
|
|
+ hasError = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (hasError) {
|
|
|
|
+ failRecords.add(newError(i + 1, msg.toString()));
|
|
|
|
+ } else {
|
|
|
|
+ courseList.add(couse);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isEmpty(failRecords)) {
|
|
|
|
+ return failRecords;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (CourseEntity cur : courseList) {
|
|
|
|
+ CourseEntity query = courseRepo.findByRootOrgIdAndCode(cur.getRootOrgId(),
|
|
|
|
+ cur.getCode());
|
|
|
|
+
|
|
|
|
+ if (null != query) {
|
|
|
|
+ query.setName(cur.getName());
|
|
|
|
+ query.setLevel(cur.getLevel());
|
|
|
|
+ courseRepo.save(query);
|
|
|
|
+ } else {
|
|
|
|
+ courseRepo.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 < EXCEL_HEADER.length; i++) {
|
|
|
|
+ if (!EXCEL_HEADER[i].equals(header[i].trim())) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|