<?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.ruoyi.system.mapper.FloodKnowledgeFileMapper">
    
    <resultMap type="FloodKnowledgeFile" id="FloodKnowledgeFileResult">
        <result property="fileId"         column="file_id"         />
        <result property="knowledgeId"    column="knowledge_id"    />
        <result property="fileName"       column="file_name"       />
        <result property="originalName"   column="original_name"   />
        <result property="filePath"       column="file_path"       />
        <result property="fileType"       column="file_type"       />
        <result property="fileSize"       column="file_size"       />
        <result property="fileUrl"        column="file_url"        />
        <result property="sortOrder"       column="sort_order"     />
        <result property="status"         column="status"         />
        <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="remark"        column="remark"         />
    </resultMap>

    <sql id="selectFloodKnowledgeFileVo">
        select file_id, knowledge_id, file_name, original_name, file_path, file_type, file_size, file_url, sort_order, status, create_by, create_time, update_by, update_time, remark 
        from flood_knowledge_file
    </sql>

    <select id="selectFloodKnowledgeFileList" parameterType="FloodKnowledgeFile" resultMap="FloodKnowledgeFileResult">
        <include refid="selectFloodKnowledgeFileVo"/>
        <where>  
            <if test="knowledgeId != null">
                and knowledge_id = #{knowledgeId}
            </if>
            <if test="fileName != null and fileName != ''">
                and file_name like concat('%', #{fileName}, '%')
            </if>
            <if test="fileType != null and fileType != ''">
                and file_type = #{fileType}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
        </where>
        order by sort_order, create_time desc
    </select>
    
    <select id="selectFloodKnowledgeFileListByKnowledgeId" parameterType="Long" resultMap="FloodKnowledgeFileResult">
        <include refid="selectFloodKnowledgeFileVo"/>
        where knowledge_id = #{knowledgeId} and status = '0'
        order by sort_order, create_time desc
    </select>
    
    <select id="selectFloodKnowledgeFileByFileId" parameterType="Long" resultMap="FloodKnowledgeFileResult">
        <include refid="selectFloodKnowledgeFileVo"/>
        where file_id = #{fileId}
    </select>
        
    <insert id="insertFloodKnowledgeFile" parameterType="FloodKnowledgeFile" useGeneratedKeys="true" keyProperty="fileId">
        insert into flood_knowledge_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="knowledgeId != null">knowledge_id,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="originalName != null and originalName != ''">original_name,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="fileType != null">file_type,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileUrl != null">file_url,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="knowledgeId != null">#{knowledgeId},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="originalName != null and originalName != ''">#{originalName},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="fileType != null">#{fileType},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateFloodKnowledgeFile" parameterType="FloodKnowledgeFile">
        update flood_knowledge_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="knowledgeId != null">knowledge_id = #{knowledgeId},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="originalName != null and originalName != ''">original_name = #{originalName},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="fileType != null">file_type = #{fileType},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where file_id = #{fileId}
    </update>

    <delete id="deleteFloodKnowledgeFileByFileId" parameterType="Long">
        delete from flood_knowledge_file where file_id = #{fileId}
    </delete>

    <delete id="deleteFloodKnowledgeFileByFileIds" parameterType="String">
        delete from flood_knowledge_file where file_id in 
        <foreach item="fileId" collection="array" open="(" separator="," close=")">
            #{fileId}
        </foreach>
    </delete>

    <delete id="deleteFloodKnowledgeFileByKnowledgeId" parameterType="Long">
        delete from flood_knowledge_file where knowledge_id = #{knowledgeId}
    </delete>
</mapper>
