<?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.modules.ai.chat.dao.ChatDao">

    <sql id="includeChat">
        id,
        history_code,
        model,
        model_version,
        role,
        COALESCE(content_type, '') content_type,
        content,
        COALESCE(duration, '') duration,
        user_id,
        COALESCE(robot_code, '') robot_code,
        del_flag,
        create_time,
        update_time
    </sql>

    <resultMap id="resultMapChat" type="com.backendsys.modules.ai.chat.entity.Chat">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="history_code" column="history_code" />
        <result property="model" column="model" />
        <result property="model_version" column="model_version" />
        <result property="role" column="role" />
        <result property="content_type" column="content_type" />
        <result property="content" column="content" />
        <result property="duration" column="duration" javaType="java.lang.Long" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="robot_code" column="robot_code" />
        <result property="del_flag" column="del_flag" javaType="java.lang.Integer" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <!-- 查 列表 -->
    <select id="selectChatList" resultMap="resultMapChat">
        SELECT <include refid="includeChat" />
        FROM ai_chat
        <where>
            <if test="user_id != null and user_id != ''">
                AND user_id = #{user_id}
            </if>
            <if test="history_code != null and history_code != ''">
                AND history_code = #{history_code}
            </if>
            <if test="del_flag != null and del_flag != ''">
                AND del_flag = #{del_flag}
            </if>
        </where>
        ORDER BY create_time DESC, id DESC
    </select>

</mapper>