<?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.goldenwater.gx.mapper.DocFileMapper">

    <resultMap type="com.goldenwater.gx.domain.DocFile" id="DocFileMap">
        <id property="id" column="ID" />
        <result property="dirId" column="DIR_ID" />
        <result property="fileName" column="FILE_NAME" />
        <result property="filePath" column="FILE_PATH" />
        <result property="fileSize" column="FILE_SIZE" />
        <result property="fileType" column="FILE_TYPE" />
        <result property="fileExt" column="FILE_EXT" />
        <result property="uploadBy" column="UPLOAD_BY" />
        <result property="uploadTime" column="UPLOAD_TIME" />
        <result property="status" column="STATUS" />
        <result property="remark" column="REMARK" />
        <result property="createBy" column="CREATE_BY" />
        <result property="createTime" column="CREATE_TIME" />
        <result property="updateBy" column="UPDATE_BY" />
        <result property="updateTime" column="UPDATE_TIME" />
        <result property="dirName" column="DIR_NAME" />
        <result property="uploadByName" column="UPLOAD_BY_NAME" />
    </resultMap>

    <select id="selectFileList" resultMap="DocFileMap">
        SELECT f.ID, f.DIR_ID, f.FILE_NAME, f.FILE_PATH, f.FILE_SIZE, f.FILE_TYPE, f.FILE_EXT,
               f.UPLOAD_BY, f.UPLOAD_TIME, f.STATUS, f.REMARK, f.CREATE_BY, f.CREATE_TIME,
               f.UPDATE_BY, f.UPDATE_TIME, d.DIR_NAME, u.NICK_NAME AS UPLOAD_BY_NAME
        FROM DOC_FILE f
        LEFT JOIN DOC_DIRECTORY d ON f.DIR_ID = d.ID
        LEFT JOIN SYS_USER u ON f.UPLOAD_BY = u.USER_NAME
        WHERE f.STATUS = '0'
          AND f.DIR_ID = #{dirId}
        ORDER BY f.UPLOAD_TIME DESC
    </select>

    <select id="selectFileById" resultMap="DocFileMap">
        SELECT ID, DIR_ID, FILE_NAME, FILE_PATH, FILE_SIZE, FILE_TYPE, FILE_EXT,
               UPLOAD_BY, UPLOAD_TIME, STATUS, REMARK, CREATE_BY, CREATE_TIME,
               UPDATE_BY, UPDATE_TIME
        FROM DOC_FILE
        WHERE ID = #{id}
    </select>

    <insert id="insertFile" parameterType="com.goldenwater.gx.domain.DocFile">
        INSERT INTO DOC_FILE (DIR_ID, FILE_NAME, FILE_PATH, FILE_SIZE, FILE_TYPE, FILE_EXT,
                              UPLOAD_BY, UPLOAD_TIME, STATUS, REMARK, CREATE_BY, CREATE_TIME)
        VALUES (#{dirId}, #{fileName}, #{filePath}, #{fileSize}, #{fileType}, #{fileExt},
                #{uploadBy}, SYSDATE, #{status}, #{remark}, #{createBy}, SYSDATE)
    </insert>

    <update id="updateFile" parameterType="com.goldenwater.gx.domain.DocFile">
        UPDATE DOC_FILE
        SET FILE_NAME = #{fileName},
            FILE_PATH = #{filePath},
            FILE_SIZE = #{fileSize},
            FILE_TYPE = #{fileType},
            FILE_EXT = #{fileExt},
            UPLOAD_BY = #{uploadBy},
            UPLOAD_TIME = SYSDATE,
            STATUS = #{status},
            REMARK = #{remark},
            UPDATE_BY = #{updateBy},
            UPDATE_TIME = SYSDATE
        WHERE ID = #{id}
    </update>

    <delete id="deleteFileById" parameterType="Long">
        DELETE FROM DOC_FILE WHERE ID = #{id}
    </delete>

    <select id="countByDirId" resultType="int">
        SELECT COUNT(*) FROM DOC_FILE WHERE DIR_ID = #{dirId} AND STATUS = '0'
    </select>
</mapper>
