-- L1 U00 151人 和 L2 U00 216人，最后完课在 U00 各 Lesson 的分布

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
    )
),
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_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_last_done AS (
    SELECT DISTINCT ON (nda.account_id) 
        nda.account_id,
        nda.level,
        uld.chapter_id
    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
),
-- 筛选最后完课在 U00 的用户
u00_users AS (
    SELECT ald.account_id, ald.level, ald.chapter_id, lul.course_level, lul.course_lesson
    FROM account_last_done ald
    JOIN bi_level_unit_lesson lul ON ald.chapter_id = lul.id
    WHERE lul.course_unit = 'U00' AND lul.course_level IN ('L1', 'L2')
)
SELECT 
    course_level AS "等级",
    course_lesson AS "课时",
    COUNT(DISTINCT account_id) AS "用户数"
FROM u00_users
GROUP BY course_level, course_lesson
ORDER BY course_level, course_lesson;
