package org.dromara.system.service.impl;

import com.hotwj.platform.resourceManagement.company.service.ISysCompanyRoleService;
import com.hotwj.platform.resourceManagement.companyPermissionGrant.service.IHotCompanyPermissionGrantService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.service.PermissionService;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.service.ISysMenuService;
import org.dromara.system.service.ISysPermissionService;
import org.dromara.system.service.ISysRoleService;
import org.springframework.stereotype.Service;

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

import static com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService.ENTERPRISE_PORT;

/**
 * 用户权限处理
 *
 * @author shihongwei
 */
@RequiredArgsConstructor
@Service
public class SysPermissionServiceImpl implements ISysPermissionService, PermissionService {

    private final ISysRoleService roleService;
    private final ISysMenuService menuService;
    private final ISysCompanyRoleService sysCompanyRoleService;
    private final IHotCompanyPermissionGrantService hotCompanyPermissionGrantService;

    /**
     * 获取角色数据权限
     *
     * @param userId 用户id
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (LoginHelper.isSuperAdmin(userId)) {
            roles.add(TenantConstants.SUPER_ADMIN_ROLE_KEY);
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(userId));
        }
        return roles;
    }

    @Override
    public Set<String> getRolePermissionByCompanyId(Long userId, Long companyId, String port) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (LoginHelper.isSuperAdmin(userId)) {
            roles.add(TenantConstants.SUPER_ADMIN_ROLE_KEY);
        } else {
            if (ENTERPRISE_PORT.equals(port)) {
                roles.addAll(sysCompanyRoleService.selectRoleKeys(userId, companyId));
            } else {
                roles.addAll(roleService.selectRolePermissionByUserIdAndCompanyId(userId, companyId, port));
            }
        }
        return roles;
    }

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

    /**
     * 获取菜单数据权限
     *
     * @param userId 用户id
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermissionByCompanyId(Long userId, Long companyId, String port) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (LoginHelper.isSuperAdmin(userId)) {
            perms.add("*:*:*");
        } else {
            if (ENTERPRISE_PORT.equals(port)) {
                perms.addAll(menuService.selectMenuPermsByUserIdAndCompany(userId, companyId, port));
                perms.addAll(hotCompanyPermissionGrantService.selectActivePermissionCodes(companyId, userId));
            } else {
                perms.addAll(menuService.selectMenuPermsByUserIdAndCompanyId(userId, companyId, port));
            }
        }
        return perms;
    }
}
