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

	<resultMap type="com.ruoyi.system.domain.SysMenu" id="SysMenuResult">
		<id     property="menuId"         column="menu_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="path"           column="path"           />
		<result property="component"      column="component"      />
		<result property="query"          column="query"          />
		<result property="isFrame"        column="is_frame"       />
		<result property="isCache"        column="is_cache"       />
		<result property="menuType"       column="menu_type"      />
		<result property="visible"        column="visible"        />
		<result property="status"         column="status"         />
		<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="delFlag"        column="del_flag"       />
		<result property="remark"         column="remark"         />
	</resultMap>

	<sql id="selectMenuVo">
        SELECT menu_id, menu_name, parent_id, order_num, path, component, `query`, is_frame, is_cache, menu_type, visible, status, ifnull(perms,'') AS perms, icon, create_time
		FROM sys_menu
    </sql>

	<select id="selectMenuList" resultMap="SysMenuResult">
		<include refid="selectMenuVo"/>
		WHERE del_flag = #{delFlag}
		<if test="sysMenu.menuName != null and sysMenu.menuName != ''">
			AND menu_name LIKE concat('%', #{sysMenu.menuName}, '%')
		</if>
		<if test="sysMenu.visible != null and sysMenu.visible != ''">
			AND visible = #{sysMenu.visible}
		</if>
		<if test="sysMenu.status != null and sysMenu.status != ''">
			AND status = #{sysMenu.status}
		</if>
		ORDER BY parent_id, order_num
	</select>

	<select id="selectMenuListByUserId" resultMap="SysMenuResult">
		SELECT DISTINCT m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') AS perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
		FROM sys_menu m
		LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
		LEFT JOIN sys_user_role ur ON rm.role_id = ur.role_id
		LEFT JOIN sys_role ro ON ur.role_id = ro.role_id
		WHERE m.del_flag = #{delFlag}
		AND ro.del_flag = #{delFlag}
		AND ur.user_id = #{userId}
		<if test="sysMenu.menuName != null and sysMenu.menuName != ''">
			AND m.menu_name like concat('%', #{sysMenu.menuName}, '%')
		</if>
		<if test="sysMenu.visible != null and sysMenu.visible != ''">
			AND m.visible = #{sysMenu.visible}
		</if>
		<if test="sysMenu.status != null and sysMenu.status != ''">
			AND m.status = #{sysMenu.status}
		</if>
		order by m.parent_id, m.order_num
	</select>

	<select id="selectMenuTreeAll" parameterType="java.lang.String" resultMap="SysMenuResult">
		SELECT DISTINCT m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') AS perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
		FROM sys_menu m
		WHERE m.del_flag = #{delFlag}
		AND m.status = #{status}
		AND m.menu_type IN
		<foreach collection="menuTypes" item="menuType" open="(" separator="," close=")">
			#{menuType}
		</foreach>
		ORDER BY m.parent_id, m.order_num
	</select>

	<select id="selectMenuTreeByUserId" resultMap="SysMenuResult">
		SELECT DISTINCT m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, m.status, ifnull(m.perms,'') AS perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
		FROM sys_menu m
		LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
		LEFT JOIN sys_user_role ur ON rm.role_id = ur.role_id
		LEFT JOIN sys_role ro ON ur.role_id = ro.role_id
		LEFT JOIN sys_user u ON ur.user_id = u.user_id
		WHERE m.del_flag = #{delFlag}
		AND u.del_flag = #{delFlag}
		AND ro.del_flag = #{delFlag}
		AND u.user_id = #{userId}
		AND m.status = #{status}
		AND ro.status = #{status}
		AND m.menu_type IN
		<foreach collection="menuTypes" item="menuType" open="(" separator="," close=")">
			#{menuType}
		</foreach>
		ORDER BY m.parent_id, m.order_num
	</select>

	<select id="selectMenuPermsByRoleId" resultType="java.lang.String">
		SELECT DISTINCT m.perms
		FROM sys_menu m
		LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
		WHERE m.del_flag = #{delFlag}
		AND m.status = #{status}
		AND rm.role_id = #{roleId}
	</select>

	<select id="selectMenuListByRoleId" resultType="java.lang.Long">
		SELECT m.menu_id
		FROM sys_menu m
		LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
		WHERE m.del_flag = #{delFlag}
		AND rm.role_id = #{roleId}
		<if test="menuCheckStrictly">
			AND m.menu_id NOT IN
				(
					SELECT m.parent_id
					FROM sys_menu m
					INNER JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
					AND m.del_flag = #{delFlag}
					AND rm.role_id = #{roleId}
				)
		</if>
		ORDER BY m.parent_id, m.order_num
	</select>

	<select id="selectMenuPermsByUserId" resultType="java.lang.String">
		SELECT DISTINCT m.perms
		FROM sys_menu m
		LEFT JOIN sys_role_menu rm ON m.menu_id = rm.menu_id
		LEFT JOIN sys_user_role ur ON rm.role_id = ur.role_id
		LEFT JOIN sys_role r ON r.role_id = ur.role_id
		WHERE m.del_flag = #{delFlag}
		AND r.del_flag = #{delFlag}
		AND m.status = #{status}
		AND r.status = #{status}
		AND ur.user_id = #{userId}
	</select>

</mapper>