Преглед на файлове

处理课程导入模板工具(单元测试)

caozixuan преди 3 години
родител
ревизия
ac25d719b5

+ 93 - 0
distributed-print/src/test/java/com/qmth/distributed/print/ImportTest.java

@@ -0,0 +1,93 @@
+package com.qmth.distributed.print;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
+import com.qmth.boot.tools.io.IOUtils;
+import com.qmth.teachcloud.common.bean.dto.excel.BasicCourseImportDto;
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import com.qmth.teachcloud.common.util.ExcelUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.*;
+
+/**
+ * @Description: 导入导出工具类
+ * @Author: CaoZixuan
+ * @Date: 2021-09-27
+ */
+@SpringBootTest
+@RunWith(SpringRunner.class)
+public class ImportTest {
+
+    /**
+     * 整理并获得新的课程导入excel - 老的excel中课程编码是重复的!
+     * @throws Exception
+     */
+    @Test
+    public void combineClazzName() throws Exception {
+        File file = new File("E:" + File.separator + "导入导出" + File.separator + "课程导入模板.xlsx");
+
+        FileInputStream input = new FileInputStream(file);
+
+        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
+        List<LinkedMultiValueMap<Integer, Object>> finalList = ExcelUtil.excelReader(multipartFile.getInputStream(), Lists.newArrayList(BasicCourseImportDto.class), (finalExcelList, finalColumnNameList, finalExcelErrorList) -> {
+            if (finalExcelErrorList.size() > 0) {
+                throw ExceptionResultEnum.ERROR.exception(JSONObject.toJSONString(finalExcelErrorList));
+            }
+            return finalExcelList;
+        });
+
+        Map<String,BasicCourseImportDto> courseMap = new HashMap<>();
+        if (Objects.nonNull(finalList) && finalList.size() > 0) {
+            for (int i = 0; i < finalList.size(); i++) {
+                LinkedMultiValueMap<Integer, Object> map = finalList.get(i);
+                List<Object> courseList = map.get(i);
+                for (int y = 0; y < Objects.requireNonNull(courseList).size(); y++) {
+                    if (courseList.get(y) instanceof BasicCourseImportDto) {
+                        BasicCourseImportDto basicCourseImportDto = (BasicCourseImportDto) courseList.get(y);
+                        String courseCode = basicCourseImportDto.getCourseCode();
+                        String courseName = basicCourseImportDto.getCourseName();
+                        String teachingRoomName = basicCourseImportDto.getTeachingRoomName();
+                        String clazz = basicCourseImportDto.getClazz();
+
+                        if (courseMap.containsKey(courseCode)){
+                            BasicCourseImportDto old = courseMap.get(courseCode);
+                            String oldClazz = old.getClazz();
+                            oldClazz = oldClazz + "," + clazz;
+                            old.setClazz(oldClazz);
+                        } else {
+                            courseMap.put(courseCode,basicCourseImportDto);
+                        }
+
+                    }
+                }
+            }
+        }
+        List<BasicCourseImportDto> basicCourseImportDtoList = new ArrayList<>();
+        for (String courseCode : courseMap.keySet()) {
+            basicCourseImportDtoList.add(courseMap.get(courseCode));
+        }
+        System.out.println(JSON.toJSONString(basicCourseImportDtoList));
+
+        File outfile = new File("E:" + File.separator + "导入导出" + File.separator + "新课程导入模板.xlsx");
+        if (!outfile.getParentFile().exists()){ //文件目录不存在
+            outfile.getParentFile().mkdirs(); //创建父目录
+        }
+
+        //2.通过子类实例化
+        OutputStream outputStream = new FileOutputStream(outfile,true);
+        ExcelUtil.excelMake(BasicCourseImportDto.class, basicCourseImportDtoList, outputStream);
+        outputStream.close();
+    }
+}

+ 5 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/bean/dto/excel/BasicCourseImportDto.java

@@ -1,6 +1,7 @@
 package com.qmth.teachcloud.common.bean.dto.excel;
 
 import com.qmth.teachcloud.common.annotation.ExcelNote;
+import com.qmth.teachcloud.common.annotation.ExcelProperty;
 
 import javax.validation.constraints.NotNull;
 import java.io.Serializable;
@@ -12,18 +13,22 @@ import java.io.Serializable;
  */
 public class BasicCourseImportDto implements Serializable {
     @ExcelNote(value = "课程名称")
+    @ExcelProperty(name = "课程名称", width = 30, index = 1)
     @NotNull
     private String courseName;
 
     @ExcelNote(value = "课程编码")
+    @ExcelProperty(name = "课程编码", width = 30, index = 2)
     @NotNull
     private String courseCode;
 
     @ExcelNote(value = "所属教研室")
+    @ExcelProperty(name = "所属教研室", width = 30, index = 3)
     @NotNull
     private String teachingRoomName;
 
     @ExcelNote(value = "授课班级")
+    @ExcelProperty(name = "授课班级", width = 30, index = 4)
     @NotNull
     private String clazz;