package com.usky.system.service.impl;

import com.usky.system.domain.SysUser;
import com.usky.system.domain.SysUserVO;
import com.usky.system.service.ISysMenuService;
import com.usky.system.service.ISysPermissionService;
import com.usky.system.service.ISysRoleService;
import com.usky.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;


@Service
public class SysPermissionServiceImpl implements ISysPermissionService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysUserService userService;

    /**
     * 获取角色数据权限
     *
     * @param userId 用户Id
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        SysUser sysUser = userService.selectUserByIdOne(userId);
        if ("01".equals(sysUser.getUserType())) {
            roles.add("admin");
        } else {
            if (SysUserVO.isAdmin(userId)) {
                roles.add("admin");
            } else {
                roles.addAll(roleService.selectRolePermissionByUserId(userId));
            }
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param userId 用户Id
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(Long userId) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        SysUser sysUser = userService.selectUserByIdOne(userId);
        if ("01".equals(sysUser.getUserType())) {
            perms.add("*:*:*");
//                perms.addAll(menuService.selectMenuPermsByUserIdOne(SecurityUtils.getTenantId()));
        } else {
            if (SysUserVO.isAdmin(userId)) {
                perms.add("*:*:*");
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(userId));
            }
        }
        return perms;
    }
}
