perf: 库位选择器改为按需懒加载——新增 /warehouse/children 接口,WarehouseSelector 移除全量树依赖
This commit is contained in:
@ -54,6 +54,41 @@ def get_tree():
|
||||
}), 500
|
||||
|
||||
|
||||
@warehouse_bp.route('/children', methods=['GET'])
|
||||
def get_children():
|
||||
"""
|
||||
懒加载:获取指定库位的直接子节点(parent_id 省略/为空 = 顶层)。
|
||||
每个节点附带 has_children 标记,前端据此渲染「进入下级」而非点进去才知道。
|
||||
与 /tree 行为一致(不额外过滤 is_enabled、按 name 升序)。
|
||||
"""
|
||||
try:
|
||||
parent_id = request.args.get('parent_id', type=int)
|
||||
if parent_id is None:
|
||||
nodes = SysWarehouseLocation.query.filter(
|
||||
SysWarehouseLocation.parent_id.is_(None)
|
||||
).order_by(SysWarehouseLocation.name.asc()).all()
|
||||
else:
|
||||
nodes = SysWarehouseLocation.query.filter(
|
||||
SysWarehouseLocation.parent_id == parent_id
|
||||
).order_by(SysWarehouseLocation.name.asc()).all()
|
||||
|
||||
# 一次查询所有"有子节点"的 parent_id,用于 has_children 判断(避免 N+1)
|
||||
parent_with_children = set(
|
||||
cid for (cid,) in db.session.query(SysWarehouseLocation.parent_id)
|
||||
.filter(SysWarehouseLocation.parent_id.isnot(None)).distinct().all()
|
||||
)
|
||||
|
||||
data = []
|
||||
for node in nodes:
|
||||
d = node.to_dict()
|
||||
d['has_children'] = node.id in parent_with_children
|
||||
data.append(d)
|
||||
|
||||
return jsonify({'code': 200, 'msg': 'success', 'data': data})
|
||||
except Exception as e:
|
||||
return jsonify({'code': 500, 'msg': str(e), 'data': None}), 500
|
||||
|
||||
|
||||
@warehouse_bp.route('', methods=['POST'])
|
||||
@jwt_required()
|
||||
@audit_log(
|
||||
|
||||
Reference in New Issue
Block a user