-- 部署 jituan 前若 migrate 报 Duplicate entry 唯一约束冲突时使用。
-- 不要用 mysql 命令行（1Panel  MySQL 不在本机 socket），请用：
--   python manage.py dbshell < deploy/pre_migrate_dedupe.sql
-- 或半路失败的 config.0006：
--   python manage.py recover_config_0006
--   python manage.py migrate config 0006 --fake
--   python manage.py migrate

-- config.0006 tupianpeizhi
DELETE FROM tupianpeizhi
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM tupianpeizhi GROUP BY club_id, ImageType
    ) AS t
);

-- config.0006 popup_page
DELETE FROM popup_page
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM popup_page GROUP BY club_id, page_key
    ) AS t
);

-- products.0005 duoci_fenhong
DELETE FROM duoci_fenhong
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM duoci_fenhong GROUP BY club_id, huiyuan, yonghuid, cishu
    ) AS t
);

-- backend.0003 提现日统计
DELETE FROM tixian_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM tixian_ri_tongji GROUP BY club_id, Date, WithdrawalType
    ) AS t
);

-- backend.0004 角色日统计
DELETE FROM shangjia_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM shangjia_ri_tongji GROUP BY club_id, MerchantID, Date
    ) AS t
);
DELETE FROM dashou_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM dashou_ri_tongji GROUP BY club_id, PlayerID, Date
    ) AS t
);
DELETE FROM zuzhang_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM zuzhang_ri_tongji GROUP BY club_id, LeaderID, Date
    ) AS t
);
DELETE FROM guanshi_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM guanshi_ri_tongji GROUP BY club_id, ManagerID, Date
    ) AS t
);
DELETE FROM guanshi_xufei_ri_tongji
WHERE id NOT IN (
    SELECT keep_id FROM (
        SELECT MAX(id) AS keep_id FROM guanshi_xufei_ri_tongji GROUP BY club_id, ManagerID, Date
    ) AS t
);

-- szjilu 应只有 1 行；多行时勿自动删，需人工核对 TotalIncome 后处理
