deason 9 місяців тому
батько
коміт
1247d02420

+ 19 - 28
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_photo/ExportStudentPhotoTask.java

@@ -6,6 +6,7 @@ import cn.com.qmth.examcloud.tool.service.CommonService;
 import cn.com.qmth.examcloud.tool.service.TaskService;
 import cn.com.qmth.examcloud.tool.service.export_student_photo.vo.StudentVO;
 import cn.com.qmth.examcloud.tool.utils.FileHelper;
+import cn.com.qmth.examcloud.tool.utils.FssHelper;
 import cn.com.qmth.examcloud.tool.utils.HttpHelper;
 import cn.com.qmth.examcloud.tool.utils.JsonMapper;
 import cn.com.qmth.examcloud.tool.vo.PageInfo;
@@ -58,8 +59,7 @@ public class ExportStudentPhotoTask implements TaskService {
         JsonMapper jsonMapper = new JsonMapper();
         long pageNo = 0, pageSize = 100, finishedCount = 0, noPhotoCount = 0;
 
-        final String url = loginUser.getServerUrl()
-                + "/api/ecs_core/student/studentPage/%s/%s?rootOrgId=%s&hasPhoto=TRUE";
+        final String url = loginUser.getServerUrl() + "/api/ecs_core/student/studentPage/%s/%s?rootOrgId=%s&hasPhoto=TRUE";
 
         List<StudentVO> hasPhotoStudents = new ArrayList<>();
         while (true) {
@@ -76,7 +76,6 @@ public class ExportStudentPhotoTask implements TaskService {
                     continue;
                 }
 
-                student.setPhotoPath(this.fixPhotoUrl(student.getPhotoPath()));
                 hasPhotoStudents.add(student);
             }
 
@@ -92,9 +91,9 @@ public class ExportStudentPhotoTask implements TaskService {
 
     private List<StudentVO> loadStudentFromExcel(String dataFilePath) {
         /*
-            select stu.id, stu.root_org_id 学校ID, stu.name 学生姓名, stu.identity_number 身份证号, sc.student_code 学号, stu.photo_path 照片
-            from ec_b_student stu left join ec_b_student_code sc on sc.student_id = stu.id
-            where stu.root_org_id = 0 and stu.photo_path is not null;
+            select stu.id, stu.root_org_id 学校ID, stu.name 学生姓名, stu.identity_number 身份证号,
+            (select GROUP_CONCAT(DISTINCT sc.student_code) from ec_b_student_code sc where sc.student_id = stu.id)学号,stu.photo_path 照片
+            from ec_b_student stu where stu.root_org_id = 0 and stu.photo_path is not null;
         */
         List<StudentVO> list = new ArrayList<>();
         try {
@@ -123,15 +122,13 @@ public class ExportStudentPhotoTask implements TaskService {
                 continue;
             }
 
-            student.setPhotoPath(this.fixPhotoUrl(student.getPhotoPath()));
-
             if (StringUtils.isNotBlank(student.getStudentCodesStr())) {
-                student.setStudentCodeList(Lists.newArrayList(student.getStudentCodesStr()));
+                student.setStudentCodeList(Lists.newArrayList(student.getStudentCodesStr().split(",")));
             }
             hasPhotoStudents.add(student);
         }
 
-        log.info("总数:{} 已获取学生数:{} 无底照学生数:{}", list.size(), hasPhotoStudents.size(), noPhotoCount);
+        log.info("总数:{} 有底照学生数:{} 无底照学生数:{}", list.size(), hasPhotoStudents.size(), noPhotoCount);
         return hasPhotoStudents;
     }
 
@@ -141,44 +138,38 @@ public class ExportStudentPhotoTask implements TaskService {
             if (fileNameByStudentCode) {
                 if (CollectionUtils.isNotEmpty(student.getStudentCodeList())) {
                     if (student.getStudentCodeList().size() > 1) {
-                        log.warn("identityNumber:{} 多个学号!", student.getIdentityNumber());
+                        log.warn("identityNumber:{} 多个学号!{}", student.getIdentityNumber(), student.getStudentCodesStr());
                     }
                     for (String studentCode : student.getStudentCodeList()) {
-                        String photoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), studentCode, photoSuffix);
-                        this.downloadPhoto(student.getPhotoPath(), photoPath);
+                        String savePhotoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), studentCode, photoSuffix);
+                        this.downloadPhoto(student.getPhotoPath(), savePhotoPath);
                     }
                 } else {
                     log.warn("identityNumber:{} 无学号!", student.getIdentityNumber());
-                    String photoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), student.getIdentityNumber(), photoSuffix);
-                    this.downloadPhoto(student.getPhotoPath(), photoPath);
+                    String savePhotoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), student.getIdentityNumber(), photoSuffix);
+                    this.downloadPhoto(student.getPhotoPath(), savePhotoPath);
                 }
             } else {
-                String photoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), student.getIdentityNumber(), photoSuffix);
-                this.downloadPhoto(student.getPhotoPath(), photoPath);
+                String savePhotoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), student.getIdentityNumber(), photoSuffix);
+                this.downloadPhoto(student.getPhotoPath(), savePhotoPath);
             }
         }
     }
 
     private void downloadPhoto(String photoUrl, String photoPath) {
+        String urlPrefix = "https://ecs-test-static.qmth.com.cn";
+        photoUrl = FssHelper.finalFileUrl(photoUrl, urlPrefix);
+
         if (new File(photoPath).exists()) {
-            log.info("【已下载】 photoUrl:{} photoPath:{}", photoUrl, photoPath);
+            log.info("【已下载】 {} {}", photoPath, photoUrl);
             return;
         }
 
         try {
             FileHelper.saveImageToFile(photoUrl, photoPath);
         } catch (Exception e) {
-            log.error("【错误】 photoUrl:{} photoPath:{} err:{}", photoUrl, photoPath, e.getMessage());
-        }
-    }
-
-    public String fixPhotoUrl(String url) {
-        if (StringUtils.isEmpty(url)) {
-            return "";
+            log.error("【错误】 {} err:{}", photoPath, e.getMessage());
         }
-
-        // ecs-static-upyun.qmth.com.cn 去掉 -upyun
-        return url.replace("-upyun.qmth.com.cn", ".qmth.com.cn");
     }
 
 }

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_photo/vo/StudentVO.java

