package com.cool.store.dao;

import com.cool.store.dto.UserRoleDTO;
import com.cool.store.entity.SysRoleDO;
import com.cool.store.mapper.SysRoleMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangnan
 * @description:
 * @date 2022/4/18 10:57 PM
 */
@Repository
public class SysRoleDao {

    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 根据角色id列表查询
     * @param roleIds
     * @return
     */
    public List<SysRoleDO> selectRoleByRoleIds( List<Long> roleIds) {
        if(CollectionUtils.isEmpty(roleIds)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.getRoleByRoleIds( roleIds);
    }

    /**
     * 根据角色id列表查询
     * @param userIdList
     * @return
     */
    public List<UserRoleDTO> getUserRoleNameByUserIdList( List<String> userIdList) {
        if(CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.getUserRoleNameByUserIdList( userIdList);
    }

    public Map<Long, String> getRoleNameMap( List<Long> roleIds){
        List<SysRoleDO> sysRoleDOS = selectRoleByRoleIds( roleIds);
        if(CollectionUtils.isEmpty(sysRoleDOS)){
            return Maps.newHashMap();
        }
        return sysRoleDOS.stream().collect(Collectors.toMap(k->k.getId(), v->v.getRoleName(), (k1, k2)->k1));
    }

}
