<?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.cd.mapper.CdDtlMapper">

	<select id="select" parameterType="cdDtl" resultType="cdDtl">
		select
			CD_DTL_ID, CD_ID, CD_DTL_NM, CD_DTL_LABEL, CD_DTL_ORD, CD_DTL_DESC, 
			USE_ST, REG_ID, REG_DT, UPD_ID, UPD_DT
		from
			JCMS_CD_DTL
		where
			DEL_DT IS NULL and
			CD_DTL_ID = #{cdDtlId} and
			CD_ID = #{cdId}
	</select>

	<select id="selectList" parameterType="cdDtl" resultType="cdDtl">
	SELECT *
	FROM (
		SELECT ROWNUM RNUM , LIST.*
		FROM (
			SELECT CD_DTL_ID, CD_ID, CD_DTL_NM, CD_DTL_LABEL, CD_DTL_ORD, CD_DTL_DESC, USE_ST
			FROM JCMS_CD_DTL
			WHERE DEL_DT IS NULL
				<if test="!empty(cdId)"> AND CD_ID = #{cdId,jdbcType=VARCHAR} </if>
				<if test="!empty(useSt)"> AND USE_ST = #{useSt} </if>
			<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="cdDtl" resultType="java.lang.Integer">
		select
			COUNT(CD_DTL_ID)
		from
			JCMS_CD_DTL
		where
			DEL_DT IS NULL
		<if test="!empty(cdId)"> and CD_ID = #{cdId} </if>
		<if test="!empty(useSt)"> and USE_ST = #{useSt} </if>
	</select>
	
	<select id="countChek" parameterType="cdDtl" resultType="java.lang.Integer">
		select
			COUNT(CD_DTL_ID)
		from
			JCMS_CD_DTL
		where
			DEL_DT IS NULL 
		<if test="!empty(cdId)"> and CD_ID = #{cdId} </if>
	</select>
	
	<select id="existCount" parameterType="cdDtl" resultType="java.lang.Integer">
		select
			COUNT(CD_DTL_ID)
		from
			JCMS_CD_DTL
		where
			DEL_DT IS NULL and
			CD_ID = #{cdId} and
			(CD_DTL_ID = #{cdDtlId} OR CD_DTL_NM = #{cdDtlNm})
	</select>

	<insert id="insert" parameterType="cdDtl">
		insert into JCMS_CD_DTL (
			CD_DTL_ID,
			CD_ID,
			CD_DTL_NM,
			CD_DTL_LABEL,
			CD_DTL_ORD,
			CD_DTL_DESC,
			USE_ST,
			REG_ID,
			REG_DT
		) values (
			#{cdDtlId},
			#{cdId},
			#{cdDtlNm},
			#{cdDtlLabel},
			#{cdDtlOrd},
			#{cdDtlDesc},
			#{useSt},
			#{regId},
			SYSDATE
		)
	</insert>
	
	<update id="update" parameterType="cdDtl">
		update JCMS_CD_DTL set
			CD_DTL_NM = #{cdDtlNm},
			CD_DTL_LABEL = #{cdDtlLabel},
			CD_DTL_ORD = #{cdDtlOrd},
			CD_DTL_DESC = #{cdDtlDesc},
			USE_ST = #{useSt},
			UPD_ID = #{updId},
			UPD_DT = SYSDATE
		where
			CD_ID = #{cdId}	and
			CD_DTL_ID = #{cdDtlId} and
			DEL_DT IS NULL
	</update>
	
	<delete id="delete" parameterType="cdDtl">
		delete from JCMS_CD_DTL 
		where
			CD_ID = #{cdId}	and
			CD_DTL_ID = #{cdDtlId}
	</delete>

	<select id="selectCdDtlList" parameterType="String" resultType="cdDtl">
		SELECT CD_DTL_ID, CD_DTL_NM
		FROM JCMS_CD_DTL
		WHERE CD_ID = #{cdId}
		ORDER BY CD_DTL_ORD
	</select>

</mapper>