content: package me.flyray.rbac.service.impl;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import org.apache.commons.lang.StringUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import me.flyray.rbac.dao.SysMenuDao;
  import me.flyray.rbac.dao.SysUserDao;
  import me.flyray.rbac.entity.SysMenuEntity;
  import me.flyray.rbac.service.SysMenuService;
  import me.flyray.rbac.service.SysRoleMenuService;
  import me.flyray.rbac.service.SysUserService;
  import me.flyray.rbac.utils.Constant;
  import me.flyray.rbac.utils.Constant.MenuType;
  
  
  @Service("sysMenuService")
  public class SysMenuServiceImpl implements SysMenuService {
  	@Autowired
  	private SysMenuDao sysMenuDao;
  	@Autowired
  	private SysUserDao sysUserDao;
  	@Autowired
  	private SysUserService sysUserService;
  	@Autowired
  	private SysRoleMenuService sysRoleMenuService;
  	
  	@Override
  	public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
  		List<SysMenuEntity> menuList = sysMenuDao.queryListParentId(parentId);
  		if(menuIdList == null){
  			return menuList;
  		}
  		
  		List<SysMenuEntity> userMenuList = new ArrayList<>();
  		for(SysMenuEntity menu : menuList){
  			if(menuIdList.contains(menu.getMenuId())){
  				userMenuList.add(menu);
  			}
  		}
  		return userMenuList;
  	}
  
  	@Override
  	public List<SysMenuEntity> queryNotButtonList() {
  		return sysMenuDao.queryNotButtonList();
  	}
  
  	@Override
  	public List<SysMenuEntity> getUserMenuList(Long userId) {
  		//系统管理员，拥有最高权限
  		if(userId == 1){
  			return getAllMenuList(null);
  		}
  		
  		//用户菜单列表
  		List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
  		return getAllMenuList(menuIdList);
  	}
  	
  	@Override
      public Set<String> getUserPermissions(long userId) {
          List<String> permsList;
  
          //系统管理员，拥有最高权限
          if(userId == Constant.SUPER_ADMIN){
              List<SysMenuEntity> menuList = sysMenuDao.queryList(new HashMap<String, Object>());
              permsList = new ArrayList<>(menuList.size());
              for(SysMenuEntity menu : menuList){
                  permsList.add(menu.getPerms());
              }
          }else{
              permsList = sysUserDao.queryAllPerms(userId);
          }
          //用户权限列表
          Set<String> permsSet = new HashSet<>();
          for(String perms : permsList){
              if(StringUtils.isBlank(perms)){
                  continue;
              }
              permsSet.addAll(Arrays.asList(perms.trim().split(",")));
          }
          return permsSet;
      }
  	
  	@Override
  	public SysMenuEntity queryObject(Long menuId) {
  		return sysMenuDao.queryObject(menuId);
  	}
  
  	@Override
  	public List<SysMenuEntity> queryList(Map<String, Object> map) {
  		return sysMenuDao.queryList(map);
  	}
  
  	@Override
  	public int queryTotal(Map<String, Object> map) {
  		return sysMenuDao.queryTotal(map);
  	}
  
  	@Override
  	public void save(SysMenuEntity menu) {
  		sysMenuDao.save(menu);
  	}
  
  	@Override
  	public void update(SysMenuEntity menu) {
  		sysMenuDao.update(menu);
  	}
  
  	@Override
  	@Transactional
  	public void deleteBatch(Long[] menuIds) {
  		sysMenuDao.deleteBatch(menuIds);
  	}
  	
  	@Override
  	public List<SysMenuEntity> queryUserList(Long userId) {
  		return sysMenuDao.queryUserList(userId);
  	}
  
  	/**
  	 * 获取所有菜单列表
  	 */
  	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList){
  		//查询根菜单列表
  		List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
  		//递归获取子菜单
  		getMenuTreeList(menuList, menuIdList);
  		
  		return menuList;
  	}
  
  	/**
  	 * 递归
  	 */
  	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList){
  		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
  		
  		for(SysMenuEntity entity : menuList){
  			if(entity.getType() == MenuType.CATALOG.getValue()){//目录
  				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
  			}
  			subMenuList.add(entity);
  		}
  		
  		return subMenuList;
  	}
  
  	@Override
  	public List<SysMenuEntity> queryListParentId(Long parentId) {
  		return sysMenuDao.queryListParentId(parentId);
  	}
  
  }
  