<?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.datai.integration.mapper.DataiIntegrationApiCallLogMapper">
    
    <resultMap type="DataiIntegrationApiCallLog" id="DataiIntegrationApiCallLogResult">
        <result property="id" column="id" />
        <result property="apiType" column="api_type" />
        <result property="connectionClass" column="connection_class" />
        <result property="methodName" column="method_name" />
        <result property="executionTime" column="execution_time" />
        <result property="status" column="status" />
        <result property="errorMessage" column="error_message" />
        <result property="callTime" column="call_time" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
        <result property="deptId" column="dept_id" />
    </resultMap>

    <sql id="selectDataiIntegrationApiCallLogVo">
        select 
            diacl.id,
            diacl.api_type,
            diacl.connection_class,
            diacl.method_name,
            diacl.execution_time,
            diacl.status,
            diacl.error_message,
            diacl.call_time,
            diacl.create_by,
            diacl.create_time,
            diacl.update_by,
            diacl.update_time,
            diacl.dept_id
        from datai_integration_api_call_log diacl
    </sql>

    <select id="selectDataiIntegrationApiCallLogList" parameterType="DataiIntegrationApiCallLog" resultMap="DataiIntegrationApiCallLogResult">
        <include refid="selectDataiIntegrationApiCallLogVo"/>
        <where>
            <if test="apiType != null  and apiType != ''"> and diacl.api_type = #{apiType}</if>
            <if test="connectionClass != null  and connectionClass != ''"> and diacl.connection_class = #{connectionClass}</if>
            <if test="methodName != null  and methodName != ''"> and diacl.method_name like concat('%', #{methodName}, '%')</if>
            <if test="executionTime != null "> and diacl.execution_time = #{executionTime}</if>
            <if test="status != null  and status != ''"> and diacl.status = #{status}</if>
            <if test="errorMessage != null  and errorMessage != ''"> and diacl.error_message = #{errorMessage}</if>
            <if test="callTime != null "> and diacl.call_time = #{callTime}</if>
        </where>
    </select>
    
    <select id="selectDataiIntegrationApiCallLogById" parameterType="Long" resultMap="DataiIntegrationApiCallLogResult">
        <include refid="selectDataiIntegrationApiCallLogVo"/>
        where diacl.id = #{id}
    </select>
        
    <insert id="insertDataiIntegrationApiCallLog" parameterType="DataiIntegrationApiCallLog" useGeneratedKeys="true" keyProperty="id">
        insert into datai_integration_api_call_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="apiType != null and apiType != ''">api_type,</if>
            <if test="connectionClass != null and connectionClass != ''">connection_class,</if>
            <if test="methodName != null and methodName != ''">method_name,</if>
            <if test="executionTime != null">execution_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="callTime != null">call_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="apiType != null and apiType != ''">#{apiType},</if>
            <if test="connectionClass != null and connectionClass != ''">#{connectionClass},</if>
            <if test="methodName != null and methodName != ''">#{methodName},</if>
            <if test="executionTime != null">#{executionTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="callTime != null">#{callTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateDataiIntegrationApiCallLog" parameterType="DataiIntegrationApiCallLog">
        update datai_integration_api_call_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="apiType != null and apiType != ''">api_type = #{apiType},</if>
            <if test="connectionClass != null and connectionClass != ''">connection_class = #{connectionClass},</if>
            <if test="methodName != null and methodName != ''">method_name = #{methodName},</if>
            <if test="executionTime != null">execution_time = #{executionTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="callTime != null">call_time = #{callTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where datai_integration_api_call_log.id = #{id}
    </update>

    <delete id="deleteDataiIntegrationApiCallLogById" parameterType="Long">
        delete from datai_integration_api_call_log where id = #{id}
    </delete>

    <delete id="deleteDataiIntegrationApiCallLogByIds" parameterType="String">
        delete from datai_integration_api_call_log where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getApiCallLogStatistics" parameterType="map" resultType="java.util.HashMap">
        select 
            COUNT(*) as totalCount,
            SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN status = 'FAILURE' THEN 1 ELSE 0 END) as failureCount,
            CASE WHEN COUNT(*) > 0 THEN SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) * 100.0 / COUNT(*) ELSE 0 END as successRate,
            AVG(execution_time) as avgExecutionTime,
            MIN(execution_time) as minExecutionTime,
            MAX(execution_time) as maxExecutionTime,
            SUM(execution_time) as totalExecutionTime
        from datai_integration_api_call_log
        <where>
            <if test="apiType != null and apiType != ''"> and api_type = #{apiType}</if>
            <if test="connectionClass != null and connectionClass != ''"> and connection_class = #{connectionClass}</if>
            <if test="methodName != null and methodName != ''"> and method_name like concat('%', #{methodName}, '%')</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="startTime != null"> and call_time &gt;= #{startTime}</if>
            <if test="endTime != null"> and call_time &lt;= #{endTime}</if>
        </where>
    </select>

    <select id="getApiCallLogGroupedStatistics" parameterType="map" resultType="java.util.HashMap">
        select 
            ${groupBy} as groupKey,
            COUNT(*) as totalCount,
            SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN status = 'FAILURE' THEN 1 ELSE 0 END) as failureCount,
            CASE WHEN COUNT(*) > 0 THEN SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) * 100.0 / COUNT(*) ELSE 0 END as successRate,
            AVG(execution_time) as avgExecutionTime,
            MIN(execution_time) as minExecutionTime,
            MAX(execution_time) as maxExecutionTime,
            SUM(execution_time) as totalExecutionTime
        from datai_integration_api_call_log
        <where>
            <if test="apiType != null and apiType != ''"> and api_type = #{apiType}</if>
            <if test="connectionClass != null and connectionClass != ''"> and connection_class = #{connectionClass}</if>
            <if test="methodName != null and methodName != ''"> and method_name like concat('%', #{methodName}, '%')</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="startTime != null"> and call_time &gt;= #{startTime}</if>
            <if test="endTime != null"> and call_time &lt;= #{endTime}</if>
        </where>
        group by ${groupBy}
        order by ${groupBy}
    </select>

    <!-- 获取趋势统计数据 -->
    <select id="getApiCallLogTrend" parameterType="map" resultType="java.util.HashMap">
        select 
            DATE_FORMAT(call_time, #{timeFormat}) as timePoint,
            COUNT(*) as totalCount,
            SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN status = 'FAILURE' THEN 1 ELSE 0 END) as failureCount,
            CASE WHEN COUNT(*) > 0 THEN SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END) * 100.0 / COUNT(*) ELSE 0 END as successRate,
            AVG(execution_time) as avgExecutionTime
        from datai_integration_api_call_log
        <where>
            <if test="apiType != null and apiType != ''"> and api_type = #{apiType}</if>
            <if test="connectionClass != null and connectionClass != ''"> and connection_class = #{connectionClass}</if>
            <if test="methodName != null and methodName != ''"> and method_name like concat('%', #{methodName}, '%')</if>
            <if test="status != null and status != ''"> and status = #{status}</if>
            <if test="startTime != null"> and call_time &gt;= #{startTime}</if>
            <if test="endTime != null"> and call_time &lt;= #{endTime}</if>
        </where>
        group by timePoint
        order by timePoint
    </select>
</mapper>