fix: 修复分类文件 移动到其他位置后 数据库对应字段未更新的bug

This commit is contained in:
imeepos 2025-07-14 16:02:57 +08:00
parent 7a9ac750ae
commit 4cb264cc32
2 changed files with 31 additions and 2 deletions

View File

@ -274,7 +274,20 @@ impl VideoClassificationService {
// 移动文件 // 移动文件
FileSystemService::move_file(&segment.file_path, target_path.to_str().unwrap())?; FileSystemService::move_file(&segment.file_path, target_path.to_str().unwrap())?;
println!("视频文件已移动到分类文件夹: {} -> {}", segment.file_path, target_path.display()); println!("✅ 视频文件已移动到分类文件夹: {} -> {}", segment.file_path, target_path.display());
// 更新片段的文件路径
let new_path = target_path.to_str().unwrap().to_string();
match self.material_repo.update_segment_file_path(&record.segment_id, &new_path).await {
Ok(()) => {
println!("✅ 片段路径已更新: {}", new_path);
}
Err(e) => {
println!("⚠️ 更新片段路径失败: {}", e);
// 注意:文件已经移动,但数据库更新失败
// 这种情况下应该考虑回滚文件移动或者记录错误
}
}
Ok(()) Ok(())
} }

View File

@ -1,5 +1,5 @@
use anyhow::Result; use anyhow::Result;
use rusqlite::{Connection, Row}; use rusqlite::{Connection, Row, params};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::data::models::material::{ use crate::data::models::material::{
Material, MaterialSegment, MaterialType, ProcessingStatus, Material, MaterialSegment, MaterialType, ProcessingStatus,
@ -492,6 +492,22 @@ impl MaterialRepository {
} }
} }
/// 更新片段的文件路径
pub async fn update_segment_file_path(&self, segment_id: &str, new_file_path: &str) -> Result<()> {
let conn = self.connection.lock().unwrap();
let updated_rows = conn.execute(
"UPDATE video_segments SET file_path = ?, updated_at = datetime('now') WHERE id = ?",
params![new_file_path, segment_id]
)?;
if updated_rows == 0 {
return Err(anyhow::anyhow!("片段不存在或更新失败: {}", segment_id));
}
Ok(())
}
/// 根据片段ID获取片段信息 /// 根据片段ID获取片段信息
pub async fn get_segment_by_id(&self, segment_id: &str) -> Result<Option<MaterialSegment>> { pub async fn get_segment_by_id(&self, segment_id: &str) -> Result<Option<MaterialSegment>> {
let conn = self.connection.lock().unwrap(); let conn = self.connection.lock().unwrap();