<?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.cooldown.MachineCooldownMapper">
    
    <!-- 结果映射 -->
    <resultMap id="MachineCooldownMap" type="com.gameplatform.server.model.entity.cooldown.MachineCooldown">
        <id property="id" column="id" />
        <result property="machineId" column="machine_id" />
        <result property="cooldownStartTime" column="cooldown_start_time" />
        <result property="cooldownEndTime" column="cooldown_end_time" />
        <result property="reason" column="reason" />
        <result property="linkTaskId" column="link_task_id" />
        <result property="status" column="status" />
        <result property="createdAt" column="created_at" />
        <result property="updatedAt" column="updated_at" />
    </resultMap>
    
    <!-- 根据设备ID查找活跃的冷却记录 -->
    <select id="findActiveCooldownByMachineId" parameterType="string" resultMap="MachineCooldownMap">
        SELECT * FROM machine_cooldown 
        WHERE machine_id = #{machineId} 
          AND status = 'ACTIVE'
          AND cooldown_end_time > NOW()
        ORDER BY created_at DESC 
        LIMIT 1
    </select>
    
    <!-- 根据设备ID和状态查找冷却记录 -->
    <select id="findByMachineIdAndStatus" resultMap="MachineCooldownMap">
        SELECT * FROM machine_cooldown 
        WHERE machine_id = #{machineId} 
          AND status = #{status}
        ORDER BY created_at DESC
    </select>
    
    <!-- 查找已过期但状态仍为ACTIVE的冷却记录 -->
    <select id="findExpiredActiveCooldowns" resultMap="MachineCooldownMap">
        SELECT * FROM machine_cooldown 
        WHERE status = 'ACTIVE'
          AND cooldown_end_time &lt;= #{currentTime}
        ORDER BY cooldown_end_time ASC
        LIMIT #{limit}
    </select>
    
    <!-- 批量更新过期的冷却记录状态 -->
    <update id="batchUpdateExpiredCooldowns">
        UPDATE machine_cooldown mc
        LEFT JOIN (
            SELECT machine_id FROM (
                SELECT DISTINCT machine_id
                FROM machine_cooldown
                WHERE status = 'EXPIRED'
            ) ex0
        ) ex ON ex.machine_id = mc.machine_id
        SET mc.status = 'EXPIRED', mc.updated_at = NOW()
        WHERE mc.status = 'ACTIVE'
          AND mc.cooldown_end_time &lt;= #{currentTime}
          AND ex.machine_id IS NULL
    </update>
    
    <!-- 手动移除设备的冷却状态 -->
    <update id="removeMachineCooldown">
        UPDATE machine_cooldown 
        SET status = 'MANUALLY_REMOVED', updated_at = NOW()
        WHERE machine_id = #{machineId} 
          AND status = 'ACTIVE'
    </update>
    
    <!-- 根据链接任务ID查找冷却记录 -->
    <select id="findByLinkTaskId" parameterType="long" resultMap="MachineCooldownMap">
        SELECT * FROM machine_cooldown 
        WHERE link_task_id = #{linkTaskId}
        ORDER BY created_at DESC
    </select>
    
    <!-- 统计活跃的冷却记录数量 -->
    <select id="countActiveCooldowns" resultType="long">
        SELECT COUNT(*) FROM machine_cooldown 
        WHERE status = 'ACTIVE'
          AND cooldown_end_time > NOW()
    </select>
    
    <!-- 统计指定时间范围内的冷却记录数量 -->
    <select id="countCooldownsByTimeRange" resultType="long">
        SELECT COUNT(*) FROM machine_cooldown 
        WHERE created_at BETWEEN #{startTime} AND #{endTime}
    </select>
    
    <!-- 清理指定时间之前的已过期冷却记录 -->
    <delete id="cleanupExpiredCooldowns">
        DELETE FROM machine_cooldown 
        WHERE status = 'EXPIRED'
          AND updated_at &lt; #{beforeTime}
    </delete>

    <!-- 在批量将 ACTIVE 更新为 EXPIRED 前，
         先删除这些设备已存在的 EXPIRED 记录，避免 (machine_id,status) 唯一键冲突 -->
    <delete id="deleteExistingExpiredForMachinesToExpire">
        DELETE mc FROM machine_cooldown mc
        JOIN (
            SELECT DISTINCT machine_id
            FROM machine_cooldown
            WHERE status = 'ACTIVE'
              AND cooldown_end_time &lt;= #{currentTime}
        ) t ON t.machine_id = mc.machine_id
        WHERE mc.status = 'EXPIRED'
    </delete>
    
    <!-- 获取指定设备的冷却历史记录 -->
    <select id="getCooldownHistory" resultMap="MachineCooldownMap">
        SELECT * FROM machine_cooldown 
        WHERE machine_id = #{machineId}
        ORDER BY created_at DESC
        LIMIT #{limit} OFFSET #{offset}
    </select>
    
</mapper>

