perf(stocktake): 库位树按公司前缀后端裁剪,树与推荐并行请求

【后端】/tree 支持 ?prefixes=Y 或 ?prefixes=C,L(逗号分隔)
只在**顶层**按 name / full_path 前缀过滤,命中即整棵子树保留 ——
不递归裁剪,避免把子树打散导致前端勾选语义错乱。不传则全量。
刻意不用懒加载:setCheckedKeys / getCheckedNodes 依赖全树已构建。

实测节点数(含子树):
  全量 3371 → IRIS (Y) 500(↓85%)→ LICA (C,L) 2871(↓15%)
IRIS 收益很大;LICA 的前缀覆盖了树的大部分分支,故提升有限。

【前端】
- getWarehouseTree(prefixes?) 透传前缀,loadLocationTree 从
  getAllowedLocPrefixes(selectedCompany) 取;后端已做过滤,
  前端不再重复过滤,删掉冗余的 filterTreeByCompany。
- fetchRecommendLocations 改为 Promise.all 并行拉树与推荐,
  取代原来的串行 await(两段网络等待不再叠加);
  setCheckedKeys 前仍保留 await nextTick() 等树渲染完。

【顺带修一个上一轮引入的 bug】
右侧自 leafOnly 改造后只存末级路径,而推荐返回的是库存级路径、可能是
非末级,原来的逐字比对必然对不上,会把正常勾选的库位误报成
「未能勾选」。改为按「自身或其祖先」判定覆盖。

实测: prefixes 过滤正确(IRIS 8 个顶层 / LICA 25 个 / 不传 33 个)
This commit is contained in:
yueli
2026-09-11 14:45:23 +08:00
parent d1694bf245
commit e573185ea4
3 changed files with 52 additions and 25 deletions

View File

@ -34,14 +34,36 @@ def build_tree(nodes, parent_id=None):
def get_tree():
"""
获取库位树形结构
查询参数:
prefixes —— 可选,逗号分隔的顶层前缀,例如 ?prefixes=Y 或 ?prefixes=C,L
只返回**顶层** name / full_path 命中这些前缀的根节点及其完整子树;
不传则返回全量。
用途前端按公司精简拉取IRIS 只要 Y*LICA 只要 C*/L*
在**保留完整子树**的前提下减少节点数与传输量 —— 不能退回懒加载,
因为 setCheckedKeys / getCheckedNodes 依赖全树已构建。
"""
try:
raw_prefixes = request.args.get('prefixes', '', type=str)
prefixes = [p.strip().upper() for p in raw_prefixes.split(',') if p.strip()]
# 查询所有库位,按 name 升序排序
all_locations = SysWarehouseLocation.query.order_by(SysWarehouseLocation.name.asc()).all()
# 构建树形结构O(N) 内存组装,见 build_tree
tree_data = build_tree(all_locations, parent_id=None)
# ★ 只在**顶层**做前缀过滤:命中即整棵子树保留,不递归裁剪,
# 避免把子树打散导致前端勾选语义错乱
if prefixes:
def _hit(node):
name = str(node.get('name') or '').upper()
path = str(node.get('full_path') or '').upper()
return any(name.startswith(p) or path.startswith(p) for p in prefixes)
tree_data = [n for n in tree_data if _hit(n)]
return jsonify({
'code': 200,
'msg': 'success',