<?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.github.tangyi.user.mapper.UserRoleMapper">
    <resultMap id="userResultMap" type="com.github.tangyi.user.api.module.UserRole">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="role_id" property="roleId"/>
    </resultMap>

    <sql id="userRoleColumns">
        a.id,
        a.user_id,
        a.role_id
    </sql>

    <select id="getByUserId" resultMap="userResultMap">
        SELECT
        <include refid="userRoleColumns"/>
        FROM sys_user_role a
        WHERE a.user_id = #{userId}
    </select>

    <select id="getByUserIds" resultMap="userResultMap">
        SELECT
        <include refid="userRoleColumns"/>
        FROM sys_user_role a
        WHERE a.user_id IN
        <foreach collection="collection" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <insert id="insert">
        insert  into sys_user_role (id, user_id, role_id)
        values (#{id}, #{userId}, #{roleId});
    </insert>

    <delete id="delete">
        delete from sys_user_role
	    where user_id = #{userId}
	    <if test="roleId != null and roleId != ''">
            and role_id = #{roleId}
        </if>
    </delete>

    <delete id="deleteByUserId">
        delete from sys_user_role
        where user_id = #{userId}
    </delete>

    <delete id="deleteByRoleId">
        delete from sys_user_role
        where role_id = #{roleId}
    </delete>
</mapper>
