haogh 8 сар өмнө
parent
commit
6095d358ef

+ 0 - 2
src/main/java/com/qmth/exam/reserve/bean/stdapply/MaterialTitleInfo.java

@@ -30,6 +30,4 @@ public class MaterialTitleInfo implements IModel {
     @ApiModelProperty("考场名称")
     private String roomName;
 
-
-
 }

+ 15 - 2
src/main/java/com/qmth/exam/reserve/controller/admin/StudentAdminController.java

@@ -58,13 +58,26 @@ public class StudentAdminController extends BaseController {
     }
 
     @ApiOperation(value = "上传考生的照片")
-    @PostMapping(value = "upload/student/photo")
+    @PostMapping(value = "upload/photo")
     public String uploadStudentPhoto(MultipartFile file) throws IOException {
+        String filename = file.getOriginalFilename();
+        if (filename != null && !filename.equalsIgnoreCase(".jpg") && !filename.equalsIgnoreCase(".png")) {
+            throw new StatusException("请上传后缀为jpg或者png的考生头像");
+        }
+        LoginUser loginUser = this.curLoginUser();
+        studentService.uploadStudentPhoto(loginUser.getId(), file);
+        return Constants.ASYNC_TIPS;
+    }
+
+
+    @ApiOperation(value = "上传考生的照片-异步多文件上传")
+    @PostMapping(value = "upload/student/photo")
+    public String uploadStudentPhotoAsync(MultipartFile file) throws IOException {
         if (!Objects.requireNonNull(file.getOriginalFilename()).endsWith(".zip")) {
             throw new StatusException("请上传zip文件");
         }
         LoginUser loginUser = this.curLoginUser();
-        studentService.uploadStudentPhoto(loginUser.getId(), file);
+        studentService.uploadStudentPhotoAsync(loginUser.getId(), file);
         return Constants.ASYNC_TIPS;
     }
 

+ 3 - 1
src/main/java/com/qmth/exam/reserve/service/StudentService.java

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

+ 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;

+ 37 - 1
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -24,12 +24,15 @@ import com.qmth.exam.reserve.entity.StudentApplyEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.enums.AsyncTaskType;
 import com.qmth.exam.reserve.enums.EventType;
+import com.qmth.exam.reserve.enums.FileUploadType;
 import com.qmth.exam.reserve.enums.Role;
 import com.qmth.exam.reserve.service.*;
 import com.qmth.exam.reserve.template.execute.StudentPhotoUploadService;
 import com.qmth.exam.reserve.util.FileUtil;
 import com.qmth.exam.reserve.util.PageUtil;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.compress.utils.FileNameUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,9 +43,11 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.StringJoiner;
 
 @Service
 public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {
@@ -280,7 +285,7 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
     }
 
     @Override
-    public void uploadStudentPhoto(Long operateId, MultipartFile file) throws IOException {
+    public void uploadStudentPhotoAsync(Long operateId, MultipartFile file) throws IOException {
         ApplyTaskEntity applyTask = applyTaskService.findApplyTask();
         if (applyTask == null) {
             throw new StatusException("未开启预约任务,无法导入");
@@ -307,4 +312,35 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
         baseMapper.updateStudentApplyNumber(studentId);
     }
 
+    @Override
+    public void uploadStudentPhoto(Long operateId, MultipartFile file) {
+        CurrentApplyTaskVO curApplyTask = applyTaskCacheService.currentApplyTask(null);
+        if (curApplyTask == null) {
+            throw new StatusException("未开启预约任务,无法导入");
+        }
+
+        //照片必须以考生的身份证命名
+        String identityNumber = FileNameUtils.getBaseName(file.getName());
+        LambdaQueryWrapper<StudentEntity> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(StudentEntity::getIdentityNumber, identityNumber);
+        queryWrapper.eq(StudentEntity::getApplyTaskId, curApplyTask.getTaskId());
+        StudentEntity student = this.getOne(queryWrapper);
+        if (student == null) {
+            throw new StatusException("上次照片必须以考生的身份证命名");
+        }
+
+        StringJoiner stringJoiner = FileUtil.getDirName(FileUploadType.UPLOAD, true);
+        String path = stringJoiner.toString().replaceAll("\\\\", "/");
+
+        try {
+            fileStore.write(path + file.getOriginalFilename(), file.getInputStream(), DigestUtils.md5Hex(file.getInputStream()));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        //更新考生照片
+
+
+    }
+
 }