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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.ai.TbAiUsageStatistics">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="user_id" property="userID" jdbcType="VARCHAR"/>
        <result column="stat_date" property="statDate" jdbcType="DATE"/>
        <result column="conversation_count" property="conversationCount" jdbcType="INTEGER"/>
        <result column="message_count" property="messageCount" jdbcType="INTEGER"/>
        <result column="total_tokens" property="totalTokens" jdbcType="INTEGER"/>
        <result column="file_count" property="fileCount" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, user_id, stat_date, conversation_count, message_count, 
        total_tokens, file_count, create_time, update_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="userID != null and userID != ''">
                AND user_id = #{userID}
            </if>
            <if test="statDate != null">
                AND stat_date = #{statDate}
            </if>
        </where>
    </sql>

    <!-- selectAiUsageStatistics -->
    <select id="selectAiUsageStatistics" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_ai_usage_statistics
        <include refid="Where_Clause"/>
        ORDER BY stat_date DESC
    </select>

</mapper>
