-- 预处理：解析 scoresmap + page 分类
-- v3: 新增 COPC（校准系数 = 真实值/预测值）
WITH t_raw AS
(
    SELECT  *
            ,REPLACE(GET_JSON_OBJECT(extend_alg,'$.scoresMap'),"\\","") AS scoresmap
            ,CASE   WHEN page IN ("回流后沉浸页&内页feed","详情后沉浸页","首页feed","详情页") THEN "推荐"
                    WHEN page IN ("回流页","其他") THEN "非推荐"
                    ELSE "其他"
            END AS page_type
    FROM    loghubods.dwd_recsys_alg_sample_all_20250212
    WHERE   dt = '${dt}'
    AND     apptype IN ("0","4")
    AND     abcode IN ("ab0","ab1","ab2","ab3","ab4","ab5","ab6","ab7","ab8","ab9")
    AND     abcode NOT IN ("ab100")
    AND     extend_alg IS NOT NULL
    AND     GET_JSON_OBJECT(extend_alg,'$.scoresMap') IS NOT NULL

)
-- 过滤：只保留推荐页面
,t_filtered AS
(
    SELECT  *
    FROM    t_raw
    WHERE   page_type = "推荐"
)


-- 特征提取与维度映射
,t_base AS
(
    SELECT  dt
            ,apptype
            ,CASE   WHEN apptype IN ("4") AND abcode IN ("ab0","ab1") THEN "实验组-先验地域降权"
                    WHEN apptype IN ("4") AND abcode IN ("ab6","ab7") THEN "实验组-str+校准&ros-统计量"
                    WHEN apptype IN ("4") AND abcode IN ("ab8","ab9") THEN "实验组-str+校准"
                    WHEN apptype IN ("4") AND abcode IN ("ab2","ab3","ab4","ab5") THEN "对照组"
                    ELSE "其他"
            END AS abcode
            ,page_type AS page
            ,mid
            ,vid
            ,is_share
            ,share_cnt
            ,is_return_1
            ,is_return_n
            ,is_return_noself
            ,return_1_uv
            ,return_n_uv
            ,return_n_uv_noself
            ,new_exposure_cnt
            ,flowpool
            ,scoresmap
            ,CAST(GET_JSON_OBJECT(scoresmap,'$.fmRov') AS DOUBLE) AS str_pred
            ,1.22 * pow(CAST(GET_JSON_OBJECT(scoresmap,'$.NorXGBScore') AS DOUBLE), 1.15) AS ros_pred
            ,CAST(GET_JSON_OBJECT(scoresmap,'$.hasReturnRovScore') AS DOUBLE) AS ros_stat
    FROM    t_filtered
)
,t_valid AS
(
    SELECT  *
    FROM    t_base
    WHERE   str_pred IS NOT NULL
    AND     ros_pred IS NOT NULL
)
SELECT  dt
        ,COALESCE(apptype,"sum") AS apptype
        ,COALESCE(abcode,"sum") AS abcode
        -- COPC（校准系数 = 真实值/预测值，>1表示低估，<1表示高估）
        ,round((SUM(is_return_noself) / COUNT(1)) / NULLIF(SUM(str_pred) / COUNT(1), 0), 4) AS str_copc
        ,round((SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0)) / NULLIF(SUM(ros_pred) / COUNT(1), 0), 4) AS ros_copc
        ,round((SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0)) / NULLIF(SUM(ros_stat) / COUNT(1), 0), 4) AS ros_stat_copc
        ,round((SUM(return_n_uv_noself) / COUNT(1)) / NULLIF(AVG(str_pred * ros_pred), 0), 4) AS rovn_copc
        ,round((SUM(return_n_uv_noself) / COUNT(1)) / NULLIF(AVG(str_pred * ros_stat), 0), 4) AS rovn_stat_copc
        -- 模型预测与真实值对比指标
        ,round(COALESCE(SUM(is_return_noself) / COUNT(1),0),6) AS str_real
        ,round(COALESCE(SUM(str_pred) / COUNT(1),0),6) AS str_pred

        ,round(COALESCE(SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0),0),6) AS ros_real
        ,round(COALESCE(SUM(ros_pred) / COUNT(1),0),6) AS ros_pred
        ,round(COALESCE(SUM(ros_stat) / COUNT(1),0),6) AS ros_stat

        ,round(SUM(return_n_uv_noself) / COUNT(1), 6) AS rovn_real
        ,round(AVG(str_pred * ros_pred), 6) AS rovn_pred
        ,round(AVG(str_pred * ros_stat), 6) AS rovn_stat

        -- 预测误差指标
        ,round(AVG(ABS(ros_pred - return_n_uv_noself)),6) AS ros_pred_mae
        ,round(AVG(ABS(ros_stat - return_n_uv_noself)),6) AS ros_stat_mae
        -- 业务指标
        ,round(COALESCE(COUNT(1) / COUNT(DISTINCT mid),0),2) AS exp_per_dau
        ,round(COALESCE(SUM(is_share) / COUNT(1),0),6) AS str_one
        ,round(COALESCE(SUM(return_n_uv) / SUM(is_share),0),6) AS ros_one
        ,round(COALESCE(SUM(share_cnt) / COUNT(1),0),6) AS str
        ,round(COALESCE(SUM(return_n_uv) / SUM(share_cnt),0),6) AS ros
        ,round(COALESCE(SUM(is_return_1) / COUNT(1),0),6) AS str_plus
        ,round(COALESCE(SUM(return_n_uv) / SUM(is_return_1),0),6) AS ros_minus
        ,round(COALESCE(SUM(return_n_uv) / COUNT(1),0),6) AS rovn
        ,round(COALESCE(SUM(new_exposure_cnt) / COUNT(1),0),6) AS vovh24
        ,COUNT(DISTINCT mid) AS dau
        ,COUNT(1) AS exp
        ,COALESCE(SUM(is_share),0) AS is_share
        ,COALESCE(SUM(share_cnt),0) AS share_cnt
        ,COALESCE(SUM(is_return_1),0) AS is_return_1
        ,COALESCE(SUM(return_n_uv),0) AS return_n_uv
        ,COALESCE(SUM(new_exposure_cnt),0) AS viewh24
        ,COALESCE(SUM(return_n_uv_noself),0) AS return_n_uv_noself
FROM    t_valid
GROUP BY dt
         ,apptype
         ,abcode
ORDER BY dt DESC,apptype,abcode
;
