fix: speed up document type group loading
This commit is contained in:
@@ -900,6 +900,67 @@ function dedupeSubtypeGroups(groups: DocumentSubtypeGroup[]): DocumentSubtypeGro
|
||||
return Array.from(groupMap.values());
|
||||
}
|
||||
|
||||
async function fetchAllEvaluationPointGroupRoots(token?: string): Promise<any[]> {
|
||||
const headers: Record<string, string> = {};
|
||||
if (token) {
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const allResponse = await axios.get(`${API_BASE_URL}/api/v3/evaluation-point-groups/all`, {
|
||||
params: {
|
||||
include_disabled: false,
|
||||
with_rule_count: false,
|
||||
},
|
||||
headers,
|
||||
});
|
||||
|
||||
return extractApiData<any[]>(allResponse.data) || [];
|
||||
}
|
||||
|
||||
function collectSubtypeGroupsFromRoots(
|
||||
roots: any[],
|
||||
documentTypeId: number,
|
||||
entryModuleId?: number | null,
|
||||
): DocumentSubtypeGroup[] {
|
||||
return dedupeSubtypeGroups(
|
||||
roots.flatMap((root: any) => {
|
||||
if (!Array.isArray(root?.children)) return [];
|
||||
if (entryModuleId && Number(root?.entry_module_id || 0) !== Number(entryModuleId)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return root.children
|
||||
.filter((child: any) => Number(child?.document_type_id || 0) === Number(documentTypeId))
|
||||
.map((child: any) => mapSubtypeChild(child, root));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getDocumentSubtypeGroupsMap(
|
||||
documentTypeIds: number[],
|
||||
token?: string,
|
||||
): Promise<{ data: Record<number, DocumentSubtypeGroup[]>; error?: never } | { data?: never; error: string; status?: number }> {
|
||||
try {
|
||||
if (!documentTypeIds.length) {
|
||||
return { data: {} };
|
||||
}
|
||||
|
||||
const roots = await fetchAllEvaluationPointGroupRoots(token);
|
||||
const ids = Array.from(new Set(documentTypeIds.map((id) => Number(id)).filter(Boolean)));
|
||||
const grouped = Object.fromEntries(
|
||||
ids.map((documentTypeId) => [documentTypeId, collectSubtypeGroupsFromRoots(roots, documentTypeId)]),
|
||||
);
|
||||
|
||||
return { data: grouped };
|
||||
} catch (error) {
|
||||
console.error("批量获取子类型分组失败:", error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : "批量获取子类型分组失败",
|
||||
status: 500,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDocumentSubtypeGroups(
|
||||
documentTypeId: number,
|
||||
token?: string,
|
||||
@@ -911,26 +972,11 @@ export async function getDocumentSubtypeGroups(
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const allResponse = await axios.get(`${API_BASE_URL}/api/v3/evaluation-point-groups/all`, {
|
||||
params: {
|
||||
include_disabled: false,
|
||||
with_rule_count: false,
|
||||
},
|
||||
headers,
|
||||
});
|
||||
const allRoots = extractApiData<any[]>(allResponse.data) || [];
|
||||
const matchedFromTree = allRoots.flatMap((root: any) => {
|
||||
if (!Array.isArray(root?.children)) return [];
|
||||
if (entryModuleId && Number(root?.entry_module_id || 0) !== Number(entryModuleId)) {
|
||||
return [];
|
||||
}
|
||||
return root.children
|
||||
.filter((child: any) => Number(child?.document_type_id || 0) === Number(documentTypeId))
|
||||
.map((child: any) => mapSubtypeChild(child, root));
|
||||
});
|
||||
const allRoots = await fetchAllEvaluationPointGroupRoots(token);
|
||||
const matchedFromTree = collectSubtypeGroupsFromRoots(allRoots, documentTypeId, entryModuleId);
|
||||
|
||||
if (matchedFromTree.length > 0) {
|
||||
return { data: dedupeSubtypeGroups(matchedFromTree) };
|
||||
return { data: matchedFromTree };
|
||||
}
|
||||
|
||||
const response = await axios.get(`${API_BASE_URL}/api/v3/evaluation-point-groups/by-document-types`, {
|
||||
|
||||
Reference in New Issue
Block a user