<?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.RoleMapper">

	<resultMap type="Role" id="roleResult">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="parent.id" column="parent_id"/>
		<result property="userCreate" column="user_create"/>
		<result property="userModified" column="user_modified"/>
		<result property="gmtCreate" column="gmt_create"/>
		<result property="gmtModified" column="gmt_modified"/>
	</resultMap>

	<sql id="role_fields">
		id,
		name,
		parent_id,
		user_create,
		user_modified,
		gmt_create,
		gmt_modified
	</sql>

	<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="Role">
		insert into sys_role
			( name,parent_id,user_create )
		values
			( #{name},#{parent.id},#{userCreate} )
	</insert>

	<update id="update" parameterType="Role">
		update sys_role
		<set>
			id=#{id},
			<if test="name !=null and name !=''">
				name=#{name},
			</if>
			<if test="parent != null and parent.id != null">
				parent_id=#{parent.id},
			</if>
			user_modified=#{userModified}
		</set>
		where id=#{id}
	</update>

	<delete id="delete" parameterType="java.lang.Long">
		delete from sys_role where id=#{value}
	</delete>

	<insert id="addRolePermissionRel" parameterType="RolePermissionRel">
		insert into sys_role_permission_rel( role_id, permission_id )
		values( #{roleId}, #{permissionId} )
	</insert>

	<select id="findPermissionIdByRoleId" resultType="java.lang.Long" parameterType="java.lang.Long">
		select permission_id from sys_role_permission_rel
		<if test="value != null">
			where role_id = #{value}
		</if>
	</select>

	<select id="isChecked" resultType="java.lang.Long">
		select permission_id from sys_role_permission_rel
		where role_id = #{roleId} and permission_id = #{permissionId}
	</select>

	<delete id="deleteByRoleId" parameterType="java.lang.Long">
		delete from sys_role_permission_rel where role_id=#{value}
	</delete>

	<select id="get" parameterType="java.lang.Long" resultMap="roleResult">
		select <include refid="role_fields"/> from sys_role where id = #{value}
	</select>

	<select id="getByNmae" parameterType="java.lang.String" resultMap="roleResult">
		select <include refid="role_fields"/> from sys_role where name = #{value}
	</select>

	<select id="findBySearch" resultMap="roleResult" parameterType="SearchRoleanization">
		select <include refid="role_fields"/> from sys_role where ISNULL(parent_id)
		order by id
	</select>

	<select id="findAll" resultMap="roleResult">
		select <include refid="role_fields"/> from sys_role
	</select>

	<select id="countBySearch" resultType="java.lang.Integer" parameterType="SearchRoleanization">
		select count(*) from sys_role
	</select>

	<select id="getByRoleName" resultMap="roleResult" parameterType="java.lang.String">
		select <include refid="role_fields"/> from sys_role where name=#{name}
	</select>

	<select id="getUserIdByRoleId" resultType="java.lang.Long" parameterType="java.lang.Long">
		select user_id from sys_user_role_rel where role_id =#{value}
	</select>

	<select id="getAdminUserId" resultType="java.lang.String">
		select user_id from sys_user_role_rel where role_id =#{roleId} and admin=#{admin}
	</select>

	<select id="findByOrgId" resultType="java.lang.Long" resultMap="roleResult">
		select <include refid="role_fields"/> from sys_role where orgId = #{value}
	</select>

	<select id="getAllPermissionId" resultType="java.lang.Long">
		select id from t_permission
	</select>

	<select id="getNodeIdByRoleId" parameterType="java.lang.Long" resultType="java.lang.Long">
		select r.permissionId from sys_role_permission_rel r
		inner join
		t_permission p on r.permissionId =p.id
		where p.parentId <![CDATA[ <> ]]>
		'' and r.roleId = #{roleId}
	</select>

	<select id="getRootIdByRoleId" parameterType="java.lang.Long"
		resultType="java.lang.Long">
		select r.permissionId from sys_role_permission_rel r
		inner
		join t_permission p on r.permissionId =p.id
		where p.parentId = null  and
		r.roleId = #{roleId}
	</select>

	<select id="getUserRoleByUserId" resultType="UserRoleRel">
		select user_id,admin,role_id from sys_user_role_rel where user_id =#{userId}
	</select>

	<select id="findGroupRole" resultMap="roleResult">
		select id,name,parent_id,user_create,user_modified,gmt_create,gmt_modified
		from sys_role where ISNULL(parent_id) and id !=1
	</select>

	<select id="findByParentRoleId" parameterType="java.lang.Long" resultMap="roleResult">
		select <include refid="role_fields"/> from sys_role where parent_id=#{parentRoleId}
	</select>

	<delete id="deleteUserRole" parameterType="java.lang.Long">
		delete from sys_user_role_rel where user_id=#{userId} and role_id=#{roleId}
	</delete>

	<select id="getByUserRole" parameterType="java.lang.Long" resultType="UserRoleRel">
		select user_id,admin,role_id from sys_user_role_rel where user_id=#{userId} and role_id=#{roleId}
	</select>
	<select id="getRoleListByUseId" parameterType="java.lang.Long" resultType="com.sunxung.factoring.entity.sys.Role">
		SELECT
			c.*
		FROM
			sys_user a,
			sys_user_role_rel b,
			sys_role c
		WHERE
			a.id = b.user_id
		  AND b.role_id = c.id
		  AND a.id = #{userId}
	</select>

	<update id="adjustOrder">
		update sys_user_role_rel set gradation=#{gradation} where role_id=#{roleId} and user_id=#{userId}
	</update>

</mapper>
