-- ============================================
-- 修复 userId=1 的消息数据问题
-- 创建时间: 2026-01-27
-- 注意：执行前先备份数据！
-- ============================================

-- 方案A：删除异常的消息记录（如果有NULL字段）
-- DELETE FROM message 
-- WHERE (user_id = 1 OR receiver_id = 1)
--   AND (
--     id IS NULL 
--     OR user_id IS NULL 
--     OR receiver_id IS NULL
--     OR content IS NULL
--     OR created_time IS NULL
--   );

-- 方案B：清空用户1的所有消息（如果数据太多或太乱）
-- DELETE FROM message WHERE user_id = 1 OR receiver_id = 1;

-- 方案C：修复特定字段的NULL值
-- UPDATE message 
-- SET is_read = 0 
-- WHERE (user_id = 1 OR receiver_id = 1) AND is_read IS NULL;

-- 方案D：如果用户1不存在，创建用户1
-- INSERT INTO user (id, phone, password, name, role, status, created_time)
-- VALUES (1, '13800138000', '$2a$10$...', '张爸爸', 'user', 1, NOW())
-- ON DUPLICATE KEY UPDATE id = id;

-- 验证修复结果
SELECT '=== 验证修复结果 ===' AS step;
SELECT COUNT(*) as total_messages FROM message WHERE user_id = 1 OR receiver_id = 1;
SELECT COUNT(*) as unread_count FROM message WHERE receiver_id = 1 AND is_read = 0;
