Explorar el Código

完成小6知识点导入excel

feick hace 5 años
padre
commit
dc7542d56a

BIN
doc/小升初专项诊断知识点列表.xlsx


+ 9 - 0
src/main/java/com/yaoxiang/diagnosis/controller/ExcelController.java

@@ -1,6 +1,7 @@
 package com.yaoxiang.diagnosis.controller;
 
 import com.yaoxiang.diagnosis.config.Constants;
+import com.yaoxiang.diagnosis.entity.SpecialKnowledge;
 import com.yaoxiang.diagnosis.entity.SubjectKnowledge;
 import com.yaoxiang.diagnosis.model.MatterVo;
 import com.yaoxiang.diagnosis.model.Result;
@@ -51,4 +52,12 @@ public class ExcelController {
         return Result.ok(list);
     }
 
+    @PostMapping("/uploadSpecial")
+    public Result uploadSpecial(@RequestParam(defaultValue = "2") Long subjectId,
+        @RequestParam(defaultValue = "6Y") String grade,
+        @RequestBody MultipartFile file) throws Exception{
+        byte[] data = file.getBytes();
+        List<SpecialKnowledge> list = KnowledgeUtil.importSpecialKnowledge(subjectId,grade,data);
+        return Result.fail();
+    }
 }

+ 164 - 0
src/main/java/com/yaoxiang/diagnosis/entity/SpecialKnowledge.java

@@ -0,0 +1,164 @@
+package com.yaoxiang.diagnosis.entity;
+
+import io.swagger.annotations.ApiModelProperty;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Transient;
+
+public class SpecialKnowledge {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    private Long subjectId;
+
+    private String grade;
+
+    @ApiModelProperty("知识点类别")
+    private String category;
+
+    private Integer level;
+
+//    @ApiModelProperty("开头两位数字表示层级,之后每两位表示一个层级 01010101")
+//    private String serial;
+
+//    @ApiModelProperty("这一层级的编号")
+//    private String number;
+
+    @ApiModelProperty("知识点编号")
+    private String code;
+    @ApiModelProperty("知识点内容")
+    private String content;
+
+    @ApiModelProperty("掌握等级 掌握 理解 应用")
+    private String masterLevel;
+
+    @ApiModelProperty("父Id")
+    private Long parentId;
+
+    @ApiModelProperty("第1级Id")
+    private Long rootId1;
+    @ApiModelProperty("第2级Id")
+    private Long rootId2;
+    @ApiModelProperty("第3级Id")
+    private Long rootId3;
+
+    @Transient
+    private SpecialKnowledge parent;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getSubjectId() {
+        return subjectId;
+    }
+
+    public void setSubjectId(Long subjectId) {
+        this.subjectId = subjectId;
+    }
+
+    public String getGrade() {
+        return grade;
+    }
+
+    public void setGrade(String grade) {
+        this.grade = grade;
+    }
+
+    public String getCategory() {
+        return category;
+    }
+
+    public void setCategory(String category) {
+        this.category = category;
+    }
+
+    public Integer getLevel() {
+        return level;
+    }
+
+    public void setLevel(Integer level) {
+        this.level = level;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
+    }
+
+    public String getMasterLevel() {
+        return masterLevel;
+    }
+
+    public void setMasterLevel(String masterLevel) {
+        this.masterLevel = masterLevel;
+    }
+
+    public Long getParentId() {
+        return parentId;
+    }
+
+    public void setParentId(Long parentId) {
+        this.parentId = parentId;
+    }
+
+    public Long getRootId1() {
+        return rootId1;
+    }
+
+    public void setRootId1(Long rootId1) {
+        this.rootId1 = rootId1;
+    }
+
+    public Long getRootId2() {
+        return rootId2;
+    }
+
+    public void setRootId2(Long rootId2) {
+        this.rootId2 = rootId2;
+    }
+
+    public Long getRootId3() {
+        return rootId3;
+    }
+
+    public void setRootId3(Long rootId3) {
+        this.rootId3 = rootId3;
+    }
+
+    public SpecialKnowledge getParent() {
+        return parent;
+    }
+
+    public void setParent(SpecialKnowledge parent) {
+        this.parent = parent;
+    }
+
+    @Override
+    public String toString() {
+        return "SpecialKnowledge{" +
+                "level=" + level +
+                ", code='" + code + '\'' +
+                ", content='" + content + '\'' +
+                ", parentId=" + parentId +
+                '}';
+    }
+}

