<?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="lingtao.net.dao.SysPermissionMapper">
	<resultMap id="BaseResultMap" type="lingtao.net.bean.SysPermission">
	    <id column="perId" jdbcType="INTEGER" property="perId" />
	    <result column="perName" jdbcType="VARCHAR" property="perName" />
	    <result column="url" jdbcType="VARCHAR" property="url" />
	    <result column="type" jdbcType="VARCHAR" property="type" />
	    <result column="perCode" jdbcType="VARCHAR" property="perCode" />
	    <result column="perIcon" jdbcType="VARCHAR" property="perIcon" />
	    <result column="parentId" jdbcType="INTEGER" property="parentId" />
	    <result column="orderNo" jdbcType="VARCHAR" property="orderNo" />
	    <result column="createBy" jdbcType="VARCHAR" property="updateBy" />
		<result column="createDate" jdbcType="TIMESTAMP" property="createDate" />
	</resultMap>	  
  
  	<select id="getAll" resultMap="BaseResultMap">
  		select * from tbl_sys_permission
  	</select>
  
  	<select id="getParentPers" resultMap="perMap">
        select * from tbl_sys_permission where parentId is null
    </select>
     <resultMap id="perMap" type="SysPermission" autoMapping="true">
        <id column="perId" property="perId"></id>
        <collection property="children" javaType="java.util.List" ofType="SysPermission" column="perId" select="getByParentId"></collection>
    </resultMap>
     <!--查询二级权限-->
    <select id="getByParentId" parameterType="int" resultMap="perMap002">
        select * from tbl_sys_permission where parentId = #{perId}
    </select>
     <!--查询三级权限-->
    <resultMap id="perMap002" type="SysPermission" autoMapping="true">
        <id column="perId" property="perId"></id>
        <collection property="children" javaType="java.util.List" ofType="SysPermission" column="perId" select="getThird"></collection>
    </resultMap>
   	<select id="getThird" parameterType="int" resultType="SysPermission">
      	select * from tbl_sys_permission where parentId = #{perId}
    </select>
   
    
    <!-- 根据登陆的用户查询权限 -->  
    <select id="getPersByUserId" parameterType="int" resultType="SysPermission">
    	SELECT 
		  * 
		FROM
		  tbl_sys_permission 
		WHERE perId IN 
		  (	SELECT 
				perId 
			FROM
		    	tbl_sys_role_permission 
		   	WHERE 
		   		roleId in (
		    		select roleId from tbl_sys_user_role where userId = #{userId}
		   		)
		  ) 
		  ORDER BY -orderNo DESC
    </select>
</mapper>