浏览代码

考生导入

haogh 8 月之前
父节点
当前提交
e65e4b70b7

+ 41 - 0
src/main/java/com/qmth/exam/reserve/bean/studentimport/StudentImportInfo.java

@@ -0,0 +1,41 @@
+package com.qmth.exam.reserve.bean.studentimport;
+
+import com.qmth.exam.reserve.bean.IModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class StudentImportInfo implements IModel {
+
+    private static final long serialVersionUID = 7953572112420836164L;
+
+    @ApiModelProperty("考生姓名")
+    private String name;
+
+    @ApiModelProperty("证件号")
+    private String identityNumber;
+
+    @ApiModelProperty("学号")
+    private String studentCode;
+
+    @ApiModelProperty("考生密码")
+    private String password;
+
+    @ApiModelProperty("所属机构ID(学校)")
+    private Long orgId;
+
+    @ApiModelProperty("所属预约任务ID")
+    private Long applyTaskId;
+
+    @ApiModelProperty("所属教学点ID")
+    private Long categoryId;
+
+    @ApiModelProperty("课程名称")
+    private String courseName;
+
+    @ApiModelProperty("课程代码")
+    private String courseCode;
+
+}

+ 2 - 0
src/main/java/com/qmth/exam/reserve/dao/StudentDao.java

@@ -21,4 +21,6 @@ public interface StudentDao extends BaseMapper<StudentEntity> {
     List<CategoryVO> listNoFinishCategory(@Param("taskId") Long taskId, @Param("cancel") Boolean cancel);
 
     IPage<StudentVO> pageStudent(Page<StudentVO> page, @Param("req") StudentReq req);
+
+    void updateStudentApplyNumber(@Param("studentId") Long studentId);
 }

+ 2 - 0
src/main/java/com/qmth/exam/reserve/service/StudentService.java

@@ -40,4 +40,6 @@ public interface StudentService extends IService<StudentEntity> {
     void modifyApplyNumber(Long studentId, Long operateId, Integer applyNumber);
 
     void uploadStudentPhoto(Long operateId, MultipartFile file) throws IOException;
+
+    void updateStudentApplyNumber(Long studentId);
 }

+ 105 - 55
src/main/java/com/qmth/exam/reserve/service/impl/StudentImportAsyncServiceImpl.java

@@ -1,42 +1,41 @@
 package com.qmth.exam.reserve.service.impl;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.tools.excel.model.DataMap;
+import com.qmth.exam.reserve.bean.studentimport.StudentImportInfo;
 import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
 import com.qmth.exam.reserve.entity.CategoryEntity;
+import com.qmth.exam.reserve.entity.StudentCourseEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
 import com.qmth.exam.reserve.enums.CategoryLevel;
 import com.qmth.exam.reserve.enums.ImportStatus;
