<?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.hzs.system.sys.mapper.SysMenuMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysMenu" id="SysMenuResult">
        <id property="menuId" column="menu_id"/>
        <result property="menuName" column="menu_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="orderNum" column="order_num"/>
        <result property="routePath" column="route_path"/>
        <result property="routeName" column="route_name"/>
        <result property="isFrame" column="is_frame"/>
        <result property="menuType" column="menu_type"/>
        <result property="visible" column="visible"/>
        <result property="perms" column="perms"/>
        <result property="icon" column="icon"/>
        <result property="pkCreator" column="PK_CREATOR"/>
        <result property="creationTime" column="CREATION_TIME"/>
        <result property="pkModified" column="PK_MODIFIED"/>
        <result property="modifiedTime" column="MODIFIED_TIME"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="fieldPerms" column="FIELD_PERMS"/>
        <result property="menuPath" column="MENU_PATH"/>
        <result property="effectiveDate" column="EFFECTIVE_DATE"/>


    </resultMap>

    <resultMap id="menuAndColumnResult" type="com.hzs.common.domain.system.sys.SysMenu" extends="SysMenuResult">
        <collection property="columnList" ofType="string" javaType="List">
            <result column="menu_column"/>
        </collection>
    </resultMap>

    <!-- 查询系统菜单列表 -->
    <select id="selectMenuList" parameterType="com.hzs.common.domain.system.sys.SysMenu" resultMap="SysMenuResult">
        select * from sys_menu m
        where m.del_flag = 0
        <if test="menuName != null and menuName != ''">
            AND m.menu_name like #{menuName} || '%'
        </if>
        <if test="visible != null">
            AND m.visible = #{visible}
        </if>
        order by m.parent_id, m.order_num, m.menu_id
    </select>

    <!-- 根据用户查询系统菜单列表 -->
    <select id="selectMenuListByUserId" resultMap="SysMenuResult">
        select m.*
        from sys_menu m
        left join sys_user_menu um
        on um.menu_id = m.menu_id
        and um.del_flag = 0
        where um.user_id = #{userId}
        <if test="menu.menuName != null and menu.menuName != ''">
            AND m.menu_name like #{menu.menuName} || '%'
        </if>
        <if test="menu.visible != null">
            AND m.visible = #{menu.visible}
        </if>
        order by m.parent_id, m.order_num, m.menu_id
    </select>

    <!-- 查询菜单 -->
    <select id="selectMenuTreeAll" resultMap="SysMenuResult">
        select *
        from sys_menu m
        where m.del_flag = 0
          and m.visible = 0
          and m.menu_type in ('M', 'C')
        order by m.parent_id, m.order_num
    </select>

    <!-- 查询全部菜单路由 -->
    <select id="selectMenuRoutersAll" parameterType="Long" resultMap="SysMenuResult">
        select m.menu_id,
               m.menu_name,
               m.parent_id,
               m.route_name,
               m.menu_type,
               m.perms
        from sys_menu m
        where m.del_flag = 0
          and m.visible = 0
          and m.menu_type in ('M', 'C')
        order by m.parent_id, m.order_num
    </select>

    <!-- 根据用户ID查询菜单路由 -->
    <select id="selectMenuRoutersByUserId" parameterType="Long" resultMap="SysMenuResult">
        select m.menu_id,
               m.menu_name,
               m.parent_id,
               m.route_name,
               m.menu_type,
               m.perms
        from sys_menu m
                 left join sys_user_menu um
                           on um.menu_id = m.menu_id
                               and um.del_flag = 0
        where m.del_flag = 0
          and m.visible = 0
          and m.menu_type in ('M', 'C')
          and um.user_id = #{userId}
        order by m.parent_id, m.order_num
    </select>

    <!-- 根据用户ID查询按钮权限 -->
    <select id="selectButtonPermsByUserId" parameterType="Long" resultType="String">
        select distinct m.perms
        from sys_menu m
                 left join sys_user_menu um on m.menu_id = um.menu_id
            and um.del_flag = 0
        where m.del_flag = 0
          and um.user_id = #{userId}
          and m.menu_type = 'F'
    </select>

    <!-- 根据菜单ID查询信息 -->
    <select id="selectMenuById" parameterType="Long" resultMap="SysMenuResult">
        select *
        from sys_menu
        where del_flag = 0
          and menu_id = #{menuId}
    </select>

    <!-- 是否存在菜单子节点 -->
    <select id="hasChildByMenuId" resultType="Integer">
        select count(1)
        from sys_menu
        where parent_id = #{menuId}
          and del_flag = 0
    </select>

    <!-- 校验菜单名称是否唯一 -->
    <select id="checkMenuNameUnique" resultMap="SysMenuResult">
        select *
        from sys_menu
        where del_flag = 0
          and menu_name = #{menuName}
          and parent_id = #{parentId}
          and rownum = 1
    </select>

    <!-- 校验菜单权限是否唯一 -->
    <select id="checkMenuPermsUnique" resultMap="SysMenuResult">
        select *
        from sys_menu
        where del_flag = 0
          and perms = #{perms}
          and menu_type in ('M', 'C')
          and rownum = 1
    </select>

    <!-- 新增菜单信息 -->
    <insert id="insertMenu" parameterType="com.hzs.common.domain.system.sys.SysMenu" useGeneratedKeys="true" keyProperty="menuId">
        <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="menuId">
            SELECT SYS_MENU_MENU_ID_SEQ.nextval AS menuId FROM dual
        </selectKey>
        insert into sys_menu(
        <if test="menuId != null and menuId != 0">menu_id,</if>
        <if test="parentId != null ">parent_id,</if>
        <if test="menuName != null and menuName != ''">menu_name,</if>
        <if test="orderNum != null">order_num,</if>
        <if test="routePath != null and routePath != ''">route_path,</if>
        <if test="routeName != null and routeName != ''">route_name,</if>
        <if test="menuType != null and menuType != ''">menu_type,</if>
        <if test="visible != null">visible,</if>
        <if test="perms !=null and perms != ''">perms,</if>
        <if test="icon != null and icon != ''">icon,</if>
        <if test="pkCreator != null">PK_CREATOR,</if>
        <if test="fieldPerms != null">FIELD_PERMS,</if>
        creation_time
        )values(
        <if test="menuId != null and menuId != 0">#{menuId},</if>
        <if test="parentId != null ">#{parentId},</if>
        <if test="menuName != null and menuName != ''">#{menuName},</if>
        <if test="orderNum != null">#{orderNum},</if>
        <if test="routePath != null and routePath != ''">#{routePath},</if>
        <if test="routeName != null and routeName != ''">#{routeName},</if>
        <if test="menuType != null and menuType != ''">#{menuType},</if>
        <if test="visible != null">#{visible},</if>
        <if test="perms !=null and perms != ''">#{perms},</if>
        <if test="icon != null and icon != ''">#{icon},</if>
        <if test="pkCreator != null">#{pkCreator},</if>
        <if test="fieldPerms != null">#{fieldPerms},</if>
        sysdate
        )
    </insert>

    <!-- 修改菜单信息 -->
    <update id="updateMenu" parameterType="com.hzs.common.domain.system.sys.SysMenu">
        update sys_menu
        <set>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="menuName != null and menuName != ''">menu_name = #{menuName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="routePath != null and routePath != ''">route_path = #{routePath},</if>
            <if test="routeName != null">route_name = #{routeName},</if>
            <if test="menuType != null and menuType != ''">menu_type = #{menuType},</if>
            <if test="visible != null">visible = #{visible},</if>
            <if test="perms !=null and perms != ''">perms = #{perms},</if>
            <if test="icon !=null and icon != ''">icon = #{icon},</if>
            <if test="pkModified != null">PK_MODIFIED = #{pkModified},</if>
            <if test="fieldPerms != null">FIELD_PERMS = #{fieldPerms},</if>
            MODIFIED_TIME = sysdate
        </set>
        where menu_id = #{menuId}
    </update>

    <!-- 删除菜单管理信息 -->
    <delete id="deleteMenuById" parameterType="Long">
        update sys_menu
        set del_flag      = 1,
            pk_modified   = #{userId},
            modified_time = sysdate
        where menu_id = #{menuId}
    </delete>

    <!-- 查询菜单+菜单列 -->
    <select id="queryMenuAndColumn" resultMap="menuAndColumnResult">
        select sm.menu_id, sm.menu_name, sm.parent_id, sm.perms, srmc.menu_column
        from sys_menu sm
                 left join SYS_ROLE_MENU_COLUMN srmc
                           on srmc.pk_menu = sm.menu_id
                               and srmc.del_flag = 0
                               and srmc.pk_role = #{roleId}
                               and srmc.show_flag = 0
        where sm.del_flag = 0
          and sm.menu_type = 'C'
          and sm.field_perms = 0
        order by sm.parent_id, sm.order_num
    </select>
    <!--导出查询全部菜单-->
    <select id="queryAllMenuAndColumn" parameterType="com.hzs.common.domain.system.sys.SysMenu" resultMap="SysMenuResult">
        SELECT CONNECT_BY_ROOT MENU_ID AS ROOT_MENU_ID,
        LEVEL AS MENU_LEVEL,
        SYS_CONNECT_BY_PATH(MENU_NAME, ' / ') AS MENU_PATH,
        MENU_ID,
        MENU_NAME,
        PARENT_ID,
        ORDER_NUM,
        ROUTE_PATH,
        ROUTE_NAME,
        IS_FRAME,
        MENU_TYPE,
        VISIBLE,
        PERMS,
        CREATION_TIME,
        CREATION_TIME as EFFECTIVE_DATE
        FROM sys_menu
        WHERE del_flag = 0
        <if test="menuName != null and menuName != ''">
            AND menu_name like #{menuName} || '%'
        </if>
        <if test="visible != null">
            AND visible = #{visible}
        </if>
        START WITH PARENT_ID =0
        CONNECT BY PRIOR MENU_ID = PARENT_ID
        ORDER SIBLINGS BY ORDER_NUM
    </select>


</mapper>
