<?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.huatek.frame.base.mapper.PermissionMapper">

	<resultMap id="BaseResultMap" type="com.huatek.frame.base.vo.PermissionVO">
		<id column="id" jdbcType="VARCHAR" property="id" />
		<result column="pname" jdbcType="VARCHAR" property="pname" />
		<result column="parentName" jdbcType="VARCHAR" property="parentName" />
		<result column="parent_id" jdbcType="VARCHAR" property="parentId" />
		<result column="resource_type" jdbcType="VARCHAR" property="resourceType" />
		<result column="deleted" jdbcType="VARCHAR" property="deleted" />
		<result column="permission" jdbcType="VARCHAR" property="permission" />
		<result column="url" jdbcType="VARCHAR" property="url" />
		<result column="iFrame" jdbcType="VARCHAR" property="iFrame" />
	</resultMap>
	<sql id="Base_Column_List">
		c.id,c.deleted,c.resource_type,c.permission,c.url,
		c.parent_id,c.pname as pname,p.pname as parentName,p.i_frame as iFrame
	</sql>

	<select id="selectPermissionPage" parameterType="com.huatek.frame.base.dto.PermissionDTO"
		resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List" />
		from
		sys_permission c
		left join
		sys_permission p
		on
		c.parent_id=p.id
		where 
			 c.deleted = #{deleted}
		<if test="pname != null and  pname != '' ">
			and c.pname like CONCAT('%',#{pname},'%')
		</if>
		<if test="parentName != null and  parentName != '' ">
			and p.pname like CONCAT('%',#{pname},'%')
		</if>
		<if test="resourceType != null and  resourceType != '' ">
			and c.resource_type = #{resourceType} 
		</if> 
		<if test="permission != null and  permission != '' ">
			and c.permission like CONCAT('%',#{permission},'%')
		</if>
	</select>
	
	<resultMap id="menuTreeMap" type="com.huatek.frame.base.vo.MenuVO" >
		<id property="value" column="id" />
		<result property="title" column="pname" />
		<result property="checked" column="checked" />
		<result property="deleted" column="deleted" />
		<result property="roleId" column="roleId" /> 
		<collection property="data"  ofType="com.huatek.frame.base.vo.MenuVO"   column="{id=id,deleted=deleted,roleId=roleId}" select="findMenuTreeChild"/> 
	</resultMap>
	
	
	<select id="findMenuTree"  resultMap="menuTreeMap" parameterType="java.util.HashMap">
		select  
			case when (select count(s.rid) 
			from sys_role_permission s where s.permission_id = permission.id 
				and s.rid = #{roleId} )>0 
			then 'true' 
				else 'false' 
			end  as checked,
			#{roleId} as roleId,
			permission.id as  id  , 
			permission.pname as pname,
			permission.deleted as deleted
		from  
			sys_permission   permission 
		where   
			permission.deleted = #{deleted}
		and 
			permission.resource_type=#{resourceType}
		and 
			permission.level=#{level}
		group by permission.id
	</select>
	
	
	<select id="findMenuTreeChild"  resultMap="menuTreeMap" parameterType="java.util.HashMap" >
	select   
		case when (select count(s.rid) 
			from sys_role_permission s where s.permission_id = permission.id 
				and s.rid = #{roleId} )>0 
			then 'true' 
				else 'false' 
			end  as checked,
		#{roleId} as roleId,
		permission.id as  id, 
		permission.deleted as deleted,
		permission.pname as pname
	from  
		sys_permission   permission  
	where 
		1=1 
	and
		permission.parent_id = #{id}
	and 
		permission.deleted = #{deleted}
	</select>

</mapper>