Skip to content

Commit 1cf8ff4

Browse files
committed
[update]添加service 层代码
1 parent b6917e6 commit 1cf8ff4

File tree

5 files changed

+171
-13
lines changed

5 files changed

+171
-13
lines changed

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/dao/MenuMapper.java

+6
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public interface MenuMapper extends MyMapper<MenuInfo> {
1313
List<MenuInfo> findUserPermissions(String userName);
1414

1515
List<MenuInfo> findUserMenus(String userName);
16+
17+
/**
18+
* 删除父节点, 子节点变成顶级节点
19+
* @param menuIds
20+
*/
21+
void changToTop(List<String> menuIds);
1622
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/service/impl/MenuServiceImpl.java

+65-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import com.example.springbootshiro.domain.Tree;
66
import com.example.springbootshiro.service.BaseService;
77
import com.example.springbootshiro.service.MenuService;
8+
import com.example.springbootshiro.service.RoleMenuService;
89
import com.example.springbootshiro.util.StringUtils;
10+
import com.example.springbootshiro.util.TreeUtils;
911
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.stereotype.Service;
1113
import org.springframework.transaction.annotation.Propagation;
1214
import org.springframework.transaction.annotation.Transactional;
1315
import tk.mybatis.mapper.entity.Example;
1416

1517
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.Date;
1620
import java.util.List;
1721

1822
/**
@@ -25,6 +29,9 @@ public class MenuServiceImpl extends BaseService<MenuInfo> implements MenuServic
2529
@Autowired
2630
private MenuMapper menuMapper;
2731

32+
@Autowired
33+
private RoleMenuService roleMenuService;
34+
2835
@Override
2936
public List<MenuInfo> findUserPermissions(String userName) {
3037
return this.menuMapper.findUserPermissions(userName);
@@ -62,42 +69,93 @@ public Tree<MenuInfo> getMenuButtonTree() {
6269
tree.setText(menuInfo.getMenuName());
6370
trees.add(tree);
6471
}
65-
Tree<MenuInfo> t = null;
72+
Tree<MenuInfo> t = TreeUtils.build(trees);
6673
return t;
6774
}
6875

6976
@Override
7077
public Tree<MenuInfo> getMenuTree() {
71-
return null;
78+
List<Tree<MenuInfo>> trees = new ArrayList<>();
79+
Example example = new Example(MenuInfo.class);
80+
example.createCriteria().andCondition("type=", 0);
81+
example.setOrderByClause("create_time");
82+
List<MenuInfo> menus = this.menuMapper.selectByExample(example);
83+
for (MenuInfo menuInfo: menus){
84+
Tree<MenuInfo> tree = new Tree<>();
85+
tree.setId(menuInfo.getMenuId().toString());
86+
tree.setParentId(menuInfo.getParentId().toString());
87+
tree.setText(menuInfo.getMenuName());
88+
89+
trees.add(tree);
90+
}
91+
92+
Tree<MenuInfo> t = TreeUtils.build(trees);
93+
return t;
7294
}
7395

7496
@Override
7597
public Tree<MenuInfo> getUserMenu(String userName) {
76-
return null;
98+
99+
List<Tree<MenuInfo>> trees = new ArrayList<>();
100+
List<MenuInfo> menus = findUserMenus(userName);
101+
102+
for (MenuInfo menu : menus) {
103+
Tree<MenuInfo> tree = new Tree<MenuInfo>();
104+
tree.setId(menu.getMenuId().toString());
105+
tree.setParentId(menu.getParentId().toString());
106+
tree.setText(menu.getMenuName());
107+
tree.setUrl(menu.getUrl());
108+
trees.add(tree);
109+
}
110+
Tree<MenuInfo> t = TreeUtils.build(trees);
111+
return t;
77112
}
78113

79114
@Override
80115
public MenuInfo findById(Long menuId) {
81-
return null;
116+
return selectByKey(menuId);
82117
}
83118

84119
@Override
85120
public MenuInfo findByNameAndType(String menuName, String type) {
86-
return null;
121+
Example example = new Example(MenuInfo.class);
122+
example.createCriteria().andCondition("lower(menu_name)=", menuName.toLowerCase())
123+
.andEqualTo("type", Long.valueOf(type));
124+
List<MenuInfo> list = menuMapper.selectByExample(example);
125+
if (list.size() == 0) {
126+
return null;
127+
}else {
128+
return list.get(0);
129+
}
87130
}
88131

89132
@Override
133+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
90134
public void addMenu(MenuInfo menu) {
91-
135+
menu.setCreateTime(new Date());
136+
if (menu.getParentId() == null) {
137+
menu.setParentId(0L);
138+
}
139+
this.save(menu);
92140
}
93141

94142
@Override
143+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
95144
public void updateMenu(MenuInfo menu) {
145+
menu.setModifyTime(new Date());
146+
if (menu.getParentId() == null) {
147+
menu.setParentId(0L);
148+
}
149+
this.updateNotNull(menu);
96150

97151
}
98152

99153
@Override
154+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
100155
public void deleteMeuns(String menuIds) {
101-
156+
List<String> list = Arrays.asList(menuIds.split(","));
157+
batchDelete(list, "menuId", MenuInfo.class);
158+
roleMenuService.deleteRoleMenusByMenuId(menuIds);
159+
menuMapper.changToTop(list);
102160
}
103161
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.example.springbootshiro.service.impl;
22

33
import com.example.springbootshiro.domain.RoleMenu;
4+
import com.example.springbootshiro.domain.UserRole;
45
import com.example.springbootshiro.service.BaseService;
56
import com.example.springbootshiro.service.RoleMenuService;
67
import org.springframework.stereotype.Service;
78
import org.springframework.transaction.annotation.Propagation;
89
import org.springframework.transaction.annotation.Transactional;
910

11+
import java.util.Arrays;
12+
import java.util.List;
13+
1014
/**
1115
* Created by Douglee on 2018/4/2.
1216
*/
@@ -15,12 +19,17 @@
1519
public class RoleMenuServiceImpl extends BaseService<RoleMenu> implements RoleMenuService {
1620

1721
@Override
22+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
1823
public void deleteRoleMenusByRoleId(String roleIds) {
19-
24+
List<String> list = Arrays.asList(roleIds.split(","));
25+
this.batchDelete(list, "roleId", RoleMenu.class);
2026
}
2127

2228
@Override
29+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
2330
public void deleteRoleMenusByMenuId(String menuIds) {
31+
List<String> list = Arrays.asList(menuIds.split(","));
32+
this.batchDelete(list, "menuId", RoleMenu.class);
2433

2534
}
2635
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/service/impl/RoleServiceImpl.java

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package com.example.springbootshiro.service.impl;
22

33
import com.example.springbootshiro.dao.RoleMapper;
4+
import com.example.springbootshiro.dao.RoleMenuMapper;
45
import com.example.springbootshiro.domain.RoleInfo;
6+
import com.example.springbootshiro.domain.RoleMenu;
7+
import com.example.springbootshiro.domain.UserRole;
58
import com.example.springbootshiro.service.BaseService;
9+
import com.example.springbootshiro.service.RoleMenuService;
610
import com.example.springbootshiro.service.RoleService;
11+
import com.example.springbootshiro.service.UserRoleService;
12+
import com.example.springbootshiro.util.StringUtils;
713
import org.springframework.beans.factory.annotation.Autowired;
814
import org.springframework.stereotype.Service;
915
import org.springframework.transaction.annotation.Propagation;
1016
import org.springframework.transaction.annotation.Transactional;
17+
import tk.mybatis.mapper.entity.Example;
1118

19+
import java.util.Arrays;
20+
import java.util.Date;
1221
import java.util.List;
1322

1423
/**
@@ -21,6 +30,14 @@ public class RoleServiceImpl extends BaseService<RoleInfo> implements RoleServic
2130
@Autowired
2231
private RoleMapper roleMapper;
2332

33+
@Autowired
34+
private RoleMenuMapper roleMenuMapper;
35+
36+
@Autowired
37+
private UserRoleService userRoleService;
38+
39+
@Autowired
40+
private RoleMenuService roleMenuService;
2441

2542
@Override
2643
public List<RoleInfo> findUserRole(String userName) {
@@ -29,26 +46,67 @@ public List<RoleInfo> findUserRole(String userName) {
2946

3047
@Override
3148
public List<RoleInfo> findAllRole(RoleInfo roleInfo) {
32-
return null;
49+
Example example = new Example(RoleInfo.class);
50+
if (!StringUtils.isNullorEmpty(roleInfo.getRoleName())){
51+
example.createCriteria().andCondition("role_name=", roleInfo.getRoleName());
52+
}
53+
example.setOrderByClause("create_time");
54+
return selectByExample(example);
3355
}
3456

3557
@Override
3658
public RoleInfo findByName(String roleName) {
37-
return null;
59+
Example example = new Example(RoleInfo.class);
60+
example.createCriteria().andCondition("lower(role_name)=", roleName.toLowerCase());
61+
List<RoleInfo> list = this.roleMapper.selectByExample(example);
62+
if (list.size() == 0){
63+
return null;
64+
}else {
65+
return list.get(0);
66+
}
3867
}
3968

4069
@Override
70+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
4171
public void addRole(RoleInfo roleInfo, Long[] menuIds) {
72+
roleInfo.setCreateTime(new Date());
73+
this.save(roleInfo);
74+
for (Long menuId: menuIds){
75+
RoleMenu roleMenu = new RoleMenu();
76+
77+
roleMenu.setMenuId(menuId);
78+
roleMenu.setRoleId(roleInfo.getRoleId());
4279

80+
this.roleMenuMapper.insert(roleMenu);
81+
}
4382
}
4483

4584
@Override
85+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
4686
public void updateRole(RoleInfo roleInfo, Long[] menuIds) {
87+
roleInfo.setModifyTime(new Date());
88+
this.roleMapper.updateByPrimaryKeySelective(roleInfo);
89+
Example example = new Example(RoleMenu.class);
90+
example.createCriteria().andCondition("role_id=", roleInfo.getRoleId());
91+
this.roleMenuMapper.deleteByExample(example);
4792

93+
for (Long menuId: menuIds){
94+
RoleMenu roleMenu = new RoleMenu();
95+
96+
roleMenu.setMenuId(menuId);
97+
roleMenu.setRoleId(roleInfo.getRoleId());
98+
this.roleMenuMapper.insert(roleMenu);
99+
}
48100
}
49101

50102
@Override
103+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
51104
public void deleteRoles(String roleIds) {
105+
List<String> list = Arrays.asList(roleIds.split(","));
106+
this.batchDelete(list, "roleId", RoleInfo.class);
107+
108+
this.roleMenuService.deleteRoleMenusByRoleId(roleIds);
109+
this.userRoleService.deleteUserRoleByRoleId(roleIds);
52110

53111
}
54112
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/util/TreeUtils.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
*/
1313
public class TreeUtils {
1414

15-
public static <T> Tree<T> build(List<Tree<T>> nodes){
15+
public static <T> Tree<T> build(List<Tree<T>> nodes) {
1616
if (nodes == null) {
1717
return null;
1818
}
1919

2020
List<Tree<T>> topNodes = new ArrayList<>();
21-
for (Tree<T> children: nodes){
21+
for (Tree<T> children : nodes) {
2222
String pid = children.getParentId();
2323
if (pid == null || "0".equals(pid)) {
2424
topNodes.add(children);
2525
continue;
2626
}
2727

28-
for (Tree<T> parent: nodes){
28+
for (Tree<T> parent : nodes) {
2929
String id = parent.getId();
3030
if (id != null && id.equals(pid)) {
3131
parent.getChildren().add(children);
@@ -48,6 +48,33 @@ public static <T> Tree<T> build(List<Tree<T>> nodes){
4848
root.setState(state);
4949

5050
return root;
51+
}
52+
53+
public static <T> List<Tree<T>> buildList(List<Tree<T>> nodes, String idParam) {
54+
if (nodes == null) {
55+
return null;
56+
}
57+
58+
List<Tree<T>> topNodes = new ArrayList<>();
59+
for (Tree<T> children : nodes) {
60+
String pid = children.getParentId();
61+
if (pid == null || idParam.equals(pid)) {
62+
topNodes.add(children);
63+
continue;
64+
}
65+
for (Tree<T> parent : nodes) {
66+
String id = parent.getId();
67+
if (id != null && id.equals(pid)) {
68+
parent.getChildren().add(children);
69+
children.setHasParent(true);
70+
parent.setHasChildren(true);
71+
continue;
72+
}
73+
}
5174

75+
}
76+
return topNodes;
5277
}
78+
79+
5380
}

0 commit comments

Comments
 (0)