--********************************************************************--
-- Author:         zhougongping@1253758386586182.onaliyun.com
-- Created Time:   2023-03-22 16:51:18
-- Description:    Write your description here
--********************************************************************--
CREATE TEMPORARY TABLE tab_user (
    id BIGINT
    ,account STRING
    ,promote_id INT
    ,phone STRING
    ,idcard STRING
    ,register_way TINYINT
    ,register_type TINYINT
    ,register_time BIGINT
    ,login_time BIGINT
    ,register_ip STRING
    ,login_ip STRING
    ,is_union TINYINT
    ,age_status TINYINT
    ,is_wechat_subscribe TINYINT
    ,platform_id INT
    ,box_account_time BIGINT
    ,channel STRING
    ,idcard_time BIGINT

    ,PRIMARY KEY(id) NOT ENFORCED
) WITH (
    'connector' = 'mysql',
    'hostname' = 'rm-bp1uj139ua9dck8n4jm.mysql.rds.aliyuncs.com',
    'port' = '3306',
    'username' = '${secret_values.rds_engine_user}',
    'password' = '${secret_values.rds_engine_pass}',
    'database-name' = 'platform',
    'table-name' = 'tab_user',
    'server-id' = '24061-24068',
    'scan.startup.mode' = 'initial',
    'server-time-zone' = 'Asia/Shanghai'
);

CREATE TEMPORARY TABLE ods_platform_account (
    uid bigint
    ,account STRING
    ,promote_id bigint
    ,phone STRING
    ,idcard STRING
    ,birthday STRING -- YYYYmmdd
    ,gender SMALLINT -- 1男2女0未知
    ,reg_way SMALLINT -- 注册方式 0游客1账号 2 手机 3微信 4QQ 5百度 6微博 7 sdk8后台添加主播
    ,reg_type SMALLINT -- 注册来源  0pc 2app
    ,reg_time TIMESTAMP
    ,reg_ip STRING
    ,is_union SMALLINT -- 是否联盟站下的用户 0否/1是
    ,age_status SMALLINT -- 0 未认证 2审核通过成年 3审核通过未成年
    ,is_wechat_subscribe SMALLINT -- 是否关注微信公众号(0否/1是)
    ,platform_id INT
    ,box_account_time BIGINT -- 更新盒子号时间
    ,box_channel STRING -- 盒子注册渠道 -1是游戏注册 0是官方 其余是各渠道
    ,auth_time TIMESTAMP -- 实名认证时间

    ,PRIMARY KEY(uid) 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_platform_account',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorreplace'
);

CREATE TEMPORARY TABLE biz_account_profile (
    user_id varchar
    ,_update_time_ TIMESTAMP
    ,birthday varchar
    ,gender int
    ,PRIMARY KEY (user_id) NOT ENFORCED
) WITH (
    'connector' = 'hologres',
    'endpoint' = 'hgprecn-cn-i7m2ssubq004-cn-hangzhou-vpc.hologres.aliyuncs.com:80',
    'username' = '${secret_values.ak}',
    'password' = '${secret_values.sk}',
    'dbname' = 'sdk_statis',
    'tablename' = 'biz.dwd_event_analyze_account',
    'ignoredelete' = 'true',
    'mutatetype' = 'insertorupdate'
);

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

INSERT INTO
    ods_platform_account
SELECT
    id as uid
    ,account as account
    ,promote_id
    ,MD5(CONCAT('0', phone)) as phone
    ,MD5(CONCAT('0', idcard)) as idcard
    ,IF(CHAR_LENGTH(idcard) = 15, CONCAT('19', SUBSTRING(idcard, 7, 6)), SUBSTRING(idcard, 7, 8)) as birthday
    ,CAST(IF(CHAR_LENGTH(idcard) = 15, SUBSTRING(idcard, 15, 1), SUBSTRING(idcard, 17, 1)) AS SMALLINT) as gender
    ,register_way as reg_way
    ,register_type as reg_type
    ,TO_TIMESTAMP_LTZ(COALESCE(register_time, login_time, 0), 0) as reg_time
    ,IFNULL(register_ip, login_ip) as reg_ip
    ,is_union
    ,age_status
    ,is_wechat_subscribe
    ,platform_id
    ,box_account_time
    ,channel as box_channel
    ,TO_TIMESTAMP_LTZ(IF(idcard IS NULL, 0, COALESCE(idcard_time, register_time, login_time, 0)), 0) as auth_time
FROM
    tab_user
;

INSERT INTO
    biz_account_profile
SELECT
    CAST(id AS VARCHAR) AS user_id
    ,TO_TIMESTAMP_LTZ(COALESCE(idcard_time, register_time, login_time, UNIX_TIMESTAMP()), 0) AS _update_time_
    ,IF(CHAR_LENGTH(idcard) = 15, CONCAT('19', SUBSTRING(idcard, 7, 6)), SUBSTRING(idcard, 7, 8)) AS birthday
    ,CAST(IF(CHAR_LENGTH(idcard) = 15, SUBSTRING(idcard, 15, 1), SUBSTRING(idcard, 17, 1)) AS INT) AS gender
FROM
    tab_user
WHERE
    idcard IS NOT NULL
    AND idcard <> ''
;

END;
