<?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="org.opengauss.admin.system.mapper.SysMenuMapper">

    <resultMap type="org.opengauss.admin.common.core.domain.entity.SysMenu" id="SysMenuResult">
        <id property="menuId" column="menu_id"/>
        <result property="menuName" column="menu_name"/>
        <result property="parentName" column="parent_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="orderNum" column="order_num"/>
        <result property="path" column="path"/>
        <result property="component" column="component"/>
        <result property="isFrame" column="is_frame"/>
        <result property="isCache" column="is_cache"/>
        <result property="menuType" column="menu_type"/>
        <result property="visible" column="visible"/>
        <result property="status" column="status"/>
        <result property="perms" column="perms"/>
        <result property="icon" column="icon"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="pluginId" column="plugin_id"/>
        <result property="pluginTheme" column="plugin_theme"/>
        <result property="menuClassify" column="menu_classify"/>
        <result property="menuEnName" column="menu_en_name"/>
    </resultMap>

    <select id="selectMenuListByUserId" parameterType="org.opengauss.admin.common.core.domain.entity.SysMenu" resultMap="SysMenuResult">
        SELECT DISTINCT
        m.menu_id,
        m.parent_id,
        m.menu_name,
        m.menu_en_name,
        m.path,
        m.component,
        m.visible,
        m.status,
        CASE WHEN m.perms IS NULL THEN '' ELSE m.perms END AS perms,
        m.is_frame,
        m.is_cache,
        m.menu_type,
        m.icon,
        m.order_num,
        m.create_time,m.plugin_theme,m.menu_classify
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role ro on ur.role_id = ro.role_id
        where ur.user_id = #{params.userId}
        <if test="menuName != null and menuName != ''">
            AND menu_name like concat('%', #{menuName}, '%')
        </if>
        <if test="visible != null and visible != ''">
            AND visible = #{visible}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuTreeByUserId" parameterType="Integer" resultMap="SysMenuResult">
        select distinct m.menu_id, m.parent_id, m.menu_name,m.menu_en_name, m.path, m.component, m.visible, m.status,
        CASE WHEN m.perms IS NULL THEN '' ELSE m.perms END as perms,
        m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time,
        m.plugin_theme,m.menu_classify
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role ro on ur.role_id = ro.role_id
        left join sys_user u on ur.user_id = u.user_id
        where u.user_id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0 AND ro.status = 0
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuListByRoleId" resultType="Integer">
        select m.menu_id
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        where rm.role_id = #{roleId}
        <if test="menuCheckStrictly">
            and m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id =
            rm.menu_id and rm.role_id = #{roleId})
        </if>
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuPerms" resultType="String">
        select distinct m.perms
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
    </select>

    <select id="selectMenuPermsByUserId" parameterType="Integer" resultType="String">
        select distinct m.perms
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        left join sys_user_role ur on rm.role_id = ur.role_id
        left join sys_role r on r.role_id = ur.role_id
        where m.status = '0' and r.status = '0' and ur.user_id = #{userId}
    </select>

    <select id="countParentMenuHasOtherPluginSubmenusByPluginId" parameterType="String" resultType="Integer">
        select count(1) from sys_menu t
        where t.plugin_id=#{pluginId} and exists(
            select 1 from sys_menu s where s.parent_id=t.menu_id
            and s.plugin_id is not null and s.plugin_id &lt;&gt; t.plugin_id
        )
    </select>

    <select id="countParentMenuHasOtherEnablePluginSubmenusByPluginId" parameterType="String" resultType="Integer">
        select count(1) from sys_menu t
        where t.plugin_id=#{pluginId} and exists(
            select 1 from sys_menu s where s.parent_id=t.menu_id
            and s.plugin_id is not null and s.plugin_id &lt;&gt; t.plugin_id
            and s.status=0
        )
    </select>

    <select id="selectParentMenuIdByPluginId" parameterType="String" resultType="Integer">
        SELECT distinct parent_id as menu_id from sys_menu where plugin_id= #{pluginId} and parent_id != 0
        and  parent_id not in (SELECT menu_id from sys_menu where plugin_id= #{pluginId})
    </select>

    <select id="selectMenusByRoleId" parameterType="java.lang.Long" resultMap="SysMenuResult">
        SELECT DISTINCT
        m.menu_id,
        m.parent_id,
        m.menu_name,
        m.menu_en_name,
        m.path,
        m.component,
        m.visible,
        m.status,
        CASE WHEN m.perms IS NULL THEN '' ELSE m.perms END AS perms,
        m.is_frame,
        m.is_cache,
        m.menu_type,
        m.icon,
        m.order_num,
        m.create_time,
        m.plugin_id,
        m.plugin_theme,
        m.menu_classify
        from sys_menu m
        left join sys_role_menu rm on m.menu_id = rm.menu_id
        where rm.role_id = #{roleId}
        order by m.parent_id, m.order_num
    </select>
</mapper>