-- 手机号身份键配套字段：user_type + ref_id
-- 分类口径（除 life_blacklist 外）：1-用户(life_user) 2-商户(store_user) 3-律师(lawyer_user)
-- life_blacklist 仍使用原有 blocker_type/blocked_type（1-商户 2-用户），本脚本不涉及
-- 执行前请备份；若列已存在请跳过对应 ADD COLUMN

-- ---------------------------------------------------------------------------
-- 1. life_fans（fans_id / followed_id）
-- ---------------------------------------------------------------------------
ALTER TABLE life_fans
    ADD COLUMN fans_user_type     TINYINT NULL COMMENT '粉丝用户分类：1-用户 2-商户 3-律师' AFTER fans_id,
    ADD COLUMN fans_ref_id        INT     NULL COMMENT '粉丝对应表主键id（与 fans_user_type 对应）' AFTER fans_user_type,
    ADD COLUMN followed_user_type TINYINT NULL COMMENT '被关注方用户分类：1-用户 2-商户 3-律师' AFTER followed_id,
    ADD COLUMN followed_ref_id    INT     NULL COMMENT '被关注方对应表主键id（与 followed_user_type 对应）' AFTER followed_user_type;

-- ---------------------------------------------------------------------------
-- 2. life_user_dynamics（phone_id）
-- ---------------------------------------------------------------------------
ALTER TABLE life_user_dynamics
    ADD COLUMN phone_user_type TINYINT NULL COMMENT '发布人用户分类：1-用户 2-商户 3-律师' AFTER phone_id,
    ADD COLUMN phone_ref_id    INT     NULL COMMENT '发布人对应表主键id（与 phone_user_type 对应）' AFTER phone_user_type;

-- ---------------------------------------------------------------------------
-- 3. life_message（sender_id / receiver_id / delete_phone_id）
-- ---------------------------------------------------------------------------
ALTER TABLE life_message
    ADD COLUMN sender_user_type       TINYINT NULL COMMENT '发送人用户分类：1-用户 2-商户 3-律师' AFTER sender_id,
    ADD COLUMN sender_ref_id          INT     NULL COMMENT '发送人对应表主键id' AFTER sender_user_type,
    ADD COLUMN receiver_user_type     TINYINT NULL COMMENT '接收人用户分类：1-用户 2-商户 3-律师' AFTER receiver_id,
    ADD COLUMN receiver_ref_id        INT     NULL COMMENT '接收人对应表主键id' AFTER receiver_user_type,
    ADD COLUMN delete_phone_user_type TINYINT NULL COMMENT '删除方用户分类：1-用户 2-商户 3-律师' AFTER delete_phone_id,
    ADD COLUMN delete_phone_ref_id    INT     NULL COMMENT '删除方对应表主键id' AFTER delete_phone_user_type;

-- ---------------------------------------------------------------------------
-- 4. life_notice（sender_id / receiver_id）
-- ---------------------------------------------------------------------------
ALTER TABLE life_notice
    ADD COLUMN sender_user_type   TINYINT NULL COMMENT '发送人用户分类：1-用户 2-商户 3-律师' AFTER sender_id,
    ADD COLUMN sender_ref_id      INT     NULL COMMENT '发送人对应表主键id' AFTER sender_user_type,
    ADD COLUMN receiver_user_type TINYINT NULL COMMENT '接收人用户分类：1-用户 2-商户 3-律师' AFTER receiver_id,
    ADD COLUMN receiver_ref_id    INT     NULL COMMENT '接收人对应表主键id' AFTER receiver_user_type;

-- ---------------------------------------------------------------------------
-- 5. life_like_record（dianzan_id）
-- ---------------------------------------------------------------------------
ALTER TABLE life_like_record
    ADD COLUMN dianzan_user_type TINYINT NULL COMMENT '点赞人用户分类：1-用户 2-商户 3-律师' AFTER dianzan_id,
    ADD COLUMN dianzan_ref_id    INT     NULL COMMENT '点赞人对应表主键id' AFTER dianzan_user_type;

-- ---------------------------------------------------------------------------
-- 6. store_clock_in（phone_id）
-- ---------------------------------------------------------------------------
ALTER TABLE store_clock_in
    ADD COLUMN phone_user_type TINYINT NULL COMMENT '打卡人用户分类：1-用户 2-商户 3-律师' AFTER phone_id,
    ADD COLUMN phone_ref_id    INT     NULL COMMENT '打卡人对应表主键id' AFTER phone_user_type;

