-- life_fans：历史数据 type + ref_id 回填（PR-1）
-- 执行前请备份；建议在从库验证后再上生产。
-- 前缀规则：user_ -> 1 + life_user.id；store_ -> 2 + store_user.id；lawyer_ -> 3 + lawyer_user.id

-- ---------------------------------------------------------------------------
-- 1. fans 侧 fans_id
-- ---------------------------------------------------------------------------
UPDATE life_fans lf
    INNER JOIN life_user lu ON lf.fans_id LIKE 'user\_%'
        AND lu.user_phone = SUBSTRING(lf.fans_id, 6) AND lu.delete_flag = 0
SET lf.fans_user_type = 1, lf.fans_ref_id = lu.id
WHERE lf.fans_user_type IS NULL AND lf.fans_id LIKE 'user\_%';

UPDATE life_fans lf
    INNER JOIN store_user su ON lf.fans_id LIKE 'store\_%'
        AND su.phone = SUBSTRING(lf.fans_id, 7) AND su.delete_flag = 0
SET lf.fans_user_type = 2, lf.fans_ref_id = su.id
WHERE lf.fans_user_type IS NULL AND lf.fans_id LIKE 'store\_%';

UPDATE life_fans lf
    INNER JOIN lawyer_user lwu ON lf.fans_id LIKE 'lawyer\_%'
        AND lwu.phone = SUBSTRING(lf.fans_id, 8) AND lwu.delete_flag = 0
SET lf.fans_user_type = 3, lf.fans_ref_id = lwu.id
WHERE lf.fans_user_type IS NULL AND lf.fans_id LIKE 'lawyer\_%';

-- ---------------------------------------------------------------------------
-- 2. 被关注方 followed_id
-- ---------------------------------------------------------------------------
UPDATE life_fans lf
    INNER JOIN life_user lu ON lf.followed_id LIKE 'user\_%'
        AND lu.user_phone = SUBSTRING(lf.followed_id, 6) AND lu.delete_flag = 0
SET lf.followed_user_type = 1, lf.followed_ref_id = lu.id
WHERE lf.followed_user_type IS NULL AND lf.followed_id LIKE 'user\_%';

UPDATE life_fans lf
    INNER JOIN store_user su ON lf.followed_id LIKE 'store\_%'
        AND su.phone = SUBSTRING(lf.followed_id, 7) AND su.delete_flag = 0
SET lf.followed_user_type = 2, lf.followed_ref_id = su.id
WHERE lf.followed_user_type IS NULL AND lf.followed_id LIKE 'store\_%';

UPDATE life_fans lf
    INNER JOIN lawyer_user lwu ON lf.followed_id LIKE 'lawyer\_%'
        AND lwu.phone = SUBSTRING(lf.followed_id, 8) AND lwu.delete_flag = 0
SET lf.followed_user_type = 3, lf.followed_ref_id = lwu.id
WHERE lf.followed_user_type IS NULL AND lf.followed_id LIKE 'lawyer\_%';

-- ---------------------------------------------------------------------------
-- 3. 校验：未回填的有效关注记录（delete_flag=0 且四方 type/ref 任一为 NULL）
-- ---------------------------------------------------------------------------
-- SELECT id, fans_id, followed_id, fans_user_type, fans_ref_id, followed_user_type, followed_ref_id
-- FROM life_fans
-- WHERE delete_flag = 0
--   AND (fans_user_type IS NULL OR fans_ref_id IS NULL
--     OR followed_user_type IS NULL OR followed_ref_id IS NULL);

-- ---------------------------------------------------------------------------
-- 4. 可选：type+ref 维度唯一约束（确认无重复后再执行）
-- ---------------------------------------------------------------------------
-- DELETE FROM life_fans
-- WHERE id IN (
--     SELECT id FROM (
--         SELECT lf.id AS id,
--                ROW_NUMBER() OVER (
--                    PARTITION BY lf.followed_user_type, lf.followed_ref_id, lf.fans_user_type, lf.fans_ref_id
--                    ORDER BY CASE WHEN lf.delete_flag = 0 THEN 0 ELSE 1 END, lf.id ASC
--                ) AS rn
--         FROM life_fans lf
--         WHERE lf.followed_user_type IS NOT NULL AND lf.followed_ref_id IS NOT NULL
--           AND lf.fans_user_type IS NOT NULL AND lf.fans_ref_id IS NOT NULL
--     ) ranked
--     WHERE ranked.rn > 1
-- );
--
-- ALTER TABLE life_fans
--     ADD UNIQUE KEY uk_life_fans_type_ref_pair (followed_user_type, followed_ref_id, fans_user_type, fans_ref_id);
