-- 取当天推送的订单数据里面看订单日期 重新汇总统计
WITH data_tmp AS
(
    SELECT t2.relation_id
            ,t2.product_id prodid
            ,t2.game_channel_id
            ,t2.game_identity
            ,t2.game_platform_id
            ,t1.ds
    FROM    ods.ORDER t1
    INNER JOIN dim.game_product_relation t2
    ON      t1.channel_id = t2.game_channel_id
    AND     t1.game_identity = t2.game_identity
    AND     t1.platform_id = t2.game_platform_id
    WHERE   t1.receive_time BETWEEN '${biz-date}' AND '${intra-day}'
    GROUP BY t2.relation_id
            ,t2.product_id
            ,t2.game_channel_id
            ,t2.game_identity
            ,t2.game_platform_id
            ,t1.ds
)
INSERT INTO "dwd"."order_user_df" (relation_id,prodid,game_channel_id,game_identity,game_platform_id,user_id,ds,pay_amount,ds_date)
SELECT  t1.relation_id
        ,t1.prodid
        ,t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,t2.user_id
        ,t1.ds ds
        ,sum(pay_amount) pay_amount
        ,'${biz-date}' ds_date
FROM    data_tmp t1
INNER JOIN ods.ORDER t2
ON      t1.game_channel_id = t2.channel_id
AND     t1.game_identity = t2.game_identity
AND     t1.game_platform_id = t2.platform_id
and     t1.ds=t2.ds
group by t1.relation_id
        ,t1.prodid
        ,t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,t2.user_id
        ,t1.ds
ON CONFLICT (relation_id, user_id, ds)
    DO UPDATE SET prodid = EXCLUDED.prodid,game_channel_id = EXCLUDED.game_channel_id,game_identity = EXCLUDED.game_identity,game_platform_id = EXCLUDED.game_platform_id,
    ds = EXCLUDED.ds,user_id = EXCLUDED.user_id,pay_amount = EXCLUDED.pay_amount,ds_date = EXCLUDED.ds_date
;