<?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.tencent.supersonic.chat.server.persistence.mapper.ChatMapper">

    <resultMap id="IntelligentConversion" type="com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO">
        <id column="chat_id" property="chatId"/>
        <result column="agent_id" property="agentId"/>
        <result column="chat_name" property="chatName"/>
        <result column="create_time" property="createTime"/>
        <result column="last_time" property="lastTime"/>
        <result column="creator" property="creator"/>
        <result column="last_question" property="lastQuestion"/>
        <result column="is_delete" property="isDelete"/>
        <result column="is_top" property="isTop"/>
    </resultMap>

    <select id="getAll" resultMap="IntelligentConversion">
        select *
        from s2_chat
        where creator = #{creator}
          and is_delete = 0
        <if test="agentId != null">
            and agent_id = #{agentId}
        </if>
        order by is_top desc, last_time desc
    </select>

    <update id="updateConversionIsTop">
        update s2_chat
        set is_top=#{isTop}
        where chat_id = #{chatId}
    </update>

    <update id="updateChatName">
        update s2_chat
        set chat_name=#{chatName},
            last_time=#{lastTime}
        where chat_id = #{chatId}
          and creator = #{creator}
    </update>

    <update id="updateLastQuestion">
        update s2_chat
        set last_question = #{lastQuestion},
            last_time = #{lastTime}
        where chat_id = #{chatId}
    </update>

    <insert id="createChat" parameterType="com.tencent.supersonic.chat.server.persistence.dataobject.ChatDO"  useGeneratedKeys="true" keyProperty="chatId">
        insert into s2_chat
        (agent_id, chat_name, create_time, last_time, creator, last_question, is_delete, is_top)
        values (#{agentId}, #{chatName}, #{createTime}, #{lastTime}, #{creator}, #{lastQuestion}, #{isDelete}, #{isTop})
    </insert>

    <update id="deleteChat">
        update s2_chat
        set is_delete=1
        where chat_id = #{chatId}
          and creator = #{userName}
    </update>


    <resultMap id="IntelligentQuery" type="com.tencent.supersonic.chat.server.persistence.dataobject.QueryDO">
        <id column="id" property="id"/>
        <result column="question_id" property="questionId"/>
        <result column="create_time" property="createTime"/>
        <result column="time" property="time"/>
        <result column="user_name" property="userName"/>
        <result column="question" property="question"/>
        <result column="query_result" property="queryResults"/>
        <result column="state" property="state"/>
        <result column="data_content" property="dataContent"/>
        <result column="name" property="name"/>
        <result column="query_type" property="queryType"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="module" property="module"/>
        <result column="aggregator" property="aggregator"/>
        <result column="top_num" property="topNum"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="query_sql" property="querySql"/>
        <result column="score" property="score"/>
        <result column="feedback" property="feedback"/>
        <result column="chat_id" property="chatId"/>
    </resultMap>

    <update id="updateFeedback" parameterType="com.tencent.supersonic.chat.server.persistence.dataobject.QueryDO">
        update s2_chat_query
        set score=#{score},
            feedback=#{feedback}
        where question_id = #{id}
    </update>

    <insert id="createQuery" parameterType="com.tencent.supersonic.chat.server.persistence.dataobject.QueryDO"
            useGeneratedKeys="true" keyProperty="id">
        insert into s2_chat_query
        (question_id, create_time, user_name, question, query_result, time, state, data_content, name, query_type,
         is_deleted, module, chat_id, aggregator, top_num, start_time, end_time, query_sql, columns, main_entity, score,
         feedback)
        values (#{questionId}, #{createTime}, #{userName}, #{question}, #{queryResults}, #{time}, #{state},
                #{dataContent}, #{name}, #{queryType}, #{isDeleted}, #{module}, #{chatId}, #{aggregator}, #{topNum},
                #{startTime}, #{endTime}, #{querySql}, #{QueryColumn}, #{EntityInfo}, #{score}, #{feedback})

    </insert>

    <select id="queryInfo" resultMap="IntelligentQuery">
        select *
        from s2_chat_query
        where user_name = #{userName}
          and chat_id = #{chatId}
          and is_deleted = 0
        order by time DESC, query_type DESC
    </select>

</mapper>