<?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.cm.common.plugin.dao.file.IFileDao">

    <cache/>

    <resultMap id="filePO" type="com.cm.common.pojo.pos.FilePO">
        <id property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="filePath" column="file_path"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileType" column="file_type"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileSummary" column="file_summary"/>
        <result property="isBack" column="is_back"/>
        <result property="creator" column="creator"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="modifier" column="modifier"/>
        <result property="gmtModified" column="gmt_modified"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <resultMap id="fileDTO" type="com.cm.common.pojo.dtos.FileDTO">
        <id property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileType" column="file_type"/>
        <result property="fileSize" column="file_size"/>
    </resultMap>

    <resultMap id="fileInfoDTO" type="com.cm.common.plugin.pojo.dtos.file.FileInfoDTO" extends="fileDTO">
        <id property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileType" column="file_type"/>
        <result property="fileSize" column="file_size"/>
        <result property="filePath" column="file_path"/>
        <result property="fileSummary" column="file_summary"/>
        <result property="isBack" column="is_back"/>
    </resultMap>

    <resultMap id="fileInfoWithPathDTO" type="com.cm.common.plugin.pojo.dtos.file.FileInfoWithPathDTO" extends="fileInfoDTO">
        <id property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileType" column="file_type"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileSummary" column="file_summary"/>
        <result property="filePath" column="file_path"/>
        <result property="isBack" column="is_back"/>
    </resultMap>

    <!-- 保存文件 -->
    <insert id="saveFile" parameterType="map" flushCache="true">
        INSERT INTO sys_file(
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{fileId},
            #{fileName},
            #{filePath},
            #{fileUrl},
            #{fileType},
            #{fileSize},
            #{fileSummary},
            #{isBack},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除文件 -->
    <update id="removeFile" parameterType="map" flushCache="true">
        UPDATE
            sys_file
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            file_id IN
        <foreach collection="fileIds" index="index" open="(" separator="," close=")">
            #{fileIds[${index}]}
        </foreach>
    </update>

    <!-- 删除文件(物理删除) -->
    <delete id="deleteFile" parameterType="map" flushCache="true">
        DELETE FROM
            sys_file
        WHERE
            file_id IN
        <foreach collection="fileIds" index="index" open="(" separator="," close=")">
            #{fileIds[${index}]}
        </foreach>
    </delete>

    <!-- 更新文件描述 -->
    <update id="updateFileSummary" parameterType="map" flushCache="true">
        UPDATE
            sys_file
        SET
            file_summary = #{fileSummary}
        WHERE
        <if test="fileId != null and fileId != ''">
            file_id = #{fileId}
        </if>
        <if test="fileIds != null and fileIds.size > 0">
            file_id IN
            <foreach collection="fileIds" index="index" open="(" separator="," close=")">
                #{fileIds[${index}]}
            </foreach>
        </if>
    </update>

    <!-- 获取文件 -->
    <select id="getFile" parameterType="map" resultMap="filePO" useCache="true">
        SELECT
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_file
        WHERE
            is_delete = 0
        AND
            file_id = #{fileId}
    </select>

    <!-- 文件列表 -->
    <select id="listFile" parameterType="map" resultMap="fileDTO" useCache="true">
        SELECT
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_file
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            file_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="fileSummary != null and fileSummary != ''">
        AND
            file_summary = #{fileSummary}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="ids != null and ids.size > 0">
        AND
            file_id IN
            <foreach collection="ids" index="index" open="(" separator="," close=")">
            #{ids[${index}]}
            </foreach>
        </if>
        <if test="fileUrl != null and fileUrl != ''">
        AND
            file_url LIKE concat('%', #{fileUrl}, '%')
        </if>
    </select>

    <!-- 文件列表 -->
    <select id="listFileInfo" parameterType="map" resultMap="fileInfoDTO" useCache="false">
        SELECT
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_file
        WHERE
            is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND
            file_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="ids != null and ids.size > 0">
        AND
            file_id IN
            <foreach collection="ids" index="index" open="(" separator="," close=")">
                #{ids[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 获取文件列表(带路径) -->
    <select id="listFileInfoWithPath" parameterType="map" resultMap="fileInfoWithPathDTO" useCache="false">
        SELECT
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_file
        WHERE
            is_delete = 0
        <if test="fileIds != null and fileIds.size > 0">
        AND
            file_id IN
            <foreach collection="fileIds" index="index" open="(" separator="," close=")">
                #{fileIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 文件列表 -->
    <select id="listFileByMd5" parameterType="java.lang.String" resultMap="fileInfoDTO" useCache="false">
        SELECT
            file_id,
            file_name,
            file_path,
            file_url,
            file_type,
            file_size,
            file_summary,
            is_back,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        FROM
            sys_file
        WHERE
            is_delete = 0
        AND
            file_summary = #{_parameter}
    </select>

</mapper>