<?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="SysMenuMapper">

	<select id="findMaxId" resultType="PageData">
		SELECT MAX(id) MID FROM sys_menu
	</select>
	
	<select id="listAllParentMenu" resultType="SysMenu">
		SELECT xsm.* FROM sys_menu xsm, sys_role_menu xsrm, sys_user xsur
		where xsm.id = xsrm.menu_id and xsrm.role_id = xsur.role_id and xsur.id = #{user_id} and xsm.parent_id='0' order by xsm.order_by ;
	</select>
	
	<!-- 根据role_id 获取中间表中的menu_id -->
	<select id="menuListOfRole" resultType="String">
		SELECT xsrm.menu_id FROM sys_role_menu xsrm
		where xsrm.role_id = #{role_id};
	</select>
	
	<select id="listAllParentMenuByRoleId" resultType="SysMenu">
		SELECT xsm.* FROM sys_menu xsm, sys_role_menu xsrm
		where xsm.id = xsrm.menu_id and xsrm.role_id = #{role_id} and xsm.parent_id='0' order by xsm.order_by ;
	</select>
	
	<select id="listAllParentMenuByAdmin" resultType="SysMenu">
		SELECT xsm.* FROM sys_menu xsm 
		where xsm.parent_id='0' order by xsm.order_by
	</select>
	
	<select id="getAllParents" resultType="SysMenu">
		SELECT  m1.* 
		FROM sys_menu m1 where 1=1
		and m1.parent_id = '0'
	</select>
	
	<select id="getAllParent" resultType="PageData">
		SELECT  m1.id,
		 		m1.name
		FROM sys_menu m1 where 1=1
		and m1.parent_id = '0'
	</select>
	
	<select id="listPageByParam" parameterType="Page" resultType="PageData">
		SELECT  m1.id,
		 		m1.name,
		 		m1.description,
		 		m1.parent_id,
		 		m1.url,
		 		m1.order_by,
		 		m1.new_order_by,
		 		m2.name as p_name 
		FROM (select m.*, (case when m.parent_id = 0 then m.id * 100 + m.order_by else m.parent_id * 100 
				+ (select order_by + 1 from sys_menu where id = m.parent_id) + m.order_by end) as new_order_by
			from sys_menu m) m1 left join sys_menu m2  
		on m1.parent_id = m2.id  where 1=1
		<if test="pd.parent_id != null and pd.parent_id != ''">
			and m1.parent_id = #{pd.parent_id}
		</if>
		<if test="pd.name !=null">
		    and m1.name LIKE CONCAT(CONCAT('%', #{pd.name}),'%')
		</if> 
		order by m1.new_order_by 
	</select>
	
	<select id="findCount" parameterType="Page" resultType="Long"> 
		SELECT  count(*) 
		FROM (select m.*, (case when m.parent_id = 0 then m.id * 100 + m.order_by else m.parent_id * 100 + 
				(select order_by + 1 from sys_menu where id = m.parent_id) + m.order_by end) as new_order_by
			from sys_menu m) m1 left join sys_menu m2  
		on m1.parent_id = m2.id  where 1=1
		<if test="pd.parent_id != null and pd.parent_id != ''">
			and m1.parent_id = #{pd.parent_id}
		</if>
		<if test="pd.name !=null">
		    and m1.name LIKE CONCAT(CONCAT('%', #{pd.name}),'%')
		</if> 
		order by m1.new_order_by 
	</select>
	
	<select id="listAllSubMenuByParentId" parameterType="String" resultType="SysMenu">
		SELECT xsm.* FROM sys_menu xsm where parent_id = #{parentId} order by order_by
	</select>
	
	<select id="listSubMenuByParentIdAndRoleId" resultType="SysMenu">
		SELECT m.* FROM sys_menu m, sys_role_menu rm
		where m.id = rm.menu_id and m.parent_id = #{parentId} and rm.role_id = #{roleId} 
		order by order_by
	</select>
	
	<select id="listSubMenuByAdmin" resultType="SysMenu">
		SELECT m.* FROM sys_menu m
		where m.parent_id = #{parentId} 
		order by order_by
	</select>
		
	<select id="getMenuById" resultType="PageData">
		select  id,
		 		name,
		 		description,
		 		url,
		 		parent_id,
		 		order_by
		 from sys_menu 
		 where id=#{id}
	</select>
	
	
	<insert id="insertMenu">
		insert into sys_menu (
			id,
			name,
			description,
			url,
			parent_id,
			order_by
		) values (
			#{id},
			#{name},
			#{description},
			#{url},
			#{parent_id},
			#{order_by}
		)
	</insert>
	
	<update id="updateMenu" parameterType="SysMenu">
		update sys_menu set 
			name=#{name},
			description=#{description} ,
			url=#{url} ,
			parent_id =#{parent_id},
			order_by=#{order_by}
		where 
			id=#{id}
	</update>
	
	<update id="editicon" parameterType="SysMenu">
		update sys_menu set 
			MENU_ICON=#{MENU_ICON}
		where MENU_ID=#{MENU_ID}
	</update>
	
	<update id="editType" parameterType="SysMenu">
		update sys_menu set 
			MENU_TYPE=#{MENU_TYPE}
		where PARENT_ID=#{MENU_ID}
	</update>
	
	<delete id="deleteMenuById" parameterType="String">
		delete from sys_menu 
		where id=#{id} 
		or parent_id=#{id}
	</delete>
</mapper>