<?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.ruoyi.system.mapper.system.SysUserRoleMapper">

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

	<delete id="deleteUserRoleByUserId" parameterType="String">
		delete from sys_user_role where user_id=#{userId}
	</delete>
	
	<select id="countUserRoleByRoleId" resultType="Integer">
	    select count(1) from sys_user_role where role_id=#{roleId}  
	</select>

	<select id="countUserRoleByRoleIds" resultType="Integer">
		select count(1) from sys_user_role where role_id in
		<foreach collection="list" item="roleId" open="(" separator="," close=")">
			#{roleId}
		</foreach>
	</select>
    <select id="getByRoleIdAndUserId" resultType="com.ruoyi.system.domain.system.SysUserRole">
		select user_id as userId, role_id as RoleId from sys_user_role where role_id = #{roleId} and user_id = #{userId}
	</select>

	<select id="listByRoleIds" resultType="com.ruoyi.system.domain.system.SysUserRole">
		select user_id as userId, role_id as RoleId from sys_user_role where role_id in
		<foreach collection="list" item="roleId" open="(" separator="," close=")">
			#{roleId}
		</foreach>
	</select>

	<delete id="deleteUserRole" parameterType="String">
 		delete from sys_user_role where user_id in
 		<foreach collection="array" item="userId" open="(" separator="," close=")">
 			#{userId}
        </foreach> 
 	</delete>
	
	<insert id="batchUserRole">
		insert into sys_user_role(user_id, role_id) values
		<foreach item="item" index="index" collection="list" separator=",">
			(#{item.userId},#{item.roleId})
		</foreach>
	</insert>
	<insert id="insertSysRole" parameterType="com.ruoyi.system.domain.system.SysUserRole">
		insert into sys_user_role(user_id, role_id) values(#{userId}, #{roleId})
	</insert>

	<delete id="deleteUserRoleInfo" parameterType="com.ruoyi.system.domain.system.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>
</mapper> 