TreeGrid 二次修复:左树 heightAuto 撑高 + 点父节点子树聚合(右表 IN 过滤)

4337720 只做通 happy path(有数据单级树 + 点叶子),浏览器 QA 发现两处运行时缺陷:

1. 左树只显示一半(全 45 个 treegrid 页):input-tree 在 grid 列内约 270px
   定高滚动框、页面下方大片留白 → convertTreeGrid 加 heightAuto:true 随内容撑满。

2. 点父/中间节点右表「暂无数据」(29 个多级 type-4 树):右表按 {rcol}=${tree}
   精确匹配点中节点自身,而业务行常只挂叶子(文件挂「技术部」非父级「技术中心」),
   点父级 0 行;老系统点父级聚合整棵子树。修法:新增 GET /api/template/treegrid
   子树聚合端点——解析「点中节点 + 全部后代」id 列表,以 List 值委托合成 _SEARCH,
   引擎 GeneralPO.get_SearchWhereClause 见 List 即构 rcol IN(...)(已 escapeSqlLiteral
   转义、无注入面,不改保真层),复用 _DISPLAY__/数据权限/分页;转换器右表 api 改指
   该端点(treeRef/gridTable/rcol/node=${tree})。

TemplateTreeController 重构出可复用 build() + collectSubtreeIds + 子树端点
(补 7 单测:构树多级/子树展开/陈旧节点退化/空节点/List IN 委托/空 node 返全部/
IDENT 守卫);转换器 tree_treeGrid 断言更新为新 api + heightAuto(60 测全过)。

浏览器实证:资料库点父级「技术中心」现出子级「技术部」文件(修前空);
单级树「通知收藏」点叶子仍正确过滤 1 行(无回归)。
注:节点级增删改按钮、type=3 声明式多级树接端点属范围外后置项。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-13 21:24:19 +08:00
parent 433772015a
commit d1ca75098a
4 changed files with 321 additions and 41 deletions

View File

