<?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="ink.wgink.module.file.dao.IFileDao">

    <cache/>

    <resultMap id="filePO" type="ink.wgink.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="ink.wgink.module.file.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="ink.wgink.module.file.pojo.dtos.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="fileUrl" column="file_url"/>
        <result property="filePath" column="file_path"/>
        <result property="fileSummary" column="file_summary"/>
        <result property="isBack" column="is_back"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_file` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `file_id` char(36) NOT NULL,
            `file_name` varchar(255) DEFAULT NULL COMMENT '文件名称',
            `file_path` varchar(500) DEFAULT NULL COMMENT '文件路径',
            `file_url` varchar(500) DEFAULT NULL COMMENT '文件URL',
            `file_type` varchar(20) DEFAULT NULL COMMENT '文件类型',
            `file_size` varchar(100) DEFAULT NULL COMMENT '文件大小',
            `file_summary` varchar(255) DEFAULT NULL COMMENT '文件说明',
            `is_back` int(2) DEFAULT '0',
            `creator` char(36) DEFAULT NULL COMMENT '创建人',
            `gmt_create` datetime DEFAULT NULL COMMENT '创建时间',
            `modifier` char(36) DEFAULT NULL COMMENT '修改人',
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`file_id`),
            UNIQUE KEY `file_id` (`file_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 保存文件 -->
    <insert id="save" 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="remove" 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="delete" 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="updateSummary" 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="getPO" 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="list" 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="listInfo" 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="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="listByMd5" 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>

    <!-- 文件列表 -->
    <select id="listPO" 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>
            <if test="fileIds != null and fileIds.size > 0">
            AND
                file_id IN
                <foreach collection="fileIds" index="index" open="(" separator="," close=")">
                    #{fileIds[${index}]}
                </foreach>
            </if>
        </where>
    </select>

</mapper>