package com.jy.system.dao;

import com.jy.framework.model.base.BaseDao;
import com.jy.system.mapper.SysUserRoleMapper;
import com.jy.system.model.entity.SysUserRole;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class SysUserRoleDao extends BaseDao<SysUserRoleMapper, SysUserRole> {

    /**
     * 根据具用户id获取角色id列表
     */
    public List<Long> getRoleIdListByUserId(Long userId) {
        return lambdaQuery().eq(SysUserRole::getUserId, userId).list()
                .stream().map(SysUserRole::getRoleId).toList();
    }

    /**
     * 根据角色id获取用户id列表
     */
    public List<Long> getUserIdListByRoleId(Long roleId) {
        return lambdaQuery().eq(SysUserRole::getRoleId, roleId).list()
                .stream().map(SysUserRole::getUserId).toList();
    }

    /**
     * 根据用户id删除角色关联
     */
    public void removeByUserId(Long userId) {
        lambdaUpdate().eq(SysUserRole::getUserId, userId).remove();
    }

    /**
     * 根据用户id删除角色关联
     */
    public void removeByUserId(Collection<Long> userIds) {
        lambdaUpdate().in(SysUserRole::getUserId, userIds).remove();
    }

    /**
     * 根据角色id和用户id列表删除
     */
    public void removeByRoleIdAndUserIds(Long roleId, Collection<Long> userIdList) {
        lambdaUpdate().eq(SysUserRole::getRoleId, roleId).in(SysUserRole::getUserId, userIdList).remove();
    }

    /**
     * 判断角色是否已关联
     */
    public boolean roleExists(Collection<Long> roleIds) {
        return lambdaQuery().in(SysUserRole::getRoleId, roleIds).exists();
    }

}
