<?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.BodArtiMapper">

	<select id="select" parameterType="bodArtiSearch" resultType="bodArti">
		select	
			ARTI_ID, BOD_ID, DEPTH, TITLE, CONT, SCRT_YN, NOTI_YN, FILE_ID,
			(select COUNT(ITEM_ID) from JCMS_FILE_ITEM where JCMS_FILE_ITEM.FILE_ID = JCMS_BOD_ARTI.FILE_ID) AS FILE_CNT,
			FILE_PRVIEW, HIT, REG_ID, REG_DT, TO_CHAR(REG_DT,'YYYY-MM-DD') AS REG_DATE, UPD_ID, UPD_DT, DEL_ID, DEL_DT, REG_NM
		from		
			JCMS_BOD_ARTI
		where
			DEL_DT IS NULL
		<if test="!empty(artiId)" > and ARTI_ID = #{artiId} </if>
	</select>
	
	<select id="selectList" parameterType="bodArtiSearch" resultType="bodArti">
		SELECT  *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT
					ARTI_ID, BOD_ID, (SELECT BOD_NM FROM JCMS_BOD WHERE JCMS_BOD_ARTI.BOD_ID = JCMS_BOD.BOD_ID) AS BOD_NM,
				    DEPTH, TITLE, CONT, SCRT_YN, NOTI_YN, FILE_ID,
					(SELECT COUNT(ITEM_ID) FROM JCMS_FILE_ITEM WHERE JCMS_FILE_ITEM.FILE_ID = JCMS_BOD_ARTI.FILE_ID) AS FILE_CNT,
					FILE_PRVIEW, HIT, REG_ID, REG_DT, TO_CHAR(REG_DT,'YYYY-MM-DD') AS REG_DATE, UPD_ID, UPD_DT, DEL_ID, DEL_DT, REG_NM
				FROM JCMS_BOD_ARTI
				WHERE DEL_DT IS NULL
					<if test="!empty(bodId)" > AND BOD_ID = #{bodId} </if>
					<if test="scrtYn != null" > AND SCRT_YN = #{scrtYn} </if>
					<if test="notiYn != null" > AND NOTI_YN = #{notiYn} </if>
					<if test="!empty(titleLike)" > AND TITLE LIKE '%'||#{titleLike}||'%' </if>
					<if test="!empty(contLike)" > AND CONT LIKE '%'||#{contLike}||'%' </if>
					<if test="regId != null" > AND REG_ID = #{regId} </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="bodArtiSearch" resultType="java.lang.Integer">
		SELECT COUNT(ARTI_ID) FROM JCMS_BOD_ARTI
		WHERE DEL_DT IS NULL
		<if test="scrtYn != null" > AND SCRT_YN = #{scrtYn} </if>
		<if test="notiYn != null" > AND NOTI_YN = #{notiYn} </if>
		<if test="!empty(bodId)" > AND BOD_ID = #{bodId} </if>
		<if test="!empty(titleLike)" > AND TITLE LIKE '%'||#{titleLike}||'%' </if>
		<if test="!empty(contLike)" > AND CONT LIKE '%'||#{contLike}||'%' </if>
		<if test="regId != null" > AND REG_ID = #{regId} </if>
	</select>
	
	<insert id="insert" parameterType="bodArti" >
		<selectKey keyProperty="artiId" resultType="java.lang.Long" order="BEFORE">
	    	select ARTI_ID_SEQ.NEXTVAL AS artiId from DUAL
	  	</selectKey>
			insert into JCMS_BOD_ARTI (
				ARTI_ID,
				BOD_ID,
				DEPTH,
				TITLE,
				CONT,
				SCRT_YN,
				NOTI_YN,
				FILE_ID,
				FILE_PRVIEW,
				HIT,
				REG_ID,
				REG_DT,
				REG_NM
			) values (
				#{artiId}
				, #{bodId}
				, #{depth}
				, #{title}
				, #{cont}
				, #{scrtYn}
				, #{notiYn}
				, #{fileId}
				, #{filePrview}
				, 0
				, #{regId}
				, SYSDATE
				, #{regNm}
			)
	</insert>

	<update id="update" parameterType="bodArti">
		update JCMS_BOD_ARTI
		set BOD_ID = #{bodId}
			,TITLE = #{title}
			,CONT = #{cont}
			,SCRT_YN = #{scrtYn}
			,NOTI_YN = #{notiYn}
			,FILE_PRVIEW = #{filePrview}
			,UPD_ID = #{updId}
			,UPD_DT = SYSDATE
		where
			ARTI_ID = #{artiId}
	</update>
	
	<update id="updateHit" parameterType="java.lang.Long">
		update JCMS_BOD_ARTI set
			HIT = HIT+1
		where
			ARTI_ID = #{artiId}
	</update>
	
	<update id="delete" parameterType="bodArti">
		update JCMS_BOD_ARTI set
			DEL_ID = #{delId}
			,DEL_DT = SYSDATE
		where
			ARTI_ID = #{artiId}
	</update>
</mapper>
