-- Active: 1775801470429@@39.107.112.174@5432@lablesystem
begin;

-- Drop Tables (按依赖关系倒序删除)
DROP TABLE IF EXISTS export_batch_item CASCADE;
DROP TABLE IF EXISTS export_batch CASCADE;
DROP TABLE IF EXISTS training_dataset CASCADE;
DROP TABLE IF EXISTS annotation_result_history CASCADE;
DROP TABLE IF EXISTS annotation_result CASCADE;
DROP TABLE IF EXISTS annotation_task_resource CASCADE;
DROP TABLE IF EXISTS annotation_task CASCADE;
DROP TABLE IF EXISTS source_resource CASCADE;
DROP TABLE IF EXISTS sys_config CASCADE;
DROP TABLE IF EXISTS sys_menu CASCADE;
DROP TABLE IF EXISTS sys_user CASCADE;
DROP TABLE IF EXISTS sys_company CASCADE;


CREATE TABLE IF NOT EXISTS sys_company (
    id BIGINT PRIMARY KEY,
    company_code VARCHAR(64) NOT NULL UNIQUE,
    company_name VARCHAR(128) NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE sys_company IS '公司表。';
COMMENT ON COLUMN sys_company.id IS '公司主键ID。';
COMMENT ON COLUMN sys_company.company_code IS '公司编码，登录时通过 companyCode 使用。';
COMMENT ON COLUMN sys_company.company_name IS '公司名称。';
COMMENT ON COLUMN sys_company.status IS '公司状态，默认 ENABLED，可按需改为 DISABLED 等状态。';
COMMENT ON COLUMN sys_company.created_at IS '创建时间。';
COMMENT ON COLUMN sys_company.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS sys_user (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    phone VARCHAR(32) NOT NULL,
    username VARCHAR(64),
    role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    position VARCHAR(32) NOT NULL DEFAULT 'ANNOTATOR',
    real_name VARCHAR(64) NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    must_change_password BOOLEAN NOT NULL DEFAULT TRUE,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    session_version INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT uq_sys_user_company_phone UNIQUE (company_id, phone),
    CONSTRAINT fk_sys_user_company FOREIGN KEY (company_id) REFERENCES sys_company(id)
);

COMMENT ON TABLE sys_user IS '用户表。role 表示数据权限角色，position 表示岗位。';
COMMENT ON COLUMN sys_user.id IS '用户主键ID。';
COMMENT ON COLUMN sys_user.company_id IS '所属公司ID，关联 sys_company.id。';
COMMENT ON COLUMN sys_user.phone IS '登录手机号，同公司内唯一。';
COMMENT ON COLUMN sys_user.username IS '用户名或账号别名，用于展示。';
COMMENT ON COLUMN sys_user.role IS '数据权限角色，默认 EMPLOYEE，可选 EMPLOYEE、MANAGER、ENGINEER。';
COMMENT ON COLUMN sys_user.position IS '岗位，默认 ANNOTATOR，可选 ANNOTATOR、DATA_TRAINER、REVIEWER、ADMIN。';
COMMENT ON COLUMN sys_user.real_name IS '用户真实姓名。';
COMMENT ON COLUMN sys_user.password_hash IS '密码哈希值。';
COMMENT ON COLUMN sys_user.must_change_password IS '是否首次登录强制改密。';
COMMENT ON COLUMN sys_user.status IS '用户状态，默认 ENABLED。';
COMMENT ON COLUMN sys_user.session_version IS '会话版本号，用于强制旧 Token 失效。';
COMMENT ON COLUMN sys_user.created_at IS '创建时间。';
COMMENT ON COLUMN sys_user.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS sys_menu (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    menu_code VARCHAR(64) NOT NULL,
    menu_name VARCHAR(128) NOT NULL,
    path VARCHAR(255) NOT NULL,
    visible_positions VARCHAR(255) NOT NULL DEFAULT 'ADMIN',
    sort_order INTEGER NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_sys_menu_company FOREIGN KEY (company_id) REFERENCES sys_company(id)
);

COMMENT ON TABLE sys_menu IS '菜单表。';
COMMENT ON COLUMN sys_menu.id IS '菜单主键ID。';
COMMENT ON COLUMN sys_menu.company_id IS '所属公司ID。';
COMMENT ON COLUMN sys_menu.menu_code IS '菜单编码。';
COMMENT ON COLUMN sys_menu.menu_name IS '菜单名称。';
COMMENT ON COLUMN sys_menu.path IS '前端路由路径。';
COMMENT ON COLUMN sys_menu.visible_positions IS '可见岗位列表，使用逗号分隔存储，如 ANNOTATOR,DATA_TRAINER,REVIEWER,ADMIN。';
COMMENT ON COLUMN sys_menu.sort_order IS '菜单排序号，默认 0。';
COMMENT ON COLUMN sys_menu.created_at IS '创建时间。';
COMMENT ON COLUMN sys_menu.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS sys_config (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    config_type VARCHAR(32) NOT NULL DEFAULT 'SYSTEM',
    config_name VARCHAR(128) NOT NULL,
    config_value TEXT NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    creator_id BIGINT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT uq_sys_config_company_name UNIQUE (company_id, config_name),
    CONSTRAINT fk_sys_config_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_sys_config_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id)
);

