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

--------------------pgsql创建digital_human_audio表语句---------------------------
-- 音频表
CREATE TABLE IF NOT EXISTS digital_human_audio (
    -- 基础字段（继承 SQLBaseCol 通用字段，与 SQLBaseDO 对齐）
    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),

    -- 音频核心字段
    name VARCHAR(128) NOT NULL, -- 音频名称
    description TEXT DEFAULT '', -- 音频描述
    script_text TEXT NOT NULL, -- 话术文本
    audio_url VARCHAR(512) DEFAULT '', -- 音频文件URL
    status SMALLINT NOT NULL DEFAULT 0, -- 状态：0生成中/1成功/2失败
    error_msg TEXT DEFAULT '', -- 错误信息
    duration INT DEFAULT 0, -- 音频时长（秒）
    external_id VARCHAR(64) DEFAULT '', -- 外部音频ID
    voice VARCHAR(32) DEFAULT 'serena', -- 音色：serena/vivian/uncle_fu/ryan/aiden/ono_anna/sohee/eric/dylan
    voice_type VARCHAR(16) DEFAULT 'preset', -- 音色类型：preset/custom（预设/克隆）
    custom_voice VARCHAR(64) DEFAULT '' -- 自定义音色ID（用于声音克隆）
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_audio_tenant_id ON digital_human_audio(tenant_id);
CREATE INDEX IF NOT EXISTS idx_audio_status ON digital_human_audio(status);
CREATE INDEX IF NOT EXISTS idx_audio_voice_type ON digital_human_audio(voice_type);
CREATE INDEX IF NOT EXISTS idx_audio_deleted_at ON digital_human_audio(deleted_at);

-- 表和字段注释
COMMENT ON TABLE digital_human_audio IS '音频表';
COMMENT ON COLUMN digital_human_audio.id IS '主键ID（非自增）';
COMMENT ON COLUMN digital_human_audio.tenant_id IS '租户ID';
COMMENT ON COLUMN digital_human_audio.creator IS '创建人';
COMMENT ON COLUMN digital_human_audio.created_at IS '创建时间';
COMMENT ON COLUMN digital_human_audio.updater IS '更新人';
COMMENT ON COLUMN digital_human_audio.updated_at IS '更新时间';
COMMENT ON COLUMN digital_human_audio.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN digital_human_audio.name IS '音频名称';
COMMENT ON COLUMN digital_human_audio.description IS '音频描述';
COMMENT ON COLUMN digital_human_audio.script_text IS '话术文本';
COMMENT ON COLUMN digital_human_audio.audio_url IS '音频文件URL';
COMMENT ON COLUMN digital_human_audio.status IS '状态：0生成中/1成功/2失败';
COMMENT ON COLUMN digital_human_audio.error_msg IS '错误信息';
COMMENT ON COLUMN digital_human_audio.duration IS '音频时长（秒）';
COMMENT ON COLUMN digital_human_audio.external_id IS '外部音频ID';
COMMENT ON COLUMN digital_human_audio.voice IS '音色：serena/vivian/uncle_fu/ryan/aiden/ono_anna/sohee/eric/dylan';
COMMENT ON COLUMN digital_human_audio.voice_type IS '音色类型：preset/custom（预设/克隆）';
COMMENT ON COLUMN digital_human_audio.custom_voice IS '自定义音色ID';

--------------------pgsql创建digital_human_custom_voice表语句---------------------------
-- 自定义音色表
CREATE TABLE IF NOT EXISTS digital_human_custom_voice (
    -- 基础字段（继承 SQLBaseCol 通用字段）
    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),

    -- 音色核心字段
    name VARCHAR(128) NOT NULL, -- 音色名称
    description TEXT DEFAULT '', -- 音色描述
    text TEXT DEFAULT '', -- 参考文本
    reference_audio BYTEA -- 参考音频数据（二进制）
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_custom_voice_tenant_id ON digital_human_custom_voice(tenant_id);
CREATE INDEX IF NOT EXISTS idx_custom_voice_name ON digital_human_custom_voice(name);
CREATE INDEX IF NOT EXISTS idx_custom_voice_deleted_at ON digital_human_custom_voice(deleted_at);

