update code level

This commit is contained in:
lychang
2026-07-21 02:17:46 +08:00
parent f572c7370d
commit 005688b58f
4 changed files with 54 additions and 22 deletions

View File

@@ -267,24 +267,30 @@ impl Database {
}).collect()
}
/// 查找重名相册,返回 (keep_id, [duplicate_ids])
pub async fn find_duplicate_albums(&self) -> Vec<(Uuid, Vec<Uuid>)> {
/// 查找重名相册,返回结构:每个元素为 (keep_id, keep_document, [(dup_id, dup_document)])
pub async fn find_duplicate_albums(&self) -> Vec<(Uuid, String, Vec<(Uuid, String)>)> {
let rows = sqlx::query(
r#"SELECT id, title FROM albums WHERE title != '' ORDER BY created_at ASC"#
r#"SELECT id, title, document FROM albums WHERE title != '' ORDER BY created_at ASC"#
).fetch_all(&self.pool).await.unwrap_or_default();
let mut title_map: std::collections::HashMap<String, Vec<Uuid>> = std::collections::HashMap::new();
// title → [(id, document)]
let mut title_map: std::collections::HashMap<String, Vec<(Uuid, String)>> = std::collections::HashMap::new();
for r in &rows {
let title: String = r.get("title");
let id_str: String = r.get("id");
let doc: String = r.get("document");
if let Ok(id) = Uuid::parse_str(&id_str) {
title_map.entry(title).or_default().push(id);
title_map.entry(title).or_default().push((id, doc));
}
}
title_map.into_iter()
.filter(|(_, ids)| ids.len() > 1)
.map(|(_, ids)| (ids[0], ids[1..].to_vec()))
.filter(|(_, items)| items.len() > 1)
.map(|(_, items)| {
let keep = items[0].clone(); // (keep_id, keep_document)
let dups = items[1..].to_vec();
(keep.0, keep.1, dups)
})
.collect()
}