COMMENT ON TABLE sys_config IS '系统配置表，保存模型配置、Prompt 配置和系统参数配置。';
COMMENT ON COLUMN sys_config.id IS '配置主键ID。';
COMMENT ON COLUMN sys_config.company_id IS '所属公司ID。';
COMMENT ON COLUMN sys_config.config_type IS '配置类型，默认 SYSTEM，可选 MODEL、PROMPT、SYSTEM。';
COMMENT ON COLUMN sys_config.config_name IS '配置名称。MODEL 类型时存模型名如 qwen-max；PROMPT 类型时存名称如 extractPrompt、verifyPrompt；SYSTEM 类型时存参数名。';
COMMENT ON COLUMN sys_config.config_value IS '配置值。MODEL 类型建议保存 JSON，至少包含 modelName、modelUrl、apiKey；PROMPT 类型保存提示词文本；SYSTEM 类型预留后续扩展。';
COMMENT ON COLUMN sys_config.status IS '配置状态，默认 ENABLED。';
COMMENT ON COLUMN sys_config.creator_id IS '创建人用户ID。';
COMMENT ON COLUMN sys_config.created_at IS '创建时间。';
COMMENT ON COLUMN sys_config.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS source_resource (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    resource_name VARCHAR(255) NOT NULL,
    resource_type VARCHAR(32) NOT NULL DEFAULT 'TEXT',
    bucket_name VARCHAR(128) NOT NULL,
    file_path VARCHAR(512) NOT NULL,
    file_size BIGINT NOT NULL DEFAULT 0,
    source_status VARCHAR(32) NOT NULL DEFAULT 'UPLOADED',
    storage_provider VARCHAR(64) NOT NULL DEFAULT 'rustfs',
    remark VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_source_resource_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_source_resource_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id)
);

COMMENT ON TABLE source_resource IS '资源表，保存文本、图片、视频资源元数据。';
COMMENT ON COLUMN source_resource.id IS '资源主键ID。';
COMMENT ON COLUMN source_resource.company_id IS '所属公司ID。';
COMMENT ON COLUMN source_resource.creator_id IS '上传人或创建人用户ID。';
COMMENT ON COLUMN source_resource.creator_role IS '创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN source_resource.resource_name IS '资源名称。';
COMMENT ON COLUMN source_resource.resource_type IS '资源类型，默认 TEXT，可选 TEXT、IMAGE、VIDEO。';
COMMENT ON COLUMN source_resource.bucket_name IS '对象存储桶名称。';
COMMENT ON COLUMN source_resource.file_path IS '文件存储路径，表示对象在存储系统中的实际路径。';
COMMENT ON COLUMN source_resource.file_size IS '文件大小，单位字节，默认 0。';
COMMENT ON COLUMN source_resource.source_status IS '资源状态，默认 UPLOADED，可选 PROCESSING、READY、ARCHIVED。';
COMMENT ON COLUMN source_resource.storage_provider IS '存储提供方，默认 rustfs。';
COMMENT ON COLUMN source_resource.remark IS '备注说明。';
COMMENT ON COLUMN source_resource.created_at IS '创建时间。';
COMMENT ON COLUMN source_resource.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS annotation_task (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    task_name VARCHAR(255) NOT NULL,
    industry_type VARCHAR(32) NOT NULL DEFAULT 'transport',
    task_type VARCHAR(32) NOT NULL DEFAULT 'EXTRACT_QA',
    extract_model_config_id BIGINT,
    extract_model_name VARCHAR(128),
    extract_model_url VARCHAR(255),
    extract_model_api_key VARCHAR(255),
    verify_model_config_id BIGINT,
    verify_model_name VARCHAR(128),
    verify_model_url VARCHAR(255),
    verify_model_api_key VARCHAR(255),
    extract_prompt_config_id BIGINT,
    extract_prompt TEXT,
    verify_prompt_config_id BIGINT,
    verify_prompt TEXT,
    task_status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
    started_at TIMESTAMP,
    finished_at TIMESTAMP,
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_annotation_task_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_annotation_task_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id),
    CONSTRAINT fk_annotation_task_extract_model_config FOREIGN KEY (extract_model_config_id) REFERENCES sys_config(id),
    CONSTRAINT fk_annotation_task_verify_model_config FOREIGN KEY (verify_model_config_id) REFERENCES sys_config(id),
    CONSTRAINT fk_annotation_task_extract_prompt_config FOREIGN KEY (extract_prompt_config_id) REFERENCES sys_config(id),
    CONSTRAINT fk_annotation_task_verify_prompt_config FOREIGN KEY (verify_prompt_config_id) REFERENCES sys_config(id)
);

