CREATE TABLE datasci.ads_996eco_index_box_m (
    index_type text,
    data_month text,
    is_box text,
    act_cnt bigint,
    pay_cnt bigint,
    pay_amount bigint,
    mau bigint,
    device_type text,
    device_type_new text,
    act_num bigint,
    reg_count bigint,

);


DELETE FROM datasci.ads_996eco_index_box_m WHERE ds = '${month}';


--付费人数和金额
WITH ga AS
(
    SELECT  id
            ,name
            ,bind_game_id
    FROM    public.gm_apply
    WHERE   bind_type = 1
    AND     bind_game_type = 1
)
,box_users AS
(
    SELECT  user_id
    FROM    public.ods_newly_account
    WHERE   appid = 1015
    GROUP BY user_id
)
,po AS
(
    SELECT  appid
            ,user_id
            ,amount / 100 AS pay_amount
    FROM    public.ods_payment_order
    WHERE   ds BETWEEN '${bizmonth}' AND '${bizdate}'
    AND     status = 1
)
,aa AS
(
    SELECT  appid
            ,user_id
    FROM    public.ods_active_account
    WHERE   ds BETWEEN '${bizmonth}' AND '${bizdate}'
)
INSERT INTO
    datasci.ads_996eco_index_box_m (index_type,data_month,is_box,act_cnt,pay_cnt,pay_amount)
SELECT  'pay' AS index_type
        ,'${month}' data_month
        ,t1.is_box
        ,t1.act_cnt
        ,t2.pay_cnt
        ,t2.pay_amount
FROM    (
            SELECT  CASE   WHEN bu.user_id IS NOT NULL THEN '盒子'
                            ELSE '非盒子'
                    END AS is_box
                    ,UNIQ(aa.user_id) AS act_cnt
            FROM    aa
            INNER JOIN ga
            ON      aa.appid = ga.id
            LEFT JOIN box_users bu
            ON      bu.user_id = aa.user_id
            GROUP BY 1
        ) t1
LEFT JOIN   (
                SELECT  CASE   WHEN bu.user_id IS NOT NULL THEN '盒子'
                                ELSE '非盒子'
                        END AS is_box
                        ,UNIQ(po.user_id) AS pay_cnt
                        ,SUM(po.pay_amount) AS pay_amount
                FROM    po
                INNER JOIN ga
                ON      po.appid = ga.id
                LEFT JOIN box_users bu
                ON      bu.user_id = po.user_id
                GROUP BY 1
            ) t2
ON      t1.is_box = t2.is_box
;

-- mau
WITH game_apply AS
(
    SELECT  id
            ,name
            ,bind_game_id
    FROM    public.gm_apply
    WHERE   bind_game_type = 1
)
INSERT INTO
    datasci.ads_996eco_index_box_m (index_type,data_month,is_box,mau)
SELECT  'mau' index_type
        ,'${month}' data_month
        ,CASE   WHEN grouping(user_flag) = 0 THEN user_flag
                ELSE '总计'
        END AS is_box
        ,uniq(user_id) AS mau
FROM    (
            SELECT  CASE   WHEN t1.channel LIKE '%996box%' THEN '盒子'
                            ELSE '非盒子'
                    END AS user_flag
                    ,user_id
            FROM    public.ods_active_account t1
            INNER JOIN game_apply t2
            ON      t1.appid = t2.id
            WHERE   t1.ds BETWEEN '${hisdate}' AND '${bizdate}'
        ) t
GROUP BY user_flag
;

-- mau-device
WITH game_apply AS
(
    SELECT  id
            ,name
            ,bind_game_id
    FROM    public.gm_apply
    WHERE   bind_game_type = 1
)
INSERT INTO
    datasci.ads_996eco_index_box_m (index_type,data_month,is_box,device_type,device_type_new,act_num)
SELECT  'mau-device' index_type
        ,'${month}' AS data_month
        ,CASE   WHEN t1.channel LIKE '%996box%' THEN '盒子'
                ELSE '非盒子'
        END AS is_box
        ,t1.device_type
        ,CASE   WHEN lower(t1.device_type) LIKE '%h5%' THEN 'H5'
                WHEN lower(t1.device_type) LIKE '%pc%' THEN 'PC'
                WHEN lower(t1.device_type) LIKE '%ios%' THEN 'IOS'
                ELSE t1.device_type
        END AS device_type_new
        ,uniq(user_id) AS act_num
