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

  <sql id="fileSearchCondition">
    <if test="target_objid != null and target_objid != ''">
      AND TARGET_OBJID LIKE #{target_objid} || '%'
    </if>
    <if test="status != null and status != ''">
      AND STATUS = #{status}
    </if>
    <if test="keyword != null and keyword != ''">
      AND (REAL_FILE_NAME ILIKE '%' || #{keyword} || '%'
        OR SAVED_FILE_NAME ILIKE '%' || #{keyword} || '%')
    </if>
  </sql>

  <select id="getFileList" parameterType="map" resultType="map">
    SELECT
        OBJID
        , TARGET_OBJID
        , SAVED_FILE_NAME
        , REAL_FILE_NAME
        , DOC_TYPE
        , DOC_TYPE_NAME
        , FILE_SIZE
        , FILE_EXT
        , FILE_PATH
        , COMPANY_CODE
        , WRITER
        , CREATED_DATE
        , STATUS
        , IS_REPRESENTATIVE
    FROM ATTACH_FILE_INFO
    WHERE 1=1
      AND STATUS != 'DELETED'
    <include refid="common.companyCodeFilter"/>
    <include refid="fileSearchCondition"/>
    ORDER BY CREATED_DATE DESC
    <include refid="common.pagination"/>
  </select>

  <select id="getFileListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM ATTACH_FILE_INFO
    WHERE 1=1
      AND STATUS != 'DELETED'
    <include refid="common.companyCodeFilter"/>
    <include refid="fileSearchCondition"/>
  </select>

  <select id="getFileInfo" parameterType="map" resultType="map">
    SELECT
        OBJID
        , TARGET_OBJID
        , SAVED_FILE_NAME
        , REAL_FILE_NAME
        , DOC_TYPE
        , DOC_TYPE_NAME
        , FILE_SIZE
        , FILE_EXT
        , FILE_PATH
        , COMPANY_CODE
        , WRITER
        , CREATED_DATE
        , STATUS
        , IS_REPRESENTATIVE
    FROM ATTACH_FILE_INFO
    WHERE OBJID = #{objid}
    <include refid="common.companyCodeFilter"/>
    LIMIT 1
  </select>

  <insert id="insertFile" parameterType="map">
    INSERT INTO ATTACH_FILE_INFO (
        OBJID
        , TARGET_OBJID
        , SAVED_FILE_NAME
        , REAL_FILE_NAME
        , DOC_TYPE
        , DOC_TYPE_NAME
        , FILE_SIZE
        , FILE_EXT
        , FILE_PATH
        , COMPANY_CODE
        , WRITER
        , CREATED_DATE
        , STATUS
        , PARENT_TARGET_OBJID
        , IS_REPRESENTATIVE
    ) VALUES (
        #{objid}
        , #{target_objid}
        , #{saved_file_name}
        , #{real_file_name}
        , #{doc_type}
        , #{doc_type_name}
        , #{file_size}
        , #{file_ext}
        , #{file_path}
        , #{company_code}
        , #{writer}
        , NOW()
        , COALESCE(#{status}, 'ACTIVE')
        , #{parent_target_objid}
        , COALESCE(#{is_representative}, FALSE)
    )
  </insert>

  <update id="updateFile" parameterType="map">
    UPDATE ATTACH_FILE_INFO
    SET
        DOC_TYPE = COALESCE(#{doc_type}, DOC_TYPE)
        , DOC_TYPE_NAME = COALESCE(#{doc_type_name}, DOC_TYPE_NAME)
        , TARGET_OBJID = COALESCE(#{target_objid}, TARGET_OBJID)
        , IS_REPRESENTATIVE = COALESCE(#{is_representative}, IS_REPRESENTATIVE)
    WHERE OBJID = #{objid}
    <include refid="common.companyCodeFilter"/>
  </update>

  <update id="softDeleteFile" parameterType="map">
    UPDATE ATTACH_FILE_INFO
    SET STATUS = 'DELETED'
    WHERE OBJID = #{objid}
    <include refid="common.companyCodeFilter"/>
  </update>

  <select id="getFilesByTarget" parameterType="map" resultType="map">
    SELECT
        OBJID
        , TARGET_OBJID
        , SAVED_FILE_NAME
        , REAL_FILE_NAME
        , DOC_TYPE
        , DOC_TYPE_NAME
        , FILE_SIZE
        , FILE_EXT
        , FILE_PATH
        , COMPANY_CODE
        , WRITER
        , CREATED_DATE
        , STATUS
        , IS_REPRESENTATIVE
    FROM ATTACH_FILE_INFO
    WHERE TARGET_OBJID LIKE #{target_objid} || '%'
      AND STATUS = 'ACTIVE'
    <include refid="common.companyCodeFilter"/>
    ORDER BY CREATED_DATE DESC
  </select>

</mapper>
