<?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.tencent.supersonic.headless.server.persistence.mapper.StatMapper">

    <resultMap id="QueryStatDO"
               type="com.tencent.supersonic.headless.api.pojo.QueryStat">
        <id column="id" property="id"/>
        <result column="trace_id" property="traceId"/>
        <result column="model_id" property="modelId"/>
        <result column="data_set_id" property="dataSetId"/>
        <result column="user" property="user"/>
        <result column="created_at" property="createdAt"/>
        <result column="query_type" property="queryType"/>
        <result column="query_type_back" property="queryTypeBack"/>
        <result column="query_sql_cmd" property="querySqlCmd"/>
        <result column="sql_cmd_md5" property="querySqlCmdMd5"/>
        <result column="query_struct_cmd" property="queryStructCmd"/>
        <result column="struct_cmd_md5" property="queryStructCmdMd5"/>
        <result column="sql" property="sql"/>
        <result column="sql_md5" property="sqlMd5"/>
        <result column="query_engine" property="queryEngine"/>
        <result column="elapsed_ms" property="elapsedMs"/>
        <result column="query_state" property="queryState"/>
        <result column="native_query" property="nativeQuery"/>
        <result column="start_date" property="startDate"/>
        <result column="end_date" property="endDate"/>
        <result column="dimensions" property="dimensions"/>
        <result column="metrics" property="metrics"/>
        <result column="select_cols" property="selectCols"/>
        <result column="agg_cols" property="aggCols"/>
        <result column="filter_cols" property="filterCols"/>
        <result column="group_by_cols" property="groupByCols"/>
        <result column="order_by_cols" property="orderByCols"/>
        <result column="use_result_cache" property="useResultCache"/>
        <result column="use_sql_cache" property="useSqlCache"/>
        <result column="sql_cache_key" property="sqlCacheKey"/>
        <result column="result_cache_key" property="resultCacheKey"/>
        <result column="query_opt_mode" property="queryOptMode"/>
    </resultMap>

    <insert id="createRecord">
        insert into s2_query_stat_info
        (
            trace_id, model_id, data_set_id, `user`, query_type, query_type_back, query_sql_cmd, sql_cmd_md5, query_struct_cmd, struct_cmd_md5, `sql`, sql_md5, query_engine,
            elapsed_ms, query_state, native_query, start_date, end_date, dimensions, metrics, select_cols, agg_cols, filter_cols, group_by_cols,
            order_by_cols, use_result_cache, use_sql_cache, sql_cache_key, result_cache_key, query_opt_mode
        )
        values
        (
            #{traceId}, #{modelId}, #{dataSetId}, #{user}, #{queryType}, #{queryTypeBack}, #{querySqlCmd}, #{querySqlCmdMd5}, #{queryStructCmd}, #{queryStructCmdMd5}, #{sql}, #{sqlMd5}, #{queryEngine},
            #{elapsedMs}, #{queryState}, #{nativeQuery}, #{startDate}, #{endDate}, #{dimensions}, #{metrics}, #{selectCols}, #{aggCols}, #{filterCols}, #{groupByCols},
            #{orderByCols}, #{useResultCache}, #{useSqlCache}, #{sqlCacheKey}, #{resultCacheKey}, #{queryOptMode}
        )
    </insert>

    <select id="getStatInfo"
            resultType="com.tencent.supersonic.headless.api.pojo.QueryStat">
        select *
        from s2_query_stat_info
        <where>
            <if test="startTime != null">
                and start_time >= #{startTime}String.valueOf(queryFilter.getValue())
            </if>
            <if test="modelId != null">
                and model_id = #{modelId}
            </if>
            <if test="modelIds != null and modelIds.size() > 0">
                and model_id in
                <foreach item="id" collection="modelIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="metric != null">
                and metrics like concat('%',#{metric},'%')
            </if>
        </where>

    </select>

</mapper>