-- ====================================================
-- V8 清理幻影站点 "000" 历史数据
-- 背景：自主注册用户充值时代码硬编码 stationId="000"，
-- 导致 t_split_record / t_settlement_record 积攒了无效站点数据
-- 发布日期：2026-06-05
-- ====================================================

-- ----------------------------
-- 1. 迁移可追溯的分账记录：将已归属用户的 "000" SplitRecord 更新为实际站点
--    通过 t_pay_log 和 t_user 关联找到用户的真实 station_id
-- ----------------------------
UPDATE t_split_record sr
INNER JOIN t_pay_log pl ON sr.trade_no = pl.transaction_id
INNER JOIN t_user u ON pl.user_id = u.id
SET sr.from_station_id = u.station_id,
    sr.to_station_id   = u.station_id
WHERE sr.type = 1
  AND sr.from_station_id = '000'
  AND sr.to_station_id   = '000'
  AND u.station_id IS NOT NULL
  AND u.station_id != ''
  AND u.station_id != '000';

-- ----------------------------
-- 2. 删除无法追溯的残余 "000" 分账记录
--    即便残留全部删除也无业务影响：
--    - 用户再次消费时会触发 OrderSettlementServiceImpl.backfillUnattributedRecharges 补建
--    - 不消费的用户充值余额始终在钱包中，无需分账
-- ----------------------------
DELETE FROM t_split_record
WHERE from_station_id = '000'
   OR to_station_id   = '000';

-- ----------------------------
-- 3. 清理 "000" 站点的结算记录
-- ----------------------------
DELETE FROM t_settlement_record
WHERE station_id = '000';

-- ----------------------------
-- 4. 清理 "000" 站点的账户记录
-- ----------------------------
DELETE FROM t_station_account
WHERE station_id = '000';

-- ----------------------------
-- 5. 清理 "000" 站点的充值配置分组关联（如有）
-- ----------------------------
DELETE FROM t_recharge_config_station_group
WHERE station_id = '000';

DELETE FROM t_recharge_config_group
WHERE name = '000';
