package cn.iocoder.yudao.module.system.api.permission;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserRoleDTO;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 权限 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class PermissionApiImpl implements PermissionApi {

    @Resource
    private PermissionService permissionService;

    @Resource
    private DictDataApi dictDataApi;

    @Override
    public Set<Long> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
        return permissionService.getUserRoleIdListByRoleId(roleIds);
    }

    @Override
    public boolean hasAnyPermissions(Long userId, String... permissions) {
        return permissionService.hasAnyPermissions(userId, permissions);
    }

    @Override
    public boolean hasAnyRoles(Long userId, String... roles) {
        return permissionService.hasAnyRoles(userId, roles);
    }

    @Override
    public DeptDataPermissionRespDTO getDeptDataPermission(Long userId) {
        return permissionService.getDeptDataPermission(userId);
    }

    /**
     * 设置用户角色
     *
     * @param userId  角色编号
     * @param roleCodes 角色编号集合
     */
    @Override
    public void assignUserRole(Long userId, Set<String> roleCodes) {
        permissionService.assignUserRoleByCodes(userId, roleCodes);
    }

    /**
     * 设置用户角色
     *
     * @author 小李
     * @date 18:58 2024/8/14
     * @param userId 用户D
     * @param roleIds 角色IDs
     **/
    @Override
    public void assignUserRoleByRoleIds(Long userId, Set<Long> roleIds){
        permissionService.assignUserRole(userId, roleIds);
    }

    /**
     * 获取某个用户的角色IDs
     * @author 小李
     * @date 19:24 2024/8/14
     * @param userId 用户ID
     **/
    @Override
    public List<Long> getRoleIdsByUserId(Long userId){
        Set<Long> roleIds = permissionService.getUserRoleIdListByUserId(userId);
        return roleIds.stream().collect(Collectors.toList());
    }

    /**
     * 通过用户id查询角色
     *
     * @param userIds 用户id集合
     * @return key用户id，value角色codes
     * @author PQZ
     * @date 14:29 2024/11/7
     **/
    @Override
    public Map<Long, List<String>> roleCodesByUserIds(List<Long> userIds) {
        List<UserRoleDTO> userRoleDTOList = permissionService.userRoleDTOList(userIds);
        Map<Long, List<String>> resultMap = userRoleDTOList.stream().collect(Collectors.groupingBy(UserRoleDTO::getUserId,Collectors.mapping(UserRoleDTO::getRoleCode,Collectors.toList())));
        return resultMap;
    }

    /**
     * 根据权限ID拿拥有该权限的所有人
     * @author 小李
     * @date 10:25 2024/8/26
     * @param roleId 权限ID
     **/
    @Override
    public List<Long> getUserIdByRoleId(Long roleId){
        return permissionService.getUserIdByRoleId(roleId);
    }

    /**
     * 判断是否有某个字典类型的角色
     *
     * @param dictType
     * @return
     */
    @Override
    public Boolean hasDictTypeRole(String dictType) {
        //根据dictType查询角色
        List<DictDataRespDTO> roleList = dictDataApi.getDictDataList(dictType);
        //角色code集合
        List<String> codes = roleList.stream().map(DictDataRespDTO::getValue).collect(Collectors.toList());

        Map<Long, List<String>> longListMap = this.roleCodesByUserIds(Collections.singletonList(SecurityFrameworkUtils.getLoginUserId()));
        List<String> roleKey = longListMap.get(SecurityFrameworkUtils.getLoginUserId());

        return roleKey != null && roleKey.stream().anyMatch(codes::contains);
    }

}
