CREATE TABLE datasci.ads_996eco_index_channel_m (
    index_type text,
    data_month text,
    re_channel text,
    pay_cnt bigint,
    pay_amount 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_channel_m WHERE ds = '${month}';


--付费人数和金额
--月付费人数和金额
WITH ga AS (
    SELECT
        id,
        name
    FROM public.gm_apply
    WHERE bind_type = 1 AND bind_game_type = 1
)
,
po AS (
    SELECT
        appid,
        ds::date ds,
        user_id,
        case when channel~*'996box' then '盒子'
        when channel~'ai6' then 'ai6'
        when channel~'玩心' then '玩心'
        when channel~'应用宝' then '腾讯应用宝'
        when channel~'纸片' then '纸片'
        when channel~'团团' then '团团'
        when channel~'335玩' then '335玩'
        when channel~'朋克' then '朋克'
        when channel~'安徽追游' then '安徽追游'
        when channel~'冰火' then '冰火'
        when channel~'心乐' then '心乐'
        when channel ~*'华为|vivo|oppo|魅族|努比亚|联想|酷派' then '硬核联盟'
        when channel~'9377'  then '9377'
        when channel~'智灵' then '智灵'
        when channel~'掌游' then '掌游'
        when channel~'上士' then '上士'
        when channel~'攒趣' then '攒趣'
        when channel~'掌玩' then '掌玩'
        when channel~'贪玩' then '贪玩'
        when channel~*'wx|微信' then '微信'
        when channel~'公众号'  then '公众号'
        when channel~*'qq' then 'QQ'
        when channel~'七牛' then '七牛'
        when channel~'霸气' then '霸气'
        when channel~*'xy' then 'XY'
        when channel~'芝麻互娱' then '芝麻互娱'
        when channel~'龙翔' then '龙翔'
        when channel~'布咕' then '布咕'
        when channel~'小柠互娱' then '小柠互娱'
        when channel~'春秋玩' then '春秋玩'
        when channel~'欢喜时代' then '欢喜时代'
        when channel~'上游' then '上游'
        when channel~'倾程' then '倾程'
        when channel~'绿洲' then '绿洲'
        when channel~'雷电' then '雷电'
        when channel~'点点' then '点点'
        when channel~'小七' then '小七'
        when channel~'瓜玩' then '瓜玩'
        when channel~'艺云' then '艺云'
        when channel~'冠游' then '冠游'
        when channel~'玄藏' then '玄藏'
        when channel~'羽龙' then '羽龙'
        when channel~'星云' then '星云'
        when channel~'爱尚游' then '爱尚游'
        when channel~'昊玩' then '昊玩'
        when channel~'8酷' then '8酷'
        when channel~'迅玩' then '迅玩'
        when channel~'星游' then '星游'
        when channel~'小米' then '小米'
        when channel~'趣城' then '趣城'
        when channel~'诺游' then '诺游'
        when channel~'洛谷' then '洛谷'
        when channel~'司墨' then '司墨'
        when channel~'脚印' then '脚印'
        when channel~'神游' then '神游'
        when channel~'拓乐' then '拓乐'
        when channel~'360' then '360'
        when channel~'日昇' then '日昇'
        when channel~'触点' then '触点'
        when channel~'冠游' then '冠游'
        when channel~'巴兔' then '巴兔'
        when channel~'天纵' then '天纵'
        when channel~'早游戏' then '早游戏'
        when channel~'爱趣' then '爱趣'
        when channel~'星楠祥' then '星楠祥'
        when channel~'贪游' then '贪游'
        when channel~'君游' then '君游'
        when channel~'996直播间' then '996直播间'
        when channel~'mumu' then 'mumu'
        when channel~'吉祥' then '吉祥直播间'
        when channel~'溪谷' then '溪谷'
        when channel~'优谷' then '优谷'
        when channel~'闪趣' then '闪趣'
        when channel~'8酷' then '8酷'
        when channel~'抖音' then '抖音'
        when channel~'斗鱼' then '斗鱼'
        when channel~'虎牙' then '虎牙'
        when channel~'巨量' then '巨量'
        when channel~'快手' then '快手'
        when channel~'爱奇艺' then '爱奇艺'
        when channel~'007' then '007'
        when channel~'圣宏' then '圣宏'
        when channel~'顺火' then '顺火'
        when channel~'飞火' then '飞火'
        when channel~'乐岛' then '乐岛'
        when channel~'传柒' then '传柒'
        when channel~'星际' then '星际'
        when channel~'玖趣' then '玖趣'
        when channel~*'ku25' then 'ku25'
        when channel~'8090' then '8090'
        when channel~*'appstore' then 'AppStore'
        when channel~'欢娱' then '欢娱'
        when channel~'逐梦' then '逐梦'
        when channel~'兴动' then '兴动'
        when channel~'3733' then '3733'
        when channel~'第一游戏' then '第一游戏'
        when channel~'公会' then '公会'
        when channel~'欢娱' then '欢娱'
        when channel~'335玩' then '335玩'
        when channel~*'4yx' then '4yx'
        when channel~'37' then '37'
        when channel~'开心' then '开心'
        when channel~* 'ios|苹果' then 'ios苹果端具体不详'
        when channel~'触点' then '触点'
        when channel~'游码' then '游码'
        when channel~*'45yx' then '45yx'
        when channel~'巨量' then '巨量'
        when channel~'小铃铛' then '小铃铛'
        when channel~'2345' then '2345'
        when channel~'梨子' then '梨子'
        when channel~'麦游' then '麦游'
        when channel~'群英' then '群英'
        when channel~'阅文' then '阅文'
        when channel~*'kopo' then 'kopo'
        when channel~'创启' then '创启'
        when channel~'4366' then '4366'
        when channel~'大楚' then '大楚'
        when channel~'巅峰' then '巅峰'
        when channel~'277' then '277'
        when channel~'112233' then '112233'
        when channel~'714' then '714'
        when channel~'4399' then '4399'
        when channel~'追忆' then '追忆'
        when channel~*'yy' then 'yy'
        when channel~'588' then '588'
        when channel~'八门' then '八门'
        when channel~*'pdd' then 'pdd'
        when channel~'9917' then '9917'
        when channel~'51517' then '51517'
        when channel~*'4yx' then '4yx'
        when channel~'好汉' then '好汉'
        when channel~'51' then '51'
        when channel~'葫芦侠' then '葫芦侠'
        when channel~'虫虫' then '虫虫'
        when channel~'跃游' then '跃游'
        when channel~'天天' then '天天'
        when channel~'66' then '66'
        when channel~'嘉禾' then '嘉禾'
        when channel~'爱游' then '爱游'
        when channel~'乐疯玩' then '乐疯玩'
        when channel~'游戏饭' then '游戏饭'
        else '其他' end as re_channel,
        amount / 100 AS pay_amount
    FROM public.ods_payment_order
    WHERE status = 1
    AND ds BETWEEN '${bizmonth}' AND '${bizdate}'
)
INSERT INTO
    datasci.ads_996eco_index_channel_m (index_type,data_month,re_channel,pay_cnt,pay_amount)
SELECT
    'pay' AS index_type,
    '${month}' AS data_month,
    re_channel,
    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
public.ods_newly_account na ON po.appid = na.appid AND po.user_id = na.user_id
GROUP BY 1,2,3
;

-- 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
;

INSERT INTO
    datasci.ads_996eco_index_channel_m (index_type,data_month,re_channel,mau)
select
    'mau' index_type,
    '${month}' data_month,
    case when channel~'盒子' or channel~'996box' then '盒子'
         when channel~'ai6' then 'ai6'
         when channel~'玩心' then '玩心'
         when channel~'应用宝' then '腾讯应用宝'
         when channel~'纸片' then '纸片'
         when channel~'团团' then '团团'
         when channel~'335玩' then '335玩'
         when channel~'朋克' then '朋克'
         when channel~'安徽追游' then '安徽追游'
         when channel~'冰火' then '冰火'
         when channel~'心乐' then '心乐'
        when channel LIKE '%华为%' OR
            channel LIKE '%vivo%' OR
            channel LIKE '%OPPO%' OR
            channel LIKE '%魅族%' OR
            channel LIKE '%努比亚%' OR
            channel LIKE '%联想%' OR
            channel LIKE '%酷派%' then '硬核联盟'
         when channel~'9377'  then '9377'
         when channel~'智灵' then '智灵'
         when channel~'掌游' then '掌游'
         when channel~'上士' then '上士'
         when channel~'攒趣' then '攒趣'
         when channel~'掌玩' then '掌玩'
         when channel~'贪玩' then '贪玩'
         when channel~'wx'or channel~'WX'or channel~'微信' then '微信'
         when channel~'公众号'  then '公众号'
         when channel~'qq' or channel~'QQ' then 'QQ'
         when channel~'quick' then 'quick'
         when channel~'七牛' then '七牛'
         when channel~'霸气' then '霸气'
         when channel~'xy' or channel~'XY' then 'XY'
         when channel~'芝麻互娱' then '芝麻互娱'
         when channel~'龙翔' then '龙翔'
         when channel~'布咕' then '布咕'
         when channel~'小柠互娱' then '小柠互娱'
         when channel~'春秋玩' then '春秋玩'
         when channel~'欢喜时代' then '欢喜时代'
         when channel~'上游' then '上游'
         when channel~'倾程' then '倾程'
         when channel~'绿洲' then '绿洲'
         when channel~'雷电' then '雷电'
         when channel~'点点' then '点点'
         when channel~'小七' then '小七'
         when channel~'瓜玩' then '瓜玩'
         when channel~'艺云' then '艺云'
         when channel~'冠游' then '冠游'
         when channel~'玄藏' then '玄藏'
         when channel~'羽龙' then '羽龙'
         when channel~'星云' then '星云'
         when channel~'爱尚游' then '爱尚游'
         when channel~'昊玩' then '昊玩'
         when channel~'8酷' then '8酷'
         when channel~'迅玩' then '迅玩'
         when channel~'星游' then '星游'
         when channel~'小米' then '小米'
         when channel~'趣城' then '趣城'
         when channel~'诺游' then '诺游'
         when channel~'洛谷' then '洛谷'
         when channel~'司墨' then '司墨'
         when channel~'脚印' then '脚印'
         when channel~'神游' then '神游'
         when channel~'拓乐' then '拓乐'
         when channel~'360' then '360'
         when channel~'日昇' then '日昇'
         when channel~'触点' then '触点'
         when channel~'冠游' then '冠游'
         when channel~'巴兔' then '巴兔'
         when channel~'天纵' then '天纵'
         when channel~'早游戏' then '早游戏'
         when channel~'爱趣' then '爱趣'
         when channel~'星楠祥' then '星楠祥'
         when channel~'贪游' then '贪游'
         when channel~'君游' then '君游'
         when channel~'996直播间' then '996直播间'
         when channel~'mumu' then 'mumu'
         when channel~'吉祥' then '吉祥直播间'
         when channel~'溪谷' then '溪谷'
         when channel~'优谷' then '优谷'
         when channel~'闪趣' then '闪趣'
         when channel~'8酷' then '8酷'
         when channel~'抖音' then '抖音'
         when channel~'斗鱼' then '斗鱼'
         when channel~'虎牙' then '虎牙'
     when channel~'巨量' then '巨量'
     when channel~'快手' then '快手'
     when channel~'爱奇艺' then '爱奇艺'
     when channel~'007' then '007'
     when channel~'圣宏' then '圣宏'
     when channel~'顺火' then '顺火'
     when channel~'飞火' then '飞火'
     when channel~'乐岛' then '乐岛'
     when channel~'传柒' then '传柒'
     when channel~'星际' then '星际'
     when channel~'玖趣' then '玖趣'
     when lower(channel)~'ku25' then 'ku25'
     when channel~'8090' then '8090'
     when lower(channel)~'appstore' then 'AppStore'
     when channel~'欢娱' then '欢娱'
     when channel~'逐梦' then '逐梦'
     when channel~'兴动' then '兴动'
     when channel~'3733' then '3733'
     when channel~'第一游戏' then '第一游戏'
     when channel~'公会' then '公会'
     when channel~'欢娱' then '欢娱'
     when channel~'335玩' then '335玩'
     when lower(channel)~'4yx' then '4yx'
     when channel~'37' then '37'
     when channel~'开心' then '开心'
     when lower(channel)~ 'ios' or channel~'苹果' then 'ios苹果端具体不详'
     when channel~'触点' then '触点'
     when channel~'游码' then '游码'
     when lower(channel)~'45yx' then '45yx'
     when channel~'巨量' then '巨量'
     when channel~'小铃铛' then '小铃铛'
     when channel~'2345' then '2345'
     when channel~'梨子' then '梨子'
     when channel~'麦游' then '麦游'
     when channel~'群英' then '群英'
     when channel~'阅文' then '阅文'
     when lower(channel)~'kopo' then 'kopo'
     when channel~'创启' then '创启'
     when channel~'4366' then '4366'
     when channel~'大楚' then '大楚'
     when channel~'巅峰' then '巅峰'
     when channel~'277' then '277'
     when channel~'112233' then '112233'
     when channel~'714' then '714'
     when channel~'4399' then '4399'
     when channel~'追忆' then '追忆'
     when lower(channel)~'yy' then 'yy'
     when channel~'588' then '588'
     when channel~'八门' then '八门'
     when lower(channel)~'pdd' then 'pdd'
     when channel~'9917' then '9917'
     when channel~'51517' then '51517'
     when lower(channel)~'4yx' then '4yx'
     when channel~'好汉' then '好汉'
     when channel~'51' then '51'
     when channel~'葫芦侠' then '葫芦侠'
     when channel~'虫虫' then '虫虫'
     when channel~'跃游' then '跃游'
     when channel~'天天' then '天天'
     when channel~'66' then '66'
     when channel~'嘉禾' then '嘉禾'
     when channel~'爱游' then '爱游'
     when channel~'乐疯玩' then '乐疯玩'
     when channel~'游戏饭' then '游戏饭'
         else '其他' end as re_channel,
    uniq(user_id) as mau
from public.ods_active_account t1
inner join
(
    select id,name,bind_game_id
    from public.gm_apply
    where bind_game_type=1
) t2 on t1.appid=t2.id
where t1.ds BETWEEN '${bizmonth}' AND '${bizdate}'
group by 1,2,3
;