USE zhibo;

-- 1. 查看当前有哪些主播
SELECT uid, nickname, phone, is_streamer 
FROM eb_user 
WHERE is_streamer = 1 
ORDER BY uid 
LIMIT 10;

-- 2. 查看有多少直播间的uid为空或无效
SELECT 
    COUNT(*) as total_rooms,
    SUM(CASE WHEN uid IS NULL OR uid = 0 THEN 1 ELSE 0 END) as invalid_uid_count,
    SUM(CASE WHEN uid IS NOT NULL AND uid > 0 THEN 1 ELSE 0 END) as valid_uid_count
FROM eb_live_room;

-- 3. 查看uid无效的直播间
SELECT id, uid, title, streamer_name, stream_key 
FROM eb_live_room 
WHERE uid IS NULL OR uid = 0 OR uid NOT IN (SELECT uid FROM eb_user)
LIMIT 10;

-- 4. 选择一个默认主播（选择第一个认证主播，或者指定某个uid）
-- 假设选择 uid=318 作为默认主播（根据之前的数据，这个用户有很多关注记录）
SET @default_streamer_id = 318;

-- 验证这个主播是否存在
SELECT uid, nickname, phone, is_streamer 
FROM eb_user 
WHERE uid = @default_streamer_id;

-- 5. 将所有uid无效的直播间分配给默认主播
UPDATE eb_live_room 
SET uid = @default_streamer_id,
    streamer_name = (SELECT nickname FROM eb_user WHERE uid = @default_streamer_id)
WHERE uid IS NULL OR uid = 0 OR uid NOT IN (SELECT uid FROM eb_user);

-- 6. 验证修复结果
SELECT 
    COUNT(*) as total_rooms,
    SUM(CASE WHEN uid IS NULL OR uid = 0 THEN 1 ELSE 0 END) as invalid_uid_count,
    SUM(CASE WHEN uid IS NOT NULL AND uid > 0 THEN 1 ELSE 0 END) as valid_uid_count
FROM eb_live_room;

-- 7. 查看修复后的直播间
SELECT id, uid, title, streamer_name, is_live 
FROM eb_live_room 
ORDER BY id DESC 
LIMIT 10;