-- ---------------------------------------------------------------------------
-- 7. life_collect（user_id 身份键字段）
-- ---------------------------------------------------------------------------
ALTER TABLE life_collect
    ADD COLUMN user_id_user_type TINYINT NULL COMMENT '收藏人用户分类：1-用户 2-商户 3-律师' AFTER user_id,
    ADD COLUMN user_id_ref_id    INT     NULL COMMENT '收藏人对应表主键id' AFTER user_id_user_type;


-- ===========================================================================
-- 可选：历史数据回填（按需执行，建议先在从库/备份环境验证）
-- 前缀规则：user_ -> 1 + life_user.id；store_ -> 2 + store_user.id；lawyer_ -> 3 + lawyer_user.id
-- ===========================================================================

-- life_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\_%';

-- life_fans.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\_%';

-- life_user_dynamics.phone_id
UPDATE life_user_dynamics lud
    INNER JOIN life_user lu ON lud.phone_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(lud.phone_id, 6) AND lu.delete_flag = 0
SET lud.phone_user_type = 1, lud.phone_ref_id = lu.id
WHERE lud.phone_user_type IS NULL AND lud.phone_id LIKE 'user\_%';

UPDATE life_user_dynamics lud
    INNER JOIN store_user su ON lud.phone_id LIKE 'store\_%' AND su.phone = SUBSTRING(lud.phone_id, 7) AND su.delete_flag = 0
SET lud.phone_user_type = 2, lud.phone_ref_id = su.id
WHERE lud.phone_user_type IS NULL AND lud.phone_id LIKE 'store\_%';

UPDATE life_user_dynamics lud
    INNER JOIN lawyer_user lwu ON lud.phone_id LIKE 'lawyer\_%' AND lwu.phone = SUBSTRING(lud.phone_id, 8) AND lwu.delete_flag = 0
SET lud.phone_user_type = 3, lud.phone_ref_id = lwu.id
WHERE lud.phone_user_type IS NULL AND lud.phone_id LIKE 'lawyer\_%';

-- life_message.sender_id / receiver_id
UPDATE life_message lm
    INNER JOIN life_user lu ON lm.sender_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(lm.sender_id, 6) AND lu.delete_flag = 0
SET lm.sender_user_type = 1, lm.sender_ref_id = lu.id
WHERE lm.sender_user_type IS NULL AND lm.sender_id LIKE 'user\_%';

UPDATE life_message lm
    INNER JOIN store_user su ON lm.sender_id LIKE 'store\_%' AND su.phone = SUBSTRING(lm.sender_id, 7) AND su.delete_flag = 0
SET lm.sender_user_type = 2, lm.sender_ref_id = su.id
WHERE lm.sender_user_type IS NULL AND lm.sender_id LIKE 'store\_%';

UPDATE life_message lm
    INNER JOIN lawyer_user lwu ON lm.sender_id LIKE 'lawyer\_%' AND lwu.phone = SUBSTRING(lm.sender_id, 8) AND lwu.delete_flag = 0
SET lm.sender_user_type = 3, lm.sender_ref_id = lwu.id
WHERE lm.sender_user_type IS NULL AND lm.sender_id LIKE 'lawyer\_%';

UPDATE life_message lm
    INNER JOIN life_user lu ON lm.receiver_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(lm.receiver_id, 6) AND lu.delete_flag = 0
SET lm.receiver_user_type = 1, lm.receiver_ref_id = lu.id
WHERE lm.receiver_user_type IS NULL AND lm.receiver_id LIKE 'user\_%';

UPDATE life_message lm
    INNER JOIN store_user su ON lm.receiver_id LIKE 'store\_%' AND su.phone = SUBSTRING(lm.receiver_id, 7) AND su.delete_flag = 0
SET lm.receiver_user_type = 2, lm.receiver_ref_id = su.id
WHERE lm.receiver_user_type IS NULL AND lm.receiver_id LIKE 'store\_%';

UPDATE life_message lm
    INNER JOIN lawyer_user lwu ON lm.receiver_id LIKE 'lawyer\_%' AND lwu.phone = SUBSTRING(lm.receiver_id, 8) AND lwu.delete_flag = 0
SET lm.receiver_user_type = 3, lm.receiver_ref_id = lwu.id
WHERE lm.receiver_user_type IS NULL AND lm.receiver_id LIKE 'lawyer\_%';

-- life_notice.sender_id / receiver_id（system 等非身份键前缀跳过）
UPDATE life_notice ln
    INNER JOIN life_user lu ON ln.sender_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(ln.sender_id, 6) AND lu.delete_flag = 0
