<?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="cokr.xit.base.file.dao.FileMapper">

<resultMap id="fileRow" type="cokr.xit.base.file.FileInfo">
	<result property="id" column="FILE_ID"/>
	<result property="infoType" column="INF_TYPE"/>
	<result property="infoKey" column="INF_KEY"/>
	<result property="subType" column="SUB_TYPE"/>
	<result property="name" column="FILE_NM"/>
	<result property="path" column="FILE_PATH"/>
	<result property="url" column="URL"/>
	<result property="mimeType" column="MIME_TYPE"/>
	<result property="size" column="FILE_SIZE"/>
	<result property="downloadCount" column="DNLD_CNT"/>
	<result property="sortOrder" column="SRT_ORD"/>
	<result property="createdAt" column="REG_DT"/>
	<result property="createdBy" column="RGTR"/>
	<result property="useYN" column="USE_YN"/>
</resultMap>

<sql id="fileDir">CONCAT('files/', DIR, DATE_FORMAT(CURRENT_DATE, '/%Y/%m/%d/'))</sql>
<sql id="fileUrl">FILE_PATH</sql>

<sql id="fileDirs">
SELECT INF_TYPE
     ,<include refid="cokr.xit.base.file.dao.FileMapper.fileDir" /> DIR
  FROM (
	   SELECT '010' INF_TYPE, 'interface/attachment/smg' DIR UNION       <!-- 국민 신문고 -->
	   SELECT '020' INF_TYPE, 'interface/attachment/saeol' DIR UNION     <!-- 세올 -->
	   SELECT '030' INF_TYPE, 'interface/attachment/epost' DIR UNION     <!-- epost -->

	   SELECT '100' INF_TYPE, 'attachment/violation' DIR UNION <!-- 단속 정보 -->
	   SELECT '110' INF_TYPE, 'attachment/opinion' DIR UNION   <!-- 의견 진술 -->
	   SELECT '120' INF_TYPE, 'attachment/rent' DIR UNION   <!-- 렌트카 첨부파일 -->
	   SELECT '190' INF_TYPE, 'attachment/capture' DIR         <!-- 캡쳐용 동영상 -->
       ) FILE_DIRS
</sql>

<sql id="selectFiles">
<if test="fileIDs != null">
SELECT A.FILE_ID
     , A.INF_TYPE
     , A.INF_KEY
     , A.SUB_TYPE
     , A.FILE_NM
     , A.FILE_PATH
     , A.MIME_TYPE
     , A.FILE_SIZE
     , A.DNLD_CNT
     , A.SRT_ORD
     , A.USE_YN
     , A.REG_DT
     , A.RGTR
     ,<include refid="cokr.xit.base.file.dao.FileMapper.fileUrl" /> AS URL
  FROM TB_FILE A
 WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)
 ORDER BY FILE_ID
</if>
<if test="fileIDs == null">
<include refid="utility.paging-prefix" />
SELECT A.FILE_ID
     , A.INF_TYPE
     , A.INF_KEY
     , A.SUB_TYPE
     , A.FILE_NM
     , A.FILE_PATH
     , A.MIME_TYPE
     , A.FILE_SIZE
     , A.DNLD_CNT
     , A.SRT_ORD
     , A.USE_YN
     , A.REG_DT
     , A.RGTR
     ,<include refid="cokr.xit.base.file.dao.FileMapper.fileUrl" /> AS URL
  FROM TB_FILE A
<where>
<if test="infoType != null">  AND A.INF_TYPE = #{infoType}</if>
<if test="infoKeys != null">  AND INF_KEY IN (<foreach collection="infoKeys" item="infoKey" separator=",">#{infoKey}</foreach>)</if>
   AND USE_YN = 'Y'
</where>
<include refid="utility.orderBy" />
<include refid="utility.paging-suffix" />
</if>
</sql>

<select id="getFileList" parameterType="map" resultType="dataobject">
/* 파일 목록 조회(fileMapper.getFileList) */
<include refid="selectFiles" />
</select>

<select id="getFilesOf" parameterType="map" resultMap="fileRow">
/* 파일 가져오기(fileMapper.getFilesOf) */
<include refid="selectFiles" />
</select>

<select id="getFiles" parameterType="map" resultMap="fileRow">
/* 파일 가져오기(fileMapper.getFiles) */
<include refid="selectFiles" />
</select>

<insert id="insertFile" parameterType="map">
/* 파일 등록(fileMapper.insertFile) */
<selectKey keyProperty="file.id,file.path" keyColumn="NEW_ID,PATH" resultType="map" order="BEFORE">
SELECT NEW_ID
     , CONCAT(DIR, NEW_ID, '.', #{file.extension}) PATH
  FROM (
       SELECT NVL(MAX(FILE_ID) + 1, CONCAT(THIS_DAY, '00001')) NEW_ID
         FROM (<include refid="utility.selectThisDay" />) B 
         LEFT OUTER JOIN TB_FILE A ON FILE_ID LIKE CONCAT(THIS_DAY, '%')
       ) T1
       , (
       <include refid="fileDirs" />
        WHERE INF_TYPE = #{file.infoType}
       ) T2
</selectKey>
INSERT INTO TB_FILE (
       FILE_ID
     , INF_TYPE
     , INF_KEY
     , SUB_TYPE
     , FILE_NM
     , FILE_PATH
     , MIME_TYPE
     , FILE_SIZE
     , DNLD_CNT
     , SRT_ORD
     , RGTR
     , REG_DT
     , USE_YN
) VALUES (
       #{file.id}
     , #{file.infoType}
     , #{file.infoKey}
     , #{file.subType}
     , #{file.name}
     , #{file.path}
     , #{file.mimeType}
     , #{file.size}
     , #{file.downloadCount}
     , #{file.sortOrder}
     , #{currentUser.id}
     ,<include refid="utility.now" />
     , 'Y'
)
</insert>

<update id="reorder" parameterType="map">
/* 파일 순서 변경(fileMapper.reorder) */
UPDATE TB_FILE 
   SET SRT_ORD = CASE FILE_ID
   <foreach collection="fileIDs" item="fileID" index="index" separator=" ">
                 WHEN #{fileID} THEN #{index}
   </foreach>
                 ELSE SRT_ORD END
 WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)
</update>

<update id="updateDownloadCount" parameterType="map">
/* 다운로드 횟수 증가(fileMapper.updateDownloadCount) */
UPDATE TB_FILE SET
       DNLD_CNT = DNLD_CNT + 1
 WHERE USE_YN = 'Y'
   AND FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)
</update>

<update id="removeFiles" parameterType="map">
/* 파일 제거(fileMapper.removeFiles) */
UPDATE TB_FILE 
   SET USE_YN = 'N'
 WHERE USE_YN = 'Y'
<if test="fileIDs != null">  AND FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)</if>
<if test="infoKeys != null">
   AND INF_TYPE = #{infoType}
   AND INF_KEY IN (<foreach collection="infoKeys" item="infoKey" separator=",">#{infoKey}</foreach>)</if>
</update>

<delete id="deleteFiles" parameterType="map">/* 파일 삭제(fileMapper.deleteFiles) */
DELETE 
  FROM TB_FILE
<if test="fileIDs != null">WHERE FILE_ID IN (<foreach collection="fileIDs" item="fileID" separator=",">#{fileID}</foreach>)</if>
</delete>

</mapper>