<?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.DataiIntegrationSyncLogMapper">
    
    <resultMap type="DataiIntegrationSyncLog" id="DataiIntegrationSyncLogResult">
        <result property="id" column="id" />
        <result property="batchId" column="batch_id" />
        <result property="objectApi" column="object_api" />
        <result property="recordId" column="record_id" />
        <result property="operationType" column="operation_type" />
        <result property="operationStatus" column="operation_status" />
        <result property="errorMessage" column="error_message" />
        <result property="executionTime" column="execution_time" />
        <result property="deptId" column="dept_id" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectDataiIntegrationSyncLogVo">
        select 
            disl.id,
            disl.batch_id,
            disl.object_api,
            disl.record_id,
            disl.operation_type,
            disl.operation_status,
            disl.error_message,
            disl.execution_time,
            disl.dept_id,
            disl.create_by,
            disl.create_time,
            disl.update_by,
            disl.update_time
        from datai_integration_sync_log disl
    </sql>

    <select id="selectDataiIntegrationSyncLogList" parameterType="DataiIntegrationSyncLog" resultMap="DataiIntegrationSyncLogResult">
        <include refid="selectDataiIntegrationSyncLogVo"/>
        <where>
            <if test="batchId != null "> and disl.batch_id = #{batchId}</if>
            <if test="objectApi != null  and objectApi != ''"> and disl.object_api = #{objectApi}</if>
            <if test="recordId != null  and recordId != ''"> and disl.record_id = #{recordId}</if>
            <if test="operationType != null  and operationType != ''"> and disl.operation_type = #{operationType}</if>
            <if test="operationStatus != null  and operationStatus != ''"> and disl.operation_status = #{operationStatus}</if>
            <if test="errorMessage != null  and errorMessage != ''"> and disl.error_message = #{errorMessage}</if>
            <if test="executionTime != null "> and disl.execution_time = #{executionTime}</if>
            <if test="deptId != null "> and disl.dept_id = #{deptId}</if>
        </where>
    </select>
    
    <select id="selectDataiIntegrationSyncLogById" parameterType="Long" resultMap="DataiIntegrationSyncLogResult">
        <include refid="selectDataiIntegrationSyncLogVo"/>
        where disl.id = #{id}
    </select>
        
    <insert id="insertDataiIntegrationSyncLog" parameterType="DataiIntegrationSyncLog" useGeneratedKeys="true" keyProperty="id">
        insert into datai_integration_sync_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchId != null">batch_id,</if>
            <if test="objectApi != null and objectApi != ''">object_api,</if>
            <if test="recordId != null">record_id,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="operationStatus != null and operationStatus != ''">operation_status,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="executionTime != null">execution_time,</if>
            <if test="deptId != null">dept_id,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchId != null">#{batchId},</if>
            <if test="objectApi != null and objectApi != ''">#{objectApi},</if>
            <if test="recordId != null">#{recordId},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="operationStatus != null and operationStatus != ''">#{operationStatus},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="executionTime != null">#{executionTime},</if>
            <if test="deptId != null">#{deptId},</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>
         </trim>
    </insert>

    <update id="updateDataiIntegrationSyncLog" parameterType="DataiIntegrationSyncLog">
        update datai_integration_sync_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="objectApi != null and objectApi != ''">object_api = #{objectApi},</if>
            <if test="recordId != null">record_id = #{recordId},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="operationStatus != null and operationStatus != ''">operation_status = #{operationStatus},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="executionTime != null">execution_time = #{executionTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</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>
        </trim>
        where datai_integration_sync_log.id = #{id}
    </update>

    <delete id="deleteDataiIntegrationSyncLogById" parameterType="Long">
        delete from datai_integration_sync_log where id = #{id}
    </delete>

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

    <select id="getLogStatistics" parameterType="map" resultType="com.datai.integration.model.dto.LogStatisticsDTO">
        SELECT 
            COUNT(*) as totalCount,
            SUM(CASE WHEN operation_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN operation_status = 'failure' THEN 1 ELSE 0 END) as failureCount,
            CASE WHEN COUNT(*) > 0 
                THEN ROUND(SUM(CASE WHEN operation_status = 'success' THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2)
                ELSE 0 
            END as successRate,
            ROUND(AVG(execution_time), 2) as avgExecutionTime,
            ROUND(MAX(execution_time), 2) as maxExecutionTime,
            ROUND(MIN(execution_time), 2) as minExecutionTime
        FROM datai_integration_sync_log
        <where>
            <if test="batchId != null">and batch_id = #{batchId}</if>
            <if test="objectApi != null and objectApi != ''">and object_api = #{objectApi}</if>
            <if test="operationType != null and operationType != ''">and operation_type = #{operationType}</if>
            <if test="operationStatus != null and operationStatus != ''">and operation_status = #{operationStatus}</if>
            <if test="startTime != null">and create_time &gt;= #{startTime}</if>
            <if test="endTime != null">and create_time &lt;= #{endTime}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
    </select>

    <select id="getOperationTypeStatistics" parameterType="map" resultType="com.datai.integration.model.dto.LogStatisticsDTO$OperationTypeStatistics">
        SELECT 
            operation_type as operationType,
            COUNT(*) as count,
            SUM(CASE WHEN operation_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN operation_status = 'failure' THEN 1 ELSE 0 END) as failureCount,
            ROUND(AVG(execution_time), 2) as avgExecutionTime
        FROM datai_integration_sync_log
        <where>
            <if test="batchId != null">and batch_id = #{batchId}</if>
            <if test="objectApi != null and objectApi != ''">and object_api = #{objectApi}</if>
            <if test="startTime != null">and create_time &gt;= #{startTime}</if>
            <if test="endTime != null">and create_time &lt;= #{endTime}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
        GROUP BY operation_type
        ORDER BY count DESC
    </select>

    <select id="getObjectApiStatistics" parameterType="map" resultType="com.datai.integration.model.dto.LogStatisticsDTO$ObjectApiStatistics">
        SELECT 
            object_api as objectApi,
            COUNT(*) as count,
            SUM(CASE WHEN operation_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN operation_status = 'failure' THEN 1 ELSE 0 END) as failureCount,
            ROUND(AVG(execution_time), 2) as avgExecutionTime
        FROM datai_integration_sync_log
        <where>
            <if test="batchId != null">and batch_id = #{batchId}</if>
            <if test="operationType != null and operationType != ''">and operation_type = #{operationType}</if>
            <if test="startTime != null">and create_time &gt;= #{startTime}</if>
            <if test="endTime != null">and create_time &lt;= #{endTime}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
        GROUP BY object_api
        ORDER BY count DESC
    </select>

    <select id="getStatusStatistics" parameterType="map" resultType="map">
        SELECT 
            operation_status as status,
            COUNT(*) as count
        FROM datai_integration_sync_log
        <where>
            <if test="batchId != null">and batch_id = #{batchId}</if>
            <if test="objectApi != null and objectApi != ''">and object_api = #{objectApi}</if>
            <if test="operationType != null and operationType != ''">and operation_type = #{operationType}</if>
            <if test="startTime != null">and create_time &gt;= #{startTime}</if>
            <if test="endTime != null">and create_time &lt;= #{endTime}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
        GROUP BY operation_status
    </select>

    <select id="getBatchStatistics" parameterType="map" resultType="com.datai.integration.model.dto.LogStatisticsDTO$BatchStatistics">
        SELECT 
            batch_id as batchId,
            COUNT(*) as count,
            SUM(CASE WHEN operation_status = 'success' THEN 1 ELSE 0 END) as successCount,
            SUM(CASE WHEN operation_status = 'failure' THEN 1 ELSE 0 END) as failureCount,
            ROUND(AVG(execution_time), 2) as avgExecutionTime
        FROM datai_integration_sync_log
        <where>
            <if test="objectApi != null and objectApi != ''">and object_api = #{objectApi}</if>
            <if test="operationType != null and operationType != ''">and operation_type = #{operationType}</if>
            <if test="startTime != null">and create_time &gt;= #{startTime}</if>
            <if test="endTime != null">and create_time &lt;= #{endTime}</if>
            <if test="deptId != null">and dept_id = #{deptId}</if>
        </where>
        GROUP BY batch_id
        ORDER BY batch_id DESC
        LIMIT 10
    </select>
</mapper>