<?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.bod.mapper.BodMapper">

	<select id="select" parameterType="bodSearch" resultType="bod">
		select	
			BOD_ID, BOD_TP, BOD_NM, BOD_HEADER, BOD_FOOTER, CMNT_YN, ANSW_YN, SCRT_YN, 
			ARTI_CNT, FILE_UPLOAD_CNT, NEW_ARTI_DT, USE_ST, 
			REG_ID, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT
		from		
			JCMS_BOD
		where
			1=1
		<if test="bodId != null" > and BOD_ID = #{bodId} </if>
	</select>
	
	<select id="selectList" parameterType="bodSearch" resultType="bod">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT
					BOD_ID, BOD_TP, BOD_NM, BOD_HEADER, BOD_FOOTER, CMNT_YN, ANSW_YN,
					SCRT_YN, ARTI_CNT, FILE_UPLOAD_CNT, NEW_ARTI_DT, USE_ST,
					REG_ID, REG_DT, UPD_ID, UPD_DT, DEL_ID, DEL_DT
				FROM JCMS_BOD
				WHERE DEL_DT IS NULL
					<if test="useSt != null" > AND USE_ST = #{useSt} </if>
					<if test="!empty(bodNmLike)" > AND BOD_NM LIKE '%'||#{bodNmLike}||'%' </if>
					<if test="!empty(bodTp)" > AND BOD_TP = #{bodTp} </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="bodSearch" resultType="java.lang.Integer">
		select 
			COUNT(BOD_ID)
		from
			JCMS_BOD
		where
	   		1 = 1
		<if test="!empty(bodNmLike)" > and BOD_NM LIKE '%'||#{bodNmLike}||'%' </if>
		<if test="!empty(bodTp)" > and BOD_TP = #{bodTp} </if>
	</select>
	
	<insert id="insert" parameterType="bod" >
		<selectKey keyProperty="bodId" resultType="java.lang.Long" order="BEFORE">
	    	select BOD_ID_SEQ.NEXTVAL AS bodId from DUAL
	  	</selectKey>
		insert into JCMS_BOD (
			BOD_ID,
			BOD_TP,
			BOD_NM,
			BOD_HEADER,
			BOD_FOOTER,
			CMNT_YN,
			ANSW_YN,
			SCRT_YN,
			ARTI_CNT,
			FILE_UPLOAD_CNT,
			NEW_ARTI_DT,
			USE_ST,
			REG_ID,
			REG_DT
		) values (
			#{bodId}
			,#{bodTp}
			, #{bodNm}
			, #{bodHeader,jdbcType=VARCHAR}
			, #{bodFooter,jdbcType=VARCHAR}
			, #{cmntYn,jdbcType=CHAR}
			, #{answYn,jdbcType=CHAR}
			, #{scrtYn,jdbcType=CHAR}
			, #{artiCnt}
			, #{fileUploadCnt}
			, #{newArtiDt}
			, #{useSt}
			, #{regId}
			, SYSDATE
		)
	</insert>

	<update id="update" parameterType="bod">
		update JCMS_BOD set 
			BOD_NM = #{bodNm}
			,BOD_HEADER = #{bodHeader,jdbcType=VARCHAR}
			,BOD_FOOTER = #{bodFooter,jdbcType=VARCHAR}
			,BOD_TP = #{bodTp}
			,CMNT_YN = #{cmntYn,jdbcType=CHAR}
			,ANSW_YN = #{answYn,jdbcType=CHAR}
			,SCRT_YN = #{scrtYn,jdbcType=CHAR}
			,ARTI_CNT = #{artiCnt}
			,FILE_UPLOAD_CNT = #{fileUploadCnt}
			,NEW_ARTI_DT = #{newArtiDt}
			,USE_ST = #{useSt}
			,UPD_ID = #{updId}
			,UPD_DT = SYSDATE
		where 
			BOD_ID = #{bodId}
	</update>
	
	<select id="selectRoleList" parameterType="role" resultType="role">
		select 
			ROLE_AUTH,
			ROLE_NM,
			ROLE_DESC
		from
			JCMS_ROLE
		where ROLE_AUTH <![CDATA[ <> ]]> 'ROLE_ANONYMOUS'
		<if test="!empty(sort) and !empty(sortOrd)">
			order by
	        <choose>  
	            <when test="sort == 'ROLE_AUTH'"> ROLE_AUTH </when>
	            <when test="sort == 'ROLE_NM'"> ROLE_NM </when> 
	            <when test="sort == 'ROLE_DESC'"> ROLE_DESC </when> 
	        </choose>  
	        <choose>
	        	<when test="sortOrd == 'asc'"> ASC </when>
	        	<when test="sortOrd == 'desc'"> DESC </when>
	        </choose>             
 		</if>
</select>

<update id="delete" parameterType="bod">
		update JCMS_BOD set 
			DEL_ID = #{delId}
			,DEL_DT = SYSDATE
		where 
			BOD_ID = #{bodId}
</update>
</mapper>
