-- 赠送方 / 接收方语义明确：与旧列保持同步回填，旧列仍可被历史脚本读取

ALTER TABLE `life_discount_coupon_store_friend`

  ADD COLUMN `receiver_store_id` int DEFAULT NULL COMMENT '接收方店铺(store_info.id)' AFTER `friend_store_user_id`,

  ADD COLUMN `receiver_user_id` int DEFAULT NULL COMMENT '接收方商户用户(store_user.id)，可选' AFTER `receiver_store_id`,

  ADD COLUMN `sender_store_id` int DEFAULT NULL COMMENT '赠送方店铺(store_info.id)' AFTER `receiver_user_id`,

  ADD COLUMN `sender_user_id` int DEFAULT NULL COMMENT '赠送方商户用户(store_user.id)' AFTER `sender_store_id`;



-- 接收店铺：沿用原 store_user_id（实为收到券的门店 id）

UPDATE `life_discount_coupon_store_friend`

SET `receiver_store_id` = `store_user_id`

WHERE `receiver_store_id` IS NULL AND `store_user_id` IS NOT NULL;



-- 赠送人：沿用原 friend_store_user_id（历史数据可能含错误值，仅作尽力回填）

UPDATE `life_discount_coupon_store_friend`

SET `sender_user_id` = `friend_store_user_id`

WHERE `sender_user_id` IS NULL AND `friend_store_user_id` IS NOT NULL;



UPDATE `life_discount_coupon_store_friend` sf

INNER JOIN `store_user` su ON su.id = sf.`sender_user_id` AND COALESCE(su.delete_flag, 0) = 0

SET sf.`sender_store_id` = su.`store_id`

WHERE sf.`sender_store_id` IS NULL AND sf.`sender_user_id` IS NOT NULL;



-- 建议（首次上线后按需执行）：为「我收到的/我送出的」列表增加索引可加速查询

-- ALTER TABLE life_discount_coupon_store_friend ADD INDEX idx_ldcsf_receiver_del (receiver_store_id, delete_flag);

-- ALTER TABLE life_discount_coupon_store_friend ADD INDEX idx_ldcsf_sender_del (sender_user_id, delete_flag);

