<?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.tofly.base.mapper.MenuMapper">

    <resultMap id="menuMap" type="com.tofly.base.entity.Menu">
        <id property="id" column="ID"/>
        <result property="name" column="NAME"/>
        <result property="permission" column="PERMISSION"/>
        <result property="path" column="PATH"/>
        <result property="parentId" column="PARENT_ID"/>
        <result property="icon" column="ICON"/>
        <result property="sort" column="SORT"/>
        <result property="keepAlive" column="KEEP_ALIVE"/>
        <result property="type" column="TYPE"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="statusFlag" column="STATUS_FLAG"/>
        <result property="code" column="CODE"/>
        <result property="mouseIcon" column="MOUSE_ICON"/>
        <result property="inpanel" column="INPANEL"/>
        <result property="panel" column="PANEL"/>
        <result property="autorun" column="AUTORUN"/>
        <result property="sysId" column="SYS_ID"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="component" column="COMPONENT"/>
        <result property="label" column="LABEL"/>
        <result property="meta" column="META"/>
        <result property="parentPathid" column="PARENT_PATHID"/>
        <result property="pathId" column="PATH_ID"/>
        <result property="redirect" column="REDIRECT"/>
        <result property="widgetid" column="WIDGETID"/>
    </resultMap>


    <!--通过角色查询菜单信息-->
    <!--通过角色查询菜单信息-->
    <select id="listMenusByRoleId" resultMap="menuMap">
        SELECT TF_SMPN_MENU_X.*
        FROM TF_SMPN_MENU_X
                 LEFT JOIN TF_SMPN_ROLEMENU_X ON TF_SMPN_MENU_X.id = TF_SMPN_ROLEMENU_X.menu_id
        WHERE TF_SMPN_MENU_X.status_flag = 1
          AND TF_SMPN_ROLEMENU_X.role_id = #{roleId}
        ORDER BY TF_SMPN_MENU_X.sort
    </select>

    <select id="getMenuListByUser" resultMap="menuMap">
        select *
        from TF_SMPN_MENU_X m
        where m.status_flag = '1'
          and exists(select 1
                     from TF_SMPN_USER_X u,
                          TF_SMPN_USERROLE_X ur,
                          TF_SMPN_ROLE_X r,
                          TF_SMPN_ROLEMENU_X rm
                     where u.id = ur.user_id
                       and ur.role_id = r.id
                       and rm.role_id = r.id
                       and rm.menu_id = m.id
                       and u.status_flag = '1'
                       and r.status_flag = '1'
                       and u.id = #{id})
          and exists(select 1 from TF_SMPN_SYSTEM_X s where m.sys_id = s.id and s.platform = #{platform,jdbcType=CHAR})
        order by sort
    </select>
    <select id="getMenuListByUserId" resultMap="menuMap">
        SELECT DISTINCT
            m.ID,m.NAME,m.PATH,m.PARENT_ID,m.ICON,m.SORT,m.KEEP_ALIVE,m.TYPE,m.SYS_ID,m.META,m.COMPONENT,m.LABEL,m.REDIRECT,m.WIDGETID,m.PATH_ID,m.PARENT_PATHID
        FROM
            TF_SMPN_MENU_X m LEFT JOIN TF_SMPN_ROLEMENU_X r ON ( m.ID = r.MENU_ID ) LEFT JOIN TF_SMPN_USERROLE_X u ON ( r.ROLE_ID = u.ROLE_ID )
        WHERE
            u.USER_ID = #{id}
          AND m.STATUS_FLAG = '1'
          AND m.TYPE IN ('sys','gis','app')
        ORDER BY m.ID
    </select>
</mapper>
