-- 修复卡在"处理中"状态的任务
-- 这些任务可能因为异常导致status=1但没有继续处理

-- 1. 查看所有卡住的任务（处理中且超过5分钟的）
SELECT id, mobile_phone, integral_value, status, retry_count, 
       FROM_UNIXTIME(createtime) as create_time,
       FROM_UNIXTIME(updatetime) as update_time,
       error_msg
FROM icbc_queue 
WHERE status = 1 
  AND updatetime < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 MINUTE))
ORDER BY id DESC;

-- 2. 将这些卡住的任务重置为待处理状态
UPDATE icbc_queue 
SET status = 0,
    error_msg = '任务处理超时，已重置为待处理'
WHERE status = 1 
  AND updatetime < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 MINUTE));

-- 3. 查看所有待处理的任务
SELECT id, mobile_phone, integral_value, status, retry_count, 
       FROM_UNIXTIME(createtime) as create_time,
       error_msg
FROM icbc_queue 
WHERE status = 0
ORDER BY id DESC
LIMIT 20;

-- 4. 查看所有失败的任务（重试3次后仍失败）
SELECT id, mobile_phone, integral_value, status, retry_count, 
       FROM_UNIXTIME(createtime) as create_time,
       error_msg, result
FROM icbc_queue 
WHERE status = 3
ORDER BY id DESC
LIMIT 20;

-- 5. 如果要重试失败的任务，将status=3改为0
-- UPDATE icbc_queue SET status = 0, retry_count = 0 WHERE status = 3;

