﻿-- PostgreSQL Database Schema for iMeeting (Multi-tenant)
-- 0 为系统预留租户 ID

-- ----------------------------
-- 0. 租户与组织
-- ----------------------------
CREATE EXTENSION IF NOT EXISTS vector;
-- 租户表
CREATE TABLE sys_tenant (
                            id              BIGSERIAL PRIMARY KEY,
                            tenant_code     VARCHAR(64) NOT NULL UNIQUE,
                            tenant_name     VARCHAR(128) NOT NULL,
                            status          SMALLINT NOT NULL DEFAULT 1,
                            expire_time     TIMESTAMP(6),
                            contact_name    VARCHAR(64),
                            contact_phone   VARCHAR(32),
                            remark          VARCHAR(255),
                            created_at      TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
                            updated_at      TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
                            is_deleted      SMALLINT DEFAULT 0
);
CREATE  INDEX uk_tenant_code ON sys_tenant (tenant_code) WHERE is_deleted = 0;

-- 组织架构表
DROP TABLE IF EXISTS sys_org CASCADE;

CREATE TABLE sys_org (
                         id          BIGSERIAL PRIMARY KEY,
                         tenant_id   BIGINT NOT NULL,
                         parent_id   BIGINT,
                         org_name    VARCHAR(128) NOT NULL,
                         org_code    VARCHAR(64),
                         org_path    VARCHAR(512),
                         sort_order  INTEGER DEFAULT 0,
                         status      SMALLINT DEFAULT 1,
                         created_at  TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
                         updated_at  TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
                         is_deleted  SMALLINT DEFAULT 0,
                         CONSTRAINT fk_org_parent FOREIGN KEY (parent_id)
                             REFERENCES sys_org(id),
                         CONSTRAINT fk_org_tenant FOREIGN KEY (tenant_id)
                             REFERENCES sys_tenant(id)
);

CREATE INDEX idx_org_tenant ON sys_org (tenant_id);

-- ----------------------------
-- 1. 用户与角色
-- ----------------------------

-- 用户表
DROP TABLE IF EXISTS sys_user CASCADE;

CREATE TABLE sys_user (
                          user_id            BIGSERIAL PRIMARY KEY,
                          username           VARCHAR(50) NOT NULL UNIQUE,
                          display_name       VARCHAR(50) NOT NULL,
                          avatar_url         VARCHAR(500),
                          email              VARCHAR(100),
                          phone              VARCHAR(30) UNIQUE,
                          password_hash      VARCHAR(255) NOT NULL,
                          status             SMALLINT NOT NULL DEFAULT 1,
                          is_deleted         SMALLINT NOT NULL DEFAULT 0,
                          pwd_reset_required SMALLINT DEFAULT 1,
                          created_at         TIMESTAMP(6) NOT NULL DEFAULT now(),
                          updated_at         TIMESTAMP(6) NOT NULL DEFAULT now(),
                          is_platform_admin  BOOLEAN DEFAULT false
);
CREATE  INDEX uk_user_username ON sys_user (username) WHERE is_deleted = 0;

-- 角色表
DROP TABLE IF EXISTS sys_role CASCADE;

CREATE TABLE sys_role (
                          role_id    BIGSERIAL PRIMARY KEY,
                          tenant_id  BIGINT NOT NULL,
                          role_code  VARCHAR(50) NOT NULL,
                          role_name  VARCHAR(50) NOT NULL,
                          data_scope_type VARCHAR(32) NOT NULL DEFAULT 'SELF',
                          status     SMALLINT NOT NULL DEFAULT 1,
                          remark     TEXT,
                          is_deleted SMALLINT NOT NULL DEFAULT 0,
                          created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
                          updated_at TIMESTAMP(6) NOT NULL DEFAULT now()
);

CREATE INDEX idx_sys_role_tenant ON sys_role (tenant_id);
CREATE  INDEX uk_role_code ON sys_role (tenant_id, role_code) WHERE is_deleted = 0;

DROP TABLE IF EXISTS sys_role_data_scope_org CASCADE;

