<?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="com.hzs.system.sys.mapper.SysUserRoleMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysUserRole" id="SysUserRoleResult">
        <result property="userId" column="user_id"/>
        <result property="roleId" column="role_id"/>
    </resultMap>

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

    <select id="countUserRoleByRoleId" resultType="Integer">
        select count(1)
        from sys_user_role sur
                 inner join sys_user su
                            on su.user_id = sur.user_id
                                and su.del_flag = 0
        where sur.role_id = #{roleId}
    </select>

    <delete id="deleteUserRole" parameterType="Long">
        delete from sys_user_role where user_id in
        <foreach collection="array" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <!-- 批量新增用户角色信息 -->
    <insert id="insertUserRoleBatch">
        INSERT ALL
        <foreach collection="list" item="item" separator=" ">
            into sys_user_role(user_id, role_id)
            values
            (#{item.userId},#{item.roleId})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <delete id="deleteUserRoleInfo" parameterType="SysUserRole">
        delete
        from sys_user_role
        where user_id = #{userId}
          and role_id = #{roleId}
    </delete>

    <delete id="deleteUserRoleInfos">
        delete from sys_user_role where role_id=#{roleId} and user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <!-- 根据用户查询角色ID列表 -->
    <select id="queryRoleIdListByUser" resultType="long">
        select sur.role_id
        from sys_user_role sur
        where sur.user_id = #{userId}
    </select>

</mapper>
