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 getRoleList() { return roleDao.getRoleList(); } public Map getRoleListByPage(int spage, int epage) { Mapmap=new HashMap(); try { int count = roleDao.getCount(); List 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 getViewTree(int rid) { Listtree=new ArrayList(); Listlist=roleDao.queryMenuByRoleId(rid); Listplist=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); } } Listclist=roleDao.queryChildMenuByPid(m.getMenu_id());//子菜单 ListchildList=new ArrayList(); 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 authList=new ArrayList(); 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 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; } }