deason 9 months ago
parent
commit
219d071038

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/tool/service/export_exam_paper_struct/ExportExamPaperStructTask.java

@@ -83,7 +83,7 @@ public class ExportExamPaperStructTask implements TaskService {
             }
         }
 
-        final String filePath = exportDir + "result_" + examId + ".xlsx";
+        final String filePath = exportDir + "/result_" + examId + ".xlsx";
         List<String> excelHeaders = Lists.newArrayList("考试Id", "课程代码", "课程名称", "抽题模式", "权重比例", "试卷类型", "试卷Id", "试卷名称");
         EasyExcel.write(filePath).head(ExcelHelper.buildHeaders(excelHeaders))
                 .registerWriteHandler(ExcelHelper.sheetStrategy()).registerWriteHandler(ExcelHelper.styleStrategy())

+ 75 - 31
src/main/java/cn/com/qmth/examcloud/tool/service/export_student_photo/ExportStudentPhotoTask.java

@@ -10,7 +10,11 @@ import cn.com.qmth.examcloud.tool.utils.HttpHelper;
 import cn.com.qmth.examcloud.tool.utils.JsonMapper;
 import cn.com.qmth.examcloud.tool.vo.PageInfo;
 import cn.com.qmth.examcloud.tool.vo.user.User;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
 import com.fasterxml.jackson.core.type.TypeReference;
+import com.google.common.collect.Lists;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -42,8 +46,8 @@ public class ExportStudentPhotoTask implements TaskService {
 
     public void execute(User loginUser, String exportDir) {
         // List<StudentVO> hasPhotoStudents = this.loadStudentFromUrl(loginUser);
-        List<StudentVO> hasPhotoStudents = this.loadStudentFromExcel();
-        this.downloadStudents(exportDir, hasPhotoStudents);
+        List<StudentVO> hasPhotoStudents = this.loadStudentFromExcel(exportDir + "/students.xlsx");
+        this.downloadStudents(hasPhotoStudents, exportDir, false);
     }
 
     private List<StudentVO> loadStudentFromUrl(User loginUser) {
@@ -52,17 +56,15 @@ public class ExportStudentPhotoTask implements TaskService {
         headers.put("token", loginUser.getToken());
 
         JsonMapper jsonMapper = new JsonMapper();
-        long pageNo = 0, pageSize = 100, total = 0, finishedCount = 0, noPhotoCount = 0;
+        long pageNo = 0, pageSize = 100, finishedCount = 0, noPhotoCount = 0;
 
         final String url = loginUser.getServerUrl()
-                + "/api/ecs_core/student/studentPage/%s/%s?rootOrgId=%s&identityNumber=&studentCode=&name=";
+                + "/api/ecs_core/student/studentPage/%s/%s?rootOrgId=%s&hasPhoto=TRUE";
 
         List<StudentVO> hasPhotoStudents = new ArrayList<>();
         while (true) {
             String json = HttpHelper.get(String.format(url, pageNo, pageSize, loginUser.getRootOrgId()), headers, null);
-
             PageInfo<StudentVO> page = jsonMapper.parseJson(json, new TypeReference<PageInfo<StudentVO>>() {
-
             });
             if (page == null || CollectionUtils.isEmpty(page.getList())) {
                 break;
@@ -79,52 +81,94 @@ public class ExportStudentPhotoTask implements TaskService {
             }
 
             pageNo++;
-            total = page.getTotal();
+            long total = page.getTotal();
             finishedCount += page.getList().size();
             float finishedRate = finishedCount * 100f / total;
-            log.info("【{}】 总数:{} 已获取学生数:{} 无底照学生数:{} 进度:{}%", pageNo, total, finishedCount,
-                    noPhotoCount, finishedRate);
+            log.info("总数:{} 已获取学生数:{} 无底照学生数:{} pageNo:{} 进度:{}%", total, finishedCount, noPhotoCount, pageNo, finishedRate);
         }
 
         return hasPhotoStudents;
     }
 
-    private List<StudentVO> loadStudentFromExcel() {
+    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;
+        */
+        List<StudentVO> list = new ArrayList<>();
+        try {
+            EasyExcel.read(dataFilePath, StudentVO.class, new AnalysisEventListener<StudentVO>() {
+                        @Override
+                        public void invoke(StudentVO data, AnalysisContext analysisContext) {
+                            list.add(data);
+                        }
+
+                        @Override
+                        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
+                            // ignore
+                        }
+                    }
+            ).sheet().doRead();
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            throw new RuntimeException("Excel内容解析错误,请使用标准模板!");
+        }
+
+        int noPhotoCount = 0;
         List<StudentVO> hasPhotoStudents = new ArrayList<>();
-        //todo
+        for (StudentVO student : list) {
+            if (StringUtils.isEmpty(student.getPhotoPath())) {
+                noPhotoCount++;
+                continue;
+            }
+
+            student.setPhotoPath(this.fixPhotoUrl(student.getPhotoPath()));
 
+            if (StringUtils.isNotBlank(student.getStudentCodesStr())) {
+                student.setStudentCodeList(Lists.newArrayList(student.getStudentCodesStr()));
+            }
+            hasPhotoStudents.add(student);
+        }
+
+        log.info("总数:{} 已获取学生数:{} 无底照学生数:{}", list.size(), hasPhotoStudents.size(), noPhotoCount);
         return hasPhotoStudents;
     }
 
-    private void downloadStudents(String exportDir, List<StudentVO> students) {
+    private void downloadStudents(List<StudentVO> students, String exportDir, boolean fileNameByStudentCode) {
         for (StudentVO student : students) {
-            // 下载底照
-            this.downloadPhoto(exportDir, student);
+            String photoSuffix = FileHelper.getFileSuffix(student.getPhotoPath());
+            if (fileNameByStudentCode) {
+                if (CollectionUtils.isNotEmpty(student.getStudentCodeList())) {
+                    if (student.getStudentCodeList().size() > 1) {
+                        log.warn("identityNumber:{} 多个学号!", student.getIdentityNumber());
+                    }
+                    for (String studentCode : student.getStudentCodeList()) {
+                        String photoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), studentCode, photoSuffix);
+                        this.downloadPhoto(student.getPhotoPath(), photoPath);
+                    }
+                } 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);
+                }
+            } else {
+                String photoPath = String.format("%s/%s/%s%s", exportDir, student.getRootOrgId(), student.getIdentityNumber(), photoSuffix);
+                this.downloadPhoto(student.getPhotoPath(), photoPath);
+            }
         }
     }
 
