-- 清理失败任务的SQL脚本

-- 删除失败的任务队列记录
DELETE FROM task_queue WHERE status = 'FAILED';

-- 删除失败的图生视频任务
DELETE FROM image_to_video_tasks WHERE status = 'FAILED';

-- 删除失败的文生视频任务  
DELETE FROM text_to_video_tasks WHERE status = 'FAILED';

-- 删除相关的积分冻结记录
DELETE FROM points_freeze_records WHERE status IN ('FROZEN', 'RETURNED', 'DEDUCTED') 
AND task_id IN (
    SELECT task_id FROM task_queue WHERE status = 'FAILED'
);

-- 显示清理结果
SELECT 'task_queue' as table_name, COUNT(*) as remaining_count FROM task_queue
UNION ALL
SELECT 'image_to_video_tasks' as table_name, COUNT(*) as remaining_count FROM image_to_video_tasks  
UNION ALL
SELECT 'text_to_video_tasks' as table_name, COUNT(*) as remaining_count FROM text_to_video_tasks
UNION ALL
SELECT 'points_freeze_records' as table_name, COUNT(*) as remaining_count FROM points_freeze_records;

