Browse Source

添加章节树形结构

tags/正式3.2.0
王宁 2 years ago
parent
commit
9915fac066

+ 33
- 30
sstudy/src/main/java/com/xhkjedu/sstudy/controller/subjectbook/DirectorController.java View File

@@ -26,83 +26,86 @@ public class DirectorController {
26 26
     private DirectorService directorService;
27 27
 
28 28
     /**
29
-     *功能描述  教材下章节树形结构
29
+     * 功能描述  教材下章节树形结构
30
+     *
31
+     * @param * @param director
32
+     * @return com.xhkjedu.vo.ResultVo
30 33
      * @author WN
31 34
      * @date 2020/6/3
32
-     * @param  * @param director
33
-     * @return com.xhkjedu.vo.ResultVo
34 35
      */
35 36
     @PostMapping("/list")
36
-    public ResultVo list(@RequestBody TDirector director){
37
-        N_Utils.validation(new Object[]{director.getLsbid(),"教材id",1});
37
+    public ResultVo list(@RequestBody TDirector director) {
38
+        N_Utils.validation(new Object[]{director.getLsbid(), "教材id", 1});
38 39
         List<TDirector> lst = directorService.lisTDirectors(director);
39
-        return new ResultVo(0,"获取教材下章节成功",lst);
40
+        return new ResultVo(0, "获取教材下章节成功", lst);
40 41
     }
41 42
 
42 43
     /**
43
-     *功能描述  添加
44
+     * 功能描述  添加
45
+     *
46
+     * @param * @param director
47
+     * @return com.xhkjedu.vo.ResultVo
44 48
      * @author WN
45 49
      * @date 2020/6/3
46
-     * @param  * @param director
47
-     * @return com.xhkjedu.vo.ResultVo
48 50
      */
49 51
     @PostMapping("/add")
50
-    public ResultVo add(@RequestBody TDirector director){
51
-        N_Utils.validation(new Object[]{director.getDirectorname(),"章节名称",2,director.getLsbid(),"教材id",1,
52
-        director.getBelong(),"章节归属",1,director.getDirectorlevel(),"归属层级",1});
52
+    public ResultVo add(@RequestBody TDirector director) {
53
+        N_Utils.validation(new Object[]{director.getDirectorname(), "章节名称", 2, director.getLsbid(), "教材id", 1,
54
+                director.getBelong(), "章节归属", 1, director.getDirectorlevel(), "归属层级", 1});
53 55
         if (director.getDirectorlevel() > 4) {
54
-            return new ResultVo(1,"超过层级限制");
56
+            return new ResultVo(1, "超过层级限制");
55 57
         }
56 58
         int result = directorService.addDirector(director);
57 59
         if (result > 0) {
58 60
             return new ResultVo(0, "保存成功");
59
-        }else{
61
+        } else {
60 62
             return new ResultVo(1, "保存失败");
61 63
         }
62 64
     }
63 65
 
64 66
     /**
65
-     *功能描述  修改章节
67
+     * 功能描述  修改章节
68
+     *
69
+     * @param * @param director
70
+     * @return com.xhkjedu.vo.ResultVo
66 71
      * @author WN
67 72
      * @date 2020/6/1
68
-     * @param  * @param director
69
-     * @return com.xhkjedu.vo.ResultVo
70 73
      */
71 74
     @PostMapping("/update")
72
-    public ResultVo update(@RequestBody TDirector director){
73
-        N_Utils.validation(new Object[]{director.getDirectorid(),"章节id",2,director.getDirectorname(),"章节名称",2});
75
+    public ResultVo update(@RequestBody TDirector director) {
76
+        N_Utils.validation(new Object[]{director.getDirectorid(), "章节id", 2, director.getDirectorname(), "章节名称", 2});
74 77
         int result = directorService.updateDirector(director);
75 78
         if (result > 0) {
76 79
             directorService.updateDirectorAll(director.getDirectorid());
77 80
             return new ResultVo(0, "修改成功");
78
-        }
79
-        else return new ResultVo(1,"修改失败");
81
+        } else return new ResultVo(1, "修改失败");
80 82
     }
81 83
 
82 84
     /**
83
-     *功能描述 删除
85
+     * 功能描述 删除
86
+     *
87
+     * @param * @param director
88
+     * @return com.xhkjedu.vo.ResultVo
84 89
      * @author WN
85 90
      * @date 2020/6/3
86
-     * @param  * @param director
87
-     * @return com.xhkjedu.vo.ResultVo
88 91
      */
89 92
     @PostMapping("/del")
90
-    public ResultVo del(@RequestBody TDirector director){
91
-        N_Utils.validation(new Object[]{director.getDirectorid(),"章节id",2});
93
+    public ResultVo del(@RequestBody TDirector director) {
94
+        N_Utils.validation(new Object[]{director.getDirectorid(), "章节id", 2});
92 95
         String useModule = directorService.findUseModule(director.getDirectorid());
93 96
         if (N_Utils.isNotEmpty(useModule)) {
94 97
 //            return new ResultVo(1,"使用章节模块【"+useModule+"】,禁止删除");
95 98
             String[] strs = useModule.split(",");
96 99
             for (String str : strs) {
97
-                if (N_Utils.isNotEmpty(str)){
100
+                if (N_Utils.isNotEmpty(str)) {
98 101
                     useModule = str;
99 102
                     break;
100 103
                 }
101 104
             }
102
-            return new ResultVo(1,"此章节下已有"+useModule+",禁止删除");
105
+            return new ResultVo(1, "此章节下已有" + useModule + ",禁止删除");
103 106
         }
104 107
         int result = directorService.delDirector(director);
105
-        if (result > 0) return new ResultVo(0,"删除成功");
106
-        else return new ResultVo(1,"删除失败");
108
+        if (result > 0) return new ResultVo(0, "删除成功");
109
+        else return new ResultVo(1, "删除失败");
107 110
     }
108 111
 }

+ 62
- 27
sstudy/src/main/java/com/xhkjedu/sstudy/service/subjectbook/DirectorService.java View File

@@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
11 11
 
12 12
 import java.util.ArrayList;
13 13
 import java.util.List;
14
+import java.util.Objects;
15
+import java.util.stream.Collectors;
14 16
 
15 17
 /**
16 18
  * @ClassName DirectorService
@@ -24,13 +26,14 @@ public class DirectorService {
24 26
     private DirectorMapper directorMapper;
25 27
 
26 28
     /**
27
-     *功能描述 新增
29
+     * 功能描述 新增
30
+     *
31
+     * @param * @param director
32
+     * @return java.lang.Integer
28 33
      * @author WN
29 34
      * @date 2020/6/3
30
-     * @param  * @param director
31
-     * @return java.lang.Integer
32 35
      */
33
-    public Integer addDirector(TDirector director){
36
+    public Integer addDirector(TDirector director) {
34 37
         String id = StudyUtil.getId();
35 38
         director.setDirectorid(id);
36 39
         if ("".equals(director.getDirectorpid())) {
@@ -42,12 +45,13 @@ public class DirectorService {
42 45
 
43 46
     /**
44 47
      * 添加并返回主键
48
+     *
49
+     * @return java.lang.Integer
45 50
      * @Param [director]
46 51
      * @Author ywx
47 52
      * @Date 2020/7/6 15:00
48
-     * @return java.lang.Integer
49 53
      **/
50
-    public String addAndReturnKey(TDirector director){
54
+    public String addAndReturnKey(TDirector director) {
51 55
         String id = StudyUtil.getId();
52 56
         director.setDirectorid(id);
53 57
         director.setCreatetime(N_Utils.getSecondTimestamp());
@@ -56,42 +60,45 @@ public class DirectorService {
56 60
     }
57 61
 
58 62
     /**
59
-     *功能描述 修改
63
+     * 功能描述 修改
64
+     *
65
+     * @param * @param director
66
+     * @return java.lang.Integer
60 67
      * @author WN
61 68
      * @date 2020/6/3
62
-     * @param  * @param director
63
-     * @return java.lang.Integer
64 69
      */
65
-    public Integer updateDirector(TDirector director){
70
+    public Integer updateDirector(TDirector director) {
66 71
         director.setCreatetime(N_Utils.getSecondTimestamp());
67 72
         return directorMapper.updateByPrimaryKeySelective(director);
68 73
     }
69 74
 
70 75
     /**
71
-     *功能描述  删除
76
+     * 功能描述  删除
77
+     *
78
+     * @param * @param director
79
+     * @return java.lang.Integer
72 80
      * @author WN
73 81
      * @date 2020/6/3
74
-     * @param  * @param director
75
-     * @return java.lang.Integer
76 82
      */
77
-    public Integer delDirector(TDirector director){
83
+    public Integer delDirector(TDirector director) {
78 84
         return directorMapper.deleteByPrimaryKey(director.getDirectorid());
79 85
     }
80 86
 
81 87
     /**
82
-     *功能描述  章节树形结构
88
+     * 功能描述  章节树形结构
89
+     *
90
+     * @param * @param director
91
+     * @return java.util.List<com.xhkjedu.model.subjectbook.TDirector>
83 92
      * @author WN
84 93
      * @date 2020/6/3
85
-     * @param  * @param director
86
-     * @return java.util.List<com.xhkjedu.model.subjectbook.TDirector>
87 94
      */
88
-    public List<TDirector> lisTDirectors(TDirector director){
95
+    public List<TDirector> lisTDirectors(TDirector director) {
89 96
         List<TDirector> lst = directorMapper.listDirectors(director);
90 97
 
91 98
         List<TDirector> directors = new ArrayList<>();
92
-        for (TDirector d:lst){
93
-            if(d.getDirectorpid() == null){
94
-                List<TDirector> childDirectors = listChildedirector(d,lst);
99
+        for (TDirector d : lst) {
100
+            if (d.getDirectorpid() == null) {
101
+                List<TDirector> childDirectors = listChildedirector(d, lst);
95 102
                 d.setChildren(childDirectors);
96 103
                 directors.add(d);
97 104
             }
@@ -101,11 +108,11 @@ public class DirectorService {
101 108
     }
102 109
 
103 110
     //获取子章节
104
-    private List<TDirector> listChildedirector(TDirector director,List<TDirector> directors){
111
+    private List<TDirector> listChildedirector(TDirector director, List<TDirector> directors) {
105 112
         List<TDirector> rtndirectors = new ArrayList<>();
106
-        for(TDirector d : directors){
107
-            if(director.getDirectorid().equals(d.getDirectorpid())){
108
-                List<TDirector> childDirectors = listChildedirector(d,directors);
113
+        for (TDirector d : directors) {
114
+            if (director.getDirectorid().equals(d.getDirectorpid())) {
115
+                List<TDirector> childDirectors = listChildedirector(d, directors);
109 116
                 d.setChildren(childDirectors);
110 117
                 rtndirectors.add(d);
111 118
             }
@@ -113,12 +120,39 @@ public class DirectorService {
113 120
         return rtndirectors;
114 121
     }
115 122
 
123
+    //2022-03-16使用stream递归
124
+    public List<TDirector> lisTDirectors2(TDirector director) {
125
+        List<TDirector> lst = directorMapper.listDirectors(director);
126
+
127
+        List<TDirector> collect = lst.stream().filter(d -> d.getDirectorpid() == null).map(
128
+                (d) -> {
129
+                    d.setChildren(getChildrens(d, lst));
130
+                    return d;
131
+                }
132
+        ).collect(Collectors.toList());
133
+        return collect;
134
+    }
135
+
136
+    //递归查询子节点
137
+    private List<TDirector> getChildrens(TDirector root, List<TDirector> all) {
138
+        List<TDirector> children = all.stream().filter(d -> {
139
+            return Objects.equals(d.getDirectorpid(), root.getDirectorid());
140
+        }).map(
141
+                (d) -> {
142
+                    d.setChildren(getChildrens(d, all));
143
+                    return d;
144
+                }
145
+        ).collect(Collectors.toList());
146
+        return children;
147
+    }
148
+
116 149
     /**
117 150
      * 教师收藏教材下的章节数量
151
+     *
152
+     * @return java.lang.Integer
118 153
      * @Param [teacherid, lsbid]
119 154
      * @Author ywx
120 155
      * @Date 2020/7/8 14:49
121
-     * @return java.lang.Integer
122 156
      **/
123 157
     public Integer countByTeacherIdLsbId(Integer teacherid, String lsbid) {
124 158
         return directorMapper.countByTeacherIdLsbId(teacherid, lsbid);
@@ -135,10 +169,11 @@ public class DirectorService {
135 169
 
136 170
     /**
137 171
      * 修改层级章节
172
+     *
173
+     * @return void
138 174
      * @Param [directorid, directorname]
139 175
      * @Author ywx
140 176
      * @Date 2022/3/14 15:14
141
-     * @return void
142 177
      **/
143 178
     public void updateDirectorAll(String directorid) {
144 179
         DirectorVo vo = directorMapper.getNameAndLevelById(directorid);

Loading…
Cancel
Save