CREATE TABLE sys_role_data_scope_org (
                                         id BIGSERIAL PRIMARY KEY,
                                         role_id BIGINT NOT NULL,
                                         org_id BIGINT NOT NULL,
                                         created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
                                         updated_at TIMESTAMP(6) NOT NULL DEFAULT now()
);

CREATE INDEX idx_role_data_scope_role_id ON sys_role_data_scope_org (role_id);
CREATE INDEX idx_role_data_scope_org_id ON sys_role_data_scope_org (org_id);

-- 用户-角色关联表 (按 tenant_id 强约束，避免跨租户角色污染)
DROP TABLE IF EXISTS sys_user_role CASCADE;

CREATE TABLE sys_user_role (
                               id         BIGSERIAL PRIMARY KEY,
                               tenant_id  BIGINT NOT NULL,
                               user_id    BIGINT NOT NULL,
                               role_id    BIGINT NOT NULL,
                               is_deleted SMALLINT NOT NULL DEFAULT 0,
                               created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
                               updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
                               UNIQUE (tenant_id, user_id, role_id)
);

-- ----------------------------
-- 2. 权限/字典/参数 (全局共享, 无 tenant_id)
-- ----------------------------

DROP TABLE IF EXISTS sys_permission CASCADE;

CREATE TABLE sys_permission (
                                perm_id     BIGSERIAL PRIMARY KEY,
                                parent_id   BIGINT,
                                name        VARCHAR(100) NOT NULL,
                                code        VARCHAR(100) NOT NULL ,
                                perm_type   VARCHAR(20) NOT NULL,
                                level       INTEGER NOT NULL,
                                path        VARCHAR(255),
                                component   VARCHAR(255),
                                icon        VARCHAR(100),
                                sort_order  INTEGER NOT NULL DEFAULT 0,
                                is_visible  SMALLINT NOT NULL DEFAULT 1,
                                status      SMALLINT NOT NULL DEFAULT 1,
                                description TEXT,
                                meta        JSONB,
                                is_deleted  SMALLINT NOT NULL DEFAULT 0,
                                created_at  TIMESTAMP(6) NOT NULL DEFAULT now(),
                                updated_at  TIMESTAMP(6) NOT NULL DEFAULT now()
);

DROP TABLE IF EXISTS sys_tenant_user CASCADE;

CREATE TABLE sys_tenant_user (
                                 id         BIGSERIAL PRIMARY KEY,
                                 user_id    BIGINT NOT NULL,
                                 tenant_id  BIGINT NOT NULL,
                                 org_id     BIGINT,
                                 status     SMALLINT DEFAULT 1,
                                 is_deleted SMALLINT DEFAULT 0,
                                 created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
                                 updated_at TIMESTAMP(6) NOT NULL DEFAULT now()
);

CREATE  INDEX uk_tenant_user
    ON sys_tenant_user (user_id, tenant_id)
    WHERE is_deleted = 0;
CREATE TABLE sys_dict_type (
    dict_type_id BIGSERIAL PRIMARY KEY,
    type_code VARCHAR(50) UNIQUE NOT NULL,
    type_name VARCHAR(50) NOT NULL,
    status SMALLINT DEFAULT 1,
    remark TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    is_deleted SMALLINT DEFAULT 0
);

CREATE TABLE sys_dict_item (
    dict_item_id BIGSERIAL PRIMARY KEY,
    type_code VARCHAR(50) NOT NULL,
    item_label VARCHAR(100) NOT NULL,
    item_value VARCHAR(100) NOT NULL,
    sort_order INT DEFAULT 0,
    status SMALLINT DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    is_deleted SMALLINT DEFAULT 0,
    remark varchar(255)
);
CREATE INDEX idx_dict_item_type ON sys_dict_item (type_code);
CREATE  INDEX uk_dict_item_value ON sys_dict_item (type_code, item_value);

CREATE TABLE sys_param (
    param_id BIGSERIAL PRIMARY KEY,
    param_key VARCHAR(100) UNIQUE NOT NULL,
    param_value TEXT NOT NULL,
    param_type VARCHAR(20) NOT NULL,
    is_system SMALLINT DEFAULT 0,
    status SMALLINT DEFAULT 1,
    description TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    is_deleted SMALLINT DEFAULT 0
);