COMMENT ON TABLE annotation_task IS '任务表，保存任务、配置引用与执行快照。';
COMMENT ON COLUMN annotation_task.id IS '任务主键ID。';
COMMENT ON COLUMN annotation_task.company_id IS '所属公司ID。';
COMMENT ON COLUMN annotation_task.creator_id IS '任务创建人用户ID。';
COMMENT ON COLUMN annotation_task.creator_role IS '任务创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN annotation_task.task_name IS '任务名称。';
COMMENT ON COLUMN annotation_task.industry_type IS '行业类型简写，默认 transport，可选值按业务扩展，例如 electricity。';
COMMENT ON COLUMN annotation_task.task_type IS '任务类型，默认 EXTRACT_QA。';
COMMENT ON COLUMN annotation_task.extract_model_config_id IS '抽取模型配置ID，关联 sys_config.id。';
COMMENT ON COLUMN annotation_task.extract_model_name IS '抽取模型名称。';
COMMENT ON COLUMN annotation_task.extract_model_url IS '抽取模型调用地址。';
COMMENT ON COLUMN annotation_task.extract_model_api_key IS '抽取模型调用密钥。';
COMMENT ON COLUMN annotation_task.verify_model_config_id IS '校验模型配置ID，关联 sys_config.id。';
COMMENT ON COLUMN annotation_task.verify_model_name IS '校验模型名称。';
COMMENT ON COLUMN annotation_task.verify_model_url IS '校验模型调用地址。';
COMMENT ON COLUMN annotation_task.verify_model_api_key IS '校验模型调用密钥。';
COMMENT ON COLUMN annotation_task.extract_prompt_config_id IS '抽取Prompt配置ID，关联 sys_config.id。';
COMMENT ON COLUMN annotation_task.extract_prompt IS '抽取 Prompt 文本。';
COMMENT ON COLUMN annotation_task.verify_prompt_config_id IS '校验Prompt配置ID，关联 sys_config.id。';
COMMENT ON COLUMN annotation_task.verify_prompt IS '校验 Prompt 文本。';
COMMENT ON COLUMN annotation_task.task_status IS '任务状态，默认 PENDING，可选 RUNNING、COMPLETED、FAILED。';
COMMENT ON COLUMN annotation_task.is_deleted IS '任务软删除标记，默认 FALSE。';
COMMENT ON COLUMN annotation_task.started_at IS '任务开始时间。';
COMMENT ON COLUMN annotation_task.finished_at IS '任务结束时间。';
COMMENT ON COLUMN annotation_task.error_message IS '任务失败错误信息。';
COMMENT ON COLUMN annotation_task.created_at IS '创建时间。';
COMMENT ON COLUMN annotation_task.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS annotation_task_resource (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    task_id BIGINT NOT NULL,
    resource_id BIGINT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT uq_annotation_task_resource UNIQUE (task_id, resource_id),
    CONSTRAINT fk_annotation_task_resource_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_annotation_task_resource_task FOREIGN KEY (task_id) REFERENCES annotation_task(id),
    CONSTRAINT fk_annotation_task_resource_resource FOREIGN KEY (resource_id) REFERENCES source_resource(id)
);