-- 表和字段注释
COMMENT ON TABLE digital_human_custom_voice IS '自定义音色表';
COMMENT ON COLUMN digital_human_custom_voice.id IS '主键ID（非自增）';
COMMENT ON COLUMN digital_human_custom_voice.tenant_id IS '租户ID';
COMMENT ON COLUMN digital_human_custom_voice.creator IS '创建人';
COMMENT ON COLUMN digital_human_custom_voice.created_at IS '创建时间';
COMMENT ON COLUMN digital_human_custom_voice.updater IS '更新人';
COMMENT ON COLUMN digital_human_custom_voice.updated_at IS '更新时间';
COMMENT ON COLUMN digital_human_custom_voice.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN digital_human_custom_voice.name IS '音色名称';
COMMENT ON COLUMN digital_human_custom_voice.description IS '音色描述';
COMMENT ON COLUMN digital_human_custom_voice.text IS '参考文本';
COMMENT ON COLUMN digital_human_custom_voice.reference_audio IS '参考音频数据（二进制）';

-- 兼容已有库：自定义音色增加状态/结果字段（对接异步模型服务）
ALTER TABLE digital_human_custom_voice ADD COLUMN IF NOT EXISTS status SMALLINT NOT NULL DEFAULT 0;
ALTER TABLE digital_human_custom_voice ADD COLUMN IF NOT EXISTS error_msg TEXT DEFAULT '';
ALTER TABLE digital_human_custom_voice ADD COLUMN IF NOT EXISTS oss_file VARCHAR(512) DEFAULT '';
COMMENT ON COLUMN digital_human_custom_voice.status IS '状态：0生成中/1成功/2失败';
COMMENT ON COLUMN digital_human_custom_voice.error_msg IS '错误信息';
COMMENT ON COLUMN digital_human_custom_voice.oss_file IS '结果文件URL（如参考音频/特征文件等）';
CREATE INDEX IF NOT EXISTS idx_custom_voice_status ON digital_human_custom_voice(status);

--------------------pgsql创建digital_human_video表语句---------------------------
-- 视频表
CREATE TABLE IF NOT EXISTS digital_human_video (
    -- 基础字段
    id BIGINT PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    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),

    -- 视频核心字段
    name VARCHAR(128) NOT NULL, -- 视频名称
    description TEXT DEFAULT '', -- 视频描述
    audio_id BIGINT, -- 关联音频ID
    script_text TEXT NOT NULL, -- 话术文本
    video_url VARCHAR(512) DEFAULT '', -- 视频文件URL
    status SMALLINT NOT NULL DEFAULT 0, -- 状态：0生成中/1成功/2失败
    error_msg TEXT DEFAULT '', -- 错误信息
    duration INT DEFAULT 0, -- 视频时长（秒）
    thumbnail_url VARCHAR(512) DEFAULT '', -- 缩略图URL
    external_id VARCHAR(64) DEFAULT '', -- 外部视频ID
    digital_human_id BIGINT DEFAULT 0, -- 数字人ID（雪花算法ID）
    digital_human_name VARCHAR(128) DEFAULT '' -- 数字人名称（冗余字段）
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_video_tenant_id ON digital_human_video(tenant_id);
CREATE INDEX IF NOT EXISTS idx_video_audio_id ON digital_human_video(audio_id);
CREATE INDEX IF NOT EXISTS idx_video_status ON digital_human_video(status);
CREATE INDEX IF NOT EXISTS idx_video_deleted_at ON digital_human_video(deleted_at);

-- 表和字段注释
COMMENT ON TABLE digital_human_video IS '视频表';
COMMENT ON COLUMN digital_human_video.id IS '主键ID（非自增）';
COMMENT ON COLUMN digital_human_video.tenant_id IS '租户ID';
COMMENT ON COLUMN digital_human_video.audio_id IS '关联音频ID';
COMMENT ON COLUMN digital_human_video.name IS '视频名称';
COMMENT ON COLUMN digital_human_video.description IS '视频描述';
COMMENT ON COLUMN digital_human_video.script_text IS '话术文本';
COMMENT ON COLUMN digital_human_video.video_url IS '视频文件URL';
COMMENT ON COLUMN digital_human_video.status IS '状态：0生成中/1成功/2失败';
COMMENT ON COLUMN digital_human_video.error_msg IS '错误信息';
COMMENT ON COLUMN digital_human_video.duration IS '视频时长（秒）';
COMMENT ON COLUMN digital_human_video.thumbnail_url IS '缩略图URL';
COMMENT ON COLUMN digital_human_video.external_id IS '外部视频ID';
COMMENT ON COLUMN digital_human_video.digital_human_id IS '数字人ID';
COMMENT ON COLUMN digital_human_video.digital_human_name IS '数字人名称（冗余字段）';

