<?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.zyd.blog.persistence.mapper.SysRoleMapper">
	<resultMap id="rm" type="com.zyd.blog.persistence.beans.SysRole">
		<result property="id" jdbcType="BIGINT" column="id"/>
		<result property="name" jdbcType="VARCHAR" column="name"/>
		<result property="description" jdbcType="VARCHAR" column="description"/>
		<result property="available" jdbcType="BIT" column="available"/>
		<result property="createTime" jdbcType="TIMESTAMP" column="create_time"/>
		<result property="updateTime" jdbcType="TIMESTAMP" column="update_time"/>
	</resultMap>
	<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.RoleConditionVO" resultMap="rm">
		SELECT
			com.*
		FROM
			sys_role com
		WHERE
			1 = 1
			<if test="keywords !=null and keywords != ''">
				AND (
					com.description  LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%')
				)
			</if>
		ORDER BY
			com.create_time DESC
	</select>

	<select id="queryRoleListWithSelected" parameterType="Integer" resultMap="rm">
		SELECT
			r.id,
			r.name,
			r.description,
			(
				CASE
				WHEN (
					SELECT
						ur.role_id
					FROM
						sys_user_role ur
					WHERE
						ur.user_id = #{userId}
					AND ur.role_id = r.id
				) THEN
					1
				ELSE
					0
				END
			) AS selected
		FROM
			sys_role r
		WHERE
			r.available = 1
	</select>

	<select id="listRolesByUserId" parameterType="Long" resultMap="rm">
		SELECT
			r.id,
			r.name,
			r.description
		FROM
			sys_role r
		INNER JOIN sys_user_role ur ON ur.role_id = r.id
		WHERE
			ur.user_id = #{userId}
		AND r.available = 1
	</select>
</mapper>

