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

	<!-- 通过<resultMap>映射实体类属性名和表的字段名对应关系 -->
	<resultMap id="BaseResultMap" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap" type="com.storlead.system.pojo.entity.MenuEntity">
	   <!-- result属性映射非匹配字段 -->
		<id column="id" property="id"/>
		<result column="menu_name" property="menuName"/>
		<result column="pid" property="pid"/>
		<result column="query_param" property="queryParam"/>
		<result column="service_mode_type" property="serviceModeType"/>

		<result column="app_id" property="appId"/>
		<result column="component" property="component"/>
		<result column="path" property="path"/>
		<result column="icon" property="icon"/>
		<result column="router_name" property="routerName"/>
		<result column="menu_type" property="menuType"/>
		<result column="perms" property="perms"/>
		<result column="visible" property="visible"/>
		<result column="is_cache" property="isCache"/>
		<result column="is_frame" property="isFrame"/>
	</resultMap>

	<!-- 获取登录用户拥有的权限 -->
	<select id="queryByUsername" parameterType="Object"  resultMap="BaseResultMap">
		   SELECT p.*
		   FROM  menu p
		   WHERE exists(
		   		SELECT a.menu_id FROM role_menu a
		   		JOIN ROLE b ON a.role_id = b.id
		   		JOIN user_role c ON c.role_id = b.id
		   		JOIN USER d ON d.id = c.user_id
		   		WHERE p.id = a.menu_id AND d.user_name = #{userName,jdbcType=VARCHAR}
		   )
		   AND p.del_flag = 0
		   ORDER BY p.sort_no ASC
	</select>


	<select id="selectUserMenuList" resultType="com.storlead.system.pojo.entity.MenuEntity">
		SELECT
			*
		FROM
			menu
		WHERE
				id IN (
				SELECT DISTINCT
					( rm.menu_id )
				FROM
					role_menu AS rm
						LEFT JOIN menu AS m ON m.id = rm.menu_id
						LEFT JOIN user_role AS ur ON ur.role_id = rm.role_id
						LEFT JOIN role AS r ON r.id = ur.role_id
				WHERE
					ur.user_id = #{userId}

				  AND r.is_delete = 0 AND r.enabled = 1
			)
	 	AND is_delete= 0
		<if test="menuTypes != null and menuTypes.size() > 0">
			AND menu_type in
			<foreach collection="menuTypes" item="item" open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
		<if test="modeTypes  != null and modeTypes.size() > 0">
			AND service_mode_type  in
			<foreach collection="modeTypes" item="item" open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
		AND enabled = 1  ORDER BY sort,id ASC;
	</select>


	<select id="selectUserMenuCount" resultType="java.lang.Integer">
		SELECT
			count(*)
		FROM
			menu
		WHERE
				id IN (
				SELECT DISTINCT
					( rm.menu_id )
				FROM
					role_menu AS rm
						LEFT JOIN menu AS m ON m.id = rm.menu_id
						LEFT JOIN user_role AS ur ON ur.role_id = rm.role_id
						LEFT JOIN role AS r ON r.id = ur.role_id
				WHERE
					ur.user_id = #{userId}  AND r.is_delete = 0 AND r.enabled = 1
			)
		  AND is_delete= 0 AND enabled = 1 and pid > 0 ORDER BY sort,id ASC;
	</select>

	<select id="selectRootLastNodeInfo" resultMap="BaseResultMap">
		SELECT * FROM menu
		<where>
			<if test="pid != null">
				(pid = #{pid} OR id = #{pid})
			</if>
			AND menu_code != ''
		</where>
		ORDER BY menu_code DESC limit 1
	</select>

	<select id="selectMenuCreateTree" resultType="com.storlead.system.pojo.entity.MenuEntity">
		select id,menu_name,pid,path,perms from menu where menu_name in ('客户管理','线索管理','商机管理','订单管理','任务管理') and is_delete = 0
		UNION ALL
		select id,menu_name,pid,path,perms from menu where pid in (select id from menu where menu_name in ('客户管理','线索管理','商机管理','订单管理','任务管理')) and is_delete = 0
		UNION ALL
		select id,menu_name,pid,path,perms  from menu where pid in (select id from menu where pid in (select id from menu where menu_name in ('客户管理','线索管理','商机管理','订单管理','任务管理'))) and is_delete = 0
		UNION ALL
		select id,menu_name,pid,path,perms from menu where pid in (select id from menu where pid in (select id from menu where pid in (select id from menu where menu_name in ('客户管理','线索管理','商机管理','订单管理','任务管理')))) and is_delete = 0
	</select>


</mapper>
