-- =============================================================================
-- etl_feiqiu / meta（ETL 调度元数据）
-- 生成日期：2026-03-15
-- 来源：测试库（通过脚本自动导出）
-- =============================================================================

CREATE SCHEMA IF NOT EXISTS meta;

-- 序列
CREATE SEQUENCE IF NOT EXISTS meta.etl_cursor_cursor_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS meta.etl_run_run_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS meta.etl_task_task_id_seq AS bigint;

-- 表
CREATE TABLE meta.etl_cursor (
    cursor_id bigint DEFAULT nextval('meta.etl_cursor_cursor_id_seq'::regclass) NOT NULL,
    task_id bigint NOT NULL,
    store_id bigint NOT NULL,
    last_start timestamp with time zone,
    last_end timestamp with time zone,
    last_id bigint,
    last_run_id bigint,
    extra jsonb DEFAULT '{}'::jsonb,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);

CREATE TABLE meta.etl_run (
    run_id bigint DEFAULT nextval('meta.etl_run_run_id_seq'::regclass) NOT NULL,
    run_uuid text NOT NULL,
    task_id bigint NOT NULL,
    store_id bigint NOT NULL,
    status text NOT NULL,
    started_at timestamp with time zone DEFAULT now(),
    ended_at timestamp with time zone,
    window_start timestamp with time zone,
    window_end timestamp with time zone,
    window_minutes integer,
    overlap_seconds integer,
    fetched_count integer DEFAULT 0,
    loaded_count integer DEFAULT 0,
    updated_count integer DEFAULT 0,
    skipped_count integer DEFAULT 0,
    error_count integer DEFAULT 0,
    unknown_fields integer DEFAULT 0,
    export_dir text,
    log_path text,
    request_params jsonb DEFAULT '{}'::jsonb,
    manifest jsonb DEFAULT '{}'::jsonb,
    error_message text,
    extra jsonb DEFAULT '{}'::jsonb
);

CREATE TABLE meta.etl_task (
    task_id bigint DEFAULT nextval('meta.etl_task_task_id_seq'::regclass) NOT NULL,
    task_code text NOT NULL,
    store_id bigint NOT NULL,
    enabled boolean DEFAULT true,
    cursor_field text,
    window_minutes_default integer DEFAULT 30,
    overlap_seconds integer DEFAULT 600,
    page_size integer DEFAULT 200,
    retry_max integer DEFAULT 3,
    params jsonb DEFAULT '{}'::jsonb,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);

-- 约束（主键 / 唯一 / 外键）
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_task_id_fkey FOREIGN KEY (task_id) REFERENCES meta.etl_task(task_id) ON DELETE CASCADE;
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_pkey PRIMARY KEY (cursor_id);
ALTER TABLE meta.etl_cursor ADD CONSTRAINT etl_cursor_task_id_store_id_key UNIQUE (task_id, store_id);
ALTER TABLE meta.etl_run ADD CONSTRAINT etl_run_task_id_fkey FOREIGN KEY (task_id) REFERENCES meta.etl_task(task_id) ON DELETE CASCADE;
ALTER TABLE meta.etl_run ADD CONSTRAINT etl_run_pkey PRIMARY KEY (run_id);
ALTER TABLE meta.etl_task ADD CONSTRAINT etl_task_pkey PRIMARY KEY (task_id);
ALTER TABLE meta.etl_task ADD CONSTRAINT etl_task_task_code_store_id_key UNIQUE (task_code, store_id);



-- =============================================================================
-- 种子数据：ODS 任务注册
-- =============================================================================

WITH target_store AS (
    SELECT 2790685415443269::bigint AS store_id  -- TODO: 替换为实际 store_id
),
task_codes AS (
    SELECT unnest(ARRAY[
        'ODS_ASSISTANT_ACCOUNT',
        'ODS_ASSISTANT_LEDGER',
        'ODS_SETTLEMENT_RECORDS',
        'ODS_TABLE_USE',
        'ODS_PAYMENT',
        'ODS_REFUND',
        'ODS_PLATFORM_COUPON',
        'ODS_MEMBER',
        'ODS_MEMBER_CARD',
        'ODS_MEMBER_BALANCE',
        'ODS_RECHARGE_SETTLE',
        'ODS_GROUP_PACKAGE',
        'ODS_GROUP_BUY_REDEMPTION',
        'ODS_INVENTORY_STOCK',
        'ODS_INVENTORY_CHANGE',
        'ODS_TABLES',
        'ODS_GOODS_CATEGORY',
        'ODS_STORE_GOODS',
        'ODS_STORE_GOODS_SALES',
        'ODS_TABLE_FEE_DISCOUNT',
        'ODS_TENANT_GOODS'
    ]) AS task_code
)
INSERT INTO meta.etl_task (task_code, store_id, enabled)
SELECT t.task_code, s.store_id, TRUE
FROM task_codes t CROSS JOIN target_store s
ON CONFLICT (task_code, store_id) DO UPDATE
SET enabled = EXCLUDED.enabled;

-- =============================================================================
-- 种子数据：调度任务注册
-- =============================================================================

WITH target_store AS (
    SELECT 2790685415443269::bigint AS store_id  -- TODO: 替换为实际 store_id
),
task_codes AS (
    SELECT unnest(ARRAY[
        'ASSISTANTS',
        'COUPON_USAGE',
        'CHECK_CUTOFF',
        'DWD_LOAD_FROM_ODS',
        'DWD_QUALITY_CHECK',
        'INIT_DWD_SCHEMA',
        'INIT_DWS_SCHEMA',
        'INIT_ODS_SCHEMA',
        'INVENTORY_CHANGE',
        'LEDGER',
        'MANUAL_INGEST',
        'MEMBERS',
        'MEMBERS_DWD',
        'ODS_JSON_ARCHIVE',
        'ORDERS',
        'PACKAGES_DEF',
        'PAYMENTS',
        'PAYMENTS_DWD',
        'PRODUCTS',
        'REFUNDS',
        'TABLE_DISCOUNT',
        'TABLES',
        'TICKET_DWD',
        'TOPUPS',
        'DWS_BUILD_ORDER_SUMMARY',
        'DWS_ASSISTANT_DAILY',
        'DWS_ASSISTANT_MONTHLY',
        'DWS_ASSISTANT_CUSTOMER',
        'DWS_ASSISTANT_SALARY',
        'DWS_ASSISTANT_FINANCE',
        'DWS_MEMBER_CONSUMPTION',
        'DWS_MEMBER_VISIT',
        'DWS_FINANCE_DAILY',
        'DWS_FINANCE_RECHARGE',
        'DWS_FINANCE_INCOME_STRUCTURE',
        'DWS_FINANCE_DISCOUNT_DETAIL',
        'DWS_GOODS_STOCK_DAILY',
        'DWS_GOODS_STOCK_WEEKLY',
        'DWS_GOODS_STOCK_MONTHLY',
        'DWS_WINBACK_INDEX',
        'DWS_NEWCONV_INDEX',
        'DWS_RELATION_INDEX',
        'DWS_ASSISTANT_PROJECT_TAG',
        'DWS_MEMBER_PROJECT_TAG',
        'DWS_ML_MANUAL_IMPORT'
    ]) AS task_code
)
INSERT INTO meta.etl_task (task_code, store_id, enabled)
SELECT t.task_code, s.store_id, TRUE
FROM task_codes t CROSS JOIN target_store s
ON CONFLICT (task_code, store_id) DO UPDATE
SET enabled = EXCLUDED.enabled,
    updated_at = now();
