fix: 创建material_usage_records表修复素材片段使用状态跟踪

- 创建material_usage_records表存储素材使用记录 (v18)
- 添加相关索引优化查询性能
- 修复模板匹配应用后片段管理显示'已使用为0'的问题
- 支持素材片段使用状态的正确跟踪和更新

现在点击'应用匹配结果'后,对应的素材片段会正确标记为已使用状态。
This commit is contained in:
imeepos 2025-07-23 19:11:30 +08:00
parent a39cc1a12d
commit f10633f64d
4 changed files with 55 additions and 1 deletions

View File

@ -172,6 +172,14 @@ impl MigrationManager {
up_sql: include_str!("migrations/017_create_matching_segment_results_tables.sql").to_string(), 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()), 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()),
});
} }
/// 添加迁移 /// 添加迁移

View File

@ -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);

View File

@ -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;

View File

@ -125,7 +125,6 @@ pub async fn create_usage_records_from_matching_result(
// 解析匹配结果并创建使用记录请求 // 解析匹配结果并创建使用记录请求
for (index, match_value) in matches.iter().enumerate() { for (index, match_value) in matches.iter().enumerate() {
println!("🔄 处理第 {} 个匹配结果", index + 1); println!("🔄 处理第 {} 个匹配结果", index + 1);
println!("📄 原始匹配数据: {}", serde_json::to_string_pretty(match_value).unwrap_or_else(|_| "无法序列化".to_string()));
// 直接从SegmentMatch结构中提取字段 // 直接从SegmentMatch结构中提取字段
let material_segment_id = match_value let material_segment_id = match_value
.get("material_segment_id") .get("material_segment_id")