<?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.ningdatech.carapi.sys.mapper.MenuMapper">


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ningdatech.carapi.sys.entity.Menu">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="label" jdbcType="VARCHAR" property="label"/>
        <result column="sort_value" jdbcType="INTEGER" property="sortValue"/>
        <result column="parent_id" jdbcType="BIGINT" property="parentId"/>
        <result column="created_by" jdbcType="BIGINT" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createOn"/>
        <result column="updated_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateOn"/>
        <result column="describe_" jdbcType="VARCHAR" property="describe"/>
        <result column="is_general" jdbcType="BIT" property="isGeneral"/>
        <result column="path" jdbcType="VARCHAR" property="path"/>
        <result column="component" jdbcType="VARCHAR" property="component"/>
        <result column="state" jdbcType="BIT" property="state"/>
        <result column="icon" jdbcType="VARCHAR" property="icon"/>
        <result column="group_" jdbcType="VARCHAR" property="group"/>
        <result column="readonly_" jdbcType="BIT" property="readonly"/>
        <result column="resource_type" jdbcType="VARCHAR" property="resourceType"/>
        <result column="is_def" jdbcType="BIT" property="isDef"/>
        <result column="custom_class" jdbcType="VARCHAR" property="customClass"/>
        <result column="data_scope" jdbcType="VARCHAR" property="dataScope"/>
        <result column="tree_grade" jdbcType="VARCHAR" property="treeGrade"/>
        <result column="tree_path" jdbcType="VARCHAR" property="treePath"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,label,sort_value,parent_id,created_by,create_time,updated_by,update_time, tree_grade, tree_path,
        describe_, is_general, path, component, state, icon, group_, readonly_, resource_type, is_def, custom_class, data_scope
    </sql>


    <!-- 通用查询结果列 -->
    <sql id="V_Column_List">
        id,  label, path, component, state, sort_value, icon, group_, parent_id, resource_type,  tree_grade, tree_path
    </sql>

    <select id="findVisibleMenu" parameterType="map"
            resultMap="BaseResultMap">
        SELECT
        <include refid="V_Column_List"/>
        from (
        SELECT
        <include refid="V_Column_List"/>
        from c_menu where state = true and is_general = true and resource_type = '20'
        UNION ALL
        SELECT
        <include refid="V_Column_List"/>
        from c_menu where state = true and is_general = false
        and id in (
        SELECT distinct authority_id FROM c_role_authority ra
        INNER JOIN c_user_role ur on ra.role_id = ur.role_id
        INNER JOIN c_role r on r.id = ra.role_id
        where ur.user_id = #{userId, jdbcType=BIGINT} and r.state = true
        and ra.authority_type = 'MENU'
        and c_menu.resource_type = '20'
        )
        ) tmp ORDER BY sort_value asc
    </select>


</mapper>
