<?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="cn.fateverse.admin.mapper.UserRoleMapper">


    <insert id="batchInsert">
        insert into sys_user_role (user_id, role_id) values
        <foreach collection="list" separator="," item="userRole">
            (#{userRole.userId},#{userRole.roleId})
        </foreach>
    </insert>

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

    <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>

    <delete id="unBind">
        delete from sys_user_role where role_id = #{roleId}
        and user_id in
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
    </delete>
    <select id="selectRoleByUserId" resultType="cn.fateverse.common.core.entity.Role">
        select r.role_id,
               r.role_name,
               r.role_key,
               r.role_sort,
               r.data_scope,
               r.`state`,
               r.del_flag,
               r.create_by,
               r.create_time,
               r.update_by,
               r.update_time,
               r.remark,
               r.role_type
        from sys_role r
                 left join sys_user_role ur on r.role_id = ur.role_id
        where ur.user_id = #{userId}
    </select>
</mapper>