-- 根据游戏汇总订单数据
-- 增量逻辑 由于数据会存在延迟推送或者新游戏接入数据补数据
-- 天:取当天推送的订单数据里面看订单日期有哪些天的订单把那几天的数据拉出来重新汇总统计
-- 月:取当天推送的订单数据里面看订单日期有哪些月的订单把那几天的数据拉出来重新汇总统计
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_df(game_channel_id,game_identity,game_platform_id,order_amount,pay_amount,pay_user_num,ds,ds_type,ds_name)
SELECT  t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,SUM(t2.amount) order_amount
        ,SUM(t2.pay_amount) pay_amount
        ,count(distinct t2.user_id) pay_user_num
        ,t2.ds ds
        ,'d' ds_type
        ,'天' ds_name
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.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,t2.ds
ON CONFLICT (game_channel_id,game_identity,game_platform_id,ds,ds_type)
DO UPDATE SET order_amount = EXCLUDED.order_amount,pay_amount = EXCLUDED.pay_amount,pay_user_num = EXCLUDED.pay_user_num
;


WITH data_tmp AS
(
    SELECT t2.relation_id
            ,t2.product_id prodid
            ,t2.game_channel_id
            ,t2.game_identity
            ,t2.game_platform_id
            ,substring(t1.ds,1,6) || '00' min_month
            ,substring(t1.ds,1,6) || '32' max_month
    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
            ,substring(t1.ds,1,6) || '00'
            ,substring(t1.ds,1,6) || '32'
)
INSERT INTO dwd.order_df(game_channel_id,game_identity,game_platform_id,order_amount,pay_amount,pay_user_num,ds,ds_type,ds_name)
SELECT  t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,SUM(t2.amount) order_amount
        ,SUM(t2.pay_amount) pay_amount
        ,count(distinct t2.user_id) pay_user_num
        ,substring(t2.ds,1,6) || '00' ds
        ,'m' ds_type
        ,'月' ds_name
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.min_month <t2.ds
AND     t1.max_month >t2.ds
GROUP BY t1.game_channel_id
        ,t1.game_identity
        ,t1.game_platform_id
        ,substring(t2.ds,1,6) || '00'
ON CONFLICT (game_channel_id,game_identity,game_platform_id,ds,ds_type)
DO UPDATE SET order_amount = EXCLUDED.order_amount,pay_amount = EXCLUDED.pay_amount,pay_user_num = EXCLUDED.pay_user_num
;
