--********************************************************************--
-- Author:         zhougongping
-- Created Time:   2024-01-03 10:40:38
-- Description:    传3活跃
--********************************************************************--
CREATE TEMPORARY TABLE sls_login_log (
    gameid INT
    ,serverid INT
    ,userid VARCHAR
    ,schrname VARCHAR
    ,saccount VARCHAR
    ,nlevel INT
    ,dcreatetime BIGINT
    ,msgid INT
    ,nonlinetime INT
    ,sipaddr VARCHAR
) WITH (
    'connector' = 'sls',
    'endPoint' = 'cn-hangzhou-intranet.log.aliyuncs.com',
    'accessId' = '${secret_values.ak}',
    'accessKey' = '${secret_values.sk}',
    'startTime' = '2024-01-02 00:00:00',
    'project' = 'chuansan',
    'logStore' = 'login-log',
    'consumerGroup' = 'c3yq2holo_engine_login_log'
);

CREATE TEMPORARY TABLE ods_active_role (
    game_id INT
    ,server_id INT
    ,user_account VARCHAR
    ,role_id VARCHAR
    ,role_name VARCHAR
    ,create_time BIGINT
    ,ds VARCHAR
    ,PRIMARY KEY (game_id, role_id, ds) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'ods_active_role',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorignore'
);

CREATE TEMPORARY TABLE ods_role (
    game_id INT
    ,server_id INT
    ,role_id VARCHAR
    ,`level` INT
    ,PRIMARY KEY (game_id, server_id, role_id) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'ods_role',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorupdate'
);

CREATE TEMPORARY TABLE dwd_role_level (
    game_id INT
    ,server_id INT
    ,role_id VARCHAR
    ,ds VARCHAR
    ,`level` INT
    ,PRIMARY KEY (game_id, server_id, role_id, ds) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'dwd_role_level',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorupdate'
);

CREATE TEMPORARY TABLE ods_role_duration (
    game_id INT
    ,server_id INT
    ,user_account VARCHAR
    ,role_id VARCHAR
    ,`duration` INT
    ,`create_time` TIMESTAMP
    ,`level` INT
    ,role_name VARCHAR

    ,PRIMARY KEY (game_id, server_id, role_id, create_time) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'ods_role_duration',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorignore'
);

CREATE TEMPORARY TABLE ods_platform_login (
    game_id INT
    ,user_id INT
    ,user_account VARCHAR
    ,promote_id INT
    ,platform_id INT
    ,login_time INT
    ,login_ip VARCHAR
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'ods_platform_login',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorignore'
);

CREATE TEMPORARY TABLE dws_analyze_account (
    game_id BIGINT
    ,server_id BIGINT
    ,user_account VARCHAR
    ,last_active_time BIGINT
    ,PRIMARY KEY (game_id, server_id, user_account) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'dws_analyze_account',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorupdate'
);

CREATE TEMPORARY TABLE dws_analyze_role (
    game_id BIGINT
    ,server_id BIGINT
    ,role_id VARCHAR
    ,last_active_time BIGINT
    ,PRIMARY KEY (game_id, server_id, role_id) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'dws_analyze_role',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorupdate'
);

CREATE TEMPORARY TABLE ods_active_account (
    game_id INT
    ,server_id INT
    ,user_account VARCHAR
    ,ds VARCHAR
    ,create_time BIGINT
    ,PRIMARY KEY (game_id, server_id, user_account, ds) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-n8m2xw5ez001-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.996engine_ak}',
    'password' = '${secret_values.996engine_sk}',
    'dbname' = 'dhsf_engine',
    'tablename' = 'ods_active_account',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorignore'
);

-- ============================================
BEGIN STATEMENT SET;

INSERT INTO
    ods_active_role
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,saccount AS user_account
    ,userid AS role_id
    ,schrname AS role_name
    -- ,nlevel AS role_level
    ,dcreatetime AS create_time
    ,FROM_UNIXTIME(dcreatetime, 'yyyyMMdd') AS ds
FROM
    sls_login_log;

INSERT INTO
    ods_role
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,userid AS role_id
    ,nlevel AS `level`
FROM
    sls_login_log;

INSERT INTO
    dwd_role_level
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,userid AS role_id
    ,FROM_UNIXTIME(dcreatetime, 'yyyyMMdd') AS ds
    ,nlevel AS `level`
FROM
    sls_login_log;

INSERT INTO
    ods_role_duration
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,saccount AS user_account
    ,userid AS role_id
    ,nonlinetime AS duration
    ,TO_TIMESTAMP_LTZ(dcreatetime, 0) AS create_time
    -- ,FROM_UNIXTIME(dcreatetime, 'yyyyMMdd') AS ds
    ,nlevel AS `level`
    ,schrname AS role_name
FROM
    sls_login_log
WHERE
    msgid = 2001
;

INSERT INTO
    ods_platform_login
SELECT
    gameid AS game_id
    ,CAST(saccount AS INT) AS user_id
    ,saccount AS user_account
    ,0 AS promote_id
    ,0 AS platform_id
    ,CAST(dcreatetime AS INT) AS login_time
    ,sipaddr AS login_ip
FROM
    sls_login_log
WHERE
    msgid = 2000
    AND REGEXP(saccount, '^\d+$')
;

INSERT INTO
    dws_analyze_account
SELECT
    gameid AS game_id
    ,-1 AS server_id
    ,saccount AS user_account
    ,CAST(dcreatetime AS BIGINT) AS last_active_time
FROM
    sls_login_log
WHERE
    saccount IS NOT NULL
    AND msgid = 2000
UNION ALL
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,saccount AS user_account
    ,CAST(dcreatetime AS BIGINT) AS last_active_time
FROM
    sls_login_log
WHERE
    saccount IS NOT NULL
    AND msgid = 2000
;

INSERT INTO
    dws_analyze_role
SELECT
    gameid AS game_id
    ,-1 AS server_id
    ,userid AS role_id
    ,CAST(dcreatetime AS BIGINT) AS last_active_time
FROM
    sls_login_log
WHERE
    userid IS NOT NULL
    AND msgid = 2000
UNION ALL
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,userid AS role_id
    ,CAST(dcreatetime AS BIGINT) AS last_active_time
FROM
    sls_login_log
WHERE
    userid IS NOT NULL
    AND msgid = 2000
;

INSERT INTO
    ods_active_account
SELECT
    gameid AS game_id
    ,serverid AS server_id
    ,saccount AS user_account
    ,FROM_UNIXTIME(dcreatetime - 1, 'yyyyMMdd') AS ds
    ,(dcreatetime - 1) AS create_time
FROM
    sls_login_log
;

END;