update
This commit is contained in:
@@ -121,13 +121,50 @@ impl Database {
|
||||
// ── 标签查询 ──
|
||||
|
||||
pub async fn get_popular_tags(&self, limit: i64) -> Vec<TagStat> {
|
||||
sqlx::query(
|
||||
// 从 tag_dict 获取过滤词(停用词、品牌词、排除名)
|
||||
// 注意:必须 clone 到本地变量,不能持 RwLockReadGuard 跨 await
|
||||
let (stop_words, excluded, included) = {
|
||||
let dict = crate::engine::tag_dict::get_tag_dict();
|
||||
(
|
||||
dict.stop_words.clone(),
|
||||
dict.excluded_person_names.clone(),
|
||||
dict.included_person_names.clone(),
|
||||
)
|
||||
};
|
||||
|
||||
// 标签自身小写的集合用于快速过滤
|
||||
let filtered_tags: Vec<TagStat> = sqlx::query(
|
||||
r#"SELECT tag, COUNT(*) as cnt, COUNT(DISTINCT album_id) as album_cnt
|
||||
FROM album_tags GROUP BY tag ORDER BY cnt DESC LIMIT ?"#
|
||||
).bind(limit).fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().map(|r| TagStat {
|
||||
tag: r.get("tag"), count: r.get("cnt"), album_count: r.get("album_cnt"),
|
||||
}).collect()
|
||||
).bind(limit * 3).fetch_all(&self.pool).await.unwrap_or_default()
|
||||
.iter().filter_map(|r| {
|
||||
let tag: String = r.get("tag");
|
||||
let tag_lower = tag.to_lowercase();
|
||||
|
||||
// 过滤停用词
|
||||
if stop_words.contains(&tag_lower) || stop_words.contains(&tag) {
|
||||
return None;
|
||||
}
|
||||
// 过滤品牌/工作室名
|
||||
if excluded.contains(&tag_lower) || excluded.contains(&tag) {
|
||||
return None;
|
||||
}
|
||||
// 过滤包含人名(如果标签本身就是个人名,也不应该出现在标签中)
|
||||
if included.contains(&tag_lower) || included.contains(&tag) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(TagStat {
|
||||
tag: r.get("tag"),
|
||||
count: r.get("cnt"),
|
||||
album_count: r.get("album_cnt"),
|
||||
})
|
||||
}).collect();
|
||||
|
||||
// 只保留 top limit 个
|
||||
let mut result: Vec<TagStat> = filtered_tags;
|
||||
result.truncate(limit as usize);
|
||||
result
|
||||
}
|
||||
|
||||
/// 批量获取所有 tag→albums 映射(供缓存使用)
|
||||
@@ -242,13 +279,20 @@ impl Database {
|
||||
sqlx::query("DELETE FROM person_albums WHERE person_id = ?").bind(person_id).execute(&self.pool).await.ok();
|
||||
}
|
||||
|
||||
/// 删除某相册的所有人物关联(写入新 person 前清旧数据,防止一个相册关联多个人物)
|
||||
pub async fn delete_album_person_albums(&self, album_id: &str) {
|
||||
/// 删除某相册的所有人物关联,返回被影响的旧 person_id 列表
|
||||
pub async fn delete_album_person_albums(&self, album_id: &str) -> Vec<i64> {
|
||||
let old_ids: Vec<i64> = sqlx::query_scalar("SELECT person_id FROM person_albums WHERE album_id = ?")
|
||||
.bind(album_id).fetch_all(&self.pool).await.unwrap_or_default();
|
||||
sqlx::query("DELETE FROM person_albums WHERE album_id = ?").bind(album_id).execute(&self.pool).await.ok();
|
||||
old_ids
|
||||
}
|
||||
|
||||
pub async fn delete_album_person_albums_with_tx(&self, tx: &mut Transaction<'_, Sqlite>, album_id: &str) {
|
||||
/// 删除某相册的所有人物关联(事务版),返回被影响的旧 person_id 列表
|
||||
pub async fn delete_album_person_albums_with_tx(&self, tx: &mut Transaction<'_, Sqlite>, album_id: &str) -> Vec<i64> {
|
||||
let old_ids: Vec<i64> = sqlx::query_scalar("SELECT person_id FROM person_albums WHERE album_id = ?")
|
||||
.bind(album_id).fetch_all(tx.deref_mut()).await.unwrap_or_default();
|
||||
sqlx::query("DELETE FROM person_albums WHERE album_id = ?").bind(album_id).execute(tx.deref_mut()).await.ok();
|
||||
old_ids
|
||||
}
|
||||
|
||||
/// 删除某人物下所有别名(写入新别名前清旧别名,配合 UNIQUE INDEX 防止重复)
|
||||
|
||||
Reference in New Issue
Block a user