@@ -1,6 +1,9 @@
package com.eip4.platform.metadata.web;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -14,9 +17,11 @@ import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
@@ -31,7 +36,12 @@ import org.springframework.web.bind.annotation.RestController;
* <p><b>构树算法</b>:把所有层级的行装进单一 {@code id→节点} 映射(id 归一化大写去空白),再按各行所在
* 层级的 parentid 列值找父节点(父在任意层级皆可,id 全局唯一)——父在则挂为 children,否则为根。
* 一次性 eager 构建(树/分类表通常小);节点总数封顶 {@code MAX_NODES},超限截断并 WARN。
* 每个节点 {@code value}=原始主键(供右表 {@code ?rcol=${tree}} 过滤),{@code label}=实体显示列。
* 每个节点 {@code value}=原始主键(供右表过滤),{@code label}=实体显示列。
*
* <p><b>子树聚合(右表过滤)</b>:多级树里业务行往往只挂在叶子节点(如文件挂「技术部」而非父级「技术中心」),
* 老系统点父节点时聚合整棵子树。本控制器的 {@code /api/template/treegrid} 端点解析「点中节点+全部后代」的
* id 列表,以 {@code List} 值委托合成搜索流 {@code <TABLE>_SEARCH}——引擎 {@code GeneralPO.get_SearchWhereClause}
* 见 List 即构 {@code rcol IN (...)}(已转义,无注入面),从而复用 _DISPLAY__/数据权限/分页,无需改保真层。
*/
@RestController
public class TemplateTreeController {
@@ -42,9 +52,11 @@ public class TemplateTreeController {
private static final String[] NAME_FALLBACKS = {"NAME", "SORT_NAME", "FULLNAME", "TITLE", "SUBJECT", "DISPLAYNAME"};
private final JdbcTemplate jdbcTemplate;
private final FlowController flowController;
public TemplateTreeController(JdbcTemplate jdbcTemplate) {
public TemplateTreeController(JdbcTemplate jdbcTemplate, FlowController flowController) {
this.jdbcTemplate = jdbcTemplate;
this.flowController = flowController;
}
/** 树层级:实体表 + 该级的 parentid 列(null=该级行为根)。 */
@@ -57,30 +69,17 @@ public class TemplateTreeController {
String parentRef;
}
/** 构树结果:{@code byId}=归一化 id→节点(children 已链);{@code roots}=根级 option 列表(稳定序)。 */
private record Built(Map<String, Node> byId, List<Map<String, Object>> roots) {}
@GetMapping("/api/template/{templateId}/tree")
public Map<String, Object> tree(@PathVariable String templateId) {
List<Map<String, Object>> options = new ArrayList<>();
List<Map<String, Object>> options;
try {
List<Level> levels = resolveLevels(templateId);
Map<String, Node> byId = new LinkedHashMap<>();
List<Node> order = new ArrayList<>();
for (Level lv : levels) {
if (order.size() >= MAX_NODES) {
log.warn("[TREE] {} 节点超 {} 截断", templateId, MAX_NODES);
break;
}
loadLevel(lv, byId, order);
}
for (Node n : order) {
Node parent = n.parentRef != null ? byId.get(n.parentRef) : null;
if (parent != null && parent != n) {
parent.children.add(n.opt);
} else {
options.add(n.opt);
}
}
options = build(templateId).roots();
} catch (DataAccessException ex) {
log.warn("[TREE] 构树失败 template={}: {}", templateId, ex.getMessage());
options = new ArrayList<>();
}
Map<String, Object> data = new LinkedHashMap<>();
data.put("options", options);
@@ -91,6 +90,108 @@ public class TemplateTreeController {
return resp;
}
/**
* 树+grid 右表数据源端点(子树聚合)。转换器对 type=4 树+grid 的右表产出
* {@code source=/api/template/treegrid?treeRef=&gridTable=&rcol=&node=${tree}&page=&perPage=}。
*
* <p>{@code node} 为点中的树节点 id(空=未选中,返回全部行);本端点解析「该节点+全部后代」id 列表,
* 以 {@code List} 值塞进合成搜索流入参 {@code map[rcol]},委托 {@link FlowController#executePost}——
* 引擎见 List 构 {@code rcol IN (子树 id...)},于是点父级也出整棵子树的业务行(对齐老系统)。
* 复用合成流故 _DISPLAY__/数据权限/分页全在;{@code gridTable}/{@code rcol} 经 {@code IDENT} 守卫。
*/
@GetMapping("/api/template/treegrid")
public ResponseEntity<Map<String, Object>> treeGrid(
@RequestParam String treeRef,
@RequestParam String gridTable,
@RequestParam String rcol,
@RequestParam(required = false) String node,
@RequestParam(required = false) String page,
@RequestParam(required = false) String perPage) {
if (!IDENT.matcher(gridTable).matches() || !IDENT.matcher(rcol).matches()) {
Map<String, Object> err = new LinkedHashMap<>();
err.put("status", 1);
err.put("msg", "非法表名/列名");
err.put("data", new LinkedHashMap<>());
return ResponseEntity.badRequest().body(err);
}
Map<String, Object> body = new HashMap<>();
if (node != null && !node.isBlank()) {
List<String> ids;
try {
ids = collectSubtreeIds(treeRef, node);
} catch (DataAccessException ex) {
log.warn("[TREEGRID] 子树解析失败 treeRef={} node={}: {}", treeRef, node, ex.getMessage());
ids = List.of(node.trim()); // 退化为精确匹配自身,绝不放空成「全部」
}
if (!ids.isEmpty()) {
body.put(rcol, ids); // List → 引擎 get_SearchWhereClause 构 IN(已转义)
}
}
if (page != null) {
body.put("page", page);
}
if (perPage != null) {
body.put("perPage", perPage);
}
return flowController.executePost(gridTable.toUpperCase() + "_SEARCH", body);
}
/** 解析「点中节点 + 全部后代」的原始 id 列表(供右表 IN 过滤)。节点不在树中(陈旧)→ 退化为仅自身。 */
List<String> collectSubtreeIds(String treeRef, String nodeId) {
if (nodeId == null || nodeId.isBlank()) {
return List.of();
}
Built built = build(treeRef);
Node start = built.byId().get(nodeId.trim().toUpperCase());
if (start == null) {
return List.of(nodeId.trim());
}
List<String> ids = new ArrayList<>();
Deque<Map<String, Object>> stack = new ArrayDeque<>();
stack.push(start.opt);
while (!stack.isEmpty() && ids.size() < MAX_NODES) {
Map<String, Object> opt = stack.pop();
Object v = opt.get("value");
if (v != null) {
ids.add(String.valueOf(v));
}
if (opt.get("children") instanceof List<?> kids) {
for (Object k : kids) {
if (k instanceof Map<?, ?> child) {
@SuppressWarnings("unchecked")
Map<String, Object> co = (Map<String, Object>) child;
stack.push(co);
}
}
}
}
return ids;
}
/** 构树:装载各层级行 → 按 parentid 链父 → 返回 byId 索引 + 根级 option(稳定序)。 */
private Built build(String templateId) {
List<Level> levels = resolveLevels(templateId);
Map<String, Node> byId = new LinkedHashMap<>();
List<Node> order = new ArrayList<>();
for (Level lv : levels) {
if (order.size() >= MAX_NODES) {
log.warn("[TREE] {} 节点超 {} 截断", templateId, MAX_NODES);
break;
}
loadLevel(lv, byId, order);
}
List<Map<String, Object>> roots = new ArrayList<>();
for (Node n : order) {
Node parent = n.parentRef != null ? byId.get(n.parentRef) : null;
if (parent != null && parent != n) {
parent.children.add(n.opt);
} else {
roots.add(n.opt);
}
}
return new Built(byId, roots);
}
/** 解析树模板 XML → 各层级(实体表 + parentid 列)。entityId→tablename;同实体取有 parentid 的声明(自嵌套)。 */
private List<Level> resolveLevels(String templateId) {
List<String> xmls = jdbcTemplate.queryForList(