-    private void downloadPhoto(String exportDir, StudentVO student) {
-        String identityNumber = student.getIdentityNumber();
-
-        String studentCode;
-        if (CollectionUtils.isNotEmpty(student.getStudentCodeList())) {
-            studentCode = student.getStudentCodeList().get(0);
-        } else {
-            log.warn("【无学号】 studentCode:{} identityNumber:{}", student.getStudentCodesStr(),
-                    student.getIdentityNumber());
-            studentCode = "IDCard_" + student.getIdentityNumber();
-        }
-
-        String filePath = exportDir + "/" + student.getRootOrgId() + "/" + studentCode + ".jpg";
-        if (new File(filePath).exists()) {
-            log.info("【已下载】 studentCode:{} identityNumber:{} filePath:{}", studentCode, identityNumber, filePath);
+    private void downloadPhoto(String photoUrl, String photoPath) {
+        if (new File(photoPath).exists()) {
+            log.info("【已下载】 photoUrl:{} photoPath:{}", photoUrl, photoPath);
             return;
         }
 
         try {
-            FileHelper.saveImageToFile(student.getPhotoPath(), filePath);
+            FileHelper.saveImageToFile(photoUrl, photoPath);
         } catch (Exception e) {
-            log.error("studentCode:{} identityNumber:{} err:{}", studentCode, identityNumber, e.getMessage());
+            log.error("【错误】 photoUrl:{} photoPath:{} err:{}", photoUrl, photoPath, e.getMessage());
         }
     }
 

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

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.tool.service.export_student_photo.vo;
 
+import com.alibaba.excel.annotation.ExcelProperty;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -12,41 +13,24 @@ public class StudentVO implements Serializable {
 
     private static final long serialVersionUID = -4647126917276922968L;
 
+    @ExcelProperty(value = "id", index = 0)
     private Long id;
 
-    /**
-     * 学校ID
-     */
+    @ExcelProperty(value = "学校ID", index = 1)
     private Long rootOrgId;
 
-    /**
-     * 学生姓名
-     */
+    @ExcelProperty(value = "学生姓名", index = 2)
     private String name;
 
-    /**
-     * 学号列表
-     */
-    private List<String> studentCodeList;
+    @ExcelProperty(value = "身份证号", index = 3)
+    private String identityNumber;
 
-    /**
-     * 学号
-     */
+    @ExcelProperty(value = "学号", index = 4)
     private String studentCodesStr;
 
-    /**
-     * 身份证号
-     */
-    private String identityNumber;
+    private List<String> studentCodeList;// 学号列表
 
-    /**
-     * 照片
-     */
+    @ExcelProperty(value = "照片", index = 5)
     private String photoPath;
 
-    /**
-     * 是否启用
-     */
-    private Boolean enable;
-
 }

+ 2 - 2
src/test/java/cn/com/qmth/examcloud/tool/DataTest.java

@@ -19,7 +19,7 @@ public class DataTest {
     }
 
     public void writeDemo() {
-        String dataFilePath = ToolTest.DATA_DIR + "ddd.xlsx";
+        String dataFilePath = ToolTest.DATA_DIR + "/ddd.xlsx";
 
         List<List<String>> excelRows = new ArrayList<>();
         for (int i = 1; i <= 100; i++) {
@@ -47,7 +47,7 @@ public class DataTest {
     }
 
     public void readDemo() {
-        String dataFilePath = ToolTest.DATA_DIR + "ddd.xlsx";
+        String dataFilePath = ToolTest.DATA_DIR + "/ddd.xlsx";
         ExamStudentInfoListener dataListener = new ExamStudentInfoListener();
         EasyExcel.read(dataFilePath, ExamStudentInfo.class, dataListener).sheet().doRead();
         List<ExamStudentInfo> dataList = dataListener.getList();

+ 3 - 3
src/test/java/cn/com/qmth/examcloud/tool/ToolTest.java

@@ -43,7 +43,7 @@ public class ToolTest {
     @Autowired
     private ExamRecordDataAuditTask examRecordDataAuditTask;
 
-    public static final String DATA_DIR = "C:/Users/deason/Desktop/data/";
+    public static final String DATA_DIR = "D:/home/data";
 
     @Test
     public void demo() throws Exception {
@@ -59,8 +59,8 @@ public class ToolTest {
     private void testTask(User loginUser) {
         Map<String, Object> params = new HashMap<>();
         params.put("examId", 0);
-        params.put("dataFilePath", DATA_DIR + "批量导入考生模板.xlsx");
-        // params.put("dataFilePath", DATA_DIR + "批量创建用户模板.xlsx");
+        params.put("dataFilePath", DATA_DIR + "/批量导入考生模板.xlsx");
+        // params.put("dataFilePath", DATA_DIR + "/批量创建用户模板.xlsx");
 
         TaskEntity task = new TaskEntity();
         task.setUserToken(loginUser.getToken());