<?xml version="1.0" encoding="UTF-8"?><!--Converted at: Wed May 11 15:49:38 KST 2016-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="nlib.bbs.service.impl.AttachFileDAO">

    <select id="listAttachFiles" parameterType="String" resultType="AttachFileVO">
        SELECT 
              A.FILE_MASK
            , A.FILE_ID
            , A.FILE_SEQ
            , A.FILE_NAME
            , A.FILE_SIZE
            , A.FILE_TYPE_CD
            , A.DOWNLOAD_COUNT
            , A.DOWNLOAD_EXPIRE_DATE
            , A.DOWNLOAD_LIMIT_COUNT
            , DATE_FORMAT(A.REG_DATE, '%Y-%m-%d') AS REG_DD
            , A.DELETE_YN
            , A.STREAMING_URL
            , A.CONTENT
            , A.ATCH_TYPE_CD
        FROM J_ATTACHFILE A
        WHERE A.FILE_ID = #{attachFileId}
        ORDER BY A.FILE_SEQ
    </select>
 	
    <insert id="insertAttachFile" parameterType="AttachFileVO">
        INSERT INTO J_ATTACHFILE (  
              FILE_MASK
            , FILE_ID
            , FILE_SEQ
            , FILE_NAME
            , FILE_SIZE
            , FILE_TYPE_CD
            , DOWNLOAD_COUNT
            , DOWNLOAD_EXPIRE_DATE
            , DOWNLOAD_LIMIT_COUNT
            , REG_DATE
            , DELETE_YN
            , STREAMING_URL
            , CONTENT
            , ATCH_TYPE_CD
        ) VALUES (
              #{fileMask}
            , #{fileId}
            , #{fileSeq}
            , #{fileName}
            , #{fileSize}
            , #{fileTypeCd}
            , 0
            , NULL
            , NULL
            , NOW()
            , 'N'
            , NULL
            , NULL
            , #{atchTypeCd}
        )
    </insert>
    
    <update id="deleteAttachFile" parameterType="AttachFileVO">
        UPDATE J_ATTACHFILE SET DELETE_YN = 'Y'
        WHERE FILE_MASK = #{fileMask}
        AND FILE_ID = #{fileId}
        AND FILE_SEQ = #{fileSeq}
    </update>  
    
    <update id="increaseDownloadCount" parameterType="AttachFileVO">
    <![CDATA[
        UPDATE J_ATTACHFILE SET DOWNLOAD_COUNT = (IFNULL(DOWNLOAD_COUNT, 0) + 1)
        WHERE FILE_MASK = #{fileMask}
        AND FILE_ID = #{fileId}
        AND FILE_SEQ = #{fileSeq}
        AND IFNULL(DOWNLOAD_COUNT, 0) < IFNULL(DOWNLOAD_LIMIT_COUNT, (IFNULL(DOWNLOAD_COUNT, 0) + 1))
    ]]>
    </update>    
    
</mapper>
