<?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.sunxung.factoring.mapper.sys.PermissionMapper">

	<resultMap type="Permission" id="permissionResult">
		<result property="id" column="id"/>
		<result property="parentId" column="parentId"/>
		<result property="type" column="type"/>
		<result property="cname" column="cname"/>
		<result property="ename" column="ename"/>
		<result property="gradation" column="gradation"/>
		<result property="moduleName" column="moduleName"/>
		<result property="url" column="url"/>
		<result property="icon" column="icon"/>
		<result property="gmtCreate" column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified"/>
	</resultMap>
	
	<sql id="fields">
		id,
		parentId,
    type,
		cname,
		ename,
		gradation,
		moduleName,
		url,
		icon,
		gmt_create,
		gmt_modified
	</sql>
	
	<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="Permission">
		insert into 
			t_permission (parentId, cname, ename, gradation, moduleName, url, icon, user_create, user_modified )  
		values
			( #{parentId}, #{cname}, #{ename}, #{gradation}, #{moduleName}, #{url}, #{icon}, #{userCreate}, #{userModified} )
	</insert>
	
	<select id="get" parameterType="java.lang.Long" resultMap="permissionResult">
		select <include refid="fields"/> from t_permission where id = #{value}
	</select>
	
	<select id="findByParentId" resultMap="permissionResult" parameterType="java.lang.String">
		select <include refid="fields"/>  from t_permission
		<if test="parentId!=null">
			where	parentId=#{parentId}
		</if>
		<if test="parentId==null">
			where	ISNULL(parentId) 
		</if>
	</select>
	
	<select id="findAll" resultMap="permissionResult">
		select <include refid="fields"/>  from t_permission ORDER BY gradation
	</select>
	
	<select id="getByEname" resultMap="permissionResult" parameterType="java.lang.String">
		select <include refid="fields"/>  from t_permission where ename=#{value}
	</select>
	
	<delete id="deleteRolePermissionRel">
		delete from sys_role_permission_rel
		<where>
			<if test="permissionId != null">
				and permission_id = #{permissionId}
			</if>
			<if test="roleId != null">
				and role_id = #{roleId}
			</if>
		</where>
	</delete>
	
	<select id="findNodesByRoleIdPermissionId" resultType="java.lang.Long">
		select p.id from sys_role_permission_rel rp
		inner join t_permission p on rp.permission_id = p.id
		where p.parentId = #{parentId} and rp.role_id = #{roleId}
	</select>
	
	 <select id="getPermissionIdByUserId" resultType="java.lang.Long">
		select rp.permission_id from sys_user_role_rel ur
		join sys_role_permission_rel rp on ur.role_id = rp.role_id
		where ur.user_id = #{user} 
	</select> 
	
	<select id="getPermissionIdByRoleId" parameterType="java.lang.Long" resultType="java.lang.Long">
		select permission_id from sys_role_permission_rel where role_id = #{roleId}
	</select>
	
	<select id="findAllByUserId" resultMap="permissionResult">
		select distinct <include refid="fields"/>   from   t_permission
		join sys_role_permission_rel  pl on pl.permission_id = id
		where pl.role_id  in
			(
			select ul.role_id from sys_user_role_rel ul
			left join sys_role  r on ul.role_id = r.id
			where ul.user_id=#{userId}
			) 
	</select>
	
</mapper>