--------------------pgsql创建digital_human表语句---------------------------
-- 数字人表
CREATE TABLE IF NOT EXISTS digital_human (
    -- 基础字段
    id BIGINT PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    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),

    -- 数字人核心字段
    name VARCHAR(128) NOT NULL, -- 数字人名称
    description TEXT DEFAULT '', -- 数字人描述
    avatar_url VARCHAR(512) DEFAULT '', -- 头像URL
    video_url VARCHAR(512) DEFAULT '', -- 形象视频URL
    voice VARCHAR(32) DEFAULT 'serena', -- 默认音色
    status SMALLINT NOT NULL DEFAULT 1 -- 状态：1启用/0停用
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_digital_human_tenant_id ON digital_human(tenant_id);
CREATE INDEX IF NOT EXISTS idx_digital_human_name ON digital_human(name);
CREATE INDEX IF NOT EXISTS idx_digital_human_status ON digital_human(status);
CREATE INDEX IF NOT EXISTS idx_digital_human_deleted_at ON digital_human(deleted_at);

-- 表和字段注释
COMMENT ON TABLE digital_human IS '数字人表';
COMMENT ON COLUMN digital_human.id IS '主键ID（非自增）';
COMMENT ON COLUMN digital_human.tenant_id IS '租户ID';
COMMENT ON COLUMN digital_human.creator IS '创建人';
COMMENT ON COLUMN digital_human.created_at IS '创建时间';
COMMENT ON COLUMN digital_human.updater IS '更新人';
COMMENT ON COLUMN digital_human.updated_at IS '更新时间';
COMMENT ON COLUMN digital_human.deleted_at IS '删除时间（软删）';
COMMENT ON COLUMN digital_human.name IS '数字人名称';
COMMENT ON COLUMN digital_human.description IS '数字人描述';
COMMENT ON COLUMN digital_human.avatar_url IS '头像URL';
COMMENT ON COLUMN digital_human.video_url IS '形象视频URL';
COMMENT ON COLUMN digital_human.voice IS '默认音色';
COMMENT ON COLUMN digital_human.status IS '状态：1启用/0停用';

--------------------pgsql创建digital_human_async_task_ref表语句---------------------------
-- 异步任务绑定表（task_id -> 业务表+业务ID）
CREATE TABLE IF NOT EXISTS digital_human_async_task_ref (
    -- 基础字段
    id BIGINT PRIMARY KEY, -- 主键ID（非自增）
    tenant_id BIGINT NOT NULL DEFAULT 0, -- 租户ID
    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),

    -- 绑定字段
    task_id VARCHAR(64) NOT NULL, -- 异步任务ID（model-asynch）
    state SMALLINT NOT NULL DEFAULT 0, -- 任务状态（与 model-asynch 对齐：0/1/2/3/4）
    table_name VARCHAR(64) NOT NULL, -- 业务表名：digital_human_audio / digital_human_custom_voice
    biz_id BIGINT NOT NULL, -- 业务表主键ID（audio/custom_voice 的 id）
    oss_file VARCHAR(512) DEFAULT '', -- 已转移后的业务侧OSS地址（可选）
    error_msg TEXT DEFAULT '' -- 错误信息（可选）
);

-- 索引
CREATE UNIQUE INDEX IF NOT EXISTS uk_async_task_ref_tenant_task_id ON digital_human_async_task_ref(tenant_id, task_id);
CREATE INDEX IF NOT EXISTS idx_async_task_ref_tenant_state ON digital_human_async_task_ref(tenant_id, state);
CREATE INDEX IF NOT EXISTS idx_async_task_ref_table_biz ON digital_human_async_task_ref(table_name, biz_id);
CREATE INDEX IF NOT EXISTS idx_async_task_ref_deleted_at ON digital_human_async_task_ref(deleted_at);

-- 注释
COMMENT ON TABLE digital_human_async_task_ref IS '异步任务绑定表（task_id -> 业务表+业务ID）';
COMMENT ON COLUMN digital_human_async_task_ref.task_id IS '异步任务ID（model-asynch）';
COMMENT ON COLUMN digital_human_async_task_ref.state IS '任务状态（与 model-asynch 对齐：0/1/2/3/4）';
COMMENT ON COLUMN digital_human_async_task_ref.table_name IS '业务表名';
COMMENT ON COLUMN digital_human_async_task_ref.biz_id IS '业务表主键ID';
COMMENT ON COLUMN digital_human_async_task_ref.oss_file IS '已转移后的业务侧OSS地址';
COMMENT ON COLUMN digital_human_async_task_ref.error_msg IS '错误信息';
