WIP: XZH-21 后端改动 + 测试验收附加用例

This commit is contained in:
AAT committed 2026-11-11 20:41:56 +08:00
commit 1bd637528d
168 files changed
+13874

No files matched your search

@@ -0,0 +1,140 @@
package com.myspace.library;
import com.myspace.assets.AssetFiles;
import com.myspace.common.WorkspaceNotifications;
import java.util.*;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
/**
* 全站唯一回收站:各模块的删除都只写 deleted_at,恢复/永久删除集中在这里处理。
* 标签文案与原型 KIND_NAME 保持一致。
*/
@Service
public class TrashService {
public record Spec(String table, String title, String name, boolean category) {}
private static final Map<String, Spec> SPECS = Map.ofEntries(
Map.entry("notes", new Spec("notes", "笔记", "title", true)),
Map.entry("blogs", new Spec("blogs", "博客", "title", true)),
Map.entry("bookmarks", new Spec("bookmarks", "书签", "title", true)),
Map.entry("assets", new Spec("assets", "素材", "name", true)),
Map.entry("tasks", new Spec("planner_items", "待办", "title", false)),
Map.entry("events", new Spec("planner_items", "日程", "title", false)),
Map.entry("snippets", new Spec("planner_items", "代码片段", "title", false)),
Map.entry("rss", new Spec("rss_sources", "RSS 订阅", "title", false)),
Map.entry("prompts", new Spec("ai_prompts", "AI 提示词", "title", false)),
Map.entry("flows", new Spec("workspace_flows", "自动化工作流", "title", false)));
public static final List<String> KINDS = List.of("notes", "blogs", "bookmarks", "assets",
"tasks", "events", "snippets", "rss", "prompts", "flows");
private static final Map<String, String> CATEGORY_TABLES = Map.of(
"notes", "note_categories", "blogs", "blog_categories",
"assets", "asset_categories", "bookmarks", "bookmark_categories");
private final JdbcTemplate db;
private final AssetFiles files;
private final WorkspaceNotifications notifications;
public TrashService(JdbcTemplate db, AssetFiles files, WorkspaceNotifications notifications) {
this.db = db;
this.files = files;
this.notifications = notifications;
}
public static boolean isPlanner(String kind) { return Objects.equals(spec(kind).table(), "planner_items"); }
public static String label(String kind) { return spec(kind).title(); }
public static String titleColumn(String kind) { return spec(kind).name(); }
public static Spec spec(String kind) {
Spec spec = SPECS.get(kind);
if (spec == null) throw missing();
return spec;
}
/** 同一张 planner_items 表按 kind 区分待办/日程/代码片段。 */
public static String match(String kind) {
return isPlanner(kind) ? "kind=? and id=?" : "id=?";
}
/**
* 供 JdbcTemplate#queryForList(String, Object...) 使用的参数。注意不要直接写成
* queryForList(sql, args(kind, id)),那样 Object[] 会被当成单个可变参数(一张表会被塞进占位符)。
*/
public static Object[] args(String kind, UUID id) {
return isPlanner(kind) ? new Object[]{kind, id} : new Object[]{id};
}
/** 读取仍在使用中的记录标题,用于删除通知。 */
public String liveTitle(String kind, UUID id) {
Object[] a = args(kind, id);
return db.queryForList("select " + titleColumn(kind) + " from " + spec(kind).table()
+ " where " + match(kind) + " and deleted_at is null", String.class, a)
.stream().findFirst().orElseThrow(TrashService::missing);
}
/** 删除后记录通知。注意:软删除后再查 liveTitle 会找不到记录,调用方应在删除前取好标题。 */
public void notifyTrashed(String kind, String title) {
notifications.notify("已删除" + label(kind) + "「" + WorkspaceNotifications.clip(title, 100) + "」,可在回收站恢复");
}
/** 删除前由调用方用给定的查询(在删除前)读取标题,再做软删除,两者处于同一事务。 */
public void notifyLive(String kind, UUID id, String selectTitle) {
Object[] a = args(kind, id);
notifyTrashed(kind, db.queryForList(selectTitle, String.class, a).stream().findFirst()
.orElseThrow(TrashService::missing));
}
public void notifyTrashed(String kind, UUID id) {
notifications.notify("已删除" + label(kind) + "「" + WorkspaceNotifications.clip(liveTitle(kind, id), 100)
+ "」,可在回收站恢复");
}
@Transactional
public void restore(String kind, UUID id) {
Spec spec = spec(kind);
var rows = query(kind, id, " and deleted_at is not null for update");
if (rows.isEmpty()) throw missing();
Object[] a = args(kind, id);
db.update("update " + spec.table() + " set deleted_at=null where " + match(kind), a);
Object category = rows.getFirst().get("category");
if (spec.category() && category != null) ensureCategory(kind, (String) category);
notifications.notify("已恢复" + label(kind) + "「"
+ WorkspaceNotifications.clip((String) rows.getFirst().get("title"), 100) + "」");
}
@Transactional
public void purge(String kind, UUID id) {
if (id == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "缺少记录 ID");
Spec spec = spec(kind);
if (Objects.equals(spec.table(), "assets")) {
// 素材沿用 AssetFiles 的规则:仍被引用时拒绝永久删除,并同步清理磁盘文件
files.purge(db, id);
return;
}
if (query(kind, id, " and deleted_at is not null for update").isEmpty()) throw missing();
Object[] a = args(kind, id);
db.update("delete from " + spec.table() + " where " + match(kind), a);
if (Objects.equals(spec.table(), "notes")) db.update("delete from note_history where note_id=?", id);
if (isPlanner(kind)) db.update("delete from inbox_items where target_kind=? and target_id=?", kind, id);
}
private List<Map<String, Object>> query(String kind, UUID id, String tail) {
Spec spec = spec(kind);
Object[] a = args(kind, id);
return db.queryForList("select " + spec.name() + " as title"
+ (spec.category() ? ",category" : "") + " from " + spec.table() + " where " + match(kind) + tail, a);
}
private void ensureCategory(String kind, String name) {
String table = CATEGORY_TABLES.get(kind);
if (table == null) return;
if (db.update("insert into " + table + "(name) values(?) on conflict (name) do nothing", name) == 1)
notifications.notify("恢复时补回「" + label(kind) + "」分类:" + WorkspaceNotifications.clip(name, 60));
}
public static ResponseStatusException missing() {
return new ResponseStatusException(HttpStatus.NOT_FOUND, "记录不在回收站或不存在");
}
}