<?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.conts.cont.mapper.ContMapper">
	
	<select id="select" parameterType="contSearch" resultType="cont">
		select	
			CONT_ID,
			CONT_NM,
			(select CASE WHEN COUNT(CONT_ID) > 0 THEN 1 ELSE 0 END from JCMS_CONT_BKMAK where JCMS_CONT_BKMAK.CONT_ID = JCMS_CONT.CONT_ID) AS BOOKMARK_YN
		from		
			JCMS_CONT
		where
			1 = 1
		<if test="!empty(contId)" >and CONT_ID = #{contId} </if>
	</select>
	
	<select id="selectList" parameterType="contSearch" resultType="cont">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT CONT_ID, CONT_NM,
					(SELECT CASE WHEN COUNT(CONT_ID) > 0 THEN 1 ELSE 0 END FROM JCMS_CONT_BKMAK WHERE JCMS_CONT_BKMAK.CONT_ID = JCMS_CONT.CONT_ID) AS BOOKMARK_YN
				FROM JCMS_CONT
				WHERE 1 = 1
				<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="contSearch" resultType="java.lang.Integer">
		select 
			COUNT(CONT_ID)
		from
			JCMS_CONT
		where
	   		1 = 1
	</select>
	
	<select id="selectSerial" resultType="java.lang.String" >
  		select 
  			NVL(MAX(CONT_ID),0)+1
		from 
			JCMS_CONT
  	</select>

	<insert id="insert" parameterType="cont" useGeneratedKeys="true">
		<selectKey keyProperty="contId" resultType="java.lang.Long" order="AFTER">
	    	select MAX(CONT_ID) from JCMS_CONT
	  	</selectKey>
		insert into JCMS_CONT (
			CONT_ID,
			CONT_NM
		) values (
			#{contId}
			,#{contNm}
		)
	</insert>
	
	<insert id="update" parameterType="cont">
		update JCMS_CONT set
			CONT_NM = #{contNm}
		where
			CONT_ID = #{contId}
	</insert>
	
	<select id="dynamic" parameterType="contSearch" resultType="java.util.Map">
	select 
		<if test="selectList != null" >
			<foreach item="item" index="index" collection="selectList" separator="," >
				${item}
			</foreach>
		</if>
	from 
		JCMS_CONT
	where
		1 = 1
	
	<if test="groupbyList != null" >
		GROUP BY
		<foreach item="item" index="index" collection="groupbyList" separator="," >
			${item}
		</foreach>
	</if>
	</select>
</mapper>
