-- 组合得失漏斗 v_combination_funnel：按 bre_cross_combination 汇总六级派生指标。
-- 花(bre_pollination) → 果(effective_count) → 种(bre_seed_lot) → 苗(bre_seedling)
--   → 定植(bre_planting) → 树(bre_tree) → 入选(bre_selection_result)。
-- 数据源全为既有表，仅聚合不改写源数据；普通 VIEW 保 fresh（F2 决策：统计管线用普通 VIEW）。
-- 幂等：DROP IF EXISTS + CREATE OR REPLACE，可重跑。
DROP VIEW IF EXISTS v_combination_funnel;
CREATE OR REPLACE VIEW v_combination_funnel AS
WITH p AS (  -- 花 → 果
    SELECT combination_id,
           SUM(flower_count)   AS flower_count,
           SUM(effective_count) AS effective_count
    FROM bre_pollination
    WHERE is_deleted = false
    GROUP BY combination_id
), sl AS (  -- 果 → 种
    SELECT combination_id,
           SUM(seed_count)       AS seed_count,
           AVG(germination_rate) AS germination_rate
    FROM bre_seed_lot
    WHERE is_deleted = false
    GROUP BY combination_id
), sd AS (  -- 种 → 苗
    SELECT combination_id,
           SUM(seedling_count)        AS seedling_count,
           SUM(strong_seedling_count) AS strong_seedling_count
    FROM bre_seedling
    WHERE is_deleted = false
    GROUP BY combination_id
), pt AS (  -- 苗 → 定植
    SELECT combination_id,
           SUM(tree_count) AS planting_count
    FROM bre_planting
    WHERE is_deleted = false
    GROUP BY combination_id
), tr AS (  -- 定植 → 单株（非软删全计，含死亡，作评价群体基数）
    SELECT combination_id,
           COUNT(*) AS tree_count
    FROM bre_tree
    WHERE is_deleted = false
    GROUP BY combination_id
), sr AS (  -- 单株 → 入选
    SELECT combination_id,
           COUNT(*) AS selected_count
    FROM bre_selection_result
    WHERE is_deleted = false
    GROUP BY combination_id
)
SELECT
    cc.id                     AS combination_id,
    cc.combination_code,
    cc.cross_year,
    cc.female_parent_id,
    cc.male_parent_id,
    cc.design_type,
    cc.stage,
    -- 花 → 果
    p.flower_count,
    p.effective_count,
    CASE WHEN p.flower_count > 0
         THEN ROUND(p.effective_count::numeric / p.flower_count * 100, 2)
         ELSE NULL END        AS fruit_set_rate,          -- 结实率(%)
    -- 果 → 种
    sl.seed_count,
    sl.germination_rate,                                  -- 种子批平均发芽率(%)
    -- 种 → 苗
    sd.seedling_count,
    sd.strong_seedling_count,
    CASE WHEN sl.seed_count > 0
         THEN ROUND(sd.seedling_count::numeric / sl.seed_count * 100, 2)
         ELSE NULL END        AS emergence_rate,          -- 出苗率(%)
    -- 苗 → 定植
    pt.planting_count,
    -- 定植 → 单株
    tr.tree_count,
    -- 单株 → 入选（选择强度：入选率）
    sr.selected_count,
    CASE WHEN tr.tree_count > 0
         THEN ROUND(sr.selected_count::numeric / tr.tree_count * 100, 2)
         ELSE NULL END        AS selection_rate           -- 入选率/选择强度(%)
FROM bre_cross_combination cc
LEFT JOIN p  ON p.combination_id  = cc.id
LEFT JOIN sl ON sl.combination_id = cc.id
LEFT JOIN sd ON sd.combination_id = cc.id
LEFT JOIN pt ON pt.combination_id = cc.id
LEFT JOIN tr ON tr.combination_id = cc.id
LEFT JOIN sr ON sr.combination_id = cc.id
WHERE cc.is_deleted = false;
