<?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.gangquan360.smartadmin.module.file.FileDao">
    <resultMap id="fileMap"
               type="com.gangquan360.smartadmin.module.file.domain.entity.FileEntity"></resultMap>
    <resultMap id="fileDTO"
               type="com.gangquan360.smartadmin.module.file.domain.dto.FileDTO"></resultMap>

    <resultMap id="fileVO" type="com.gangquan360.smartadmin.module.file.domain.vo.FileVO"></resultMap>

    <insert id="insertFileBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO t_file (
        module_id,
        module_type,
        file_name,
        file_size,
        file_type,
        file_path,
        creater_user,
        file_location_type
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.moduleId},#{item.moduleType},#{item.fileName},#{item.fileSize},#{item.fileType},#{item.filePath},#{item.createrUser},#{item.createrUserType},#{item.fileLocationType})
        </foreach>
    </insert>

    <insert id="insertFileEntityBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO t_file (
        module_id,
        module_type,
        file_name,
        file_size,
        file_type,
        file_path,
        creater_user,
        file_location_type
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.moduleId},#{item.moduleType},#{item.fileName},#{item.fileSize},#{item.fileType},#{item.filePath},#{item.createrUser},#{item.createrUserType},#{item.fileLocationType})
        </foreach>
    </insert>

    <delete id="deleteFilesByModuleId">
        DELETE FROM t_file WHERE module_id =#{moduleId}
    </delete>

    <delete id="deleteFilesByModuleIdAndModuleType">
        DELETE FROM t_file WHERE module_id =#{moduleId} and module_type=#{moduleType}
    </delete>

    <select id="listFilesByModuleId" resultMap="fileVO">
        SELECT * FROM t_file WHERE module_id =#{moduleId}
    </select>

    <select id="listFilesByModuleIdAndModuleType" resultMap="fileVO">
        SELECT * FROM t_file WHERE module_id =#{moduleId} and module_type=#{moduleType}
    </select>

    <select id="listFilesByModuleIdAndModuleTypes" resultMap="fileVO">
        SELECT * FROM t_file WHERE module_id =#{moduleId} and module_type IN
        <foreach collection="moduleTypes" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>

    <select id="listFilesByModuleIdsAndModuleType" resultMap="fileVO">
        SELECT * FROM t_file WHERE
        module_id IN
        <foreach collection="moduleIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and module_type=#{moduleType}
    </select>


    <select id="listFilesByFileIds" resultMap="fileVO">
        SELECT * FROM t_file WHERE id IN
        <foreach collection="fileIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>
    <select id="queryListByPage" resultMap="fileVO">
        SELECT * FROM t_file
        <where>
            <if test="queryDTO.moduleType != null and queryDTO.moduleType != ''">
                AND module_type = #{queryDTO.moduleType}
            </if>
            <if test="queryDTO.fileName != null and queryDTO.fileName != '' ">
                AND INSTR(file_name,#{queryDTO.fileName})
            </if>
            <if test="queryDTO.fileLocationType != null ">
                AND file_location_type = #{queryDTO.fileLocationType}
            </if>
        </where>
        order by create_time desc
    </select>

</mapper>