package com.inspect.system.service.impl;

import com.inspect.system.base.domain.SysUser;
import com.inspect.system.service.ISysMenuService;
import com.inspect.system.service.ISysPermissionService;
import com.inspect.system.service.ISysRoleService;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysPermissionServiceImpl implements ISysPermissionService {
    private final ISysRoleService roleService;
    private final ISysMenuService menuService;

    public SysPermissionServiceImpl(ISysRoleService roleService, ISysMenuService menuService) {
        this.roleService = roleService;
        this.menuService = menuService;
    }

    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<>();
        if (SysUser.isAdmin(userId)) {
            roles.add("admin");
        } else {
            roles.addAll(this.roleService.selectRolePermissionByUserId(userId));
        }

        return roles;
    }

    public Set<String> getMenuPermission(Long userId) {
        Set<String> perms = new HashSet<>();
        if (SysUser.isAdmin(userId)) {
            perms.add("*:*:*");
        } else {
            perms.addAll(this.menuService.selectMenuPermsByUserId(userId));
        }

        return perms;
    }
}
