7/10 缴费分析,人员分析 数据对接
This commit is contained in:
@@ -68,43 +68,94 @@ interface Tree {
|
||||
type ValidTreeKey = {
|
||||
[K in keyof Tree]: Tree[K] extends string | number ? K : never;
|
||||
}[keyof Tree];
|
||||
// 全局缓存:houseId -> 对应字段路径数组
|
||||
const pathCache = new Map<ValidTreeKey, Map<string, (string | number)[]>>();
|
||||
|
||||
/**
|
||||
* 根据 houseId 查找完整路径(楼栋 > 单元 > 楼层 > 房屋)
|
||||
* @param treeData 转换后的树形数据
|
||||
* @param houseId 目标房屋ID
|
||||
* @param response
|
||||
* @returns string[] 路径数组,如 ['1号楼', '1单元', '3层', '301']
|
||||
* 初始化缓存,树形数据加载完成执行一次
|
||||
* @param treeData 完整树形数据
|
||||
*/
|
||||
export function getHousePathById(treeData: Tree[], houseId: string | number, response: ValidTreeKey = 'value'): (string | number)[] {
|
||||
const path: (string | number)[] = [];
|
||||
export function buildHousePathCache(treeData: Tree[]) {
|
||||
// 清空旧缓存
|
||||
pathCache.set('label', new Map());
|
||||
pathCache.set('value', new Map());
|
||||
|
||||
// 递归查找
|
||||
function findNode(nodes: Tree[]): boolean {
|
||||
for (const node of nodes) {
|
||||
path.push(node[response]); // 把当前节点加入路径
|
||||
const stack: [Tree, (string | number)[], (string | number)[]][] = treeData.map(node => [
|
||||
node,
|
||||
[node.label],
|
||||
[node.value]
|
||||
]);
|
||||
|
||||
// 找到目标房屋
|
||||
if (node.type === 'houseId' && node.value === houseId) {
|
||||
return true;
|
||||
}
|
||||
while (stack.length) {
|
||||
const [node, labelPath, valPath] = stack.pop()!;
|
||||
|
||||
// 递归子节点
|
||||
if (node.children && node.children.length > 0) {
|
||||
if (findNode(node.children)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
path.pop(); // 回溯,移除当前节点
|
||||
// 房屋节点存入两套索引
|
||||
if (node.type === 'houseId') {
|
||||
const id = String(node.value);
|
||||
pathCache.get('label')!.set(id, labelPath);
|
||||
pathCache.get('value')!.set(id, valPath);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
findNode(treeData);
|
||||
return path;
|
||||
if (node.children?.length) {
|
||||
const reverseChildren = [...node.children].reverse();
|
||||
for (const child of reverseChildren) {
|
||||
stack.push([
|
||||
child,
|
||||
[...labelPath, child.label],
|
||||
[...valPath, child.value]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存版极速查询房屋路径,O(1)
|
||||
* @param houseId 房屋ID
|
||||
* @param fieldKey 返回字段 label / value
|
||||
*/
|
||||
export function getHousePathById(houseId: string | number, fieldKey: ValidTreeKey = 'value'): (string | number)[] {
|
||||
const id = String(houseId);
|
||||
console.log(pathCache);
|
||||
const cacheMap = pathCache.get(fieldKey);
|
||||
if (!cacheMap) return [];
|
||||
return cacheMap.get(id) ?? [];
|
||||
}
|
||||
// /**
|
||||
// * 根据 houseId 查找完整路径(楼栋 > 单元 > 楼层 > 房屋)
|
||||
// * @param treeData 转换后的树形数据
|
||||
// * @param houseId 目标房屋ID
|
||||
// * @param response
|
||||
// * @returns string[] 路径数组,如 ['1号楼', '1单元', '3层', '301']
|
||||
// */
|
||||
// export function getHousePathById(treeData: Tree[], houseId: string | number, response: ValidTreeKey = 'value'): (string | number)[] {
|
||||
// const path: (string | number)[] = [];
|
||||
// // 递归查找
|
||||
// function findNode(nodes: Tree[]): boolean {
|
||||
// for (const node of nodes) {
|
||||
// path.push(node[response]); // 把当前节点加入路径
|
||||
//
|
||||
// // 找到目标房屋
|
||||
// if (node.type === 'houseId' && node.value === houseId) {
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// // 递归子节点
|
||||
// if (node.children && node.children.length > 0) {
|
||||
// if (findNode(node.children)) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// path.pop(); // 回溯,移除当前节点
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// findNode(treeData);
|
||||
// return path;
|
||||
// }
|
||||
|
||||
// 对字符串 图片地址,处理
|
||||
export function splitStringUrl(str: string, splitLabel: string = ',') {
|
||||
const list = str.split(splitLabel);
|
||||
|
||||
@@ -43,8 +43,8 @@ const whiteVillageId = [
|
||||
'/payRecordList/payRecordList/list',
|
||||
'/warehouse',
|
||||
'/notification/read', // 已读
|
||||
'/cash/billList',
|
||||
'/building/list'
|
||||
'/cash/billList'
|
||||
// '/building/list'
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user