王佳豪
2021-06-26 718f604deb342b0bee6c588bb44e22ced3371fb8
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.slcf.service.impl;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.slcf.dao.RoleDao;
import com.slcf.pojo.MenuBean;
import com.slcf.pojo.RoleBean;
import com.slcf.pojo.RoleMenu;
import com.slcf.service.RoleService;
import com.slcf.util.Trees;
import com.slcf.util.ViewTree;
 
@Service
public class RoleServiceImpl implements RoleService {
 
    @Resource
    RoleDao roleDao;
    
    public List<RoleBean> getRoleList() {
        return roleDao.getRoleList();
    }
 
    public Map<String, Object> getRoleListByPage(int spage, int epage) {
        Map<String,Object>map=new HashMap<String, Object>();
        try {
            int count = roleDao.getCount();
            List<RoleBean> list = roleDao.getRoleListByPage(spage, epage);
            map.put("total", count);
            map.put("rows", list);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return map;
    }
 
    public boolean checkRoleName(String name,int rid) {
        boolean flag=false;
        try {
            RoleBean role = roleDao.queryRoleByName(name);
            if (rid != 0) {// 修改
                if ((role != null && role.getRole_id() == rid) || role == null) {
                    flag = true;
                }
            } else {// 添加
                if (role == null) {
                    flag = true;
                }
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return flag;
    }
 
    public int saveRole(RoleBean role, HttpServletRequest request) {
        int i=0;
//        String name=(String)request.getSession().getAttribute("NAME");
        try {
            i = roleDao.saveRole(role);
            // int id=roleDao.queryRoleByName(role.getRole_name()).getRole_id();
            // if(i>0){
            // j=roleDao.saveRoleOpt(name, "添加", id);
            // }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    public int delRole(int rid) {        
        int j=0;
        try {
            j=roleDao.delRole(rid);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return j;
    }
 
    public RoleBean queryRoleById(int rid) {
        try {
            return roleDao.queryRoleById(rid);
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
 
    public int upRole(RoleBean role,HttpServletRequest request) {
        int i=0;
        try {
            i=roleDao.upRole(role);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return i;
    }
 
    /**
     * 菜单
     */
    public List<ViewTree> getViewTree(int rid) {
        List<ViewTree>tree=new ArrayList<ViewTree>();
        List<Integer>list=roleDao.queryMenuByRoleId(rid);
        List<MenuBean>plist=roleDao.getParentMenuList();//父菜单
        try {
            for(MenuBean m:plist){
                ViewTree parent=new ViewTree();
                parent.setId(m.getMenu_id());
                parent.setPid(m.getParentId());
                parent.setText(m.getMenu_name());
                parent.setIcon(m.getIcon());
                
                for(Integer k:list){
                    if(m.getMenu_id().equals(k)){
                        Trees t=new Trees();
                        t.setChecked(true);
                        parent.setState(t);
                        }
                }
                List<MenuBean>clist=roleDao.queryChildMenuByPid(m.getMenu_id());//子菜单
                List<ViewTree>childList=new ArrayList<ViewTree>();
                for(MenuBean c:clist){
                    ViewTree child=new ViewTree();
                    child.setId(c.getMenu_id());
                    child.setPid(c.getParentId());
                    child.setText(c.getMenu_name());
                    child.setIcon(c.getIcon());
                    for(Integer k:list){
                        if(c.getMenu_id().equals(k)){
                            Trees tt=new Trees();
                            tt.setChecked(true);
                            child.setState(tt);
                            }
                    }
                    
                    String auth_code = c.getAuth_code();
                    List<ViewTree> authList=new ArrayList<ViewTree>();
                    if(auth_code!=null && !auth_code.equals("")) {
                        String[] authArr = auth_code.split(";");
                        if(authArr!=null) {
                            for(String authStr : authArr) {
                                String[] array = authStr.split(":");
                                if(array==null || array.length<2) {
                                    continue;
                                }
                                ViewTree authTree=new ViewTree();
                                authTree.setId(Integer.parseInt(array[0]));
                                authTree.setPid(c.getMenu_id());
                                authTree.setText(array[1]);
                                authTree.setIcon(c.getIcon());
                                
                                String str = array[0].substring(3, 5);
                                String authStrs=roleDao.queryAuthStrsByRoleMenu(rid,c.getMenu_id());
                                if(authStrs!=null && authStrs.length()>0) {
                                    String[] arrayAuth = authStrs.split(",");
                                    if(arrayAuth!=null && Arrays.asList(arrayAuth).contains(str)) {
                                        Trees t=new Trees();
                                        t.setChecked(true);
                                        authTree.setState(t);
                                    }
                                }
                                
                                authList.add(authTree);
                            }
                            child.setNodes(authList);
                        }
                    }
                    
                    childList.add(child);
                }
                if(childList.size()>=1){
                    parent.setNodes(childList);
                }
                tree.add(parent);
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tree;
    }
 
    @Transactional
    public boolean saveRoleMenu(int rid, int[] mid, int[] auths) throws Exception{
        boolean flag=false;
        String auth_code="";
        try {
            int sum = 0;
            List<RoleMenu> rmList = roleDao.queryRoleMenu(rid);
            if (rmList != null && rmList.size() > 0) {
                int x = roleDao.delRoleMenu(rid);
                if (x > 0) {
                    for (Integer i : mid) {
                        auth_code = "";
                        for(Integer m : auths) {
                            String head = m.toString().substring(0, 3);
                            String body = m.toString().substring(3, 5);
                            if(i==Integer.parseInt(head)) {
                                auth_code += body + ",";
                            }
                        }
                        if(auth_code.length()>0) {
                            auth_code = auth_code.substring(0,auth_code.length()-1);
                        }
                        int j = roleDao.saveRoleMenu(rid, i,auth_code);
                        sum += j;
                    }
                    if (sum == mid.length) {
                        flag = true;
                    }
                }
            } else {
                for (Integer i : mid) {
                    auth_code = "";
                    for(Integer m : auths) {
                        String head = m.toString().substring(0, 3);
                        String body = m.toString().substring(3, 5);
                        if(i==Integer.parseInt(head)) {
                            auth_code += body + ",";
                        }
                    }
                    if(auth_code.length()>0) {
                        auth_code = auth_code.substring(0,auth_code.length()-1);
                    }
                    int j = roleDao.saveRoleMenu(rid, i, auth_code);
                    sum += j;
                }
                if (sum == mid.length) {
                    flag = true;
                }
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return flag;
    }
    
    /**
     * 根据角色id集合,用户id查询按钮权限
     * @param rids
     * @param menu_id
     * @return
     */
    public String getAuthListByRoleMenu(int rid,int menu_id){
        String result = "";
        try {
            result = roleDao.queryAuthStrsByRoleMenu(rid, menu_id);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }
}