-- =============================================================================
-- 迁移脚本：创建员工档案 ODS + DWD 表
-- 日期：2026-02-22
-- 说明：从 SearchSystemStaffInfo API 抓取员工数据，ODS 落地后清洗至 DWD 维度表
-- 需求：US-1, US-2, US-3
-- =============================================================================

-- ---------------------------------------------------------------------------
-- 1. ODS 层：ods.staff_info_master
-- ---------------------------------------------------------------------------
CREATE TABLE ods.staff_info_master (
    id                    BIGINT NOT NULL,
    tenant_id             BIGINT,
    site_id               BIGINT,
    tenant_org_id         BIGINT,
    system_user_id        BIGINT,
    staff_name            TEXT,
    alias_name            TEXT,
    mobile                TEXT,
    avatar                TEXT,
    gender                INTEGER,
    job                   TEXT,
    job_num               TEXT,
    staff_identity        INTEGER,
    status                INTEGER,
    account_status        INTEGER,
    system_role_id        INTEGER,
    rank_id               INTEGER,
    rank_name             TEXT,
    new_rank_id           INTEGER,
    new_staff_identity    INTEGER,
    leave_status          INTEGER,
    entry_time            TIMESTAMP WITHOUT TIME ZONE,
    resign_time           TIMESTAMP WITHOUT TIME ZONE,
    create_time           TIMESTAMP WITHOUT TIME ZONE,
    is_delete             INTEGER,
    is_reserve            INTEGER,
    shop_name             TEXT,
    site_label            TEXT,
    cashier_point_id      BIGINT,
    cashier_point_name    TEXT,
    group_id              BIGINT,
    group_name            TEXT,
    staff_profile_id      BIGINT,
    auth_code             TEXT,
    auth_code_create      TIMESTAMP WITHOUT TIME ZONE,
    ding_talk_synced      INTEGER,
    salary_grant_enabled  INTEGER,
    entry_type            INTEGER,
    entry_sign_status     INTEGER,
    resign_sign_status    INTEGER,
    criticism_status      INTEGER,
    user_roles            JSONB,
    -- ETL 元数据
    content_hash          TEXT NOT NULL,
    source_file           TEXT,
    fetched_at            TIMESTAMP WITH TIME ZONE DEFAULT now(),
    payload               JSONB NOT NULL
);

COMMENT ON TABLE ods.staff_info_master IS '员工档案主数据（来源：SearchSystemStaffInfo API）';

-- ---------------------------------------------------------------------------
-- 2. DWD 层：dwd.dim_staff（主表，核心业务字段，SCD2）
-- ---------------------------------------------------------------------------
CREATE TABLE dwd.dim_staff (
    staff_id              BIGINT NOT NULL,
    staff_name            TEXT,
    alias_name            TEXT,
    mobile                TEXT,
    gender                INTEGER,
    job                   TEXT,
    tenant_id             BIGINT,
    site_id               BIGINT,
    system_role_id        INTEGER,
    staff_identity        INTEGER,
    status                INTEGER,
    leave_status          INTEGER,
    entry_time            TIMESTAMP WITH TIME ZONE,
    resign_time           TIMESTAMP WITH TIME ZONE,
    is_delete             INTEGER,
    -- SCD2
    scd2_start_time       TIMESTAMP WITH TIME ZONE NOT NULL,
    scd2_end_time         TIMESTAMP WITH TIME ZONE,
    scd2_is_current       INTEGER,
    scd2_version          INTEGER,
    PRIMARY KEY (staff_id, scd2_start_time)
);

COMMENT ON TABLE dwd.dim_staff IS '员工档案维度主表（SCD2）';

-- ---------------------------------------------------------------------------
-- 3. DWD 层：dwd.dim_staff_ex（扩展表，次要/低频变更字段，SCD2）
-- ---------------------------------------------------------------------------
CREATE TABLE dwd.dim_staff_ex (
    staff_id              BIGINT NOT NULL,
    avatar                TEXT,
    job_num               TEXT,
    account_status        INTEGER,
    rank_id               INTEGER,
    rank_name             TEXT,
    new_rank_id           INTEGER,
    new_staff_identity    INTEGER,
    is_reserve            INTEGER,
    shop_name             TEXT,
    site_label            TEXT,
    tenant_org_id         BIGINT,
    system_user_id        BIGINT,
    cashier_point_id      BIGINT,
    cashier_point_name    TEXT,
    group_id              BIGINT,
    group_name            TEXT,
    staff_profile_id      BIGINT,
    auth_code             TEXT,
    auth_code_create      TIMESTAMP WITH TIME ZONE,
    ding_talk_synced      INTEGER,
    salary_grant_enabled  INTEGER,
    entry_type            INTEGER,
    entry_sign_status     INTEGER,
    resign_sign_status    INTEGER,
    criticism_status      INTEGER,
    create_time           TIMESTAMP WITH TIME ZONE,
    user_roles            JSONB,
    -- SCD2
    scd2_start_time       TIMESTAMP WITH TIME ZONE NOT NULL,
    scd2_end_time         TIMESTAMP WITH TIME ZONE,
    scd2_is_current       INTEGER,
    scd2_version          INTEGER,
    PRIMARY KEY (staff_id, scd2_start_time)
);

COMMENT ON TABLE dwd.dim_staff_ex IS '员工档案维度扩展表（SCD2）';

-- =============================================================================
-- 回滚脚本（如需撤销）
-- DROP TABLE IF EXISTS dwd.dim_staff_ex;
-- DROP TABLE IF EXISTS dwd.dim_staff;
-- DROP TABLE IF EXISTS ods.staff_info_master;
-- =============================================================================
