package com.railway.framework.web.service;

import com.railway.common.core.domain.entity.SysUser;
import com.railway.system.service.ISysMenuService;
import com.railway.system.service.ISysRoleService;
import java.util.HashSet;
import java.util.Set;
import org.springframework.stereotype.Component;

/**
 * 用户权限处理
 *
 * @author railway
 */
@Component
public class SysPermissionService {

  private final ISysRoleService roleService;
  private final ISysMenuService menuService;

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

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

  /**
   * 获取菜单数据权限
   *
   * @param user 用户信息
   * @return 菜单权限信息
   */
  public Set<String> getMenuPermission(SysUser user) {
    Set<String> perms = new HashSet<>();
    // 管理员拥有所有权限
    if (user.isAdmin()) {
      perms.add("*:*:*");
    } else {
      perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
    }
    return perms;
  }
}
