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
     private DirectorService directorService;
26
     private DirectorService directorService;
27
 
27
 
28
     /**
28
     /**
29
-     *功能描述  教材下章节树形结构
29
+     * 功能描述  教材下章节树形结构
30
+     *
31
+     * @param * @param director
32
+     * @return com.xhkjedu.vo.ResultVo
30
      * @author WN
33
      * @author WN
31
      * @date 2020/6/3
34
      * @date 2020/6/3
32
-     * @param  * @param director
33
-     * @return com.xhkjedu.vo.ResultVo
34
      */
35
      */
35
     @PostMapping("/list")
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
         List<TDirector> lst = directorService.lisTDirectors(director);
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
      * @author WN
48
      * @author WN
45
      * @date 2020/6/3
49
      * @date 2020/6/3
46
-     * @param  * @param director
47
-     * @return com.xhkjedu.vo.ResultVo
48
      */
50
      */
49
     @PostMapping("/add")
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
         if (director.getDirectorlevel() > 4) {
55
         if (director.getDirectorlevel() > 4) {
54
-            return new ResultVo(1,"超过层级限制");
56
+            return new ResultVo(1, "超过层级限制");
55
         }
57
         }
56
         int result = directorService.addDirector(director);
58
         int result = directorService.addDirector(director);
