<?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="kr.co.jinwoosi.clfd.framework.security.mapper.UserRoleMapper">

	<select id="selectList" parameterType="userRole" resultType="userRole">
			select
				USER_ID, ROLE_AUTH
			from
				JCMS_USER_ROLE
			where
				1 = 1		
		<if test="!empty(userId)" > and USER_ID = #{userId} </if>
		<if test="!empty(roleAuth)" > and ROLE_AUTH = #{roleAuth} </if>
	</select>
	
	<select id="select" parameterType="userRole" resultType="userRole">
			select
				USER_ID, ROLE_AUTH
			from
				JCMS_USER_ROLE
			where
				1 = 1	
		<if test="!empty(userId)" > and USER_ID = #{userId} </if>
		<if test="!empty(roleAuth)" > and ROLE_AUTH = #{roleAuth} </if>
	</select>
	
	<select id="selectRoleList" parameterType="userRole" resultType="userRole">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT ROLE.ROLE_AUTH, ROLE.ROLE_NM, ROLE.ROLE_DESC, AUTH.USER_ID
				FROM JCMS_ROLE ROLE
					LEFT JOIN
					(SELECT * FROM JCMS_USER_ROLE WHERE 1 = 1 <if test="!empty(userId)" > AND USER_ID = #{userId} </if>	) AUTH
					ON ROLE.ROLE_AUTH = AUTH.ROLE_AUTH
				<if test="!empty(sort) and !empty(sortOrd)">
				ORDER BY ${sort} ${sortOrd}
			    </if>
	       	) LIST
		)
	    <if test="pagingYn == true" >
	    	<![CDATA[
				WHERE RNUM > #{start} AND RNUM <= #{start} + #{length}
			]]>
		</if>
	</select>
	
	<select id="count" parameterType="userRole" resultType="java.lang.Integer">
		select
			COUNT(ROLE_AUTH)
		from
			JCMS_ROLE
	</select>
	
	<select id="existCount" parameterType="userRole" resultType="java.lang.Integer">
		select
			COUNT(ROLE_AUTH)
		from
			JCMS_ROLE
		where
			(ROLE_AUTH = #{roleAuth} OR ROLE_NM = #{roleNm})
	</select>
	
	<insert id="insert" parameterType="userRole">
		insert into JCMS_USER_ROLE(
			USER_ID,
			ROLE_AUTH
		) values(
			#{userId},
			#{roleAuth}
		)
	</insert>
	
	<update id="update" parameterType="userRole">
		UPDATE JCMS_USER_ROLE SET
			USER_ID = #{userId},
			ROLE_AUTH = #{roleAuth}
		WHERE
			USER_ID = #{userId} 
	</update>
	
	<delete id="delete" parameterType="userRole">
		delete from JCMS_USER_ROLE
		where USER_ID = #{userId}
	</delete>
</mapper>