feat: 项目层级树+合作方绑定KOC资源+客户群全量可选+精简编辑表单,移除复盘报表

- 项目支持父子层级:树形折叠展示、详情面板内联管理总群成员、推送聚合子项目任务、环检测与子项目删除守卫
- 外部KOC合作方绑定 KOC 资源账号(唯一占用、换绑/解绑、资源页一键设为合作方),列表直显联系方式
- 绑定客户群改为全量外部群缓存(含各群主),同步按钮强刷;member_count 缺失以 member_list 兜底
- 任务/项目编辑模式隐藏不可变字段(平台/内容类型/品牌/总群成员),详情面板维护成员时保留 parent_id
- 删除复盘报表整栏(导航/页面/task_report_list/导出路由/CSV lib 及测试)
This commit is contained in:
ABAPPLO
2026-09-23 11:13:17 +08:00
parent 8f1ebf7697
commit ab78b187c0
20 changed files with 1354 additions and 958 deletions

View File

@@ -17,6 +17,7 @@ export type ProjectRow = {
main_group_member_ids: string;
owner_userid: string;
main_chat_id: string;
parent_id: string;
status: string;
note: string;
created_at: string;
@@ -32,6 +33,7 @@ export type ProjectSummary = {
mainGroupMemberIds: string[];
ownerUserid: string;
mainChatId: string;
parentProjectId: string;
taskCount: number;
};
@@ -49,6 +51,16 @@ export type ProjectTaskRow = {
share_token: string | null;
owner_userid: string;
task_chat_id: string;
project_name?: string;
};
export type ProjectSubproject = {
id: string;
name: string;
brand: string;
ownerUserid: string;
mainChatId: string;
taskCount: number;
};
export type ProjectMainChat = {
@@ -60,6 +72,8 @@ export type ProjectDetail = {
project: ProjectSummary;
tasks: ProjectTaskRow[];
mainChat: ProjectMainChat;
subprojects: ProjectSubproject[];
pushTasks: ProjectTaskRow[];
};
export type MainChatEnsureReason =
@@ -224,6 +238,59 @@ export async function ensureProjectMainChat(
}
}
async function loadProjectParents(
db: DatabaseClient,
): Promise<Map<string, string>> {
const result = await db
.prepare("SELECT id, parent_id FROM projects")
.all<{ id: string; parent_id: string }>();
return new Map(result.results.map((row) => [row.id, text(row.parent_id)]));
}
// candidate 是否位于 ancestor 的子孙链上(沿 candidate 的祖先链向上找 ancestor)
function isDescendantProject(
parents: Map<string, string>,
ancestorId: string,
candidateId: string,
): boolean {
let current = candidateId;
const seen = new Set<string>();
while (current) {
if (current === ancestorId) return true;
if (seen.has(current)) return false;
seen.add(current);
current = parents.get(current) ?? "";
}
return false;
}
export async function collectDescendantIds(
db: DatabaseClient,
projectId: string,
): Promise<string[]> {
const parents = await loadProjectParents(db);
const childrenOf = new Map<string, string[]>();
for (const [id, parent] of parents) {
if (!parent) continue;
const list = childrenOf.get(parent) ?? [];
list.push(id);
childrenOf.set(parent, list);
}
const descendants: string[] = [];
const queue = [text(projectId)];
const visited = new Set(queue);
while (queue.length > 0) {
const current = queue.shift() as string;
for (const child of childrenOf.get(current) ?? []) {
if (visited.has(child)) continue;
visited.add(child);
descendants.push(child);
queue.push(child);
}
}
return descendants;
}
export async function listProjects(
db: DatabaseClient,
ownerUserid?: string,
@@ -236,7 +303,7 @@ export async function listProjects(
const result = await (ownerFilter
? db
.prepare(
`SELECT p.id, p.name, p.brand, p.main_group_member_ids, p.owner_userid, p.main_chat_id, p.status, p.note, p.created_at,
`SELECT p.id, p.name, p.brand, p.main_group_member_ids, p.owner_userid, p.main_chat_id, p.parent_id, p.status, p.note, p.created_at,
(SELECT COUNT(*) FROM tasks t WHERE t.project_id = p.id) AS task_count
FROM projects p
WHERE p.owner_userid = ?
@@ -245,7 +312,7 @@ export async function listProjects(
.bind(ownerFilter)
: db
.prepare(
`SELECT p.id, p.name, p.brand, p.main_group_member_ids, p.owner_userid, p.main_chat_id, p.status, p.note, p.created_at,
`SELECT p.id, p.name, p.brand, p.main_group_member_ids, p.owner_userid, p.main_chat_id, p.parent_id, p.status, p.note, p.created_at,
(SELECT COUNT(*) FROM tasks t WHERE t.project_id = p.id) AS task_count
FROM projects p
ORDER BY p.created_at DESC`,
@@ -261,6 +328,7 @@ export async function listProjects(
mainGroupMemberIds: parseJsonArray(row.main_group_member_ids),
ownerUserid: row.owner_userid ?? "",
mainChatId: row.main_chat_id ?? "",
parentProjectId: row.parent_id ?? "",
taskCount: Number(row.task_count),
}));
}
@@ -272,7 +340,7 @@ export async function getProjectDetail(
const id = text(projectId);
const project = await db
.prepare(
`SELECT id, name, brand, main_group_member_ids, owner_userid, main_chat_id, status, note, created_at
`SELECT id, name, brand, main_group_member_ids, owner_userid, main_chat_id, parent_id, status, note, created_at
FROM projects WHERE id = ?`,
)
.bind(id)
@@ -289,6 +357,33 @@ export async function getProjectDetail(
.bind(id)
.all<ProjectTaskRow>();
const subprojects = await db
.prepare(
`SELECT p.id, p.name, p.brand, p.owner_userid, p.main_chat_id,
(SELECT COUNT(*) FROM tasks t WHERE t.project_id = p.id) AS task_count
FROM projects p
WHERE p.parent_id = ?
ORDER BY p.created_at DESC`,
)
.bind(id)
.all<ProjectRow & { task_count: number }>();
const descendantIds = await collectDescendantIds(db, id);
const pushIds = [id, ...descendantIds];
const placeholders = pushIds.map(() => "?").join(", ");
const pushTasks = await db
.prepare(
`SELECT t.id, t.name, t.brand, t.quantity, t.claimed_quantity, t.due_at, t.status,
t.task_type, t.platform, t.content_format, t.share_token, t.owner_userid, t.task_chat_id,
pj.name AS project_name
FROM tasks t
LEFT JOIN projects pj ON pj.id = t.project_id
WHERE t.project_id IN (${placeholders})
ORDER BY t.created_at DESC`,
)
.bind(...pushIds)
.all<ProjectTaskRow>();
return {
project: {
id: project.id,
@@ -300,6 +395,7 @@ export async function getProjectDetail(
mainGroupMemberIds: parseJsonArray(project.main_group_member_ids),
ownerUserid: project.owner_userid ?? "",
mainChatId: project.main_chat_id ?? "",
parentProjectId: project.parent_id ?? "",
taskCount: tasks.results.length,
},
tasks: tasks.results,
@@ -310,6 +406,15 @@ export async function getProjectDetail(
project.owner_userid,
),
},
subprojects: subprojects.results.map((row) => ({
id: row.id,
name: row.name,
brand: row.brand,
ownerUserid: row.owner_userid ?? "",
mainChatId: row.main_chat_id ?? "",
taskCount: Number(row.task_count),
})),
pushTasks: pushTasks.results,
};
}
@@ -320,6 +425,7 @@ export type SaveProjectInput = {
mainGroupMemberIds?: unknown;
ownerUserid?: unknown;
note?: unknown;
parentProjectId?: unknown;
};
export async function saveProject(
@@ -333,19 +439,33 @@ export async function saveProject(
const brand = text(input.brand);
const note = text(input.note ?? "");
const ownerUserid = text(input.ownerUserid).slice(0, 64);
const parentProjectId = text(input.parentProjectId);
const memberIds = effectiveProjectMembers(input.mainGroupMemberIds, ownerUserid);
if (!name) throw new ProjectError("请填写项目名称");
if (parentProjectId) {
if (parentProjectId === id) {
throw new ProjectError("上级项目不能是项目自身");
}
const parents = await loadProjectParents(db);
if (!parents.has(parentProjectId)) {
throw new ProjectError("上级项目不存在", 404);
}
if (id && isDescendantProject(parents, id, parentProjectId)) {
throw new ProjectError("上级项目不能是当前项目的子项目");
}
}
const now = new Date().toISOString();
let projectId = id;
if (id) {
const result = await db
.prepare(
`UPDATE projects
SET name = ?, brand = ?, main_group_member_ids = ?, owner_userid = ?, note = ?
SET name = ?, brand = ?, main_group_member_ids = ?, owner_userid = ?, note = ?, parent_id = ?
WHERE id = ?`,
)
.bind(name, brand, JSON.stringify(memberIds), ownerUserid, note, id)
.bind(name, brand, JSON.stringify(memberIds), ownerUserid, note, parentProjectId, id)
.run();
if (result.meta.changes === 0) throw new ProjectError("项目不存在", 404);
} else {
@@ -353,8 +473,8 @@ export async function saveProject(
await db
.prepare(
`INSERT INTO projects
(id, name, brand, main_group_member_ids, owner_userid, status, note, created_at)
VALUES (?, ?, ?, ?, ?, 'active', ?, ?)`,
(id, name, brand, main_group_member_ids, owner_userid, parent_id, status, note, created_at)
VALUES (?, ?, ?, ?, ?, ?, 'active', ?, ?)`,
)
.bind(
projectId,
@@ -362,6 +482,7 @@ export async function saveProject(
brand,
JSON.stringify(memberIds),
ownerUserid,
parentProjectId,
note,
now,
)
@@ -384,6 +505,13 @@ export async function deleteProject(
): Promise<void> {
const id = text(projectId);
if (!id) throw new ProjectError("缺少项目 ID");
const childCount = await db
.prepare("SELECT COUNT(*) AS count FROM projects WHERE parent_id = ?")
.bind(id)
.first<{ count: number }>();
if (Number(childCount?.count ?? 0) > 0) {
throw new ProjectError("该项目下还有子项目,请先删除子项目或把它们移到其他项目下");
}
const result = await db
.prepare("DELETE FROM projects WHERE id = ?")
.bind(id)