<?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.iflytek.astron.console.hub.mapper.ChatReasonRecordsMapper">

    <!-- Result mapping -->
    <resultMap id="BaseResultMap" type="com.iflytek.astron.console.commons.entity.chat.ChatReasonRecords">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="uid" property="uid" jdbcType="BIGINT"/>
        <result column="chat_id" property="chatId" jdbcType="BIGINT"/>
        <result column="req_id" property="reqId" jdbcType="BIGINT"/>
        <result column="content" property="content" jdbcType="LONGVARCHAR"/>
        <result column="thinking_elapsed_secs" property="thinkingElapsedSecs" jdbcType="BIGINT"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- Base columns -->
    <sql id="Base_Column_List">
        id, uid, chat_id, req_id, content, thinking_elapsed_secs, type, create_time, update_time
    </sql>

    <!-- Query reasoning records by request ID -->
    <select id="selectByReqId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_reason_records
        WHERE req_id = #{reqId}
        ORDER BY create_time DESC
    </select>

    <!-- Query reasoning records by chat ID -->
    <select id="selectByChatId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_reason_records
        WHERE chat_id = #{chatId}
        ORDER BY create_time DESC
    </select>

    <!-- Query reasoning records by user ID and chat ID -->
    <select id="selectByUidAndChatId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_reason_records
        WHERE uid = #{uid} AND chat_id = #{chatId}
        ORDER BY create_time DESC
    </select>

    <!-- Query reasoning records by reasoning type -->
    <select id="selectByType" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM chat_reason_records
        WHERE type = #{type}
        ORDER BY create_time DESC
    </select>

    <!-- Delete reasoning records older than specified days -->
    <delete id="deleteByCreateTimeBefore">
        DELETE FROM chat_reason_records
        WHERE create_time &lt; DATE_SUB(NOW(), INTERVAL #{days} DAY)
    </delete>

</mapper>