COMMENT ON TABLE annotation_task_resource IS '任务与资源关联表，一个任务可绑定多个资源。';
COMMENT ON COLUMN annotation_task_resource.id IS '任务资源关联主键ID。';
COMMENT ON COLUMN annotation_task_resource.company_id IS '所属公司ID。';
COMMENT ON COLUMN annotation_task_resource.task_id IS '任务ID。';
COMMENT ON COLUMN annotation_task_resource.resource_id IS '资源ID。';
COMMENT ON COLUMN annotation_task_resource.created_at IS '创建时间。';

CREATE TABLE IF NOT EXISTS annotation_result (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    task_id BIGINT NOT NULL,
    resource_id BIGINT NOT NULL,
    qa_content_json TEXT NOT NULL DEFAULT '{}',
    qa_content_storage_mode VARCHAR(32) NOT NULL DEFAULT 'INLINE',
    qa_content_file_path VARCHAR(512),
    diff_summary TEXT NOT NULL DEFAULT '{}',
    requires_manual_review BOOLEAN NOT NULL DEFAULT FALSE,
    is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
    reviewer_id BIGINT,
    review_comment TEXT,
    reviewed_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_annotation_result_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_annotation_result_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id),
    CONSTRAINT fk_annotation_result_task FOREIGN KEY (task_id) REFERENCES annotation_task(id),
    CONSTRAINT fk_annotation_result_resource FOREIGN KEY (resource_id) REFERENCES source_resource(id),
    CONSTRAINT fk_annotation_result_reviewer FOREIGN KEY (reviewer_id) REFERENCES sys_user(id)
);

COMMENT ON TABLE annotation_result IS '当前标注结果表。';
COMMENT ON COLUMN annotation_result.id IS '标注结果主键ID。';
COMMENT ON COLUMN annotation_result.company_id IS '所属公司ID。';
COMMENT ON COLUMN annotation_result.creator_id IS '结果创建人用户ID。';
COMMENT ON COLUMN annotation_result.creator_role IS '结果创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN annotation_result.task_id IS '关联任务ID。';
COMMENT ON COLUMN annotation_result.resource_id IS '关联资源ID。';
COMMENT ON COLUMN annotation_result.qa_content_json IS '问答内容 JSON 字符串。字段类型为 TEXT，建议结构为 {\"question\":\"...\",\"answer\":\"...\"}。中小体积内容默认直接入库。';
COMMENT ON COLUMN annotation_result.qa_content_storage_mode IS '问答内容存储模式，默认 INLINE，可选 INLINE、EXTERNAL。当完整问答内容较大时，可设为 EXTERNAL，仅在表内保留摘要或索引信息。';
COMMENT ON COLUMN annotation_result.qa_content_file_path IS '当 qa_content_storage_mode = EXTERNAL 时，记录外置问答内容文件路径。';
COMMENT ON COLUMN annotation_result.diff_summary IS '差异摘要 JSON 字符串。字段类型为 TEXT，建议结构为 {\"extract_question\":\"...\",\"extract_answer\":\"...\",\"verify_answer\":\"...\",\"mismatch_fields\":[\"question\",\"answer\"],\"reason\":\"...\"}。';
COMMENT ON COLUMN annotation_result.requires_manual_review IS '是否需要人工审核，默认 FALSE。';
COMMENT ON COLUMN annotation_result.is_deleted IS '软删除标记，默认 FALSE。';
COMMENT ON COLUMN annotation_result.reviewer_id IS '审核人用户ID。';
COMMENT ON COLUMN annotation_result.review_comment IS '审核意见。';
COMMENT ON COLUMN annotation_result.reviewed_at IS '审核时间。';
COMMENT ON COLUMN annotation_result.created_at IS '创建时间。';
COMMENT ON COLUMN annotation_result.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS annotation_result_history (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    source_result_id BIGINT,
    task_id BIGINT NOT NULL,
    resource_id BIGINT NOT NULL,
    qa_content_json TEXT NOT NULL DEFAULT '{}',
    qa_content_storage_mode VARCHAR(32) NOT NULL DEFAULT 'INLINE',
    qa_content_file_path VARCHAR(512),
    archive_reason VARCHAR(255),
    archived_by BIGINT,
    archived_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_annotation_result_history_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_annotation_result_history_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id),
    CONSTRAINT fk_annotation_result_history_result FOREIGN KEY (source_result_id) REFERENCES annotation_result(id),
    CONSTRAINT fk_annotation_result_history_task FOREIGN KEY (task_id) REFERENCES annotation_task(id),
    CONSTRAINT fk_annotation_result_history_resource FOREIGN KEY (resource_id) REFERENCES source_resource(id),
    CONSTRAINT fk_annotation_result_history_archived_by FOREIGN KEY (archived_by) REFERENCES sys_user(id)
);

