ModuleServiceImpl.java
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.bsth.service.sys.impl;
import com.bsth.common.ResponseCode;
import com.bsth.entity.sys.Module;
import com.bsth.entity.sys.Role;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.sys.ModuleRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.sys.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
@Service
public class ModuleServiceImpl extends BaseServiceImpl<Module, Integer> implements ModuleService{
@Autowired
ModuleRepository moduleRepository;
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public List<Module> findByGroupType(String group) {
String[] array;
if(group.indexOf(",") != -1){
array = group.split(",");
}
else
array = new String[]{group};
return moduleRepository.findByGroupType(array);
}
@Override
public Map<String, Object> delete(Integer id) {
Map<String, Object> map = new HashMap<>();
//判断删除的节点是否有子节点
List<Module> list = moduleRepository.findByPId(id);
if(null != list && list.size() > 0){
map.put("status", ResponseCode.ERROR);
map.put("msg", "失败,要删除的项还存在子节点");
}
else{
map = super.delete(id);
}
return map;
}
@Override
public List<Module> findByCurrentUser() {
SysUser user = SecurityUtils.getCurrentUser();
Set<Role> roles = user.getRoles();
String inCond = "";
for(Role r : roles)
inCond += ("," + r.getId());
inCond = "(" + inCond.substring(1) + ")";
String sql = "select ID,CREATE_DATE,`ENABLE`,GROUP_TYPE,ICON,MAPP_SYMBOL,NAME,P_ID,PATH,UPDATE_DATE,CONTAINER from bsth_c_sys_module m where id in (select modules from bsth_c_sys_role_modules where roles in "+inCond+") or group_type != 3";
List<Module> all = jdbcTemplate.query(sql, new ModuleRowMapper())
,rs = new ArrayList<>();
Map<Integer, Module> map = new HashMap<>();
for(Module m : all){
map.put(m.getId(), m);
if(m.getGroupType().equals("3"))
rs.add(m);
}
//上层目录和组节点
Set<Module> pSet = new HashSet<>();
for(Module m : rs){
searchParentNode(m, map, pSet);
}
rs.addAll(pSet);
//排序
Collections.sort(rs, new Comparator<Module>() {
@Override
public int compare(Module o1, Module o2) {
return o1.getId() - o2.getId();
}
});
return rs;
}
@Override
public List<Module> findAll_distinct() {
return moduleRepository.findAll_distinct();
}
/**
*
* @Title: searchParentNode
* @Description: TODO(搜索上层节点)
* @param @param m 当前节点
* @param @param idMap 全量的ID和节点对照
* @param @param pSet 上层节点容器
* @throws
*/
public void searchParentNode(Module m, Map<Integer, Module> idMap, Set<Module> pSet){
int pId = m.getpId();
if(pId > 0){
Module pModule = idMap.get(pId);
pSet.add(pModule);
if(null != pModule &&
null != pModule.getpId()
&& pModule.getpId() > 0)
searchParentNode(pModule, idMap, pSet);
}
}
public class ModuleRowMapper implements RowMapper<Module>{
@Override
public Module mapRow(ResultSet rs, int rowNum) throws SQLException {
Module module = new Module();
module.setId(rs.getInt("ID"));
module.setCreateDate(rs.getDate("CREATE_DATE"));
module.setEnable(rs.getBoolean("ENABLE"));
module.setGroupType(rs.getString("GROUP_TYPE"));
module.setIcon(rs.getString("ICON"));
module.setMappSymbol(rs.getString("MAPP_SYMBOL"));
module.setName(rs.getString("NAME"));
module.setpId(rs.getInt("P_ID"));
module.setPath(rs.getString("PATH"));
module.setUpdateDate(rs.getDate("UPDATE_DATE"));
module.setContainer(rs.getString("CONTAINER"));
return module;
}
}
}