<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dromara.member.mapper.AppUserRoleMapper">

    <resultMap type="org.dromara.member.domain.AppUserRole" id="AppUserRoleResult">
        <result property="userId" column="user_id"/>
        <result property="roleId" column="role_id"/>
    </resultMap>

    <!-- 通过用户ID删除用户和角色关联 -->
    <delete id="deleteUserRoleByUserId" parameterType="Long">
        delete
        from app_user_role
        where user_id = #{userId}
    </delete>

    <!-- 批量删除用户和角色关联 -->
    <delete id="deleteUserRole" parameterType="Long">
        delete from app_user_role where user_id in
        <foreach collection="array" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <!-- 通过角色ID查询角色使用数量 -->
    <select id="countUserRoleByRoleId" parameterType="Long" resultType="int">
        select count(1)
        from app_user_role
        where role_id = #{roleId}
    </select>

    <!-- 批量新增用户角色信息 -->
    <insert id="batchUserRole">
        insert into app_user_role(user_id, role_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId}, #{item.roleId})
        </foreach>
    </insert>

</mapper>