COMMENT ON TABLE annotation_result_history IS '历史归档结果表。';
COMMENT ON COLUMN annotation_result_history.id IS '历史结果主键ID。';
COMMENT ON COLUMN annotation_result_history.company_id IS '所属公司ID。';
COMMENT ON COLUMN annotation_result_history.creator_id IS '历史记录创建人用户ID。';
COMMENT ON COLUMN annotation_result_history.creator_role IS '历史记录创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN annotation_result_history.source_result_id IS '来源运行态结果ID。';
COMMENT ON COLUMN annotation_result_history.task_id IS '关联任务ID。';
COMMENT ON COLUMN annotation_result_history.resource_id IS '关联资源ID。';
COMMENT ON COLUMN annotation_result_history.qa_content_json IS '归档后的问答内容 JSON 字符串。字段类型为 TEXT，建议结构为 {"question":"...","answer":"..."}。';
COMMENT ON COLUMN annotation_result_history.qa_content_storage_mode IS '归档后的问答内容存储模式，默认 INLINE，可选 INLINE、EXTERNAL。';
COMMENT ON COLUMN annotation_result_history.qa_content_file_path IS '当 qa_content_storage_mode = EXTERNAL 时，记录归档后的外置问答内容文件路径。';
COMMENT ON COLUMN annotation_result_history.archive_reason IS '归档原因说明。';
COMMENT ON COLUMN annotation_result_history.archived_by IS '归档操作人用户ID。';
COMMENT ON COLUMN annotation_result_history.archived_at IS '归档时间。';
COMMENT ON COLUMN annotation_result_history.created_at IS '创建时间。';

CREATE TABLE IF NOT EXISTS training_dataset (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    result_history_id BIGINT NOT NULL,
    sample_type VARCHAR(32) NOT NULL DEFAULT 'TEXT',
    glm_format_json TEXT NOT NULL,
    dataset_status VARCHAR(32) NOT NULL DEFAULT 'DRAFT',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_training_dataset_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_training_dataset_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id),
    CONSTRAINT fk_training_dataset_result_history FOREIGN KEY (result_history_id) REFERENCES annotation_result_history(id)
);

COMMENT ON TABLE training_dataset IS '训练样本表。';
COMMENT ON COLUMN training_dataset.id IS '训练样本主键ID。';
COMMENT ON COLUMN training_dataset.company_id IS '所属公司ID。';
COMMENT ON COLUMN training_dataset.creator_id IS '样本创建人用户ID。';
COMMENT ON COLUMN training_dataset.creator_role IS '样本创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN training_dataset.result_history_id IS '来源历史结果ID。';
COMMENT ON COLUMN training_dataset.sample_type IS '样本类型，默认 TEXT，可按需扩展 IMAGE、VIDEO。';
COMMENT ON COLUMN training_dataset.glm_format_json IS 'GLM微调格式 JSON 字符串。';
COMMENT ON COLUMN training_dataset.dataset_status IS '样本状态，默认 DRAFT，可选 EXPORTED。';
COMMENT ON COLUMN training_dataset.created_at IS '创建时间。';
COMMENT ON COLUMN training_dataset.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS export_batch (
    id BIGINT PRIMARY KEY,
    company_id BIGINT NOT NULL,
    creator_id BIGINT NOT NULL,
    creator_role VARCHAR(32) NOT NULL DEFAULT 'EMPLOYEE',
    batch_no VARCHAR(64) NOT NULL UNIQUE,
    dataset_file_path VARCHAR(512),
    sample_count INTEGER NOT NULL DEFAULT 0,
    finetune_job_id VARCHAR(128),
    finetune_status VARCHAR(32) NOT NULL DEFAULT 'NOT_STARTED',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_export_batch_company FOREIGN KEY (company_id) REFERENCES sys_company(id),
    CONSTRAINT fk_export_batch_creator FOREIGN KEY (creator_id) REFERENCES sys_user(id)
);