@@ -26,7 +26,7 @@ public class StudentVO implements Serializable {
     private String identityNumber;
 
     @ExcelProperty(value = "学号", index = 4)
-    private String studentCodesStr;
+    private String studentCodesStr;// 多个学号时用逗号分隔
 
     private List<String> studentCodeList;// 学号列表
 

+ 42 - 0
src/main/java/cn/com/qmth/examcloud/tool/utils/FssHelper.java

@@ -0,0 +1,42 @@
+package cn.com.qmth.examcloud.tool.utils;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class FssHelper {
+
+    /**
+     * 获取文件的最终完整访问路径(兼容旧数据)
+     *
+     * @param filePath 文件存储路径,格式示例:
+     *                 https://xxx.xxx/abc/123.jpg
+     *                 upyun-1://abc/123.jpg
+     *                 aliyun-1://abc/123.jpg
+     *                 aliyun-2://abc/123.jpg
+     *                 /abc/123.jpg
+     *                 abc/123.jpg
+     * @return 最终访问地址,如: https://xxx.xxx/abc/123.jpg
+     */
+    public static String finalFileUrl(String filePath, String urlPrefix) {
+        if (StringUtils.isEmpty(filePath)) {
+            return "";
+        }
+
+        if (filePath.toLowerCase().startsWith("http")) {
+            return filePath;
+        }
+
+        filePath = filePath.replace("upyun-1://", "")
+                .replace("aliyun-1://", "")
+                .replace("aliyun-2://", "");
+
+        if (StringUtils.isEmpty(urlPrefix)) {
+            urlPrefix = "https://ecs-static.qmth.com.cn";
+        }
+        if (filePath.startsWith("/")) {
+            return urlPrefix + filePath;
+        }
+
+        return urlPrefix + "/" + filePath;
+    }
+
+}