<?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="org.xyzh.ai.mapper.TbChatMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbChat">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="chat_id" property="chatId" jdbcType="VARCHAR"/>
        <result column="agent_id" property="agentId" jdbcType="VARCHAR"/>
        <result column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="user_type" property="userType" jdbcType="BOOLEAN"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        optsn, chat_id, agent_id, user_id, user_type, title,
        create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertChat" parameterType="org.xyzh.api.ai.dto.TbChat">
        INSERT INTO ai.tb_chat (
            optsn, chat_id, agent_id, user_id, title
            <if test="userType != null">, user_type</if>
        ) VALUES (
            #{optsn}, #{chatId}, #{agentId}, #{userId}, #{title}
            <if test="userType != null">, #{userType}</if>
        )
    </insert>

    <update id="updateChat" parameterType="org.xyzh.api.ai.dto.TbChat">
        UPDATE ai.tb_chat
        <set>
            <if test="title != null">title = #{title},</if>
            update_time = now()
        </set>
        WHERE chat_id = #{chatId} AND deleted = false
    </update>

    <update id="deleteChat" parameterType="org.xyzh.api.ai.dto.TbChat">
        UPDATE ai.tb_chat
        SET deleted = true,
            delete_time = now()
        WHERE chat_id = #{chatId} AND deleted = false
    </update>

    <select id="selectChatById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat
        WHERE chat_id = #{chatId} AND deleted = false
    </select>

    <select id="selectChatList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat
        WHERE deleted = false
        <if test="agentId != null and agentId != ''">
            AND agent_id = #{agentId}
        </if>
        <if test="userId != null and userId != ''">
            AND user_id = #{userId}
        </if>
        ORDER BY update_time DESC, create_time DESC
    </select>

    <select id="selectChatPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_chat
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.agentId != null and filter.agentId != ''">
                AND agent_id = #{filter.agentId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
        </if>
        ORDER BY update_time DESC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countChats" resultType="long">
        SELECT COUNT(*)
        FROM ai.tb_chat
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.agentId != null and filter.agentId != ''">
                AND agent_id = #{filter.agentId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
        </if>
    </select>
</mapper>
