<?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.gameplatform.server.mapper.log.ScriptOperationLogMapper">
    
    <resultMap id="ScriptOperationLogMap" type="com.gameplatform.server.model.entity.log.ScriptOperationLog">
        <id property="id" column="id" />
        <result property="operationType" column="operation_type" typeHandler="org.apache.ibatis.type.EnumTypeHandler" />
        <result property="machineId" column="machine_id" />
        <result property="requestUrl" column="request_url" />
        <result property="requestParams" column="request_params" />
        <result property="responseResult" column="response_result" />
        <result property="success" column="success" />
        <result property="errorMessage" column="error_message" />
        <result property="durationMs" column="duration_ms" />
        <result property="requestTime" column="request_time" />
        <result property="responseTime" column="response_time" />
        <result property="createdAt" column="created_at" />
        <result property="updatedAt" column="updated_at" />
    </resultMap>

    <sql id="BaseColumns">
        id, operation_type, machine_id, request_url, request_params, response_result, 
        success, error_message, duration_ms, request_time, response_time, created_at, updated_at
    </sql>

    <select id="findById" parameterType="long" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE id = #{id}
        LIMIT 1
    </select>

    <select id="findByMachineId" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE machine_id = #{machineId}
        ORDER BY request_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <select id="findByOperationType" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE operation_type = #{operationType}
        ORDER BY request_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <select id="findByTimeRange" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE request_time BETWEEN #{startTime} AND #{endTime}
        ORDER BY request_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <select id="findByMachineIdAndOperationType" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE machine_id = #{machineId} AND operation_type = #{operationType}
        ORDER BY request_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <select id="findFailedOperations" resultMap="ScriptOperationLogMap">
        SELECT <include refid="BaseColumns" />
        FROM script_operation_log
        WHERE success = false
        <if test="startTime != null and endTime != null">
            AND request_time BETWEEN #{startTime} AND #{endTime}
        </if>
        ORDER BY request_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <select id="getOperationStatistics" resultType="java.util.Map">
        SELECT 
            operation_type,
            COUNT(*) as total_count,
            SUM(CASE WHEN success = true THEN 1 ELSE 0 END) as success_count,
            SUM(CASE WHEN success = false THEN 1 ELSE 0 END) as failed_count,
            ROUND(AVG(duration_ms), 2) as avg_duration_ms,
            ROUND(SUM(CASE WHEN success = true THEN 1 ELSE 0 END) * 100.0 / COUNT(*), 2) as success_rate
        FROM script_operation_log
        WHERE request_time BETWEEN #{startTime} AND #{endTime}
        GROUP BY operation_type
        ORDER BY operation_type
    </select>

    <insert id="insert" parameterType="com.gameplatform.server.model.entity.log.ScriptOperationLog" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO script_operation_log (
            operation_type, machine_id, request_url, request_params, response_result,
            success, error_message, duration_ms, request_time, response_time, created_at, updated_at
        ) VALUES (
            #{operationType}, #{machineId}, #{requestUrl}, #{requestParams}, #{responseResult},
            #{success}, #{errorMessage}, #{durationMs}, #{requestTime}, #{responseTime}, #{createdAt}, #{updatedAt}
        )
    </insert>

    <update id="update" parameterType="com.gameplatform.server.model.entity.log.ScriptOperationLog">
        UPDATE script_operation_log
        <set>
            <if test="responseResult != null">response_result = #{responseResult},</if>
            <if test="success != null">success = #{success},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="durationMs != null">duration_ms = #{durationMs},</if>
            <if test="responseTime != null">response_time = #{responseTime},</if>
            updated_at = #{updatedAt}
        </set>
        WHERE id = #{id}
    </update>

    <delete id="deleteOldRecords">
        DELETE FROM script_operation_log
        WHERE created_at &lt; #{beforeTime}
    </delete>

</mapper>
