<?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.web.report.mapper.ReportMenuMapper">

    <select id="selectRootMenuId" resultType="Long">
        select menu_id
        from sys_menu
        where path = 'report'
          and menu_type = 'M'
        limit 1
    </select>

    <insert id="insertRootMenu">
        insert into sys_menu (
            menu_name, parent_id, order_num, path, component, query, route_name,
            is_frame, is_cache, menu_type, visible, status, perms, icon,
            create_by, create_time, remark
        ) values (
            '报表中心', 0, 5, 'report', null, '', '',
            1, 0, 'M', '0', '0', '', 'chart',
            #{createBy}, now(), '发布报表自动补建目录'
        )
    </insert>

    <insert id="insertMenu">
        insert into sys_menu (
            menu_name, parent_id, order_num, path, component, query, route_name,
            is_frame, is_cache, menu_type, visible, status, perms, icon,
            create_by, create_time, remark
        ) values (
            #{menuName}, #{parentId}, #{orderNum}, #{path}, #{component}, #{query}, '',
            1, 0, 'C', '0', '0', #{perms}, #{icon},
            #{createBy}, now(), '发布报表自动创建菜单'
        )
    </insert>

    <select id="selectLastInsertMenuId" resultType="Long">
        select last_insert_id()
    </select>

    <update id="updateMenuById">
        update sys_menu
        set menu_name = #{menuName},
            path = #{path},
            component = #{component},
            query = #{query},
            status = #{status},
            update_by = #{updateBy},
            update_time = now()
        where menu_id = #{menuId}
    </update>

    <select id="selectMenuIdByParentAndPath" resultType="Long">
        select menu_id
        from sys_menu
        where parent_id = #{parentId}
          and path = #{path}
          and menu_type = 'C'
        limit 1
    </select>

    <insert id="insertMenuBinding">
        insert into rpt_menu_binding (
            report_id, menu_id, menu_path, component, query_text, create_by, create_time
        ) values (
            #{reportId}, #{menuId}, #{menuPath}, #{component}, #{queryText}, #{createBy}, now()
        )
    </insert>

    <update id="updateMenuBinding">
        update rpt_menu_binding
        set menu_id = #{menuId},
            menu_path = #{menuPath},
            component = #{component},
            query_text = #{queryText},
            update_by = #{updateBy},
            update_time = now()
        where report_id = #{reportId}
    </update>

    <insert id="insertAdminRoleMenuIfAbsent">
        insert into sys_role_menu (role_id, menu_id)
        select 1, #{menuId}
        from dual
        where not exists (
            select 1 from sys_role_menu where role_id = 1 and menu_id = #{menuId}
        )
    </insert>

    <insert id="insertRoleMenuIfAbsent">
        insert into sys_role_menu (role_id, menu_id)
        select #{roleId}, #{menuId}
        from dual
        where not exists (
            select 1 from sys_role_menu where role_id = #{roleId} and menu_id = #{menuId}
        )
    </insert>

    <insert id="insertRoleMenusByUserNameIfAbsent">
        insert into sys_role_menu (role_id, menu_id)
        select distinct ur.role_id, #{menuId}
        from sys_user u
                 inner join sys_user_role ur on ur.user_id = u.user_id
                 inner join sys_role r on r.role_id = ur.role_id
        where u.user_name = #{userName}
          and u.status = '0'
          and r.status = '0'
          and not exists (
            select 1
            from sys_role_menu srm
            where srm.role_id = ur.role_id
              and srm.menu_id = #{menuId}
        )
    </insert>

    <select id="selectMaxOrderNumByParentId" parameterType="Long" resultType="Integer">
        select ifnull(max(order_num), 0)
        from sys_menu
        where parent_id = #{parentId}
    </select>
</mapper>