FROM    public.ods_active_account t1
INNER JOIN game_apply t2
ON      t1.appid = t2.id
WHERE   t1.ds BETWEEN '${hisdate}' AND '${bizdate}'
GROUP BY 1,2,3,4
;


-- reg
WITH legend_apps AS
(
    SELECT  id AS appid
    FROM    gm_apply
    WHERE   bind_game_type = 1
    AND     bind_type = 1 -- 默认应用
)
---传奇游戏
,tmp_ods_newly_account AS
(
    SELECT  *
    FROM    ods_newly_account
    WHERE   ds BETWEEN '${hisdate}' AND '${bizdate}'
)
,box_users AS
(
    SELECT  user_id
    FROM    tmp_ods_newly_account
    WHERE   appid = 1015
    OR      channel ~ '996box'
    OR      channel IN (
                SELECT  value AS channel
                FROM    public.dws_app_props
                WHERE   appid = 1015
                AND     prop = 2
            )
    GROUP BY user_id
)
-- 筛选出有盒子记录以及玩过传奇游戏的玩家
,box_users_with_legend AS
(
    SELECT  acc.user_id
    FROM    tmp_ods_newly_account acc
    WHERE
EXISTS(
                SELECT  1
                FROM    legend_apps app
                WHERE   acc.appid = app.appid
            )
    AND
EXISTS(
                SELECT  1
                FROM    box_users box
                WHERE   box.user_id = acc.user_id
            )
)
,reg_account AS
(
    -- 普通用户的传奇记录
    SELECT  acc.gmid
            ,acc.appid
            ,acc.channel
            ,acc.user_id
            ,acc.device_type
            ,acc.event_time
            ,acc.ds
            ,'非盒子用户' AS user_flag --表示没有玩过盒子的普通传奇用户
            ,'0' AS sub_channel
    FROM    tmp_ods_newly_account acc
    WHERE
EXISTS(
                SELECT  1
                FROM    legend_apps app
                WHERE   app.appid = acc.appid
            )
    AND     NOT
EXISTS(
                SELECT  1
                FROM    box_users box
                WHERE   box.user_id = acc.user_id
            )
    UNION ALL -- 盒子用户玩过传奇的全量记录
    SELECT  acc.gmid
            ,acc.appid
            ,CASE   WHEN bxchl.channel_desc IS NOT NULL THEN bxchl.channel_desc
                    ELSE acc.channel
            END AS channel
            ,acc.user_id
            ,acc.device_type
            ,acc.event_time
            ,acc.ds
            ,'盒子用户' AS user_flag ---表示玩过盒子的用户
            ,bxchl.channel_desc AS sub_channel
    FROM    tmp_ods_newly_account acc
    LEFT JOIN   (
                    SELECT  appid
                            ,value AS channel
                            ,ext AS channel_desc
                    FROM    public.dws_app_props
                    WHERE   appid = 1015
                    AND     prop = 2 -- and length(ext)>0
                ) bxchl
    ON      acc.appid = bxchl.appid
    AND     acc.channel = bxchl.channel
    WHERE
EXISTS(
                SELECT  1
                FROM    box_users_with_legend b
                WHERE   b.user_id = acc.user_id
            )
)
INSERT INTO
    datasci.ads_996eco_index_box_m (index_type,data_month,is_box,reg_count)
SELECT  'reg' index_type
        ,'${month}' AS data_month
        ,CASE   WHEN user_flag = '非盒子用户' THEN '非盒子注册用户'
                WHEN user_flag = '盒子用户'
                    AND (appid = 1015
                    OR channel ~ '996box'
                    OR sub_channel IS NOT NULL) THEN '盒子注册用户'
                ELSE '非盒子注册用户'
        END AS is_box
        ,uniq(mi.user_id) AS reg_count
FROM    reg_account acc
INNER JOIN  (
                SELECT  user_id
                        ,MIN(event_time) AS event_time_min
                FROM    reg_account
                GROUP BY user_id
            ) mi ---从玩家的众多游戏记录中选取最早的一条
ON      acc.user_id = mi.user_id
AND     acc.event_time = mi.event_time_min
GROUP BY 1
         ,2
         ,3
;





