|
@@ -5,13 +5,25 @@ import cn.com.qmth.examcloud.tool.config.SysProperty;
|
|
|
import cn.com.qmth.examcloud.tool.entity.TaskEntity;
|
|
|
import cn.com.qmth.examcloud.tool.service.CommonService;
|
|
|
import cn.com.qmth.examcloud.tool.service.TaskService;
|
|
|
+import cn.com.qmth.examcloud.tool.service.batch_import_exam_student.vo.ExamStudentInfo;
|
|
|
+import cn.com.qmth.examcloud.tool.service.batch_import_exam_student.vo.ExamStudentInfoListener;
|
|
|
+import cn.com.qmth.examcloud.tool.utils.HttpHelper;
|
|
|
+import cn.com.qmth.examcloud.tool.utils.JsonMapper;
|
|
|
import cn.com.qmth.examcloud.tool.utils.StatusException;
|
|
|
import cn.com.qmth.examcloud.tool.vo.user.User;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
@Component
|
|
|
public class BatchImportExamStudentTask implements TaskService {
|
|
|
|
|
@@ -34,7 +46,182 @@ public class BatchImportExamStudentTask implements TaskService {
|
|
|
}
|
|
|
|
|
|
private void execute(User loginUser, TaskEntity task) {
|
|
|
- log.info("todo...");
|
|
|
+ JsonNode jsonParams = new JsonMapper().getNode(task.getParams());
|
|
|
+ if (jsonParams == null) {
|
|
|
+ throw new StatusException("任务参数解析错误!");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long examId = jsonParams.get("examId").asLong();
|
|
|
+ String dataFilePath = jsonParams.get("dataFilePath").asText();
|
|
|
+ if (StringUtils.isBlank(dataFilePath)) {
|
|
|
+ throw new StatusException("参数dataFilePath不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ ExamStudentInfoListener dataListener = new ExamStudentInfoListener();
|
|
|
+ EasyExcel.read(dataFilePath, ExamStudentInfo.class, dataListener).sheet().doRead();
|
|
|
+ List<ExamStudentInfo> dataList = dataListener.getList();
|
|
|
+
|
|
|
+ Map<String, Long> orgMaps = new HashMap<>();
|
|
|
+ Map<String, Long> courseMaps = new HashMap<>();
|
|
|
+
|
|
|
+ int total = dataList.size();
|
|
|
+ boolean hasError = false;
|
|
|
+ for (int i = 0; i < total; i++) {
|
|
|
+ ExamStudentInfo examStudent = dataList.get(i);
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(examStudent.getStudentName())) {
|
|
|
+ log.warn("共{}条 第{}条 姓名不能为空!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(examStudent.getStudentCode())) {
|
|
|
+ log.warn("共{}条 第{}条 学号不能为空!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(examStudent.getIdentityNumber())) {
|
|
|
+ log.warn("共{}条 第{}条 身份证号不能为空!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(examStudent.getOrgCode())) {
|
|
|
+ log.warn("共{}条 第{}条 机构代码不能为空!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(examStudent.getCourseCode())) {
|
|
|
+ log.warn("共{}条 第{}条 课程代码不能为空!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ Long orgId = orgMaps.get(examStudent.getOrgCode());
|
|
|
+ if (orgId == null) {
|
|
|
+ orgId = this.queryOrgIdByCode(loginUser, examStudent.getOrgCode());
|
|
|
+ if (orgId == null) {
|
|
|
+ log.warn("共{}条 第{}条 机构代码不正确!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ orgMaps.put(examStudent.getOrgCode(), orgId);
|
|
|
+ }
|
|
|
+
|
|
|
+ Long courseId = courseMaps.get(examStudent.getCourseCode());
|
|
|
+ if (courseId == null) {
|
|
|
+ courseId = this.queryCourseIdByCode(loginUser, examStudent.getCourseCode());
|
|
|
+ if (courseId == null) {
|
|
|
+ log.warn("共{}条 第{}条 课程代码不正确!", total, i + 1);
|
|
|
+ hasError = true;
|
|
|
+ }
|
|
|
+ courseMaps.put(examStudent.getCourseCode(), courseId);
|
|
|
+ }
|
|
|
+
|
|
|
+ examStudent.setExamId(examId);
|
|
|
+ examStudent.setOrgId(orgId);
|
|
|
+ examStudent.setCourseId(courseId);
|
|
|
+ examStudent.setStudentName(examStudent.getStudentName().trim());
|
|
|
+ examStudent.setStudentCode(examStudent.getStudentCode().trim());
|
|
|
+ examStudent.setIdentityNumber(examStudent.getIdentityNumber().trim());
|
|
|
+ examStudent.setOrgCode(examStudent.getOrgCode().trim());
|
|
|
+ examStudent.setCourseCode(examStudent.getCourseCode().trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hasError) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int failCount = 0;
|
|
|
+ for (int i = 0; i < total; i++) {
|
|
|
+ ExamStudentInfo examStudent = dataList.get(i);
|
|
|
+
|
|
|
+ try {
|
|
|
+ this.saveStudent(loginUser, examStudent);
|
|
|
+ log.info("共{}条 第{}条 已执行!{}", total, i + 1, examStudent.getStudentCode());
|
|
|
+ } catch (Exception e) {
|
|
|
+ failCount++;
|
|
|
+ log.error("共{}条 第{}条 执行失败!{} err:{}", total, i + 1, examStudent.getStudentCode(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String msg = String.format("共%s条 成功%s条 失败%s条!", total, total - failCount, failCount);
|
|
|
+ log.warn(msg);
|
|
|
+ task.setDescription(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long queryCourseIdByCode(User loginUser, String courseCode) {
|
|
|
+ String url = loginUser.getServerUrl() + "/api/ecs_core/course/byCode";
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("key", loginUser.getKey());
|
|
|
+ headers.put("token", loginUser.getToken());
|
|
|
+
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("rootOrgId", String.valueOf(loginUser.getRootOrgId()));
|
|
|
+ params.put("code", courseCode);
|
|
|
+
|
|
|
+ String result = HttpHelper.get(url, headers, params);
|
|
|
+ log.info(result);
|
|
|
+
|
|
|
+ JsonNode value = new JsonMapper().getNode(result);
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String id = value.get("id").asText();
|
|
|
+ if (StringUtils.isNotEmpty(id)) {
|
|
|
+ return Long.parseLong(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long queryOrgIdByCode(User loginUser, String orgCode) {
|
|
|
+ String url = loginUser.getServerUrl() + "/api/ecs_core/org/subOrgPage/0/5";
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("key", loginUser.getKey());
|
|
|
+ headers.put("token", loginUser.getToken());
|
|
|
+
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("parentId", String.valueOf(loginUser.getRootOrgId()));
|
|
|
+ params.put("code", orgCode);
|
|
|
+
|
|
|
+ String result = HttpHelper.get(url, headers, params);
|
|
|
+ log.info(result);
|
|
|
+
|
|
|
+ JsonNode value = new JsonMapper().getNode(result);
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ int total = value.get("total").asInt();
|
|
|
+ if (total > 0) {
|
|
|
+ Iterator<JsonNode> list = value.get("list").iterator();
|
|
|
+ while (list.hasNext()) {
|
|
|
+ JsonNode node = list.next();
|
|
|
+ if (orgCode.equals(node.get("code").asText())) {
|
|
|
+ return node.get("id").asLong();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveStudent(User loginUser, ExamStudentInfo examStudent) {
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("key", loginUser.getKey());
|
|
|
+ headers.put("token", loginUser.getToken());
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("examId", examStudent.getExamId());
|
|
|
+ params.put("courseId", examStudent.getCourseId());
|
|
|
+ params.put("orgId", examStudent.getOrgId());
|
|
|
+ params.put("studentName", examStudent.getStudentName());
|
|
|
+ params.put("studentCode", examStudent.getStudentCode());
|
|
|
+ params.put("identityNumber", examStudent.getIdentityNumber());
|
|
|
+ params.put("specialtyName", examStudent.getSpecialtyName());
|
|
|
+
|
|
|
+ String url = loginUser.getServerUrl() + "/api/ecs_exam_work/exam_student";
|
|
|
+ String result = HttpHelper.put(url, headers, params);
|
|
|
+ // log.info(result);
|
|
|
+
|
|
|
+ JsonNode value = new JsonMapper().getNode(result);
|
|
|
+ if (value == null || !value.has("id")) {
|
|
|
+ throw new StatusException(result);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|