-- =============================================================================
-- 关系指数与 ML 人工台账迁移脚本
-- 版本: 2026-02-08
-- AI_CHANGELOG [2026-02-13] 移除 INSERT 中的 source_mode 参数（ML 仅用人工台账）
-- 说明:
--   1) 新增关系指数结果表 dws_member_assistant_relation_index
--   2) 新增 ML 人工台账宽表/窄表
--   3) 补充 RS/OS/MS/ML 参数并下线 INTIMACY
-- =============================================================================

BEGIN;

-- -----------------------------------------------------------------------------
-- 1) 关系指数结果表
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS billiards_dws.dws_member_assistant_relation_index (
    relation_id            BIGSERIAL PRIMARY KEY,
    site_id                BIGINT NOT NULL,
    tenant_id              BIGINT NOT NULL,
    member_id              BIGINT NOT NULL,
    assistant_id           BIGINT NOT NULL,
    session_count          INTEGER NOT NULL DEFAULT 0,
    total_duration_minutes INTEGER NOT NULL DEFAULT 0,
    basic_session_count    INTEGER NOT NULL DEFAULT 0,
    incentive_session_count INTEGER NOT NULL DEFAULT 0,
    days_since_last_session INTEGER,
    rs_f                   NUMERIC(14,6) NOT NULL DEFAULT 0,
    rs_d                   NUMERIC(14,6) NOT NULL DEFAULT 0,
    rs_r                   NUMERIC(14,6) NOT NULL DEFAULT 0,
    rs_raw                 NUMERIC(14,6) NOT NULL DEFAULT 0,
    rs_display             NUMERIC(4,2) NOT NULL DEFAULT 0,
    os_share               NUMERIC(10,6) NOT NULL DEFAULT 0,
    os_label               VARCHAR(20) NOT NULL DEFAULT 'POOL',
    os_rank                INTEGER,
    ms_f_short             NUMERIC(14,6) NOT NULL DEFAULT 0,
    ms_f_long              NUMERIC(14,6) NOT NULL DEFAULT 0,
    ms_raw                 NUMERIC(14,6) NOT NULL DEFAULT 0,
    ms_display             NUMERIC(4,2) NOT NULL DEFAULT 0,
    ml_order_count         INTEGER NOT NULL DEFAULT 0,
    ml_allocated_amount    NUMERIC(14,2) NOT NULL DEFAULT 0,
    ml_raw                 NUMERIC(14,6) NOT NULL DEFAULT 0,
    ml_display             NUMERIC(4,2) NOT NULL DEFAULT 0,
    calc_time              TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    created_at             TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at             TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uk_dws_member_assistant_relation_index UNIQUE (site_id, member_id, assistant_id)
);

CREATE INDEX IF NOT EXISTS idx_dws_relation_member
    ON billiards_dws.dws_member_assistant_relation_index (site_id, member_id, os_share DESC);
CREATE INDEX IF NOT EXISTS idx_dws_relation_assistant
    ON billiards_dws.dws_member_assistant_relation_index (site_id, assistant_id, rs_display DESC);
CREATE INDEX IF NOT EXISTS idx_dws_relation_calc_time
    ON billiards_dws.dws_member_assistant_relation_index (calc_time);

-- -----------------------------------------------------------------------------
-- 2) ML 人工台账宽表
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS billiards_dws.dws_ml_manual_order_source (
    source_id          BIGSERIAL PRIMARY KEY,
    site_id            BIGINT NOT NULL,
    biz_date           DATE NOT NULL,
    external_id        VARCHAR(128) NOT NULL,
    member_id          BIGINT NOT NULL DEFAULT 0,
    pay_time           TIMESTAMPTZ NOT NULL,
    order_amount       NUMERIC(14,2) NOT NULL DEFAULT 0,
    currency           VARCHAR(16) NOT NULL DEFAULT 'CNY',
    assistant_id_1     BIGINT,
    assistant_name_1   VARCHAR(128),
    assistant_id_2     BIGINT,
    assistant_name_2   VARCHAR(128),
    assistant_id_3     BIGINT,
    assistant_name_3   VARCHAR(128),
    assistant_id_4     BIGINT,
    assistant_name_4   VARCHAR(128),
    assistant_id_5     BIGINT,
    assistant_name_5   VARCHAR(128),
    import_batch_no    VARCHAR(64) NOT NULL,
    import_file_name   VARCHAR(255) NOT NULL,
    import_scope_key   VARCHAR(128) NOT NULL,
    import_time        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    import_user        VARCHAR(64),
    row_no             INTEGER NOT NULL,
    remark             TEXT,
    created_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uk_dws_ml_manual_order_source UNIQUE (site_id, external_id, import_scope_key, row_no)
);

CREATE INDEX IF NOT EXISTS idx_dws_ml_source_scope
    ON billiards_dws.dws_ml_manual_order_source (site_id, biz_date);
CREATE INDEX IF NOT EXISTS idx_dws_ml_source_external
    ON billiards_dws.dws_ml_manual_order_source (site_id, external_id);

-- -----------------------------------------------------------------------------
-- 3) ML 人工台账窄表
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS billiards_dws.dws_ml_manual_order_alloc (
    alloc_id           BIGSERIAL PRIMARY KEY,
    site_id            BIGINT NOT NULL,
    biz_date           DATE NOT NULL,
    external_id        VARCHAR(128) NOT NULL,
    member_id          BIGINT NOT NULL DEFAULT 0,
    pay_time           TIMESTAMPTZ NOT NULL,
    order_amount       NUMERIC(14,2) NOT NULL DEFAULT 0,
    assistant_id       BIGINT NOT NULL,
    assistant_name     VARCHAR(128),
    share_ratio        NUMERIC(14,8) NOT NULL DEFAULT 0,
    allocated_amount   NUMERIC(14,2) NOT NULL DEFAULT 0,
    currency           VARCHAR(16) NOT NULL DEFAULT 'CNY',
    import_scope_key   VARCHAR(128) NOT NULL,
    import_batch_no    VARCHAR(64) NOT NULL,
    import_file_name   VARCHAR(255) NOT NULL,
    import_time        TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    import_user        VARCHAR(64),
    created_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uk_dws_ml_manual_order_alloc UNIQUE (site_id, external_id, assistant_id)
);

CREATE INDEX IF NOT EXISTS idx_dws_ml_alloc_scope
    ON billiards_dws.dws_ml_manual_order_alloc (site_id, biz_date);
CREATE INDEX IF NOT EXISTS idx_dws_ml_alloc_member_assistant
    ON billiards_dws.dws_ml_manual_order_alloc (site_id, member_id, assistant_id);

-- -----------------------------------------------------------------------------
-- 4) 参数切换
-- -----------------------------------------------------------------------------
UPDATE billiards_dws.cfg_index_parameters
SET effective_to = DATE '2025-12-31',
    updated_at = NOW()
WHERE index_type = 'INTIMACY'
  AND (effective_to IS NULL OR effective_to > DATE '2025-12-31');

INSERT INTO billiards_dws.cfg_index_parameters
    (index_type, param_name, param_value, description, effective_from)
VALUES
    ('OS', 'ownership_gap_threshold', 0.150000, '主责与次席份额差阈值', DATE '2026-01-01')
ON CONFLICT (index_type, param_name, effective_from) DO UPDATE SET
    param_value = EXCLUDED.param_value,
    description = EXCLUDED.description,
    updated_at = NOW();

COMMIT;
