<?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.ChatDatasetMapper">

    <resultMap type="ChatDataset" id="ChatDatasetResult">
        <result property="datasetId"    column="dataset_id"    />
        <result property="roleId"    column="role_id"    />
        <result property="datasetName"    column="dataset_name"    />
        <result property="extId"    column="ext_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectChatDatasetVo">
        select dataset_id, role_id, dataset_name, ext_id, create_time,company_id from chat_dataset
    </sql>

    <select id="selectChatDatasetList" parameterType="ChatDataset" resultMap="ChatDatasetResult">
        <include refid="selectChatDatasetVo"/>
        <where>
            <if test="roleId != null "> and role_id = #{roleId}</if>
            <if test="datasetName != null  and datasetName != ''"> and dataset_name like concat('%', #{datasetName}, '%')</if>
            <if test="extId != null  and extId != ''"> and ext_id = #{extId}</if>
            <if test="companyId != null  and companyId != ''"> and company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectChatDatasetByDatasetId" parameterType="Long" resultMap="ChatDatasetResult">
        <include refid="selectChatDatasetVo"/>
        where dataset_id = #{datasetId}
    </select>

    <insert id="insertChatDataset" parameterType="ChatDataset" useGeneratedKeys="true" keyProperty="datasetId">
        insert into chat_dataset
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="roleId != null">role_id,</if>
            <if test="datasetName != null">dataset_name,</if>
            <if test="extId != null">ext_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="roleId != null">#{roleId},</if>
            <if test="datasetName != null">#{datasetName},</if>
            <if test="extId != null">#{extId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateChatDataset" parameterType="ChatDataset">
        update chat_dataset
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="datasetName != null">dataset_name = #{datasetName},</if>
            <if test="extId != null">ext_id = #{extId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where dataset_id = #{datasetId}
    </update>

    <delete id="deleteChatDatasetByDatasetId" parameterType="Long">
        delete from chat_dataset where dataset_id = #{datasetId}
    </delete>

    <delete id="deleteChatDatasetByDatasetIds" parameterType="String">
        delete from chat_dataset where dataset_id in
        <foreach item="datasetId" collection="array" open="(" separator="," close=")">
            #{datasetId}
        </foreach>
    </delete>
</mapper>