+ 148 - 1
src/main/java/com/yaoxiang/diagnosis/word/KnowledgeUtil.java

@@ -1,15 +1,19 @@
 package com.yaoxiang.diagnosis.word;
 
+import com.yaoxiang.diagnosis.entity.SpecialKnowledge;
 import com.yaoxiang.diagnosis.entity.SubjectKnowledge;
 import com.yaoxiang.diagnosis.model.MatterVo;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 public class KnowledgeUtil {
 
@@ -59,7 +63,7 @@ public class KnowledgeUtil {
         for (int numRow = 2; numRow < sheet.getLastRowNum(); numRow++) {
             XSSFRow xRow = sheet.getRow(numRow);
 //            xRow.getCell(1).setCellValue((float) (numRow + 3) / 5);
-            if(xRow.getCell(3) == null){
+            if (xRow.getCell(3) == null) {
                 continue;
             }
             MatterVo vo = new MatterVo();
@@ -77,4 +81,147 @@ public class KnowledgeUtil {
         System.out.println(result);
         return result;
     }
+
+    public static List<SpecialKnowledge> importSpecialKnowledge(Long subjectId, String grade, byte[] data) throws Exception {
+        XSSFWorkbook book = new XSSFWorkbook(new ByteArrayInputStream(data));
+        XSSFSheet sheet = book.getSheet("知识点+编号");
+        List<SpecialKnowledge> result = new ArrayList<>();
+        //numRow从0开始
+//        System.out.println(sheet.getRow(3).getLastCellNum());
+//        String value = sheet.getRow(3).getCell(0).getStringCellValue();
+        SpecialKnowledge parent = null;
+//        result.add(knowledge);
+        for (int i = 3; i <= 110; i++) {
+            XSSFRow row = sheet.getRow(i);
+//            int colNum = row.getLastCellNum();
+            String content = row.getCell(0).getStringCellValue();
+            if (StringUtils.isNotBlank(content)) {
+                SpecialKnowledge knowledge = new SpecialKnowledge();
+                knowledge.setLevel(0);
+                knowledge.setContent(content);
+                knowledge.setSubjectId(subjectId);
+                knowledge.setGrade(grade);
+                parent = knowledge;
+                result.add(knowledge);
+            }
+            initKnowLedge(row, 1, row.getLastCellNum(), parent, result);
+            //共5列
+
+//            String title = row.getCell(0).getStringCellValue();
+//            if(StringUtils.isNotBlank(title)){
+//                curTitle = title;
+//            }
+//            String l1 = row.getCell(1).getStringCellValue();
+//            if(StringUtils.isNotBlank(l1)){
+//                curl[0]=l1;
+//            }
+//            String l2 = row.getCell(2).getStringCellValue();
+//            if(StringUtils.isNotBlank(l2)){
+//                curl[1]=l2;
+//            }
+//            String l3 = row.getCell(3).getStringCellValue();
+//            if(StringUtils.isNotBlank(l3)){
+//                curl[2]=l3;
+//            }
+//            String code = row.getCell(4).getStringCellValue();
+//            knowledge.setCode(code);
+//            knowledge.setLevel();
+        }
+        return result;
+    }
+
+    private static void initKnowLedge(XSSFRow row, int index, int max, SpecialKnowledge parent, List<SpecialKnowledge> result) {
+        if (index == max - 1) {
+            return;
+        }
+        XSSFCell cell = row.getCell(index);
+        String value = cell.getStringCellValue();
+        SpecialKnowledge knowledge;
+        if (StringUtils.isNotBlank(value)) {
+            knowledge = new SpecialKnowledge();
+            knowledge.setGrade("6Y");
+            if (index == max - 2) {
+                knowledge.setCode(row.getCell(max - 1).getStringCellValue());
+            }
+            knowledge.setLevel(index);
+            knowledge.setContent(value);
+            knowledge.setParent(parent);
+            parent = knowledge;
+            result.add(knowledge);
+        }
+        initKnowLedge(row, index + 1, max, parent, result);
+    }
+
+    public static List<SpecialKnowledge> importSpecialKnowledge1(Long subjectId, String grade, byte[] data) throws Exception {
+        XSSFWorkbook book = new XSSFWorkbook(new ByteArrayInputStream(data));
+        XSSFSheet sheet = book.getSheet("知识点+编号");
+        List<SpecialKnowledge> result = new ArrayList<>();
+
+//        int num = sheet.getLastRowNum();
+//        List<SpecialKnowledge> knowledges = new ArrayList<>();
+//        Queue<SpecialKnowledge> queue = new LinkedList<>();
+//        Map<String,SpecialKnowledge> index = new HashMap<>();
+        for (int i = 3; i <= sheet.getLastRowNum(); i++) {
+            XSSFRow row = sheet.getRow(i);
+            for (int j = 0; j < row.getLastCellNum() - 1; j++) {
+                XSSFCell cell = row.getCell(j);
+                String content = cell.getStringCellValue();
+                if (StringUtils.isBlank(content)) {
+                    cell.setCellValue(sheet.getRow(i - 1).getCell(j).getStringCellValue());
+                }
+            }
+        }
+        Map<String, SpecialKnowledge> map = new HashMap<>();
+        for (int i = 3; i <= sheet.getLastRowNum(); i++) {
+            XSSFRow row = sheet.getRow(i);
+            for (int j = 0; j < row.getLastCellNum() - 1; j++) {
+                String content = row.getCell(j).getStringCellValue();
+                if(map.containsKey(content)){
+                    continue;
+                }
+                SpecialKnowledge k = new SpecialKnowledge();
+                if (j > 0) {
+                    String parentContent = row.getCell(j - 1).getStringCellValue();
+                    k.setParent(map.get(parentContent));
+                }
+                if (j == row.getLastCellNum() - 2) {
+                    k.setCode(row.getCell(j + 1).getStringCellValue());
+                }
+                k.setLevel(j);
+                k.setGrade(grade);
+                k.setSubjectId(subjectId);
+                k.setContent(content);
+                map.put(content,k);
+                result.add(k);
+            }
+        }
+
+        //一列一列来读取
+//        int colNum = sheet.getRow(3).getLastCellNum();
+//        for (int j = 0;j < colNum -1 ;j++){
+//            SpecialKnowledge p = queue.poll();
+//            for (int i=3;i<sheet.getLastRowNum();i++){
+//                String content = sheet.getRow(i).getCell(j).getStringCellValue();
+////                if(p != null && a != null && i == a + 1){
+////                    p = queue.poll();
+////                    a = index.poll();
+////                }
+//                if(StringUtils.isNotBlank(content)){
+//                    SpecialKnowledge k = new SpecialKnowledge();
+//                    k.setParent(p);
+//                    k.setLevel(j);
+//                    k.setGrade(grade);
+//                    k.setSubjectId(subjectId);
+//                    k.setContent(content);
+//                    result.add(k);
+//                    queue.add(k);
+//                    index.put(content,k);
+//                }
+//
+//            }
+//
+//        }
+        return result;
+    }
+
 }

+ 35 - 0
src/test/java/com/yaoxiang/diagnosis/service/KnowledgeTest.java

@@ -0,0 +1,35 @@
+package com.yaoxiang.diagnosis.service;
+
+import com.yaoxiang.diagnosis.entity.SpecialKnowledge;
+import com.yaoxiang.diagnosis.word.KnowledgeUtil;
+import org.junit.Test;
+import org.springframework.util.FileCopyUtils;
+
+import java.io.File;
+import java.util.List;
+
+public class KnowledgeTest {
+
+    @Test
+    public void test() throws Exception{
+        File file = new File("doc/小升初专项诊断知识点列表.xlsx");
+        byte[] data = FileCopyUtils.copyToByteArray(file);
+        //有点问题,最底层的第二个以后的parentId不对
+        List<SpecialKnowledge> list = KnowledgeUtil.importSpecialKnowledge1(2L,"6Y",data);
+        for (SpecialKnowledge specialKnowledge : list) {
+            System.out.println(specialKnowledge + " " + specialKnowledge.hashCode() + " " + (specialKnowledge.getParent() != null ? specialKnowledge.getParent().hashCode(): "null"));
+        }
+    }
+
+    @Test
+    public void test1(){
+        System.out.println(fun(10));
+    }
+
+    public static int fun(int n){
+        if(n == 0){
+           return 1;
+        }
+        return n * fun(n-1);
+    }
+}