-import com.qmth.exam.reserve.service.CategoryService;
-import com.qmth.exam.reserve.service.StudentImportAsyncService;
-import com.qmth.exam.reserve.service.StudentImportTaskService;
-import com.qmth.exam.reserve.service.StudentService;
+import com.qmth.exam.reserve.service.*;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 @Service
 public class StudentImportAsyncServiceImpl implements StudentImportAsyncService {
 
     private final static Logger log = LoggerFactory.getLogger(StudentImportAsyncServiceImpl.class);
 
-    private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点代码", "所属教学点名称", "可约时段数" };
+    private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点代码", "所属教学点名称", "科目名称", "科目代码" };
 
     @Autowired
     private CategoryService categoryService;
@@ -50,17 +49,20 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
     @Autowired
     private ApplyTaskCacheService cacheService;
 
+    @Autowired
+    private StudentCourseService studentCourseService;
+
     @Async
     @Transactional
     @Override
     public void asyncImportStudent(StudentImportTaskEntity task, Long orgId, List<DataMap> lineList) {
         Map<String, CategoryEntity> teachingCache = getTeachingCache();
-        List<StudentEntity> studentList = new ArrayList<StudentEntity>();
+        List<StudentImportInfo> studentList = new ArrayList<>();
         List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
         for (int i = 0; i < lineList.size(); i++) {
             DataMap line = lineList.get(i);
             StringBuilder msg = new StringBuilder();
-            StudentEntity student = new StudentEntity();
+            StudentImportInfo student = new StudentImportInfo();
             String studentCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
             if (StringUtils.isBlank(studentCode)) {
                 msg.append(" 学号不能为空");
@@ -110,16 +112,21 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
                 student.setOrgId(orgId);
                 student.setApplyTaskId(task.getApplyTaskId());
             }
-            try {
-                String applyNumber = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
-                if (StringUtils.isBlank(applyNumber)) {
-                    msg.append(" 可约时段数不能为空");
-                } else {
-                    student.setApplyNumber(Integer.parseInt(applyNumber));
-                }
-            } catch (Exception e) {
-                msg.append(" 可约时段只能为数字");
+
+            String courseName = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
+            if (StringUtils.isBlank(courseName)) {
+                msg.append(" 科目名称不能为空");
+            } else {
+                student.setCourseName(courseName);
             }
+
+            String courseCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[6]));
+            if (StringUtils.isBlank(courseCode)) {
+                msg.append(" 科目代码不能为空");
+            } else {
+                student.setCourseCode(courseCode);
+            }
+
             if (msg.length() > 0) {
                 failRecords.add(newError(i + 1, msg.toString()));
             } else {
@@ -131,10 +138,67 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
             updateStudentImportTask(task, ImportStatus.FAILURE.toString(), failRecords);
             return;
         }
+
+        //更新和保存考生以及考生的考试科目
+        long start = System.currentTimeMillis();
         for (int i = 0; i < studentList.size(); i++) {
-            StudentEntity studentEntity = studentList.get(i);
+            StudentImportInfo studentInfo = studentList.get(i);
             try {
-                saveStudent(studentEntity);
+                //考生信息
+                StudentEntity studentEntity = new StudentEntity();
+                BeanUtils.copyProperties(studentInfo, studentEntity);
+
+                //考生的考试科目
+                StudentCourseEntity studentCourseEntity = new StudentCourseEntity();
+                studentCourseEntity.setCourseCode(studentInfo.getCourseCode());
+                studentCourseEntity.setCourseName(studentInfo.getCourseName());
+
+                LambdaQueryWrapper<StudentEntity> wrapper = new LambdaQueryWrapper<StudentEntity>()
+                        .eq(StudentEntity::getStudentCode, studentEntity.getStudentCode())
+                        .eq(StudentEntity::getApplyTaskId, studentEntity.getApplyTaskId());
+                StudentEntity existStudent = studentService.getOne(wrapper);
+
+                LambdaQueryWrapper<StudentCourseEntity> studentCourseQueryWrapper = new LambdaQueryWrapper<>();
+                studentCourseQueryWrapper.eq(StudentCourseEntity::getCourseCode, studentCourseEntity.getCourseCode());
+
+                Long studentId;
+                if (existStudent != null) {
+                    studentId = existStudent.getId();
+                    existStudent.setName(studentEntity.getName());
+                    existStudent.setIdentityNumber(studentEntity.getIdentityNumber());
+                    existStudent.setCategoryId(studentEntity.getCategoryId());
+                    studentService.updateById(existStudent);
+
+                    studentCourseQueryWrapper.eq(StudentCourseEntity::getStudentId, studentId);
+                    StudentCourseEntity existStudentCourse = studentCourseService.getOne(studentCourseQueryWrapper);
+
+                    //更新考生的考试科目
+                    if(existStudentCourse != null) {
+                        existStudentCourse.setCourseName(studentCourseEntity.getCourseName());
+                        studentCourseService.updateById(existStudentCourse);
+                    } else {
+                        studentCourseEntity.setStudentId(existStudent.getId());
+                        studentCourseService.save(studentCourseEntity);
+                    }
+                } else {
+                    studentEntity.setApplyFinished(Boolean.FALSE);
+                    studentEntity.setApplyNumber(0);
+                    studentEntity.setPhotoPath(studentEntity.getIdentityNumber()+".jpg");
+                    studentService.save(studentEntity);
+
+                    studentId = studentEntity.getId();
+
+                    //保存考生科目
+                    studentCourseEntity.setStudentId(studentId);
+                    studentCourseService.save(studentCourseEntity);
+                }
+
+                //更新考生的预约次数
+                studentService.updateStudentApplyNumber(studentId);
+
+                // 清空缓存
+                cacheService.clearStudentApplyNumberCache(studentId);
+
             } catch (StatusException e) {
                 failRecords.add(newError(i + 1, e.getMessage()));
             } catch (Exception e) {
@@ -144,7 +208,13 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
                 return;
             }
         }
-        updateStudentImportTask(task, ImportStatus.SUCCESS.toString(), failRecords);
+        long end = System.currentTimeMillis();
+        log.warn("[耗时]{}ms", end - start);
+
+        //更新成功标志
+        task.setMessage(failRecords.isEmpty() ? null : failRecords.toString());
+        task.setStatus(ImportStatus.SUCCESS.toString());
+        importTaskService.saveOrUpdate(task);
     }
 
     private void updateStudentImportTask(StudentImportTaskEntity task, String status,
@@ -154,26 +224,6 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
         importTaskService.saveOrUpdate(task);
     }
 
-    private void saveStudent(StudentEntity studentEntity) {
-        LambdaQueryWrapper<StudentEntity> wrapper = new LambdaQueryWrapper<StudentEntity>()
-                .eq(StudentEntity::getStudentCode, studentEntity.getStudentCode())
-                .eq(StudentEntity::getApplyTaskId, studentEntity.getApplyTaskId());
-        StudentEntity existStudent = studentService.getOne(wrapper);
-        if (existStudent != null) {
-            existStudent.setName(studentEntity.getName());
-            existStudent.setIdentityNumber(studentEntity.getIdentityNumber());
-            existStudent.setCategoryId(studentEntity.getCategoryId());
-            existStudent.setApplyNumber(studentEntity.getApplyNumber());
-            studentService.updateById(existStudent);
-            // 清空缓存
-            cacheService.clearStudentApplyNumberCache(existStudent.getId());
-        } else {
-            studentEntity.setApplyFinished(Boolean.FALSE);
-            studentEntity.setPhotoPath(studentEntity.getIdentityNumber()+".jpg");
-            studentService.save(studentEntity);
-        }
-    }
-
     private String trimAndNullIfBlank(String s) {
         if (StringUtils.isBlank(s)) {
             return null;

+ 1 - 2
src/main/java/com/qmth/exam/reserve/service/impl/StudentImportTaskServiceImpl.java

@@ -44,7 +44,7 @@ public class StudentImportTaskServiceImpl extends ServiceImpl<StudentImportTaskD
 
     @Override
     public void importStudent(Long operateId, String filePath, InputStream inputStream) {
-        List<DataMap> lineList = null;
+        List<DataMap> lineList;
         ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
         try {
             lineList = reader.getDataMapList();
@@ -65,7 +65,6 @@ public class StudentImportTaskServiceImpl extends ServiceImpl<StudentImportTaskD
         studentTask.setStatus(ImportStatus.RUNNING.toString());
         studentTask.setFilePath(filePath);
         studentTaskService.save(studentTask);
-        // TODO 原始文件上传到OSS
         asyncImportService.asyncImportStudent(studentTask, task.getOrgId(), lineList);
     }
 

+ 5 - 0
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -254,4 +254,9 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
         }
     }
 
+    @Override
+    public void updateStudentApplyNumber(Long studentId) {
+        baseMapper.updateStudentApplyNumber(studentId);
+    }
+
 }

+ 1 - 1
src/main/java/com/qmth/exam/reserve/util/UnionUtil.java

@@ -12,7 +12,7 @@ import java.util.function.Function;
 public class UnionUtil {
 
     /**
-     *  个List根据对象中的属性取并集
+     *  个List根据对象中的属性取并集
      * @param listOne 集合1
      * @param listTwo 集合2
      * @param attributeExtractor 比较的属性

+ 5 - 0
src/main/resources/mapper/StudentMapper.xml

@@ -1,6 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.qmth.exam.reserve.dao.StudentDao">
+    <update id="updateStudentApplyNumber" parameterType="java.lang.Long">
+        UPDATE t_student s SET s.apply_number =( SELECT count( 1 ) FROM t_student_course WHERE student_id = #{studentId} )
+        WHERE
+            s.id = #{studentId}
+    </update>
     <select id="listNoFinishStudent" resultType="com.qmth.exam.reserve.entity.StudentEntity">
         SELECT s.id,s.apply_number,s.category_id
         FROM t_student s

二进制
src/main/resources/templates/studentImport.xlsx