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

    <resultMap id="roleMap" type="com.tofly.base.entity.Role">
        <id property="id" column="ID"/>
        <result property="roleName" column="ROLE_NAME"/>
        <result property="roleCode" column="ROLE_CODE"/>
        <result property="roleDesc" column="ROLE_DESC"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="statusFlag" column="STATUS_FLAG"/>
        <result property="portaluserId" column="PORTALUSER_ID"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="sysId" column="SYS_ID"/>
        <result property="treeValue" column="TREE_VALUE"/>
        <result property="updateUsers" column="UPDATE_USERS"/>
    </resultMap>
    <select id="listMenuByRoleId" parameterType="map" resultType="map" databaseId="oracle">
        select m.*,
               decode((select 1
                       from tf_smpn_rolemenu_x rm
                       where m.id = rm.menu_id
                         and rm.role_id = #{roleId,jdbcType=NUMERIC}),
                      1,
                      'true',
                      'false') is_exists
        from tf_smpn_menu_x m
        where sys_id = #{systemId}
        order by sort
    </select>

    <select id="listMenuByRoleId" parameterType="map" resultType="map" databaseId="mysql">
        select m.*,

               case (select 1
                     from TF_SMPN_ROLEMENU_X rm
                     where m.id = rm.menu_id
                       and rm.role_id = #{roleId,jdbcType=NUMERIC})
                   when 1 then 'true'
                   else 'false' end is_exists
        from TF_SMPN_MENU_X m
        where sys_id = #{systemId}
        order by sort
    </select>

    <!-- 通过用户ID，查询角色信息-->
    <select id="listRolesByUserId"  resultMap="roleMap">
        SELECT
            r.*
        FROM
            TF_SMPN_ROLE_X r, TF_SMPN_USERROLE_X ur WHERE r.id = ur.role_id AND r.status_flag = 1 and  ur.user_id = #{userId}
    </select>
</mapper>
