package com.yanzhu.system.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.yanzhu.common.core.constant.SecurityConstants;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.system.api.RemoteProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.system.api.domain.SysRole;
import com.yanzhu.system.api.domain.SysUser;
import com.yanzhu.system.service.ISysMenuService;
import com.yanzhu.system.service.ISysPermissionService;
import com.yanzhu.system.service.ISysRoleService;

/**
 * 用户权限处理
 * 
 * @author ruoyi
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService
{
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private RemoteProService remoteProService;

    /**
     * 获取角色数据权限
     * 
     * @param user 用户信息
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            if(SysRole.isGsAdmin(getRoleKeys(user.getRoles()))){
                roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
            }else{
                roles.addAll(roleService.selectRolePermissionByUserIdAndDept(user.getUserId(),user.getActiveProjectId()));
            }
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     * 
     * @param user 用户信息
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser user)
    {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            perms.add("*:*:*");
        }
        else
        {
            List<SysRole> roles = user.getRoles();
            if (!roles.isEmpty() && roles.size() > 1)
            {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles)
                {
                    Set<String> rolePerms;
                    if(SysRole.isGsAdmin(getRoleKeys(user.getRoles()))){
                        rolePerms = menuService.selectMenuPermsByRoleId(role.getRoleId());
                    }else{
                        rolePerms = menuService.selectMenuPermsByUserIdAndDept(role.getRoleId(),user.getActiveProjectId());
                    }
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            }
            else
            {
                if(SysRole.isGsAdmin(getRoleKeys(user.getRoles()))){
                    perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
                }else{
                    perms.addAll(menuService.selectMenuPermsByUserIdAndDept(user.getUserId(), user.getActiveProjectId()));
                }
            }
        }
        return perms;
    }

    /**
     * 读取roleKey
     * @param roles
     * @return
     */
    private List<String> getRoleKeys(List<SysRole> roles){
        return roles.stream().map(SysRole::getRoleKey)
                .collect(Collectors.toList());
    }
}
