<?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.platform.mapper.PlatformMenuMapper">

    <resultMap type="com.ruoyi.common.core.domain.platform.vo.PlatformMenuVO" id="SysMenuResult">
        <id     property="id"         column="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="url"            column="url"            />
        <result property="target"         column="target"         />
        <result property="menuType"       column="menu_type"      />
        <result property="visible"        column="visible"        />
        <result property="isRefresh"      column="is_refresh"     />
        <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"         />
    </resultMap>

    <sql id="selectMenuVo">
        select id, menu_name, parent_id, order_num, url, target, menu_type, visible, is_refresh, ifnull(perms,'') as perms, icon, create_by, create_time
        from platform_menu
    </sql>

    <select id="selectMenusByUserId" parameterType="String" resultMap="SysMenuResult">
        select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
        from platform_menu m
                 left join platform_role_menu rm on m.id = rm.menu_id
                 left join platform_user_role ur on rm.role_id = ur.role_id
                 LEFT JOIN platform_role ro on ur.role_id = ro.id
        where ur.user_id = #{userId} and m.menu_type in ('M', 'C') and m.visible = 0  AND ro.status = 0
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuAll" resultMap="SysMenuResult">
        <include refid="selectMenuVo"/>
        order by parent_id, order_num
    </select>

    <select id="selectMenuAllByUserId" parameterType="String" resultMap="SysMenuResult">
        select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
        from platform_menu m
                 left join platform_role_menu rm on m.id = rm.menu_id
                 left join platform_user_role ur on rm.role_id = ur.role_id
                 LEFT JOIN platform_role ro on ur.role_id = ro.id
        where ur.user_id = #{userId}
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuNormalAll" resultMap="SysMenuResult">
        select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
        from platform_menu m
        where m.menu_type in ('M', 'C') and m.visible = 0
        order by m.parent_id, m.order_num
    </select>

    <select id="selectPermsByUserId" parameterType="String" resultType="String">
        select distinct m.perms
        from platform_menu m
                 left join platform_role_menu rm on m.id = rm.menu_id
                 left join platform_user_role ur on rm.role_id = ur.role_id
                 left join platform_role r on r.id = ur.role_id
        where m.visible = '0' and r.status = '0' and ur.user_id = #{userId}
    </select>

    <select id="selectMenuTree" parameterType="String" resultType="String">
        select concat(m.id, ifnull(m.perms,'')) as perms
        from platform_menu m
                 left join platform_role_menu rm on m.id = rm.menu_id
        where rm.role_id = #{roleId}
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuList" parameterType="com.ruoyi.common.core.domain.platform.dto.PlatformMenuDTO" resultMap="SysMenuResult">
        <include refid="selectMenuVo"/>
        <where>
            <if test="menuName != null and menuName != ''">
                AND menu_name like concat('%', #{menuName}, '%')
            </if>
            <if test="visible != null and visible != ''">
                AND visible = #{visible}
            </if>
        </where>
        order by parent_id, order_num
    </select>

    <select id="selectMenuListByUserId" parameterType="com.ruoyi.common.core.domain.platform.dto.PlatformMenuDTO" resultMap="SysMenuResult">
        select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
        from platform_menu m
        left join platform_role_menu rm on m.id = rm.menu_id
        left join platform_user_role ur on rm.role_id = ur.role_id
        LEFT JOIN platform_role ro on ur.role_id = ro.id
        where ur.user_id = #{params.userId}
        <if test="menuName != null and menuName != ''">
            AND m.menu_name like concat('%', #{menuName}, '%')
        </if>
        <if test="visible != null and visible != ''">
            AND m.visible = #{visible}
        </if>
        order by m.parent_id, m.order_num
    </select>

    <delete id="deleteMenuById" parameterType="String">
        delete from platform_menu where id = #{menuId} or parent_id = #{menuId}
    </delete>

    <select id="selectMenuById" parameterType="String" resultMap="SysMenuResult">
        SELECT t.id, t.parent_id, t.menu_name, t.order_num, t.url, t.target, t.menu_type, t.visible, t.is_refresh, t.perms, t.icon, t.remark,
               (SELECT menu_name FROM platform_menu WHERE id = t.parent_id) parent_name
        FROM platform_menu t
        where t.id = #{menuId}
    </select>

</mapper>