COMMENT ON TABLE export_batch IS '导出批次表。';
COMMENT ON COLUMN export_batch.id IS '导出批次主键ID。';
COMMENT ON COLUMN export_batch.company_id IS '所属公司ID。';
COMMENT ON COLUMN export_batch.creator_id IS '批次创建人用户ID。';
COMMENT ON COLUMN export_batch.creator_role IS '批次创建人数据权限角色，默认 EMPLOYEE。';
COMMENT ON COLUMN export_batch.batch_no IS '批次编号，全局唯一。';
COMMENT ON COLUMN export_batch.dataset_file_path IS '导出的数据文件路径。';
COMMENT ON COLUMN export_batch.sample_count IS '批次包含的样本数量，默认 0。';
COMMENT ON COLUMN export_batch.finetune_job_id IS '微调任务ID。';
COMMENT ON COLUMN export_batch.finetune_status IS '微调状态，默认 NOT_STARTED，可选 RUNNING、SUCCESS、FAILED。';
COMMENT ON COLUMN export_batch.created_at IS '创建时间。';
COMMENT ON COLUMN export_batch.updated_at IS '更新时间。';

CREATE TABLE IF NOT EXISTS export_batch_item (
    id BIGINT PRIMARY KEY,
    batch_id BIGINT NOT NULL,
    dataset_id BIGINT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_export_batch_item_batch FOREIGN KEY (batch_id) REFERENCES export_batch(id),
    CONSTRAINT fk_export_batch_item_dataset FOREIGN KEY (dataset_id) REFERENCES training_dataset(id),
    CONSTRAINT uq_export_batch_item UNIQUE (batch_id, dataset_id)
);

COMMENT ON TABLE export_batch_item IS '导出批次与训练样本关系表。';
COMMENT ON COLUMN export_batch_item.id IS '批次样本关系主键ID。';
COMMENT ON COLUMN export_batch_item.batch_id IS '关联导出批次ID。';
COMMENT ON COLUMN export_batch_item.dataset_id IS '关联训练样本ID。';
COMMENT ON COLUMN export_batch_item.created_at IS '创建时间。';

CREATE INDEX IF NOT EXISTS idx_sys_user_company ON sys_user(company_id);
CREATE INDEX IF NOT EXISTS idx_sys_user_role ON sys_user(company_id, role);
CREATE INDEX IF NOT EXISTS idx_sys_user_position ON sys_user(company_id, position);
CREATE INDEX IF NOT EXISTS idx_sys_menu_company_sort ON sys_menu(company_id, sort_order);
CREATE INDEX IF NOT EXISTS idx_sys_config_company_type ON sys_config(company_id, config_type);
CREATE INDEX IF NOT EXISTS idx_source_resource_company_type ON source_resource(company_id, resource_type);
CREATE INDEX IF NOT EXISTS idx_source_resource_company_status ON source_resource(company_id, source_status);
CREATE INDEX IF NOT EXISTS idx_source_resource_creator ON source_resource(company_id, creator_id);
CREATE INDEX IF NOT EXISTS idx_annotation_task_company_status ON annotation_task(company_id, task_status);
CREATE INDEX IF NOT EXISTS idx_annotation_task_company_deleted ON annotation_task(company_id, is_deleted);
CREATE INDEX IF NOT EXISTS idx_annotation_task_creator ON annotation_task(company_id, creator_id);
CREATE INDEX IF NOT EXISTS idx_annotation_task_resource_company_task ON annotation_task_resource(company_id, task_id);
CREATE INDEX IF NOT EXISTS idx_annotation_task_resource_company_resource ON annotation_task_resource(company_id, resource_id);
CREATE INDEX IF NOT EXISTS idx_annotation_result_company_deleted ON annotation_result(company_id, is_deleted);
CREATE INDEX IF NOT EXISTS idx_annotation_result_company_manual ON annotation_result(company_id, requires_manual_review);
CREATE INDEX IF NOT EXISTS idx_annotation_result_task ON annotation_result(company_id, task_id);
CREATE INDEX IF NOT EXISTS idx_annotation_result_history_company ON annotation_result_history(company_id);
CREATE INDEX IF NOT EXISTS idx_annotation_result_history_task ON annotation_result_history(company_id, task_id);
CREATE INDEX IF NOT EXISTS idx_annotation_result_history_resource ON annotation_result_history(company_id, resource_id);
CREATE INDEX IF NOT EXISTS idx_training_dataset_company_status ON training_dataset(company_id, dataset_status);
CREATE INDEX IF NOT EXISTS idx_export_batch_company_status ON export_batch(company_id, finetune_status);

COMMIT;
