haogh 8 miesięcy temu
rodzic
commit
ebd9035637

+ 5 - 0
src/main/java/com/qmth/exam/reserve/bean/Constants.java

@@ -49,4 +49,9 @@ public interface Constants {
      */
     String DEFAULT_PASSWORD = "123456";
 
+    /**
+     * 文件大小限制
+     */
+    long FILE_SIZE_LIMIT = 5 * 1024 * 1024;
+
 }

+ 3 - 4
src/main/java/com/qmth/exam/reserve/controller/admin/StudentAdminController.java

@@ -59,20 +59,19 @@ public class StudentAdminController extends BaseController {
 
     @ApiOperation(value = "上传考生的照片")
     @PostMapping(value = "upload/photo")
-    public String uploadStudentPhoto(MultipartFile file) throws IOException {
+    public void uploadStudentPhoto(@RequestParam MultipartFile file) {
         String filename = file.getOriginalFilename();
-        if (filename != null && !filename.equalsIgnoreCase(".jpg") && !filename.equalsIgnoreCase(".png")) {
+        if (filename != null && !filename.toLowerCase().endsWith(".jpg") && !filename.toLowerCase().endsWith(".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 {
+    public String uploadStudentPhotoAsync(@RequestParam MultipartFile file) throws IOException {
         if (!Objects.requireNonNull(file.getOriginalFilename()).endsWith(".zip")) {
             throw new StatusException("请上传zip文件");
         }

+ 5 - 1
src/main/java/com/qmth/exam/reserve/service/impl/StudentImportAsyncServiceImpl.java

@@ -116,6 +116,8 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
             String courseName = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
             if (StringUtils.isBlank(courseName)) {
                 msg.append(" 科目名称不能为空");
+            } else if (courseName.length() > 50) {
+                msg.append(" 科目名称的长度不能超过50");
             } else {
                 student.setCourseName(courseName);
             }
@@ -123,6 +125,8 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
             String courseCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[6]));
             if (StringUtils.isBlank(courseCode)) {
                 msg.append(" 科目代码不能为空");
+            } else if (courseCode.length() > 20) {
+                msg.append(" 科目代码的长度不能超过20");
             } else {
                 student.setCourseCode(courseCode);
             }
@@ -209,7 +213,7 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
             }
         }
         long end = System.currentTimeMillis();
-        log.warn("[耗时]{}ms", end - start);
+        log.warn("[导入考生]耗时:{}ms", end - start);
 
         //更新成功标志
         task.setMessage(failRecords.isEmpty() ? null : failRecords.toString());

+ 19 - 9
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.fss.store.FileStore;
+import com.qmth.exam.reserve.bean.Constants;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
@@ -43,11 +44,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;
+import java.util.stream.Collectors;
 
 @Service
 public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {
@@ -314,19 +315,24 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
 
     @Override
     public void uploadStudentPhoto(Long operateId, MultipartFile file) {
+        //文件大小限制
+        if (file.getSize() > Constants.FILE_SIZE_LIMIT) {
+            throw new StatusException("上传的考生照片大小不能超过5M");
+        }
+
         CurrentApplyTaskVO curApplyTask = applyTaskCacheService.currentApplyTask(null);
         if (curApplyTask == null) {
             throw new StatusException("未开启预约任务,无法导入");
         }
 
         //照片必须以考生的身份证命名
-        String identityNumber = FileNameUtils.getBaseName(file.getName());
+        String identityNumber = FileNameUtils.getBaseName(file.getOriginalFilename());
         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("上次照片必须以考生的身份证命名");
+        List<StudentEntity> studentList = this.list(queryWrapper);
+        if (CollectionUtils.isEmpty(studentList)) {
+            throw new StatusException("考生照片必须以考生的身份证命名");
         }
 
         StringJoiner stringJoiner = FileUtil.getDirName(FileUploadType.UPLOAD, true);
@@ -335,12 +341,16 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
         try {
             fileStore.write(path + file.getOriginalFilename(), file.getInputStream(), DigestUtils.md5Hex(file.getInputStream()));
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            log.error("[上传考生头像],上传失败:{}", e.getMessage());
+            throw new StatusException("考生头像上传失败,请稍后重试");
         }
 
-        //更新考生照片
-
-
+        //更新考生照片路径
+        List<Long> studentIds = studentList.stream().map(StudentEntity::getId).collect(Collectors.toList());
+        LambdaUpdateWrapper<StudentEntity> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(StudentEntity::getPhotoPath, path + file.getOriginalFilename());
+        updateWrapper.in(StudentEntity::getId, studentIds);
+        this.update(null, updateWrapper);
     }
 
 }