-- 近14天无完课行为的付费用户，最后一次完课分布在哪个 Level/Unit（优化版）
-- 先筛选无完课用户ID集合，再查分表，避免全表扫描

WITH paying_user_goods AS (
    SELECT 
        o.account_id,
        array_agg(DISTINCT o.goods_id) AS goods_ids
    FROM bi_vala_order o
    JOIN bi_vala_app_account a ON o.account_id = a.id AND a.status = 1
    WHERE o.pay_success_date IS NOT NULL 
      AND o.order_status = 3
    GROUP BY o.account_id
),
user_level AS (
    SELECT 
        account_id,
        CASE
            WHEN 61 = ANY(goods_ids) THEN 'L1+L2'
            WHEN (57 = ANY(goods_ids) OR 60 = ANY(goods_ids) OR 63 = ANY(goods_ids))
                 AND (31 = ANY(goods_ids) OR 32 = ANY(goods_ids) OR 33 = ANY(goods_ids) OR 54 = ANY(goods_ids))
                 THEN 'L1+L2'
            WHEN (57 = ANY(goods_ids) OR 60 = ANY(goods_ids) OR 63 = ANY(goods_ids)) THEN '仅L1'
            WHEN (31 = ANY(goods_ids) OR 32 = ANY(goods_ids) OR 33 = ANY(goods_ids) OR 54 = ANY(goods_ids)) THEN '仅L2'
            ELSE '其他'
        END AS level
    FROM paying_user_goods
),
done_14d AS (
    SELECT DISTINCT user_id
    FROM (
        SELECT user_id FROM bi_user_chapter_play_record_0 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_1 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_2 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_3 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_4 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_5 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_6 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
        UNION
        SELECT user_id FROM bi_user_chapter_play_record_7 WHERE play_status = 1 AND created_at >= '2026-05-09' AND created_at < '2026-05-23'
    ) t
),
no_done_accounts AS (
    SELECT DISTINCT ul.account_id, ul.level
    FROM user_level ul
    WHERE NOT EXISTS (
        SELECT 1 FROM bi_vala_app_character c
        JOIN done_14d d ON c.id = d.user_id
        WHERE c.account_id = ul.account_id
    )
),
-- 缩小范围：只查无完课用户对应的 user_id
no_done_user_ids AS (
    SELECT DISTINCT c.id AS user_id
    FROM no_done_accounts nda
    JOIN bi_vala_app_character c ON nda.account_id = c.account_id
),
-- 每个 user_id 的最后一次完课记录（只扫无完课用户）
user_last_done AS (
    SELECT user_id, chapter_id, created_at
    FROM (
        SELECT user_id, chapter_id, created_at,
            ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC) AS rn
        FROM (
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_0 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_1 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_2 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_3 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_4 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_5 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_6 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
            UNION ALL
            SELECT pr.user_id, pr.chapter_id, pr.created_at 
            FROM bi_user_chapter_play_record_7 pr
            JOIN no_done_user_ids n ON pr.user_id = n.user_id
            WHERE pr.play_status = 1
        ) t
    ) t2 WHERE rn = 1
),
-- account 级别：取所有角色中最近一次完课
account_last_done AS (
    SELECT DISTINCT ON (nda.account_id) 
        nda.account_id,
        nda.level,
        uld.chapter_id,
        uld.created_at
    FROM no_done_accounts nda
    JOIN bi_vala_app_character c ON nda.account_id = c.account_id
    JOIN user_last_done uld ON c.id = uld.user_id
    ORDER BY nda.account_id, uld.created_at DESC
)
-- 映射到 Level/Unit 统计
SELECT 
    lul.course_level AS "课程等级",
    lul.course_unit AS "单元",
    COUNT(DISTINCT ald.account_id) AS "用户数"
FROM account_last_done ald
JOIN bi_level_unit_lesson lul ON ald.chapter_id = lul.id
GROUP BY lul.course_level, lul.course_unit
ORDER BY lul.course_level, lul.course_unit;
