<?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.huiju.estateagents.center.taUser.mapper.TaUserAuthButtonMapper">

    <sql id="queryColumn">
		t.btn_id,
		t.menu_id,
		t.name,
		t.status,
		t.code
	</sql>

    <select id="sysButtonListByUserId" resultType="com.huiju.estateagents.entity.SysButtonInMenu">
        select
          <include refid="queryColumn"></include>
        from
          sys_button_in_menu t
        where
            t.btn_id in (
                SELECT
                a.btn_id
                from
                ta_role_button a
                where
                a.role_id in (
                    select
                    role_id
                    from
                    ta_user_role b
                    where
                    b.user_id = #{userId}
                )
            )
    </select>

    <select id="sysButtonListByRoleId" resultType="com.huiju.estateagents.entity.SysButtonInMenu">
        select
          <include refid="queryColumn"></include>
        from
        sys_button_in_menu t
    </select>

    <delete id="deleteUserAuthButton">
      delete from ta_role_button where role_id in (
        select role_id from ta_user_role r where r.user_id = #{userId}
      )
    </delete>

    <update id="addUserAuthButton">
        <foreach item="item" index="index" collection="sysButtons" separator=";" >
            insert into ta_role_button (role_id, btn_id)
            values
            (
            (select role_id from ta_user_role u where u.user_id = #{userId}),
            #{item.btnId}
            )
        </foreach>
    </update>

</mapper>
