-- L2 每个课时首次通关耗时统计
-- 遍历 8 张 user_chapter_play_record 分表

WITH l2_chapters AS (
  SELECT id AS chapter_id, course_season, course_unit, course_lesson
  FROM bi_level_unit_lesson
  WHERE course_level = 'L2'
),
all_first_done AS (
  SELECT chapter_id, user_id, chapter_unique_id, first_done_time
  FROM (
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_0
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_1
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_2
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_3
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_4
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_5
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_6
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
    UNION ALL
    SELECT chapter_id, user_id, chapter_unique_id, MIN(created_at) AS first_done_time
    FROM bi_user_chapter_play_record_7
    WHERE play_status = 1 AND chapter_id IN (SELECT chapter_id FROM l2_chapters)
    GROUP BY chapter_id, user_id, chapter_unique_id
  ) t
),
chapter_time AS (
  SELECT 
    fd.chapter_id,
    fd.chapter_unique_id,
    SUM(comp.interval_time) AS total_ms
  FROM all_first_done fd
  JOIN LATERAL (
    SELECT SUM(c.interval_time) AS interval_time
    FROM bi_user_component_play_record_0 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_1 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_2 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_3 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_4 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_5 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_6 c WHERE c.chapter_unique_id = fd.chapter_unique_id
    UNION ALL
    SELECT SUM(c.interval_time) FROM bi_user_component_play_record_7 c WHERE c.chapter_unique_id = fd.chapter_unique_id
  ) comp ON true
  GROUP BY fd.chapter_id, fd.chapter_unique_id
)
SELECT 
  lc.course_season,
  lc.course_unit,
  lc.course_lesson,
  lc.chapter_id,
  COUNT(*) AS 完成人数,
  ROUND(AVG(ct.total_ms) / 60000.0, 1) AS 平均通关用时_分钟
FROM l2_chapters lc
LEFT JOIN chapter_time ct ON lc.chapter_id = ct.chapter_id
GROUP BY lc.course_season, lc.course_unit, lc.course_lesson, lc.chapter_id
ORDER BY lc.course_season, lc.course_unit, lc.course_lesson;
