From f10633f64def7c7b2a696bfb750a11ad5869b0c0 Mon Sep 17 00:00:00 2001 From: imeepos Date: Wed, 23 Jul 2025 19:11:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9B=E5=BB=BAmaterial=5Fusage=5Frec?= =?UTF-8?q?ords=E8=A1=A8=E4=BF=AE=E5=A4=8D=E7=B4=A0=E6=9D=90=E7=89=87?= =?UTF-8?q?=E6=AE=B5=E4=BD=BF=E7=94=A8=E7=8A=B6=E6=80=81=E8=B7=9F=E8=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建material_usage_records表存储素材使用记录 (v18) - 添加相关索引优化查询性能 - 修复模板匹配应用后片段管理显示'已使用为0'的问题 - 支持素材片段使用状态的正确跟踪和更新 现在点击'应用匹配结果'后,对应的素材片段会正确标记为已使用状态。 --- .../src/infrastructure/database/migrations.rs | 8 +++++ ...18_create_material_usage_records_table.sql | 33 +++++++++++++++++++ ...eate_material_usage_records_table_down.sql | 14 ++++++++ .../commands/material_usage_commands.rs | 1 - 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table.sql create mode 100644 apps/desktop/src-tauri/src/infrastructure/database/migrations/018_create_material_usage_records_table_down.sql 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")