CREATE TABLE sys_role_permission (
                                     "id" BIGSERIAL PRIMARY KEY,
                                     "role_id" int8 NOT NULL,
                                     "perm_id" int8 NOT NULL,
                                     "is_deleted" int2 NOT NULL DEFAULT 0,
                                     "created_at" timestamp(6) NOT NULL DEFAULT now(),
                                     "updated_at" timestamp(6) NOT NULL DEFAULT now()
);

DROP TABLE IF EXISTS sys_bot_credential CASCADE;

CREATE TABLE sys_bot_credential (
                                    id BIGSERIAL PRIMARY KEY,
                                    bot_id VARCHAR(64) NOT NULL,
                                    secret_hash VARCHAR(64) NOT NULL,
                                    secret_salt VARCHAR(32),
                                    user_id BIGINT NOT NULL,
                                    status CHAR(1) NOT NULL DEFAULT '0',
                                    expire_time TIMESTAMP(6),
                                    last_access_time TIMESTAMP(6),
                                    last_access_ip VARCHAR(128),
                                    remark VARCHAR(500),
                                    create_by VARCHAR(64),
                                    create_time TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                    update_by VARCHAR(64),
                                    update_time TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE UNIQUE INDEX uk_sys_bot_credential_bot_id ON sys_bot_credential (bot_id);
CREATE INDEX idx_sys_bot_credential_user_id ON sys_bot_credential (user_id);
-- ----------------------------
-- 3. 日志 (租户隔离)
-- ----------------------------

CREATE TABLE sys_log (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    user_id BIGINT,
    username VARCHAR(50),
    source_system VARCHAR(64),
    log_type VARCHAR(20), -- LOGIN, OPERATION
    module_name VARCHAR(100),
    action_name VARCHAR(100),
    operation VARCHAR(100) NOT NULL,
    method VARCHAR(200),
    params TEXT,
    status SMALLINT DEFAULT 1,
    ip VARCHAR(50),
    duration BIGINT,
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_log_tenant_type ON sys_log (tenant_id, log_type, created_at);
CREATE INDEX idx_log_tenant_type_module_time ON sys_log (tenant_id, log_type, module_name, created_at);
CREATE INDEX idx_log_tenant_type_user_time ON sys_log (tenant_id, log_type, username, created_at);
CREATE INDEX idx_log_tenant_type_source_time ON sys_log (tenant_id, log_type, source_system, created_at);

-- ----------------------------
-- 4. 平台配置 (系统品牌化)
-- ----------------------------
DROP TABLE IF EXISTS sys_platform_config CASCADE;
CREATE TABLE sys_platform_config (
    id                BIGINT PRIMARY KEY, -- 固定为 1
    project_name      VARCHAR(128) NOT NULL,
    logo_url          VARCHAR(512),
    icon_url          VARCHAR(512),
    login_bg_url      VARCHAR(512),
    icp_info          VARCHAR(128),
    copyright_info    VARCHAR(255),
    system_description TEXT,
    created_at        TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    updated_at        TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    is_deleted        SMALLINT DEFAULT 0
);



-- ----------------------------
-- 6. 业务模块 - 声纹管理
-- ----------------------------
DROP TABLE IF EXISTS biz_speakers CASCADE;
CREATE TABLE biz_speakers (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL,           -- 租户ID
    creator_id      BIGINT NOT NULL,           -- 创建人ID，用于声纹库管理归属
    user_id         BIGINT,                    -- 关联系统用户ID，可为空
    external_speaker_id VARCHAR(100),          -- 第三方声纹库中的人员ID
    name            VARCHAR(100) NOT NULL,     -- 发言人姓名
    voice_path      VARCHAR(512),              -- 原始声纹文件存储路径
    voice_ext       VARCHAR(10),               -- 文件后缀
    voice_size      BIGINT,                    -- 文件大小
    status          SMALLINT DEFAULT 1,        -- 状态: 1=已保存, 2=注册中, 3=已注册, 4=失败
    embedding       VECTOR(512),               -- 声纹特征向量 (预留 pgvector 字段)
    remark          TEXT,                      -- 备注
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_speaker_tenant ON biz_speakers (tenant_id) WHERE is_deleted = 0;
CREATE INDEX idx_speaker_creator ON biz_speakers (creator_id) WHERE is_deleted = 0;
CREATE INDEX idx_speaker_user ON biz_speakers (user_id) WHERE is_deleted = 0;
CREATE INDEX idx_speaker_external ON biz_speakers (external_speaker_id) WHERE is_deleted = 0;
CREATE UNIQUE INDEX uk_speaker_tenant_name ON biz_speakers (tenant_id, name) WHERE is_deleted = 0;

COMMENT ON TABLE biz_speakers IS '声纹发言人基础信息表 (声纹库资源)';

-- ----------------------------
-- 7. 业务模块 - 热词管理
-- ----------------------------
DROP TABLE IF EXISTS biz_hot_word_groups CASCADE;
CREATE TABLE biz_hot_word_groups (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL,            -- 租户ID
    group_name      VARCHAR(100) NOT NULL,      -- 热词组名称
    creator_id      BIGINT,                     -- 创建人ID
    status          SMALLINT DEFAULT 1,         -- 状态: 1:启用, 0:禁用
    remark          VARCHAR(255),               -- 备注
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_hot_word_group_tenant ON biz_hot_word_groups (tenant_id) WHERE is_deleted = 0;
CREATE UNIQUE INDEX uk_hot_word_group_name_scope ON biz_hot_word_groups (tenant_id, group_name) WHERE is_deleted = 0;

COMMENT ON TABLE biz_hot_word_groups IS '热词组表';

DROP TABLE IF EXISTS biz_hot_words CASCADE;
CREATE TABLE biz_hot_words (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL,           -- 租户ID (强制隔离)
    word            VARCHAR(100) NOT NULL,     -- 热词原文
    is_public       SMALLINT DEFAULT 0,        -- 1:租户公开, 0:个人私有
    creator_id      BIGINT,                     -- 创建者ID
    pinyin_list     text,                     -- 拼音数组(支持多音字, 如 ["i mi ting", "i mei ting"])
    match_strategy  SMALLINT DEFAULT 1,        -- 匹配策略: 1:精确匹配, 2:拼音模糊匹配
    category        VARCHAR(50),               -- 类别 (人名、术语、地名)
    hot_word_group_id BIGINT,                  -- 所属热词组 ID
    weight          INTEGER DEFAULT 10,        -- 权重 (1-100)
    status          SMALLINT DEFAULT 1,        -- 状态: 1:启用, 0:禁用
    is_synced       SMALLINT DEFAULT 0,        -- 是否已同步至第三方引擎: 0:未同步, 1:已同步
    remark          TEXT,                      -- 备注
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_hotword_tenant ON biz_hot_words (tenant_id);
CREATE INDEX idx_hotword_word ON biz_hot_words (word) WHERE is_deleted = 0;
CREATE INDEX idx_hotword_group ON biz_hot_words (hot_word_group_id) WHERE is_deleted = 0;

COMMENT ON TABLE biz_hot_words IS '语音识别热词表';

-- ----------------------------
-- 8. 业务模块 - 提示词模板
-- ----------------------------
DROP TABLE IF EXISTS biz_prompt_templates CASCADE;
CREATE TABLE biz_prompt_templates (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL DEFAULT 0,  -- 租户ID (0为系统级)
    template_name   VARCHAR(100) NOT NULL,      -- 模板名称
    description     VARCHAR(255),               -- 模板描述
    category        VARCHAR(20),                -- 分类 (字典: biz_prompt_category)
    is_system       SMALLINT DEFAULT 0,         -- 是否系统预置 (1:是, 0:否)
    creator_id      BIGINT,                     -- 创建人ID
    tags            text,                      -- 标签数组 (JSONB)
    hot_word_group_id BIGINT,                  -- 绑定热词组 ID
    usage_count     INTEGER DEFAULT 0,          -- 使用次数
    prompt_content  TEXT NOT NULL,              -- 提示词内容
    status          SMALLINT DEFAULT 1,         -- 状态: 1:启用, 0:禁用
    remark          VARCHAR(255),               -- 备注
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_prompt_tenant ON biz_prompt_templates (tenant_id);
CREATE INDEX idx_prompt_system ON biz_prompt_templates (is_system) WHERE is_deleted = 0;
CREATE INDEX idx_prompt_group ON biz_prompt_templates (hot_word_group_id) WHERE is_deleted = 0;

COMMENT ON TABLE biz_prompt_templates IS '会议总结提示词模板表';

-- ----------------------------
-- 9. 业务模块 - AI 模型管理
-- ----------------------------
DROP TABLE IF EXISTS biz_asr_models CASCADE;
CREATE TABLE biz_asr_models (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL DEFAULT 0,
    model_name      VARCHAR(100) NOT NULL,
    provider        VARCHAR(50),
    base_url        VARCHAR(255),
    api_key         VARCHAR(255),
    model_code      VARCHAR(100),
    ws_url          VARCHAR(255),
    media_config    text,
    is_default      SMALLINT DEFAULT 0,
    sort_order      INTEGER NOT NULL DEFAULT 0,
    status          SMALLINT DEFAULT 1,
    remark          VARCHAR(255),
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

DROP TABLE IF EXISTS biz_llm_models CASCADE;
CREATE TABLE biz_llm_models (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL DEFAULT 0,
    model_name      VARCHAR(100) NOT NULL,
    provider        VARCHAR(50),
    base_url        VARCHAR(255),
    api_path        VARCHAR(100),
    api_key         VARCHAR(255),
    model_code      VARCHAR(100),
    temperature     DECIMAL(3,2) DEFAULT 0.7,
    top_p           DECIMAL(3,2) DEFAULT 0.9,
    is_default      SMALLINT DEFAULT 0,
    sort_order      INTEGER NOT NULL DEFAULT 0,
    status          SMALLINT DEFAULT 1,
    remark          VARCHAR(255),
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_asr_model_tenant ON biz_asr_models (tenant_id);
CREATE INDEX idx_asr_model_default ON biz_asr_models (is_default) WHERE is_deleted = 0;
CREATE INDEX idx_asr_model_sort_order ON biz_asr_models (tenant_id, is_default, sort_order) WHERE is_deleted = 0;
CREATE UNIQUE INDEX uk_asr_model_default_enabled_tenant ON biz_asr_models (tenant_id) WHERE is_deleted = 0 AND status = 1 AND is_default = 1;
CREATE INDEX idx_llm_model_tenant ON biz_llm_models (tenant_id);
CREATE INDEX idx_llm_model_default ON biz_llm_models (is_default) WHERE is_deleted = 0;
CREATE INDEX idx_llm_model_sort_order ON biz_llm_models (tenant_id, is_default, sort_order) WHERE is_deleted = 0;
CREATE UNIQUE INDEX uk_llm_model_default_enabled_tenant ON biz_llm_models (tenant_id) WHERE is_deleted = 0 AND status = 1 AND is_default = 1;

COMMENT ON TABLE biz_asr_models IS 'ASR 模型配置表';
COMMENT ON TABLE biz_llm_models IS 'LLM 模型配置表';

-- ----------------------------
-- 10. 业务模块 - 会议主表
-- ----------------------------
DROP TABLE IF EXISTS biz_meetings CASCADE;
CREATE TABLE biz_meetings (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL DEFAULT 0,
    title           VARCHAR(200) NOT NULL,
    meeting_time    TIMESTAMP(6),
    participants    TEXT,
    tags            VARCHAR(255),
    audio_url       VARCHAR(500),
    meeting_type    VARCHAR(32),                  -- OFFLINE / REALTIME
    meeting_source  VARCHAR(32),                  -- WEB / ANDROID
    creator_id      BIGINT,                     -- 发起人ID
    creator_name    VARCHAR(100),               -- 发起人姓名
    host_user_id    BIGINT,                     -- 主持人用户ID
    host_name       VARCHAR(100),               -- 主持人展示名称
    access_password VARCHAR(128),               -- 兼容旧版安卓预览访问的会议访问密码
    latest_summary_task_id BIGINT,              -- 最新成功总结任务ID
    audio_save_status VARCHAR(20) DEFAULT 'NONE', -- 实时音频保存状态：NONE/SUCCESS/FAILED
    audio_save_message VARCHAR(500),            -- 实时音频保存失败提示信息
    status          SMALLINT DEFAULT 0,         -- 0:待处理, 1:处理中, 2:成功, 3:失败
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at      TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted      SMALLINT NOT NULL DEFAULT 0
);

-- ----------------------------
-- 11. 业务模块 - 转录明细表
-- ----------------------------
DROP TABLE IF EXISTS biz_meeting_transcripts CASCADE;
CREATE TABLE biz_meeting_transcripts (
    id              BIGSERIAL PRIMARY KEY,
    meeting_id      BIGINT NOT NULL,
    speaker_id      VARCHAR(50),                -- ASR返回的发言人标识
    speaker_name    VARCHAR(100),               -- 修改后的发言人姓名
    speaker_label   VARCHAR(50),                -- 发言人标签
    content         TEXT,                       -- 转录内容
    start_time      INTEGER,                    -- 开始时间(ms)
    end_time        INTEGER,                    -- 结束时间(ms)
    sort_order      INTEGER,
    created_at      TIMESTAMP(6) NOT NULL DEFAULT now()
);

-- ----------------------------
-- 12. 业务模块 - AI 异步任务日志表
-- ----------------------------
DROP TABLE IF EXISTS biz_ai_tasks CASCADE;
CREATE TABLE biz_ai_tasks (
    id              BIGSERIAL PRIMARY KEY,
    meeting_id      BIGINT NOT NULL,
    task_type       VARCHAR(20),                -- ASR / SUMMARY
    status          SMALLINT DEFAULT 0,         -- 0:排队, 1:执行中, 2:成功, 3:失败
    request_data    text,                      -- 请求三方原始JSON
    response_data   text,                      -- 三方返回原始JSON
    task_config     text,                      -- 任务配置参数快照
    result_file_path VARCHAR(500),              -- 结果文件路径
    error_msg       TEXT,                       -- 错误堆栈
    started_at      TIMESTAMP(6),
    completed_at    TIMESTAMP(6)
);

CREATE INDEX idx_meeting_tenant ON biz_meetings (tenant_id);
CREATE INDEX idx_transcript_meeting ON biz_meeting_transcripts (meeting_id);
CREATE INDEX idx_aitask_meeting ON biz_ai_tasks (meeting_id);

COMMENT ON TABLE biz_meetings IS '会议管理主表';
COMMENT ON TABLE biz_meeting_transcripts IS '会议转录明细表';
COMMENT ON TABLE biz_ai_tasks IS 'AI 任务流水日志表';
DROP TABLE IF EXISTS "biz_prompt_template_user_config";
CREATE TABLE "biz_prompt_template_user_config" (
                                                   "id" BIGSERIAL PRIMARY KEY,
                                                   "tenant_id" int8 NOT NULL DEFAULT 0,
                                                   "user_id" int8 NOT NULL,
                                                   "template_id" int8 NOT NULL,
                                                   "status" int2 DEFAULT 1,
                                                   "created_at" timestamp(6) NOT NULL DEFAULT now(),
                                                   "updated_at" timestamp(6) NOT NULL DEFAULT now(),
                                                   "is_deleted" int2 NOT NULL DEFAULT 0
);

-- ----------------------------
-- 13. 业务模块 - 旧版安卓兼容
-- ----------------------------
DROP TABLE IF EXISTS biz_client_downloads CASCADE;
CREATE TABLE biz_client_downloads (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    platform_type VARCHAR(32),                  -- 平台类型
    platform_name VARCHAR(64),                  -- 平台显示名称
    platform_code VARCHAR(64) NOT NULL,         -- 平台编码，如 android / ios / windows
    version VARCHAR(64) NOT NULL,               -- 版本名称
    version_code BIGINT,                        -- 版本号
    download_url VARCHAR(512) NOT NULL,         -- 下载地址
    file_size BIGINT,                           -- 文件大小
    release_notes TEXT,                         -- 发布说明
    is_latest SMALLINT NOT NULL DEFAULT 0,      -- 是否当前平台最新版本：1-是，0-否
    min_system_version VARCHAR(64),             -- 最低系统版本要求
    created_by BIGINT,                          -- 创建人
    status SMALLINT NOT NULL DEFAULT 1,         -- 状态
    remark VARCHAR(255),                        -- 备注
    created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_client_downloads_platform_code
    ON biz_client_downloads (platform_code);

CREATE INDEX idx_client_downloads_latest
    ON biz_client_downloads (platform_code, is_latest)
    WHERE is_deleted = 0;

COMMENT ON TABLE biz_client_downloads IS '旧版安卓客户端版本兼容表';

DROP TABLE IF EXISTS biz_external_apps CASCADE;
CREATE TABLE biz_external_apps (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    app_name VARCHAR(128) NOT NULL,             -- 应用名称
    app_type VARCHAR(32) NOT NULL,              -- 应用类型：native / web
    app_info JSONB,                             -- 应用附加信息，如 web_url / package_name / apk_url
    icon_url VARCHAR(512),                      -- 图标地址
    description VARCHAR(255),                   -- 描述
    sort_order INTEGER NOT NULL DEFAULT 0,      -- 排序值
    created_by BIGINT,                          -- 创建人
    status SMALLINT NOT NULL DEFAULT 1,         -- 状态
    remark VARCHAR(255),                        -- 备注
    created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_external_apps_status_sort
    ON biz_external_apps (status, sort_order);

COMMENT ON TABLE biz_external_apps IS '旧版安卓首页外部应用兼容表';


-- ----------------------------
-- 5. 基础初始化数据
-- ----------------------------


-- ----------------------------
-- 6. 屏保模块
-- ----------------------------

CREATE TABLE biz_screen_savers (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    scope_type VARCHAR(32) NOT NULL DEFAULT 'PLATFORM',
    owner_user_id BIGINT,
    name VARCHAR(128) NOT NULL,
    image_url VARCHAR(512) NOT NULL,
    description VARCHAR(255),
    display_duration_sec INTEGER NOT NULL DEFAULT 15,
    image_width INTEGER,
    image_height INTEGER,
    image_format VARCHAR(16),
    sort_order INTEGER NOT NULL DEFAULT 0,
    created_by BIGINT,
    status SMALLINT NOT NULL DEFAULT 1,
    remark VARCHAR(255),
    created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted SMALLINT NOT NULL DEFAULT 0
);

CREATE INDEX idx_screen_savers_status_sort
    ON biz_screen_savers (status, sort_order);

CREATE INDEX idx_screen_savers_scope_owner_status_sort
    ON biz_screen_savers (scope_type, owner_user_id, status, sort_order);

CREATE TABLE biz_screen_saver_user_config (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    user_id BIGINT NOT NULL,
    screen_saver_id BIGINT NOT NULL,
    status SMALLINT NOT NULL DEFAULT 1,
    created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted SMALLINT NOT NULL DEFAULT 0
);

CREATE UNIQUE INDEX uk_screen_saver_user_cfg_user_item
    ON biz_screen_saver_user_config (tenant_id, user_id, screen_saver_id)
    WHERE is_deleted = 0;

CREATE INDEX idx_screen_saver_user_cfg_item
    ON biz_screen_saver_user_config (screen_saver_id)
    WHERE is_deleted = 0;

CREATE TABLE biz_screen_saver_user_settings (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    user_id BIGINT NOT NULL,
    display_duration_sec INTEGER NOT NULL DEFAULT 15,
    created_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    updated_at TIMESTAMP(6) NOT NULL DEFAULT now(),
    is_deleted SMALLINT NOT NULL DEFAULT 0
);

CREATE UNIQUE INDEX uk_screen_saver_user_settings_user
    ON biz_screen_saver_user_settings (tenant_id, user_id)
    WHERE is_deleted = 0;

