<?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="com.interplug.qcast.biz.file.FileMapper">
    <select id="selectFile" parameterType="com.interplug.qcast.biz.file.dto.FileRequest" resultType="com.interplug.qcast.biz.file.dto.FileResponse">
        /* sqlid : com.interplug.qcast.biz.file.selectFile */
        SELECT
              TOP 1
              U.OBJECT_NO
            , U.PLAN_NO
            , U.NO
            , U.FAILE_NAME
            , U.CATEGORY
        FROM T_UPLOAD U WITH (NOLOCK)
        WHERE U.OBJECT_NO = #{objectNo}
        <if test="planNo != null and planNo != ''">
        AND U.PLAN_NO = #{planNo}
        </if>
        <if test='(planNo == null or planNo == "" ) and planNoNullChkFlg == "1"'>
        AND U.PLAN_NO IS NULL
        </if>
        <if test="no != null and no != ''">
        AND U.NO = #{no}
        </if>
        <if test="category != null and category != ''">
        AND U.CATEGORY = #{category}
        </if>
    </select>

    <select id="selectFileList" parameterType="com.interplug.qcast.biz.file.dto.FileRequest" resultType="com.interplug.qcast.biz.file.dto.FileResponse">
        /* sqlid : com.interplug.qcast.biz.file.selectFileList */
        SELECT
              U.OBJECT_NO
            , U.PLAN_NO
            , U.NO
            , U.CATEGORY
            , U.FAILE_NAME
            , U.UPLOAD__DATETIME
            , U.USER_ID
            , U.DEL_FLG
            , U.LAST_EDIT_DATETIME
            , U.LAST_EDIT_USER
        FROM T_UPLOAD U WITH (NOLOCK)
        WHERE U.OBJECT_NO = #{objectNo}
            AND U.DEL_FLG = '0'
        <if test="planNo != null and planNo != ''">
        AND U.PLAN_NO = #{planNo}
        </if>
        <if test='(planNo == null or planNo == "" ) and planNoNullChkFlg == "1"'>
        AND U.PLAN_NO IS NULL
        </if>
        <if test="no != null and no != ''">
        AND U.NO = #{no}
        </if>
        <if test="category != null and category != ''">
        AND U.CATEGORY = #{category}
        </if>
    </select>

    <insert id="insertFile" parameterType="com.interplug.qcast.biz.file.dto.FileRequest" >
        /* sqlid : com.interplug.qcast.biz.file.insertFile */
        INSERT INTO T_UPLOAD (
              OBJECT_NO
            , NO
            , CATEGORY
            , FAILE_NAME
            , UPLOAD__DATETIME
            , USER_ID
            , DEL_FLG
            , LAST_EDIT_DATETIME
            , LAST_EDIT_USER
            , PLAN_NO
        ) VALUES (
              #{objectNo}
            , (SELECT COALESCE(MAX(NO) + 1, 1) FROM T_UPLOAD WHERE OBJECT_NO = #{objectNo})
            , #{category}
            , #{faileName}
            , GETDATE()
            , #{userId}
            , 0
            , GETDATE()
            , #{userId}
            , #{planNo}
         )
    </insert>

    <update id="deleteFile" parameterType="com.interplug.qcast.biz.file.dto.FileRequest" >
    /* sqlid : com.interplug.qcast.biz.file.deleteFile */
        UPDATE T_UPLOAD
        SET
              DEL_FLG = 1
            , LAST_EDIT_DATETIME = GETDATE()
            , LAST_EDIT_USER = #{userId}
        WHERE OBJECT_NO = #{objectNo}
        <if test="planNo != null and planNo != ''">
            AND PLAN_NO = #{planNo}
        </if>
        <if test="no != null and no != ''">
            AND NO = #{no}
        </if>
    </update>

</mapper>