<?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.file.mapper.FileItemMapper">

	<select id="select" parameterType="fileItemSearch" resultType="fileItem">
		select	
			FILE_ID, ITEM_ID, FILE_NM, FILE_EXT, MEDIA_TP, FILE_SIZE, SAVE_PATH, SAVE_FILE_NM, DWN_CNT, REG_ID, REG_DT
		from		
			JCMS_FILE_ITEM
		where
			1=1
		<if test="!empty(fileId)" > and FILE_ID = #{fileId} </if>
		<if test="itemId != null" > and ITEM_ID = #{itemId} </if>
	</select>
	
	<select id="selectList" parameterType="fileItemSearch" resultType="fileItem">
		SELECT *
		FROM (
			SELECT ROWNUM RNUM , LIST.*
			FROM (
				SELECT FILE_ID, ITEM_ID, FILE_NM, FILE_EXT, MEDIA_TP, FILE_SIZE, SAVE_PATH, SAVE_FILE_NM, DWN_CNT, REG_ID, REG_DT
				FROM JCMS_FILE_ITEM
				WHERE 1=1
					<if test="!empty(fileId)" > AND FILE_ID = #{fileId} </if>
					<if test="itemId != null" > AND ITEM_ID = #{itemId} </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="selectSerial" parameterType="java.lang.String" resultType="java.lang.Integer" >
  		select 
			NVL(MAX(ITEM_ID), 0)+1
		from 
			JCMS_FILE_ITEM
		where
			FILE_ID = #{fileId}
  	</select>
	
	<insert id="insert" parameterType="fileItem" >
		<selectKey keyProperty="itemId" resultType="java.lang.Long" order="BEFORE" >
	    	select FILE_ITEM_ID_SEQ.NEXTVAL from DUAL
	  	</selectKey>
		insert into JCMS_FILE_ITEM (
			ITEM_ID,
			FILE_ID,
			FILE_NM,
			FILE_EXT,
			MEDIA_TP,
			FILE_SIZE,
			SAVE_PATH,
			SAVE_FILE_NM,
			REG_ID,
			REG_DT
		) values (
			#{itemId}
			, #{fileId,jdbcType=NUMERIC}
			, #{fileNm}
			, #{fileExt}
			, #{mediaTp}
			, #{fileSize}
			, #{savePath}
			, #{saveFileNm}
			, #{regId}
			, SYSDATE
		)
	</insert>

	<delete id="delete" parameterType="fileItem">
		delete from JCMS_FILE_ITEM
		where 
			FILE_ID = #{fileId}
			and ITEM_ID = #{itemId}
	</delete>
	
	<delete id="updateFileId" parameterType="fileItem">
		update JCMS_FILE_ITEM set
			FILE_ID = #{fileId}
		where 
			ITEM_ID = #{itemId,jdbcType=VARCHAR}
	</delete>
	
	<update id="updateDwnCnt" parameterType="java.lang.Long">
		update JCMS_FILE_ITEM set
			DWN_CNT = DWN_CNT+1
		where
			ITEM_ID = #{itemId}
	</update>
</mapper>