57
         if (result > 0) {
59
         if (result > 0) {
58
             return new ResultVo(0, "保存成功");
60
             return new ResultVo(0, "保存成功");
59
-        }else{
61
+        } else {
60
             return new ResultVo(1, "保存失败");
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
      * @author WN
71
      * @author WN
67
      * @date 2020/6/1
72
      * @date 2020/6/1
68
-     * @param  * @param director
69
-     * @return com.xhkjedu.vo.ResultVo
70
      */
73
      */
71
     @PostMapping("/update")
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
         int result = directorService.updateDirector(director);
77
         int result = directorService.updateDirector(director);
75
         if (result > 0) {
78
         if (result > 0) {
76
             directorService.updateDirectorAll(director.getDirectorid());
79
             directorService.updateDirectorAll(director.getDirectorid());
77
             return new ResultVo(0, "修改成功");
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
      * @author WN
89
      * @author WN
85
      * @date 2020/6/3
90
      * @date 2020/6/3
86
-     * @param  * @param director
87
-     * @return com.xhkjedu.vo.ResultVo
88
      */
91
      */
89
     @PostMapping("/del")
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
         String useModule = directorService.findUseModule(director.getDirectorid());
95
         String useModule = directorService.findUseModule(director.getDirectorid());
93
         if (N_Utils.isNotEmpty(useModule)) {
96
         if (N_Utils.isNotEmpty(useModule)) {
94
 //            return new ResultVo(1,"使用章节模块【"+useModule+"】,禁止删除");
97
 //            return new ResultVo(1,"使用章节模块【"+useModule+"】,禁止删除");
95
             String[] strs = useModule.split(",");
98
             String[] strs = useModule.split(",");
96
             for (String str : strs) {
99
             for (String str : strs) {
97
-                if (N_Utils.isNotEmpty(str)){
100
+                if (N_Utils.isNotEmpty(str)) {
98
                     useModule = str;
101
                     useModule = str;
99
                     break;
102
                     break;
100
                 }
103
                 }
101
             }
104
             }
102
-            return new ResultVo(1,"此章节下已有"+useModule+",禁止删除");
105
+            return new ResultVo(1, "此章节下已有" + useModule + ",禁止删除");
103
         }
106
         }
104
         int result = directorService.delDirector(director);
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
 
11
 
12
 import java.util.ArrayList;
12
 import java.util.ArrayList;
13
 import java.util.List;
13
 import java.util.List;
14
+import java.util.Objects;
15
+import java.util.stream.Collectors;
14
 
16
 
15
 /**
17
 /**
16
  * @ClassName DirectorService
18
  * @ClassName DirectorService
24
     private DirectorMapper directorMapper;
26
     private DirectorMapper directorMapper;
25
 
27
 
26
     /**
28
     /**
27
-     *功能描述 新增
29
+     * 功能描述 新增
30
+     *
31
+     * @param * @param director
32
+     * @return java.lang.Integer
28
      * @author WN
33
      * @author WN
29
      * @date 2020/6/3
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
         String id = StudyUtil.getId();
37
         String id = StudyUtil.getId();
35
         director.setDirectorid(id);
38
         director.setDirectorid(id);
36
         if ("".equals(director.getDirectorpid())) {
39
         if ("".equals(director.getDirectorpid())) {
42
 
45
 
43
     /**
46
     /**
44
      * 添加并返回主键
47
      * 添加并返回主键
48
+     *
49
+     * @return java.lang.Integer
45
      * @Param [director]
50
      * @Param [director]
46
      * @Author ywx
51
      * @Author ywx
47
      * @Date 2020/7/6 15:00
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
         String id = StudyUtil.getId();
55
         String id = StudyUtil.getId();
52
         director.setDirectorid(id);
56
         director.setDirectorid(id);
53
         director.setCreatetime(N_Utils.getSecondTimestamp());
57
         director.setCreatetime(N_Utils.getSecondTimestamp());
56
     }
60
     }
57
 
61
 
58
     /**
62
     /**
59
-     *功能描述 修改
63
+     * 功能描述 修改
64
+     *
65
+     * @param * @param director
66
+     * @return java.lang.Integer
60
      * @author WN
67
      * @author WN
61
      * @date 2020/6/3
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
         director.setCreatetime(N_Utils.getSecondTimestamp());
71
         director.setCreatetime(N_Utils.getSecondTimestamp());
67
         return directorMapper.updateByPrimaryKeySelective(director);
72
         return directorMapper.updateByPrimaryKeySelective(director);
68
     }
73
     }
69
 
74
 
70
     /**
75
     /**
71
-     *功能描述  删除
76
+     * 功能描述  删除
77
+     *
78
+     * @param * @param director
79
+     * @return java.lang.Integer
72
      * @author WN
80
      * @author WN
73
      * @date 2020/6/3
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
         return directorMapper.deleteByPrimaryKey(director.getDirectorid());
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
      * @author WN
92
      * @author WN
84
      * @date 2020/6/3
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
         List<TDirector> lst = directorMapper.listDirectors(director);
96
         List<TDirector> lst = directorMapper.listDirectors(director);
90
 
97
 
91
         List<TDirector> directors = new ArrayList<>();
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
                 d.setChildren(childDirectors);
102
                 d.setChildren(childDirectors);
96
                 directors.add(d);
103
                 directors.add(d);
97
             }
104
             }
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
         List<TDirector> rtndirectors = new ArrayList<>();
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
                 d.setChildren(childDirectors);
116
                 d.setChildren(childDirectors);
110
                 rtndirectors.add(d);
117
                 rtndirectors.add(d);
111
             }
118
             }
113
         return rtndirectors;
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
      * @Param [teacherid, lsbid]
153
      * @Param [teacherid, lsbid]
119
      * @Author ywx
154
      * @Author ywx
120
      * @Date 2020/7/8 14:49
155
      * @Date 2020/7/8 14:49
121
-     * @return java.lang.Integer
122
      **/
156
      **/
123
     public Integer countByTeacherIdLsbId(Integer teacherid, String lsbid) {
157
     public Integer countByTeacherIdLsbId(Integer teacherid, String lsbid) {
124
         return directorMapper.countByTeacherIdLsbId(teacherid, lsbid);
158
         return directorMapper.countByTeacherIdLsbId(teacherid, lsbid);
135
 
169
 
136
     /**
170
     /**
137
      * 修改层级章节
171
      * 修改层级章节
172
+     *
173
+     * @return void
138
      * @Param [directorid, directorname]
174
      * @Param [directorid, directorname]
139
      * @Author ywx
175
      * @Author ywx
140
      * @Date 2022/3/14 15:14
176
      * @Date 2022/3/14 15:14
141
-     * @return void
142
      **/
177
      **/
143
     public void updateDirectorAll(String directorid) {
178
     public void updateDirectorAll(String directorid) {
144
         DirectorVo vo = directorMapper.getNameAndLevelById(directorid);
179
         DirectorVo vo = directorMapper.getNameAndLevelById(directorid);

Loading…
Cancel
Save