diff --git a/apps/desktop/src-tauri/src/infrastructure/database/migrations.rs b/apps/desktop/src-tauri/src/infrastructure/database/migrations.rs index 080437b..3eb5a65 100644 --- a/apps/desktop/src-tauri/src/infrastructure/database/migrations.rs +++ b/apps/desktop/src-tauri/src/infrastructure/database/migrations.rs @@ -172,6 +172,14 @@ impl MigrationManager { up_sql: include_str!("migrations/017_create_matching_segment_results_tables.sql").to_string(), down_sql: Some(include_str!("migrations/017_create_matching_segment_results_tables_down.sql").to_string()), }); + + // 迁移 18: 创建material_usage_records表 + self.add_migration(Migration { + version: 18, + description: "创建material_usage_records表".to_string(), + up_sql: include_str!("migrations/018_create_material_usage_records_table.sql").to_string(), + down_sql: Some(include_str!("migrations/018_create_material_usage_records_table_down.sql").to_string()), + }); } /// 添加迁移 diff --git a/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table.sql b/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table.sql new file mode 100644 index 0000000..b87b629 --- /dev/null +++ b/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table.sql @@ -0,0 +1,33 @@ +-- 创建素材使用记录表 +-- 用于跟踪素材片段在各种场景下的使用情况 + +CREATE TABLE IF NOT EXISTS material_usage_records ( + id TEXT PRIMARY KEY, + material_segment_id TEXT NOT NULL, + material_id TEXT NOT NULL, + project_id TEXT NOT NULL, + template_matching_result_id TEXT NOT NULL, + template_id TEXT NOT NULL, + binding_id TEXT NOT NULL, + track_segment_id TEXT NOT NULL, + usage_type TEXT NOT NULL, -- JSON格式的MaterialUsageType枚举 + usage_context TEXT, -- JSON格式的使用上下文信息 + created_at DATETIME NOT NULL DEFAULT (datetime('now', 'utc') || 'Z'), + FOREIGN KEY (material_segment_id) REFERENCES material_segments (id) ON DELETE CASCADE, + FOREIGN KEY (material_id) REFERENCES materials (id) ON DELETE CASCADE, + FOREIGN KEY (project_id) REFERENCES projects (id) ON DELETE CASCADE, + FOREIGN KEY (template_matching_result_id) REFERENCES template_matching_results (id) ON DELETE CASCADE, + FOREIGN KEY (template_id) REFERENCES templates (id) ON DELETE CASCADE, + FOREIGN KEY (binding_id) REFERENCES project_template_bindings (id) ON DELETE CASCADE, + FOREIGN KEY (track_segment_id) REFERENCES track_segments (id) ON DELETE CASCADE +); + +-- 创建索引以优化查询性能 +CREATE INDEX IF NOT EXISTS idx_material_usage_records_material_segment_id ON material_usage_records (material_segment_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_material_id ON material_usage_records (material_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_project_id ON material_usage_records (project_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_template_matching_result_id ON material_usage_records (template_matching_result_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_template_id ON material_usage_records (template_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_binding_id ON material_usage_records (binding_id); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_usage_type ON material_usage_records (usage_type); +CREATE INDEX IF NOT EXISTS idx_material_usage_records_created_at ON material_usage_records (created_at); diff --git a/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table_down.sql b/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table_down.sql new file mode 100644 index 0000000..a28cb59 --- /dev/null +++ b/apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table_down.sql @@ -0,0 +1,14 @@ +-- 回滚:删除素材使用记录表 + +-- 删除索引 +DROP INDEX IF EXISTS idx_material_usage_records_material_segment_id; +DROP INDEX IF EXISTS idx_material_usage_records_material_id; +DROP INDEX IF EXISTS idx_material_usage_records_project_id; +DROP INDEX IF EXISTS idx_material_usage_records_template_matching_result_id; +DROP INDEX IF EXISTS idx_material_usage_records_template_id; +DROP INDEX IF EXISTS idx_material_usage_records_binding_id; +DROP INDEX IF EXISTS idx_material_usage_records_usage_type; +DROP INDEX IF EXISTS idx_material_usage_records_created_at; + +-- 删除表 +DROP TABLE IF EXISTS material_usage_records; diff --git a/apps/desktop/src-tauri/src/presentation/commands/material_usage_commands.rs b/apps/desktop/src-tauri/src/presentation/commands/material_usage_commands.rs index da744e0..2f37283 100644 --- a/apps/desktop/src-tauri/src/presentation/commands/material_usage_commands.rs +++ b/apps/desktop/src-tauri/src/presentation/commands/material_usage_commands.rs @@ -125,7 +125,6 @@ pub async fn create_usage_records_from_matching_result( // 解析匹配结果并创建使用记录请求 for (index, match_value) in matches.iter().enumerate() { println!("🔄 处理第 {} 个匹配结果", index + 1); - println!("📄 原始匹配数据: {}", serde_json::to_string_pretty(match_value).unwrap_or_else(|_| "无法序列化".to_string())); // 直接从SegmentMatch结构中提取字段 let material_segment_id = match_value .get("material_segment_id")