-----------张斌2025-06-16 15:00:00--------------

--------------------pgsql创建customer_server_account表语句---------------------------
-- 客服账号表（RAG智能客服）
CREATE TABLE IF NOT EXISTS customer_server_account (
    -- 基础字段（完全对齐项目规范）
    id BIGINT PRIMARY KEY, -- 主键ID（非自增）
    tenant_id BIGINT NOT NULL DEFAULT 0, -- 租户ID int8
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at timestamp(6),

    -- 业务字段
    account_code VARCHAR(128) NOT NULL, -- 客服账号编码
    account_name VARCHAR(128) NOT NULL, -- 客服账号名称
    status SMALLINT NOT NULL DEFAULT 1, -- 状态：1启用/0停用
    platform VARCHAR(32) NOT NULL, -- 客服平台
    greeting TEXT DEFAULT '', -- 开场白
    keyword_option TEXT[] DEFAULT '{}', -- 关键词选项数组
    self_identity TEXT DEFAULT '', -- AI身份描述
    dataset_ids BIGINT[] DEFAULT '{}', -- 绑定的数据集ID列表
    document_ids BIGINT[] DEFAULT '{}', -- 绑定的文档ID列表
    expand_data JSONB DEFAULT '{}'::JSONB -- 扩展数据(JSONB)
);

-- 索引（高频查询）
CREATE INDEX idx_csa_tenant_id ON customer_server_account(tenant_id);
CREATE INDEX idx_csa_account_code ON customer_server_account(account_code);
CREATE INDEX idx_csa_account_name ON customer_server_account(account_name);
CREATE INDEX idx_csa_status ON customer_server_account(status);
CREATE INDEX idx_csa_platform ON customer_server_account(platform);
CREATE INDEX idx_csa_deleted_at ON customer_server_account(deleted_at);

-- 表和字段注释
COMMENT ON TABLE customer_server_account IS '客服账号表（RAG智能客服配置）';
COMMENT ON COLUMN customer_server_account.id IS '主键ID（非自增）';
COMMENT ON COLUMN customer_server_account.tenant_id IS '租户ID';
COMMENT ON COLUMN customer_server_account.creator IS '创建人';
COMMENT ON COLUMN customer_server_account.created_at IS '创建时间';
COMMENT ON COLUMN customer_server_account.updater IS '更新人';
COMMENT ON COLUMN customer_server_account.updated_at IS '更新时间';
COMMENT ON COLUMN customer_server_account.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN customer_server_account.account_code IS '客服账号编码';
COMMENT ON COLUMN customer_server_account.account_name IS '客服账号名称';
COMMENT ON COLUMN customer_server_account.status IS '客服账号状态';
COMMENT ON COLUMN customer_server_account.platform IS '客服平台';
COMMENT ON COLUMN customer_server_account.greeting IS '开场白';
COMMENT ON COLUMN customer_server_account.keyword_option IS '关键词选项数组';
COMMENT ON COLUMN customer_server_account.self_identity IS 'AI身份描述';
COMMENT ON COLUMN customer_server_account.dataset_ids IS '绑定的数据集ID列表';
COMMENT ON COLUMN customer_server_account.document_ids IS '绑定的文档ID列表';
COMMENT ON COLUMN customer_server_account.expand_data IS '扩展数据(JSONB)';

--------------------pgsql创建customer_server_account表语句---------------------------

--------------------pgsql创建customer_server_scripted_speech表语句---------------------------
-- 客服话术表（自定义话术）
CREATE TABLE IF NOT EXISTS customer_server_scripted_speech (
    -- 基础字段（完全对齐项目规范）
    id BIGINT PRIMARY KEY, -- 主键ID（非自增）
    tenant_id BIGINT NOT NULL DEFAULT 0, -- 租户ID int8
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at timestamp(6),

    -- 业务字段
    dataset_id BIGINT NOT NULL, -- 数据集ID
    scene_type SMALLINT NOT NULL, -- 场景类型
    question_content TEXT NOT NULL -- 问题内容
    );

