Browse Source

班级组织树形结构修改

guq 7 years ago
parent
commit
abd62dc384

+ 13 - 1
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/SchoolApplication.java

@@ -6,6 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
 import org.springframework.context.annotation.ComponentScan;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
 /**
  * @author: guq
@@ -16,8 +18,18 @@ import org.springframework.context.annotation.ComponentScan;
 @EnableEurekaClient
 @EnableFeignClients("com.usoftchina.smartschool")
 @ComponentScan(basePackages = {"com.usoftchina.smartschool"})
-public class SchoolApplication {
+public class SchoolApplication extends WebMvcConfigurerAdapter {
     public static void main(String[] args) {
         SpringApplication.run(SchoolApplication.class, args);
     }
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+
+        registry.addMapping("/**")
+                .allowCredentials(true)
+                .allowedHeaders("*")
+                .allowedOrigins("*")
+                .allowedMethods("*");
+    }
 }

+ 2 - 1
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/basic/controller/GradeController.java

@@ -3,6 +3,7 @@ package com.usoftchina.smartschool.school.basic.controller;
 import com.usoftchina.smartschool.base.Result;
 import com.usoftchina.smartschool.school.basic.service.GradeService;
 import com.usoftchina.smartschool.school.po.SysSchool;
+import com.usoftchina.smartschool.school.po.TreeNode;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -28,7 +29,7 @@ public class GradeController {
     */
     @RequestMapping("/read/{id}")
     public Result getSchoolTree(@PathVariable("id") Long id) {
-        SysSchool school = gradeService.getSchoolTree(id);
+        TreeNode school = gradeService.getSchoolTree(id);
         return Result.success(school);
     }
 

+ 2 - 1
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/basic/service/GradeService.java

@@ -1,7 +1,8 @@
 package com.usoftchina.smartschool.school.basic.service;
 
 import com.usoftchina.smartschool.school.po.SysSchool;
+import com.usoftchina.smartschool.school.po.TreeNode;
 
 public interface GradeService {
-    SysSchool getSchoolTree(Long id);
+    TreeNode getSchoolTree(Long id);
 }

+ 37 - 7
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/basic/service/impl/GradeServiceImpl.java

@@ -6,12 +6,11 @@ import com.usoftchina.smartschool.school.exception.BizExceptionCode;
 import com.usoftchina.smartschool.school.mapper.SysClazzMapper;
 import com.usoftchina.smartschool.school.mapper.SysGradeMapper;
 import com.usoftchina.smartschool.school.mapper.SysSchoolMapper;
-import com.usoftchina.smartschool.school.po.SysClazz;
-import com.usoftchina.smartschool.school.po.SysGrade;
-import com.usoftchina.smartschool.school.po.SysSchool;
+import com.usoftchina.smartschool.school.po.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -31,20 +30,51 @@ public class GradeServiceImpl implements GradeService{
     private SysClazzMapper sysClazzMapper;
 
     @Override
-    public SysSchool getSchoolTree(Long id) {
+    public TreeNode getSchoolTree(Long id) {
         if (null == id || "0".equals(id)) {
             throw new BizException(BizExceptionCode.USELESS_DATA);
         }
         id = 1l;
+
         SysSchool school = sysSchoolMapper.selectByPrimaryKey(id);
+        TreeNode schoolTree = new TreeNode();
+        TreeNode gradeNode = null;
+        TreeNode classNode = null;
+        List<TreeNode> gradesTree = new ArrayList<>();
+        List<TreeNode> classesTree = new ArrayList<>();
+        schoolTree.setId(school.getSchool_id());
+        schoolTree.setLeaf(false);
+        schoolTree.setText(school.getSchool_name());
+        schoolTree.setType(SCHOOLTYPE.SCHOOL.name());
+
         List<SysGrade> grades = sysGradeMapper.selectBySchool(id);
         if (null != grades && grades.size() > 0) {
+
             for (SysGrade grade : grades) {
+                gradeNode = new TreeNode();
+                gradeNode.setType(SCHOOLTYPE.GRADE.name());
+                gradeNode.setLeaf(false);
+                gradeNode.setText(grade.getGrade_name());
+                gradeNode.setId(grade.getGrade_id());
+
                 List<SysClazz> classes = sysClazzMapper.selectBygrade(grade.getGrade_id());
-                grade.setChildren(classes);
+                if (null != classes && classes.size() > 0) {
+                    for (SysClazz class_ : classes) {
+                        classNode = new TreeNode();
+                        classNode.setId(class_.getClazz_id());
+                        classNode.setLeaf(true);
+                        classNode.setText(class_.getClazz_name());
+                        classNode.setType(SCHOOLTYPE.CALSS.name());
+                        classesTree.add(classNode);
+                    }
+                }
+                gradeNode.setChildren(classesTree);
+                gradesTree.add(gradeNode);
+                //grade.setChildren(classes);
             }
         }
-        school.setChildren(grades);
-        return school;
+        schoolTree.setChildren(gradesTree);
+        //school.setChildren(grades);
+        return schoolTree;
     }
 }

+ 22 - 0
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/po/SCHOOLTYPE.java

@@ -0,0 +1,22 @@
+package com.usoftchina.smartschool.school.po;
+
+public enum SCHOOLTYPE {
+
+
+    SCHOOL("学校"),
+
+    GRADE("年纪"),
+
+    CLASS("班级")
+    ;
+
+
+    private String display;
+    private SCHOOLTYPE(String display){
+        this.display = display;
+    }
+
+    public String getDisplay(){
+        return display;
+    }
+}

+ 57 - 0
applications/school/school-server/src/main/java/com/usoftchina/smartschool/school/po/TreeNode.java

@@ -0,0 +1,57 @@
+package com.usoftchina.smartschool.school.po;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author: guq
+ * @create: 2019-01-24 19:31
+ **/
+public class TreeNode implements Serializable{
+
+    private Long id;
+    private String type;
+    private String text;
+    private boolean leaf;
+    private List<TreeNode> children;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getType() {
+        return type;
+    }
+
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    public String getText() {
+        return text;
+    }
+
+    public void setText(String text) {
+        this.text = text;
+    }
+
+    public boolean isLeaf() {
+        return leaf;
+    }
+
+    public void setLeaf(boolean leaf) {
+        this.leaf = leaf;
+    }
+
+    public List<TreeNode> getChildren() {
+        return children;
+    }
+
+    public void setChildren(List<TreeNode> children) {
+        this.children = children;
+    }
+}