deason 1 年之前
父節點
當前提交
7765b34ea7

+ 19 - 0
src/main/java/cn/com/qmth/examcloud/tool/controller/IndexController.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.tool.controller;
 
+import cn.com.qmth.examcloud.tool.config.Constants;
 import cn.com.qmth.examcloud.tool.enums.TaskStatus;
 import cn.com.qmth.examcloud.tool.enums.TaskType;
 import cn.com.qmth.examcloud.tool.service.CommonService;
@@ -8,6 +9,10 @@ import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.net.URL;
+
 @Controller
 public class IndexController extends BaseController {
 
@@ -83,4 +88,18 @@ public class IndexController extends BaseController {
         return "admin/batchImportExamStudent";
     }
 
+    @GetMapping("/admin/downloadTemplate/importExamStudent")
+    public void downloadTemplate1(HttpServletResponse response) {
+        URL url = Constants.class.getClassLoader().getResource("templates/import_exam_student.xlsx");
+        File file = new File(url.getPath());
+        super.download(response, file, "考生导入模板.xlsx");
+    }
+
+    @GetMapping("/admin/downloadTemplate/importUser")
+    public void downloadTemplate2(HttpServletResponse response) {
+        URL url = Constants.class.getClassLoader().getResource("templates/import_user.xlsx");
+        File file = new File(url.getPath());
+        super.download(response, file, "用户账号创建模板.xlsx");
+    }
+
 }

+ 76 - 10
src/main/java/cn/com/qmth/examcloud/tool/service/CommonService.java

@@ -8,16 +8,14 @@ import cn.com.qmth.examcloud.tool.utils.JsonMapper;
 import cn.com.qmth.examcloud.tool.vo.Pager;
 import cn.com.qmth.examcloud.tool.vo.user.User;
 import com.fasterxml.jackson.core.type.TypeReference;