-- 索引（高频查询）
CREATE INDEX idx_csss_tenant_id ON customer_server_scripted_speech(tenant_id);
CREATE INDEX idx_csss_dataset_id ON customer_server_scripted_speech(dataset_id);
CREATE INDEX idx_csss_deleted_at ON customer_server_scripted_speech(deleted_at);

-- 表和字段注释
COMMENT ON TABLE customer_server_scripted_speech IS '客服话术表（自定义话术）';
COMMENT ON COLUMN customer_server_scripted_speech.id IS '主键ID（非自增）';
COMMENT ON COLUMN customer_server_scripted_speech.tenant_id IS '租户ID';
COMMENT ON COLUMN customer_server_scripted_speech.creator IS '创建人';
COMMENT ON COLUMN customer_server_scripted_speech.created_at IS '创建时间';
COMMENT ON COLUMN customer_server_scripted_speech.updater IS '更新人';
COMMENT ON COLUMN customer_server_scripted_speech.updated_at IS '更新时间';
COMMENT ON COLUMN customer_server_scripted_speech.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN customer_server_scripted_speech.dataset_id IS '数据集ID';
COMMENT ON COLUMN customer_server_scripted_speech.scene_type IS '场景类型';
COMMENT ON COLUMN customer_server_scripted_speech.question_content IS '问题内容';

--------------------pgsql创建customer_server_scripted_speech表语句---------------------------

--------------------pgsql创建customer_server_account_user_dialog表语句---------------------------
-- 客服账号用户对话统计表
CREATE TABLE IF NOT EXISTS customer_server_account_user_dialog (
    -- 基础字段（完全对齐项目规范）
    id BIGINT PRIMARY KEY, -- 主键ID（非自增）
    tenant_id BIGINT NOT NULL DEFAULT 0, -- 租户ID int8
    creator VARCHAR(64) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) NOT NULL,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at timestamp(6),

    -- 业务字段
    account_id BIGINT NOT NULL, -- 客服ID
    user_id VARCHAR(255) NOT NULL, -- 用户ID
    dialog_count BIGINT NOT NULL DEFAULT 0 -- 对话次数
    );

-- 唯一索引：租户 + 客服ID + 用户ID 唯一
CREATE UNIQUE INDEX uk_csaud_tenant_account_user
    ON customer_server_account_user_dialog(tenant_id, account_id, user_id)
    WHERE deleted_at IS NULL;

-- 索引（高频查询）
CREATE INDEX idx_csaud_tenant_id ON customer_server_account_user_dialog(tenant_id);
CREATE INDEX idx_csaud_account_id ON customer_server_account_user_dialog(account_id);
CREATE INDEX idx_csaud_user_id ON customer_server_account_user_dialog(user_id);
CREATE INDEX idx_csaud_deleted_at ON customer_server_account_user_dialog(deleted_at);

-- 表和字段注释
COMMENT ON TABLE customer_server_account_user_dialog IS '客服账号用户对话统计表';
COMMENT ON COLUMN customer_server_account_user_dialog.id IS '主键ID（非自增）';
COMMENT ON COLUMN customer_server_account_user_dialog.tenant_id IS '租户ID';
COMMENT ON COLUMN customer_server_account_user_dialog.creator IS '创建人';
COMMENT ON COLUMN customer_server_account_user_dialog.created_at IS '创建时间';
COMMENT ON COLUMN customer_server_account_user_dialog.updater IS '更新人';
COMMENT ON COLUMN customer_server_account_user_dialog.updated_at IS '更新时间';
COMMENT ON COLUMN customer_server_account_user_dialog.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN customer_server_account_user_dialog.account_id IS '客服ID';
COMMENT ON COLUMN customer_server_account_user_dialog.user_id IS '用户ID';
COMMENT ON COLUMN customer_server_account_user_dialog.dialog_count IS '对话次数';

--------------------pgsql创建customer_server_account_user_dialog表语句---------------------------