<?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.TaUserAuthMenuMapper">

    <sql id="queryColumn">
		t.menu_id,
		t.code,
		t.name,
		t.parent_code,
		t.icon,
		t.app_type,
		t.status,
		t.menu_root
	</sql>

    <select id="sysMenuListByUserId" resultType="com.huiju.estateagents.entity.SysMenu">
        select
          <include refid="queryColumn"></include>
        from
            sys_menu t
        where
            t.menu_id in (
                SELECT
                    a.menu_id
                from
                    ta_role_menu a
                where
                    a.role_id in (
                        select
                            role_id
                        from
                            ta_user_role b
                        where
                            b.user_id = #{userId}
                    )
            )
        order by t.sort_num desc
    </select>

    <select id="sysMenuListByRoleId" resultType="com.huiju.estateagents.entity.SysMenu">
        select
          <include refid="queryColumn"></include>
        from
          sys_menu t
        where
          t.app_type = 'estateagent-admin'
          order by t.sort_num
    </select>

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

    <update id="addUserAuthMenu">
        <foreach item="item" index="index" collection="sysMenus" separator=";" >
            insert into ta_role_menu (role_id, menu_id)
            values
            (
              (select role_id from ta_user_role u where u.user_id = #{userId}),
               #{item.menuId}
            )
        </foreach>
    </update>

</mapper>
