<?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.banner.mapper.BannerMapper">

	<select id="select" parameterType="bannerSearch" resultType="banner">
		select	
			BANNER_ID, BANNER_NM, BANNER_TP,
			(select CD_DTL_NM from JCMS_CD_DTL where CD_ID = 'banner' and CD_DTL_ID = BANNER_TP) AS BANNER_TP_NM,
			(select CD_DTL_LABEL from JCMS_CD_DTL where CD_ID = 'banner' and CD_DTL_ID = BANNER_TP) AS BANNER_TP_LABEL,
			(select SITE_NM from JCMS_SITE where JCMS_SITE.SITE_ID = JCMS_BANNER.SITE_ID ) AS SITE_NM,
			FILE_ID, SITE_ID, BANNER_ALT, LINK_URL, START_DT, END_DT, 
			REG_ID, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT
		from		
			JCMS_BANNER
		where
			DEL_DT IS NULL
		<if test="!empty(bannerId)" > and BANNER_ID = #{bannerId} </if>
		<if test="!empty(bannerTp)" > and BANNER_TP = #{bannerTp} </if>
	</select>
	
	<select id="selectList" parameterType="bannerSearch" resultType="banner">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT BANNER_ID, BANNER_NM, BANNER_TP,
					(SELECT CD_DTL_NM FROM JCMS_CD_DTL WHERE CD_ID = 'banner' AND CD_DTL_ID = BANNER_TP) AS BANNER_TP_NM,
					(SELECT CD_DTL_LABEL FROM JCMS_CD_DTL WHERE CD_ID = 'banner' AND CD_DTL_ID = BANNER_TP) AS BANNER_TP_LABEL,
					FILE_ID, SITE_ID, BANNER_ALT, LINK_URL, START_DT, END_DT, REG_ID, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT
				FROM JCMS_BANNER
				WHERE DEL_DT IS NULL
					<if test="!empty(bannerNmLike)" > AND BANNER_NM LIKE '%'||{bannerNmLike}||'%' </if>
					<if test="!empty(linkUrlLike)" > AND LINK_URL LIKE '%'||#{linkUrlLike}||'%' </if>
					<if test="!empty(bannerTp)" > AND BANNER_TP = #{bannerTp}  </if>
					<if test="startDtThanOrEquals != null" > AND START_DT >= #{startDtThanOrEquals}||' 00:00:00' </if>
					<if test="endDtLessOrEquals != null" ><![CDATA[ AND END_DT <= #{endDtLessOrEquals}||' 23:59:59' ]]></if>
					<if test="bannerIdIn != null" > AND BANNER_ID IN
						<foreach item="item" index="index" collection="bannerIdIn" open="(" separator="," close=")">
							#{item}
						</foreach>
					</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="bannerSearch" resultType="java.lang.Integer">
		select 
			COUNT(BANNER_ID)
		from
			JCMS_BANNER
		where
	   		DEL_DT IS NULL
		<if test="!empty(bannerNmLike)" > and BANNER_NM LIKE '%'||#{bannerNmLike}||'%' </if>
		<if test="!empty(linkUrlLike)" > and LINK_URL LIKE '%'||#{linkUrlLike}||'%' </if>
		<if test="!empty(bannerTp)" > and BANNER_TP = #{bannerTp} </if>
		<if test="startDtThanOrEquals != null" > and START_DT >= #{startDtThanOrEquals}||' 00:00:00' </if>
		<if test="startDtLessOrEquals != null" > <![CDATA[ and START_DT <= #{startDtLessOrEquals}||' 23:59:59' ]]> </if>
		<if test="endDtThanOrEquals != null" > and END_DT >= #{endDtThanOrEquals}||' 00:00:00' </if>
		<if test="endDtLessOrEquals != null" > <![CDATA[ and END_DT <= #{endDtLessOrEquals}||' 23:59:59' ]]> </if>
	</select>

	<insert id="insert" parameterType="banner">
		insert into JCMS_BANNER (
			BANNER_ID,
			BANNER_NM,
			BANNER_TP,
			FILE_ID,
			SITE_ID,
			BANNER_ALT,
			LINK_URL,
			START_DT,
			END_DT,
			REG_ID,
			REG_DT
		) values (
			BANNER_ID_SEQ.NEXTVAL
			, #{bannerNm}
			, #{bannerTp}
			, #{fileId}
			, #{siteId}
			, #{bannerAlt,jdbcType=VARCHAR}
			, #{linkUrl,jdbcType=VARCHAR}
			, #{startDt}
			, #{endDt}
			, #{regId}
			, SYSDATE
		)
	</insert>

	<update id="update" parameterType="banner">
		update JCMS_BANNER set
			BANNER_NM = #{bannerNm}
			,BANNER_TP = #{bannerTp}
			,FILE_ID = #{fileId}
			,SITE_ID = #{siteId} 
			,BANNER_ALT = #{bannerAlt,jdbcType=VARCHAR}
			,LINK_URL = #{linkUrl,jdbcType=VARCHAR}
			,START_DT = #{startDt}
			,END_DT = #{endDt}
			,UPD_ID = #{updId}
			,UPD_DT = SYSDATE
		where 
			BANNER_ID = #{bannerId}
	</update>
	
	<update id="delete" parameterType="banner">
		update JCMS_BANNER set
			DEL_ID = #{delId}
			,DEL_DT = SYSDATE
		where 
			BANNER_ID = #{bannerId}
	</update>
	
</mapper>
