<?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.fs.chat.mapper.ChatDatasetFileMapper">
    
    <resultMap type="ChatDatasetFile" id="ChatDatasetFileResult">
        <result property="fileId"    column="file_id"    />
        <result property="datasetId"    column="dataset_id"    />
        <result property="roleId"    column="role_id"    />
        <result property="extId"    column="ext_id"    />
        <result property="fileName"    column="file_name"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectChatDatasetFileVo">
        select file_id, dataset_id, role_id, ext_id, file_name, create_time from chat_dataset_file
    </sql>

    <select id="selectChatDatasetFileList" parameterType="ChatDatasetFile" resultMap="ChatDatasetFileResult">
        <include refid="selectChatDatasetFileVo"/>
        <where>  
            <if test="datasetId != null "> and dataset_id = #{datasetId}</if>
            <if test="roleId != null "> and role_id = #{roleId}</if>
            <if test="extId != null  and extId != ''"> and ext_id = #{extId}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
        </where>
    </select>
    
    <select id="selectChatDatasetFileByFileId" parameterType="Long" resultMap="ChatDatasetFileResult">
        <include refid="selectChatDatasetFileVo"/>
        where file_id = #{fileId}
    </select>
        
    <insert id="insertChatDatasetFile" parameterType="ChatDatasetFile" useGeneratedKeys="true" keyProperty="fileId">
        insert into chat_dataset_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="datasetId != null">dataset_id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="extId != null">ext_id,</if>
            <if test="fileName != null">file_name,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="datasetId != null">#{datasetId},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="extId != null">#{extId},</if>
            <if test="fileName != null">#{fileName},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateChatDatasetFile" parameterType="ChatDatasetFile">
        update chat_dataset_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="datasetId != null">dataset_id = #{datasetId},</if>
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="extId != null">ext_id = #{extId},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where file_id = #{fileId}
    </update>

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

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