+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.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Component
 public class CommonService {
@@ -61,21 +59,20 @@ public class CommonService {
     /**
      * 获取考试相关的课程列表
      */
-    public List<CourseVO> getExamCourseList(User user, Long examId) {
+    public List<CourseVO> getExamCourseList(User loginUser, Long examId) {
         Map<String, Object> params = new HashMap<>();
         params.put("examId", examId);
         params.put("pageNo", 1);
         params.put("pageSize", 1000);
 
         Map<String, String> headers = new HashMap<>();
-        headers.put("key", user.getKey());
-        headers.put("token", user.getToken());
+        headers.put("key", loginUser.getKey());
+        headers.put("token", loginUser.getToken());
 
-        String url = user.getServerUrl() + "/api/ecs_exam_work/exam/course/list";
+        String url = loginUser.getServerUrl() + "/api/ecs_exam_work/exam/course/list";
         String json = HttpHelper.post(url, headers, params);
 
-        JsonMapper jsonMapper = new JsonMapper();
-        Pager<CourseVO> page = jsonMapper.parseJson(json, new TypeReference<Pager<CourseVO>>() {
+        Pager<CourseVO> page = new JsonMapper().parseJson(json, new TypeReference<Pager<CourseVO>>() {
 
         });
 
@@ -86,4 +83,73 @@ public class CommonService {
         return new ArrayList<>();
     }
 
+
+    public 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;
+        try {
+            result = HttpHelper.get(url, headers, params);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            return null;
+        }
+
+        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;
+    }
+
+    public 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;
+        try {
+            result = HttpHelper.get(url, headers, params);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            return null;
+        }
+
+        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;
+    }
+
 }

+ 40 - 91
src/main/java/cn/com/qmth/examcloud/tool/service/batch_import_exam_student/BatchImportExamStudentTask.java

@@ -19,8 +19,8 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -61,67 +61,72 @@ public class BatchImportExamStudentTask implements TaskService {
         EasyExcel.read(dataFilePath, ExamStudentInfo.class, dataListener).sheet().doRead();
         List<ExamStudentInfo> dataList = dataListener.getList();
 
+        int total = dataList.size();
+        log.info("共{}条数据!examId:{}", total, examId);
+
+        // 检验数据
         Map<String, Long> orgMaps = new HashMap<>();
         Map<String, Long> courseMaps = new HashMap<>();
-
-        int total = dataList.size();
-        boolean hasError = false;
+        List<String> errMessages = new ArrayList<>();
         for (int i = 0; i < total; i++) {
             ExamStudentInfo examStudent = dataList.get(i);
+            examStudent.setExamId(examId);
 
             if (StringUtils.isBlank(examStudent.getStudentName())) {
-                log.warn("共{}条 第{}条 姓名不能为空!", total, i + 1);
-                hasError = true;
+                errMessages.add("第" + (i + 1) + "条 姓名不能为空!");
+            } else {
+                examStudent.setStudentName(examStudent.getStudentName().trim());
             }
+
             if (StringUtils.isBlank(examStudent.getStudentCode())) {
-                log.warn("共{}条 第{}条 学号不能为空!", total, i + 1);
-                hasError = true;
+                errMessages.add("第" + (i + 1) + "条 学号不能为空!");
+            } else {
+                examStudent.setStudentCode(examStudent.getStudentCode().trim());
             }
+
             if (StringUtils.isBlank(examStudent.getIdentityNumber())) {
-                log.warn("共{}条 第{}条 身份证号不能为空!", total, i + 1);
-                hasError = true;
+                errMessages.add("第" + (i + 1) + "条 身份证号不能为空!");
+            } else {
+                examStudent.setIdentityNumber(examStudent.getIdentityNumber().trim());
             }
+
             if (StringUtils.isBlank(examStudent.getOrgCode())) {
-                log.warn("共{}条 第{}条 机构代码不能为空!", total, i + 1);
-                hasError = true;
+                errMessages.add("第" + (i + 1) + "条 机构代码不能为空!");
+            } else {
+                examStudent.setOrgCode(examStudent.getOrgCode().trim());
             }
+
             if (StringUtils.isBlank(examStudent.getCourseCode())) {
-                log.warn("共{}条 第{}条 课程代码不能为空!", total, i + 1);
-                hasError = true;
+                errMessages.add("第" + (i + 1) + "条 课程代码不能为空!");
+            } else {
+                examStudent.setCourseCode(examStudent.getCourseCode().trim());
             }
 
             Long orgId = orgMaps.get(examStudent.getOrgCode());
             if (orgId == null) {
-                orgId = this.queryOrgIdByCode(loginUser, examStudent.getOrgCode());
+                orgId = commonService.queryOrgIdByCode(loginUser, examStudent.getOrgCode());
                 if (orgId == null) {
-                    log.warn("共{}条 第{}条 机构代码不正确!", total, i + 1);
-                    hasError = true;
+                    errMessages.add("第" + (i + 1) + "条 机构代码不正确!");
+                } else {
+                    orgMaps.put(examStudent.getOrgCode(), orgId);
                 }
-                orgMaps.put(examStudent.getOrgCode(), orgId);
             }
+            examStudent.setOrgId(orgId);
 
             Long courseId = courseMaps.get(examStudent.getCourseCode());
             if (courseId == null) {
-                courseId = this.queryCourseIdByCode(loginUser, examStudent.getCourseCode());
+                courseId = commonService.queryCourseIdByCode(loginUser, examStudent.getCourseCode());
                 if (courseId == null) {
-                    log.warn("共{}条 第{}条 课程代码不正确!", total, i + 1);
-                    hasError = true;
+                    errMessages.add("第" + (i + 1) + "条 课程代码不正确!");
+                } else {
+                    courseMaps.put(examStudent.getCourseCode(), courseId);
                 }
-                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;
+        if (!errMessages.isEmpty()) {
+            throw new StatusException(StringUtils.join(errMessages, "\n"));
         }
 
         int failCount = 0;
@@ -138,68 +143,10 @@ public class BatchImportExamStudentTask implements TaskService {
         }
 
         String msg = String.format("共%s条 成功%s条 失败%s条!", total, total - failCount, failCount);
-        log.warn(msg);
+        log.info(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());
@@ -213,6 +160,7 @@ public class BatchImportExamStudentTask implements TaskService {
         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);
@@ -220,6 +168,7 @@ public class BatchImportExamStudentTask implements TaskService {
 
         JsonNode value = new JsonMapper().getNode(result);
         if (value == null || !value.has("id")) {
+            // 创建成功会返回ID,否则失败
             throw new StatusException(result);
         }
     }

+ 6 - 9
src/main/resources/templates/admin/batchCreateUser.ftlh

@@ -16,7 +16,7 @@
                     <h3 style="text-align: center">批量创建用户</h3>
 
                     <el-form label-position="right" label-width="100px" :model="curForm" ref="curForm" :rules="rules">
-                        <el-form-item label=" " prop="filePath">
+                        <el-form-item label=" " prop="dataFilePath">
                             <el-upload action="${base}/upload"
                                        accept=".xlsx"
                                        limit="1"
@@ -46,10 +46,10 @@
         el: '#myVue',
         data: {
             curForm: {
-                filePath: null,
+                dataFilePath: null,
             },
             rules: {
-                filePath: [
+                dataFilePath: [
                     {required: true, message: '请选择文件!', trigger: 'blur'}
                 ]
             }
@@ -84,17 +84,14 @@
             },
             handleSuccess(response) {
                 console.log("上传成功!", response);
-                this.curForm.filePath = response;
+                this.curForm.dataFilePath = response;
             },
             handleRemove(file) {
                 console.log("移除文件!", file);
-                this.curForm.filePath = null;
+                this.curForm.dataFilePath = null;
             },
             downloadTemplate() {
-                this.$notify({
-                    message: "施工中。。。",
-                    type: "error",
-                });
+                window.location.href = "${base}/admin/downloadTemplate/importUser";
             }
         }
     });

+ 16 - 9
src/main/resources/templates/admin/batchImportExamStudent.ftlh

@@ -16,7 +16,11 @@
                     <h3 style="text-align: center">批量导入考生</h3>
 
                     <el-form label-position="right" label-width="100px" :model="curForm" ref="curForm" :rules="rules">
-                        <el-form-item label=" " prop="filePath">
+                        <el-form-item label="考试ID" prop="examId">
+                            <el-input v-model="curForm.examId"></el-input>
+                        </el-form-item>
+
+                        <el-form-item label=" " prop="dataFilePath">
                             <el-upload action="${base}/upload"
                                        accept=".xlsx"
                                        limit="1"
@@ -46,10 +50,15 @@
         el: '#myVue',
         data: {
             curForm: {
-                filePath: null,
+                examId: null,
+                dataFilePath: null,
             },
             rules: {
-                filePath: [
+                examId: [
+                    {required: true, message: '请输入考试ID', trigger: 'blur'},
+                    {pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
+                ],
+                dataFilePath: [
                     {required: true, message: '请选择文件!', trigger: 'blur'}
                 ]
             }
@@ -63,6 +72,7 @@
 
                     let req = {
                         type: "BATCH_IMPORT_EXAM_STUDENT",
+                        examId: this.curForm.examId,
                         params: JSON.stringify(this.curForm)
                     };
 
@@ -84,17 +94,14 @@
             },
             handleSuccess(response) {
                 console.log("上传成功!", response);
-                this.curForm.filePath = response;
+                this.curForm.dataFilePath = response;
             },
             handleRemove(file) {
                 console.log("移除文件!", file);
-                this.curForm.filePath = null;
+                this.curForm.dataFilePath = null;
             },
             downloadTemplate() {
-                this.$notify({
-                    message: "施工中。。。",
-                    type: "error",
-                });
+                window.location.href = "${base}/admin/downloadTemplate/importExamStudent";
             }
         }
     });

二進制
src/main/resources/templates/import_exam_student.xlsx


二進制
src/main/resources/templates/import_user.xlsx