-- 设备状态变更记录清理测试脚本
-- 用于验证24小时清理功能

-- 1. 检查表结构
SELECT 'Table Structure:' as info;
DESC device_status_transition;

-- 2. 查看当前记录数
SELECT 'Current Records Count:' as info;
SELECT COUNT(*) as total_records FROM device_status_transition;

-- 3. 查看24小时前的记录数（将被清理的记录）
SELECT '24+ Hours Old Records (to be cleaned):' as info;
SELECT COUNT(*) as old_records 
FROM device_status_transition 
WHERE created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR);

-- 4. 查看最近24小时的记录数（将被保留的记录）
SELECT 'Recent 24 Hours Records (to be kept):' as info;
SELECT COUNT(*) as recent_records 
FROM device_status_transition 
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR);

-- 5. 查看记录的时间分布
SELECT 'Records Distribution:' as info;
SELECT 
    DATE(created_at) as date,
    COUNT(*) as count
FROM device_status_transition 
GROUP BY DATE(created_at)
ORDER BY date DESC
LIMIT 10;

-- 6. 预览将被删除的记录（最多显示5条）
SELECT 'Sample Records to be Deleted:' as info;
SELECT device_id, prev_status, new_status, created_at
FROM device_status_transition 
WHERE created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR)
ORDER BY created_at DESC
LIMIT 5;

-- 测试删除语句（注释掉，仅用于验证语法）
-- DELETE FROM device_status_transition WHERE created_at < DATE_SUB(NOW(), INTERVAL 24 HOUR);
