fix: 修复素材绑定页面显示暂无素材的问题

- 添加get_all_materials后端命令获取所有项目的素材
- 在MaterialRepository中实现get_all方法
- 在MaterialService中添加get_all_materials方法
- 修改MaterialModelBindingService的getMaterialsByFilter方法,在没有指定项目时获取所有素材而不是返回空数组
- 确保素材绑定页面能正确显示所有素材数据,与统计信息保持一致
- 支持在全局素材列表上应用绑定状态和搜索过滤
This commit is contained in:
imeepos 2025-07-15 14:18:48 +08:00
parent de446b6410
commit 7c40da1b83
5 changed files with 73 additions and 6 deletions

View File

@ -183,12 +183,26 @@ impl MaterialService {
project_id: &str, project_id: &str,
) -> Result<Vec<Material>> { ) -> Result<Vec<Material>> {
let mut materials = repository.get_by_project_id(project_id)?; let mut materials = repository.get_by_project_id(project_id)?;
// 为每个素材加载片段信息 // 为每个素材加载片段信息
for material in &mut materials { for material in &mut materials {
material.segments = repository.get_segments(&material.id)?; material.segments = repository.get_segments(&material.id)?;
} }
Ok(materials)
}
/// 获取所有素材
pub fn get_all_materials(
repository: &MaterialRepository,
) -> Result<Vec<Material>> {
let mut materials = repository.get_all()?;
// 为每个素材加载片段信息
for material in &mut materials {
material.segments = repository.get_segments(&material.id)?;
}
Ok(materials) Ok(materials)
} }

View File

@ -88,7 +88,7 @@ impl MaterialRepository {
/// 根据项目ID获取所有素材 /// 根据项目ID获取所有素材
pub fn get_by_project_id(&self, project_id: &str) -> Result<Vec<Material>> { pub fn get_by_project_id(&self, project_id: &str) -> Result<Vec<Material>> {
let conn = self.connection.lock().unwrap(); let conn = self.connection.lock().unwrap();
let mut stmt = conn.prepare( let mut stmt = conn.prepare(
"SELECT id, project_id, model_id, name, original_path, file_size, md5_hash, "SELECT id, project_id, model_id, name, original_path, file_size, md5_hash,
material_type, processing_status, metadata, scene_detection, material_type, processing_status, metadata, scene_detection,
@ -108,6 +108,29 @@ impl MaterialRepository {
Ok(materials) Ok(materials)
} }
/// 获取所有素材
pub fn get_all(&self) -> Result<Vec<Material>> {
let conn = self.connection.lock().unwrap();
let mut stmt = conn.prepare(
"SELECT id, project_id, model_id, name, original_path, file_size, md5_hash,
material_type, processing_status, metadata, scene_detection,
created_at, updated_at, processed_at, error_message
FROM materials ORDER BY created_at DESC"
)?;
let material_iter = stmt.query_map([], |row| {
self.row_to_material(row)
})?;
let mut materials = Vec::new();
for material in material_iter {
materials.push(material?);
}
Ok(materials)
}
/// 根据MD5哈希检查素材是否存在 /// 根据MD5哈希检查素材是否存在
pub fn exists_by_md5(&self, project_id: &str, md5_hash: &str) -> Result<bool> { pub fn exists_by_md5(&self, project_id: &str, md5_hash: &str) -> Result<bool> {
let conn = self.connection.lock().unwrap(); let conn = self.connection.lock().unwrap();

View File

@ -51,6 +51,7 @@ pub fn run() {
commands::material_commands::select_material_folders, commands::material_commands::select_material_folders,
commands::material_commands::scan_folder_materials, commands::material_commands::scan_folder_materials,
commands::material_commands::get_project_materials, commands::material_commands::get_project_materials,
commands::material_commands::get_all_materials,
commands::material_commands::get_material_by_id, commands::material_commands::get_material_by_id,
commands::material_commands::delete_material, commands::material_commands::delete_material,
commands::material_commands::get_project_material_stats, commands::material_commands::get_project_material_stats,

View File

@ -514,7 +514,7 @@ pub async fn get_project_materials(
) -> Result<Vec<Material>, String> { ) -> Result<Vec<Material>, String> {
let repository_guard = state.get_material_repository() let repository_guard = state.get_material_repository()
.map_err(|e| format!("获取素材仓库失败: {}", e))?; .map_err(|e| format!("获取素材仓库失败: {}", e))?;
let repository = repository_guard.as_ref() let repository = repository_guard.as_ref()
.ok_or("素材仓库未初始化")?; .ok_or("素材仓库未初始化")?;
@ -522,6 +522,21 @@ pub async fn get_project_materials(
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
/// 获取所有素材列表命令
#[command]
pub async fn get_all_materials(
state: State<'_, AppState>,
) -> Result<Vec<Material>, String> {
let repository_guard = state.get_material_repository()
.map_err(|e| format!("获取素材仓库失败: {}", e))?;
let repository = repository_guard.as_ref()
.ok_or("素材仓库未初始化")?;
MaterialService::get_all_materials(repository)
.map_err(|e| e.to_string())
}
/// 获取素材详情命令 /// 获取素材详情命令
#[command] #[command]
pub async fn get_material_by_id( pub async fn get_material_by_id(

View File

@ -154,6 +154,13 @@ export class MaterialModelBindingService {
}); });
} }
/**
*
*/
static async getAllMaterials(): Promise<Material[]> {
return await invoke<Material[]>('get_all_materials');
}
/** /**
* *
*/ */
@ -178,8 +185,15 @@ export class MaterialModelBindingService {
materials = materials.filter(m => m.model_id); materials = materials.filter(m => m.model_id);
} }
} else { } else {
// 如果没有指定项目,返回空数组 // 如果没有指定项目,获取所有素材
return []; materials = await this.getAllMaterials();
// 如果指定了绑定状态过滤
if (filter.bound === true) {
materials = materials.filter(m => m.model_id);
} else if (filter.bound === false) {
materials = materials.filter(m => !m.model_id);
}
} }
// 应用搜索过滤 // 应用搜索过滤