<?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="FileManageDAO">


	<resultMap id="fileList" type="egovframework.com.cmm.service.FileVO">
		<result property="atchFileId" column="ATCH_FILE_ID"/>
		<result property="fileCn" column="FILE_CN"/>
		<result property="fileExtsn" column="FILE_EXTSN"/>
		<result property="fileMg" column="FILE_SIZE"/>
		<result property="fileSn" column="FILE_SN"/>
		<result property="fileStreCours" column="FILE_STRE_COURS"/>
		<result property="orignlFileNm" column="ORIGNL_FILE_NM"/>
		<result property="streFileNm" column="STRE_FILE_NM"/>
		<result property="creatDt" column="CREAT_DT"/>			
	</resultMap>

	<resultMap id="fileDetail" type="egovframework.com.cmm.service.FileVO">
		<result property="atchFileId" column="ATCH_FILE_ID"/>
		<result property="fileCn" column="FILE_CN"/>
		<result property="fileExtsn" column="FILE_EXTSN"/>
		<result property="fileMg" column="FILE_SIZE"/>
		<result property="fileSn" column="FILE_SN"/>
		<result property="fileStreCours" column="FILE_STRE_COURS"/>
		<result property="orignlFileNm" column="ORIGNL_FILE_NM"/>
		<result property="streFileNm" column="STRE_FILE_NM"/>
	</resultMap>	


 	<select id="FileManageDAO.selectFileList" parameterType="FileVO" resultMap="fileList">
 		
			SELECT 
				a.ATCH_FILE_ID, b.FILE_CN, b.FILE_SN, b.FILE_STRE_COURS, b.STRE_FILE_NM,
				b.FILE_EXTSN, b.ORIGNL_FILE_NM, b.FILE_SIZE, a.CREAT_DT
			FROM
				LETTNFILE a, LETTNFILEDETAIL b
			WHERE
				a.ATCH_FILE_ID = #{atchFileId}
			AND 
				a.ATCH_FILE_ID = b.ATCH_FILE_ID
			AND 
				a.USE_AT = 'Y'				
			ORDER BY b.FILE_SN	
 		
 	</select>
 	
	<insert id="FileManageDAO.insertFileMaster" parameterType="FileVO">
		
			INSERT INTO LETTNFILE
			(ATCH_FILE_ID, CREAT_DT, USE_AT)
			VALUES
			( #{atchFileId}, now(), 'Y')
		
	</insert>
	
	<insert id="FileManageDAO.insertFileDetail" parameterType="FileVO">
		
			INSERT INTO LETTNFILEDETAIL
			( ATCH_FILE_ID, FILE_SN, FILE_STRE_COURS, STRE_FILE_NM, 
			  ORIGNL_FILE_NM, FILE_EXTSN, FILE_SIZE, FILE_CN )
			VALUES
			( #{atchFileId}, #{fileSn}, #{fileStreCours}, #{streFileNm}, 
			  #{orignlFileNm}, #{fileExtsn}, #{fileMg}, #{fileCn} )			
		
	</insert>	
	
	<delete id="FileManageDAO.deleteFileDetail" parameterType="FileVO">
		
			DELETE FROM LETTNFILEDETAIL
			WHERE
				ATCH_FILE_ID = #{atchFileId}
			AND	
				FILE_SN = #{fileSn}			
			
	</delete>
 	
	<select id="FileManageDAO.getMaxFileSN" parameterType="FileVO" resultType="java.lang.Integer">
		
			SELECT COALESCE(MAX(FILE_SN),0)+1 AS FILE_SN
			FROM LETTNFILEDETAIL
			WHERE ATCH_FILE_ID =  #{atchFileId}		
		
	</select>

 	<select id="FileManageDAO.selectFileInf" parameterType="FileVO" resultMap="fileDetail">
 		
			SELECT 
				ATCH_FILE_ID, FILE_CN, FILE_SN, FILE_STRE_COURS, STRE_FILE_NM,
				FILE_EXTSN, ORIGNL_FILE_NM, FILE_SIZE
			FROM
				LETTNFILEDETAIL
			WHERE
				ATCH_FILE_ID = #{atchFileId}
			AND 
				FILE_SN = #{fileSn}	
 		
 	</select>

	<update id="FileManageDAO.deleteCOMTNFILE" parameterType="FileVO">
		
			UPDATE LETTNFILE
			SET USE_AT = 'N'
			WHERE ATCH_FILE_ID = #{atchFileId}
		
	</update>

 	<select id="FileManageDAO.selectFileListByFileNm" parameterType="FileVO" resultMap="fileList">
 		
			SELECT 
				a.ATCH_FILE_ID, b.FILE_CN, b.FILE_SN, b.FILE_STRE_COURS, b.STRE_FILE_NM,
				b.FILE_EXTSN, b.ORIGNL_FILE_NM, b.FILE_SIZE, a.CREAT_DT
			FROM
				LETTNFILE a, LETTNFILEDETAIL b
			WHERE
				a.ATCH_FILE_ID = b.ATCH_FILE_ID
			AND 
				a.USE_AT = 'Y'
 		
			<if test="searchCnd == &quot;streFileNm&quot;">AND
					b.STRE_FILE_NM LIKE CONCAT ('%', #{searchWrd},'%') 		
			</if>
			<if test="searchCnd == &quot;orignlFileNm&quot;">AND
					b.ORIGNL_FILE_NM LIKE CONCAT ('%', #{searchWrd},'%') 		
			</if>	
					
			ORDER BY a.ATCH_FILE_ID, b.FILE_SN	
			LIMIT #{recordCountPerPage} OFFSET #{firstIndex}
				 		
 	</select>

 	<select id="FileManageDAO.selectFileListCntByFileNm" parameterType="FileVO" resultType="java.lang.Integer">
 		
			SELECT 
				COUNT(a.ATCH_FILE_ID)
			FROM
				LETTNFILE a, LETTNFILEDETAIL b
			WHERE
				a.ATCH_FILE_ID = b.ATCH_FILE_ID
			AND 
				a.USE_AT = 'Y'				
 		
			<if test="searchCnd == &quot;streFileNm&quot;">AND
					b.STRE_FILE_NM LIKE CONCAT ('%', #{searchWrd},'%') 		
			</if>
			<if test="searchCnd == &quot;orignlFileNm&quot;">AND
					b.ORIGNL_FILE_NM LIKE CONCAT ('%', #{searchWrd},'%') 		
			</if>	 		
 	</select>
 	
  	<select id="FileManageDAO.selectImageFileList" parameterType="FileVO" resultMap="fileList">
 		
			SELECT 
				a.ATCH_FILE_ID, b.FILE_CN, b.FILE_SN, b.FILE_STRE_COURS, b.STRE_FILE_NM,
				b.FILE_EXTSN, b.ORIGNL_FILE_NM, b.FILE_SIZE, a.CREAT_DT
			FROM
				LETTNFILE a, LETTNFILEDETAIL b
			WHERE
				a.ATCH_FILE_ID = #{atchFileId}
			AND 
				a.ATCH_FILE_ID = b.ATCH_FILE_ID
			AND
				UPPER(b.FILE_EXTSN) IN ('GIF','JPG','BMP','PNG')
			AND 
				a.USE_AT = 'Y'				
			ORDER BY b.FILE_SN	
 		
 	</select>	
 	
</mapper>