<?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.backendsys.mapper.Ai.AiChatHistoryMapper">

    <sql id="includeAiChatHistory">
        ath.id id,
        ath.id chat_history_id,
        ath.history_code history_code,
        ath.user_id user_id,
        COALESCE(ath.robot_code, '') robot_code,
        ath.last_content last_content,
        ath.create_time create_time,
        ath.update_time update_time
    </sql>
    <resultMap id="resultMapAiChatHistory" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="chat_history_id" column="chat_history_id" javaType="java.lang.Long"/>
        <result property="history_code" column="history_code" />
        <result property="user_id" column="user_id" javaType="java.lang.Long"/>
        <result property="robot_code" column="robot_code" />
        <result property="last_content" column="last_content" />
        <result property="chat_count" column="chat_count" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryAiChatHistory" resultMap="resultMapAiChatHistory">
        SELECT <include refid="includeAiChatHistory" />, COUNT(at.id) chat_count
        FROM ai_chat_history ath
            LEFT JOIN ai_chat at ON at.history_code = ath.history_code
        <where>
            <if test="user_id != null and user_id != ''">
                AND ath.user_id = #{user_id}
            </if>
            <if test="history_code != null and history_code != ''">
                AND ath.history_code = #{history_code}
            </if>
            <if test="robot_code != null and robot_code != ''">
                AND ath.robot_code = #{robot_code}
            </if>
        </where>
        GROUP BY ath.id, ath.history_code
        ORDER BY ath.update_time DESC
    </select>

    <!-- 查询历史记录下的 对话数量 -->
    <select id="queryAiChatHistoryByCode" resultType="java.util.LinkedHashMap">
        SELECT <include refid="includeAiChatHistory" />
        FROM ai_chat_history ath
        WHERE ath.history_code = #{history_code}
    </select>

    <insert id="insertAiChatHistory" parameterType="com.backendsys.entity.Ai.AiChatHistoryDTO"
        useGeneratedKeys="true" keyProperty="history_code">
        INSERT INTO ai_chat_history (history_code, last_content
        <if test="user_id != null and user_id != ''">, user_id</if>
        <if test="robot_code != null and robot_code != ''">, robot_code</if>
        )
        VALUES (#{history_code}, #{last_content}
        <if test="user_id != null and user_id != ''">, #{user_id}</if>
        <if test="robot_code != null and robot_code != ''">, #{robot_code}</if>
        )
    </insert>

    <!-- <update id="updateAiChatHistory" parameterType="com.backendsys.entity.Ai.AiChatHistoryDTO"
        useGeneratedKeys="true" keyProperty="history_code">
        UPDATE ai_chat_history
        SET content = #{content}
            <if test="user_nickname != null and user_nickname != ''">, user_nickname = #{user_nickname}</if>
            <if test="user_avatar != null and user_avatar != ''">, user_avatar = #{user_avatar}</if>
        WHERE history_code = #{history_code}
    </update> -->

    <delete id="deleteAiChatHistory">
        DELETE FROM ai_chat WHERE history_code = #{history_code};
        DELETE FROM ai_chat_history WHERE history_code = #{history_code};
    </delete>

</mapper>