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

    <sql id="includeAiChat">
        id id,
        id chat_id,
        history_code,
        user_id,
        COALESCE(user_nickname, '') user_nickname,
        COALESCE(user_avatar, '') user_avatar,
        COALESCE(robot_code, '') robot_code,
        role,
        content,
        create_time,
        update_time
    </sql>
    <resultMap id="resultMapAiChat" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="chat_id" column="chat_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="user_nickname" column="user_nickname" />
        <result property="user_avatar" column="user_avatar" />
        <result property="robot_code" column="robot_code" />
        <result property="role" column="role" />
        <result property="content" column="content" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryAiChatList" resultMap="resultMapAiChat">
        SELECT
        <include refid="includeAiChat" />
        FROM ai_chat
        <where>
            <if test="history_code != null and history_code != ''">
                AND history_code = #{history_code}
            </if>
            <if test="user_id != null and user_id != ''">
                AND user_id = #{user_id}
            </if>
            <if test="robot_code != null and robot_code != ''">
                AND robot_code = #{robot_code}
            </if>
            <if test="role != null and role != ''">
                AND role = #{role}
            </if>
        </where>
        ORDER BY update_time ASC
    </select>

    <insert id="insertAiChat" parameterType="com.backendsys.entity.Ai.AiChatDTO">
        INSERT INTO ai_chat (history_code, content
        <if test="user_id != null and user_id != ''">, user_id</if>
        <if test="user_nickname != null and user_nickname != ''">, user_nickname</if>
        <if test="user_avatar != null and user_avatar != ''">, user_avatar</if>
        <if test="robot_code != null and robot_code != ''">, robot_code</if>
        <if test="role != null and role != ''">, role</if>
        )
        VALUES (#{history_code}, #{content}
        <if test="user_id != null and user_id != ''">, #{user_id}</if>
        <if test="user_nickname != null and user_nickname != ''">, #{user_nickname}</if>
        <if test="user_avatar != null and user_avatar != ''">, #{user_avatar}</if>
        <if test="robot_code != null and robot_code != ''">, #{robot_code}</if>
        <if test="role != null and role != ''">, #{role}</if>
        )
    </insert>

    <!-- <update id="updateAiChat" parameterType="com.backendsys.entity.Ai.AiChatDTO"
        useGeneratedKeys="true" keyProperty="history_code">
        UPDATE ai_chat
        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="deleteAiChat">
        DELETE FROM ai_chat WHERE history_code = #{history_code}
    </delete>

</mapper>