SET ln.sender_user_type = 1, ln.sender_ref_id = lu.id
WHERE ln.sender_user_type IS NULL AND ln.sender_id LIKE 'user\_%';

UPDATE life_notice ln
    INNER JOIN store_user su ON ln.sender_id LIKE 'store\_%' AND su.phone = SUBSTRING(ln.sender_id, 7) AND su.delete_flag = 0
SET ln.sender_user_type = 2, ln.sender_ref_id = su.id
WHERE ln.sender_user_type IS NULL AND ln.sender_id LIKE 'store\_%';

UPDATE life_notice ln
    INNER JOIN lawyer_user lwu ON ln.sender_id LIKE 'lawyer\_%' AND lwu.phone = SUBSTRING(ln.sender_id, 8) AND lwu.delete_flag = 0
SET ln.sender_user_type = 3, ln.sender_ref_id = lwu.id
WHERE ln.sender_user_type IS NULL AND ln.sender_id LIKE 'lawyer\_%';

UPDATE life_notice ln
    INNER JOIN life_user lu ON ln.receiver_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(ln.receiver_id, 6) AND lu.delete_flag = 0
SET ln.receiver_user_type = 1, ln.receiver_ref_id = lu.id
WHERE ln.receiver_user_type IS NULL AND ln.receiver_id LIKE 'user\_%';

UPDATE life_notice ln
    INNER JOIN store_user su ON ln.receiver_id LIKE 'store\_%' AND su.phone = SUBSTRING(ln.receiver_id, 7) AND su.delete_flag = 0
SET ln.receiver_user_type = 2, ln.receiver_ref_id = su.id
WHERE ln.receiver_user_type IS NULL AND ln.receiver_id LIKE 'store\_%';

UPDATE life_notice ln
    INNER JOIN lawyer_user lwu ON ln.receiver_id LIKE 'lawyer\_%' AND lwu.phone = SUBSTRING(ln.receiver_id, 8) AND lwu.delete_flag = 0
SET ln.receiver_user_type = 3, ln.receiver_ref_id = lwu.id
WHERE ln.receiver_user_type IS NULL AND ln.receiver_id LIKE 'lawyer\_%';

-- life_like_record.dianzan_id
UPDATE life_like_record llr
    INNER JOIN life_user lu ON llr.dianzan_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(llr.dianzan_id, 6) AND lu.delete_flag = 0
SET llr.dianzan_user_type = 1, llr.dianzan_ref_id = lu.id
WHERE llr.dianzan_user_type IS NULL AND llr.dianzan_id LIKE 'user\_%';

UPDATE life_like_record llr
    INNER JOIN store_user su ON llr.dianzan_id LIKE 'store\_%' AND su.phone = SUBSTRING(llr.dianzan_id, 7) AND su.delete_flag = 0
SET llr.dianzan_user_type = 2, llr.dianzan_ref_id = su.id
WHERE llr.dianzan_user_type IS NULL AND llr.dianzan_id LIKE 'store\_%';

-- store_clock_in.phone_id（当前业务多为 user_）
UPDATE store_clock_in sci
    INNER JOIN life_user lu ON sci.phone_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(sci.phone_id, 6) AND lu.delete_flag = 0
SET sci.phone_user_type = 1, sci.phone_ref_id = lu.id
WHERE sci.phone_user_type IS NULL AND sci.phone_id LIKE 'user\_%';

UPDATE store_clock_in sci
    INNER JOIN store_user su ON sci.phone_id LIKE 'store\_%' AND su.phone = SUBSTRING(sci.phone_id, 7) AND su.delete_flag = 0
SET sci.phone_user_type = 2, sci.phone_ref_id = su.id
WHERE sci.phone_user_type IS NULL AND sci.phone_id LIKE 'store\_%';

-- life_collect.user_id（仅回填 user_{phone} 格式；纯数字历史 userId 需另行处理）
UPDATE life_collect lc
    INNER JOIN life_user lu ON lc.user_id LIKE 'user\_%' AND lu.user_phone = SUBSTRING(lc.user_id, 6) AND lu.delete_flag = 0
SET lc.user_id_user_type = 1, lc.user_id_ref_id = lu.id
WHERE lc.user_id_user_type IS NULL AND lc.user_id LIKE 'user\_%';

UPDATE life_collect lc
    INNER JOIN store_user su ON lc.user_id LIKE 'store\_%' AND su.phone = SUBSTRING(lc.user_id, 7) AND su.delete_flag = 0
SET lc.user_id_user_type = 2, lc.user_id_ref_id = su.id
WHERE lc.user_id_user_type IS NULL AND lc.user_id LIKE 'store\_%';
