diff --git a/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java b/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java index 1c09b55..210ec12 100644 --- a/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java +++ b/platform-converter/src/main/java/com/eip4/platform/converter/TemplateToAmisConverter.java @@ -1208,6 +1208,9 @@ public class TemplateToAmisConverter { // 把该 className 内的 .cxd-Tree max-height 抬到视口相对(calc(100vh-230px))+ 内部滚动。既非定高框 // 只露一半,也非 heightAuto 随内容无限撑高把页面拉爆(大树数千 px)。 treeSchema.put("className", "eip4-treegrid-tree"); + final String treeCompId = "treegrid_tree"; + treeSchema.put("id", treeCompId); + Map treeAddBtn = null; // 新增分类弹窗按钮(替代内联 creatable,满足多必填列实体) if (treeTemplate != null && !treeRef.isBlank()) { treeSchema.put("source", "/api/template/" + treeRef + "/tree"); // 选中树节点 → 重载右表(${tree} 进右表 api 过滤);TemplateTreeController 运行时构树 @@ -1216,8 +1219,9 @@ public class TemplateToAmisConverter { new LinkedHashMap<>(Map.of("actionType", "reload", "componentId", gridCrudId))))))); result.note("tree+grid: treetemplate ref=" + treeRef + " → input-tree source /api/template/" + treeRef + "/tree(运行时构树)+ 选中重载右表"); - // 左树分类节点增删改:treetemplate ref 的 → amis 原生 creatable/editable/removable - applyTreeNodeCrud(treeSchema, treeTemplate, treeRef, result); + // 左树分类节点 CRUD:编辑/删除保留 input-tree 原生内联;新增改弹窗表单(真实实体需 CODE/名称/排序 + // 等多必填列 + 顶级 parentid 留空,InputTree 内联只给 label 满足不了)→ 返回新增按钮。 + treeAddBtn = applyTreeNodeCrud(treeSchema, treeTemplate, treeRef, treeCompId, result); } Map gridSchema = new LinkedHashMap<>(); gridSchema.put("type", "crud"); @@ -1274,8 +1278,10 @@ public class TemplateToAmisConverter { // (非 flex.items——flex 的 items 必须是渲染器本身,误用会「找不到对应的渲染器」)。 Map hbox = new LinkedHashMap<>(); hbox.put("type", "grid"); + // 左列 body:有新增按钮则 [新增分类按钮, 树],否则仅树 + Object leftBody = treeAddBtn != null ? List.of(treeAddBtn, treeSchema) : treeSchema; hbox.put("columns", List.of( - new LinkedHashMap<>(Map.of("md", 3, "body", treeSchema)), + new LinkedHashMap<>(Map.of("md", 3, "body", leftBody)), new LinkedHashMap<>(Map.of("md", 9, "body", gridSchema)))); // F2-13:title 优先取 view_template.name String treeGridTitle = templateName != null ? templateName : "树+列表"; @@ -1291,13 +1297,13 @@ public class TemplateToAmisConverter { * edit 送 {主键:${value}, 名称列:${label}},delete 送 {主键:${value}}。InputTree 配了 source, * 增删改后 amis 自动重拉 source 刷新树。无 或实体未解析 → 跳过(只读树)。 */ - private void applyTreeNodeCrud(Map treeSchema, Element treeTemplate, String treeRef, - ConversionResult result) { + private Map applyTreeNodeCrud(Map treeSchema, Element treeTemplate, + String treeRef, String treeCompId, ConversionResult result) { Element refRoot = loadRefRoot(treeRef, result); Element node = refRoot != null ? refRoot.element("node") : null; Element buttonsEl = node != null ? node.element("buttons") : null; if (buttonsEl == null) { - return; + return null; } boolean hasInsert = false, hasUpdate = false, hasDelete = false; String addSvc = "", deleteSvc = ""; @@ -1319,18 +1325,14 @@ public class TemplateToAmisConverter { String treeTable = resolveTableName(treeTemplate.attributeValue("entityid", ""), result); if (treeTable == null || treeTable.isBlank()) { result.degrade("tree+grid: 左树实体未解析 → 跳过节点增删改(树仍可浏览)"); - return; + return null; } treeTable = treeTable.toUpperCase(); String pk = treeTable + "_ID"; String nameCol = resolveDisplayColumn(treeTable); String parentCol = resolveTreeParentCol(refRoot); - if (hasInsert && !addSvc.isBlank()) { - treeSchema.put("creatable", true); - treeSchema.put("rootCreatable", true); - treeSchema.put("addApi", flowApi(addSvc, mapOf(nameCol, "${label}", parentCol, "${parent.value}"))); - } + // 编辑/删除:input-tree 原生内联(编辑只改名列,原行其它必填列已存在;删除只送主键)。 if (hasUpdate && !addSvc.isBlank()) { treeSchema.put("editable", true); treeSchema.put("editApi", flowApi(addSvc, mapOf(pk, "${value}", nameCol, "${label}"))); @@ -1339,10 +1341,96 @@ public class TemplateToAmisConverter { treeSchema.put("removable", true); treeSchema.put("deleteApi", flowApi(deleteSvc, mapOf(pk, "${value}"))); } - result.note("tree+grid: 左树节点 CRUD creatable=" + (hasInsert && !addSvc.isBlank()) - + " editable=" + (hasUpdate && !addSvc.isBlank()) + " removable=" + (hasDelete && !deleteSvc.isBlank()) + // 新增:改弹窗表单(真实实体新增需 CODE/名称/排序等多必填列 + 顶级 parentid 留空, + // InputTree 内联 creatable 只能给 label 满足不了 → 报「xx 必填」「父引用被删除」)。 + Map addBtn = null; + if (hasInsert && !addSvc.isBlank()) { + addBtn = buildTreeNodeAddButton(treeTable, addSvc, parentCol, treeRef, treeCompId, result); + } + result.note("tree+grid: 左树节点 CRUD 新增=弹窗(" + (addBtn != null) + + ") editable=" + (hasUpdate && !addSvc.isBlank()) + " removable=" + (hasDelete && !deleteSvc.isBlank()) + "(表=" + treeTable + " 名列=" + nameCol + " 父列=" + parentCol + " add=" + addSvc + " del=" + deleteSvc + ")"); + return addBtn; + } + + /** + * 左树「新增分类」弹窗按钮:表单含实体可编辑必填列(排序号默认 0)+ 上级选择器(tree-select, + * 留空=顶级、省略 parentid 避免空引用校验失败)。提交整表单到 add 服务流,成功后重载左树。 + */ + private Map buildTreeNodeAddButton(String treeTable, String addSvc, String parentCol, + String treeRef, String treeCompId, ConversionResult result) { + List> fields = buildTreeAddFormFields(treeTable, parentCol, result); + // 上级选择器:tree-select 复用左树同源数据;clearable,留空即顶级(amis 不下发空值 → 引擎按 null 建顶级)。 + Map parentField = new LinkedHashMap<>(); + parentField.put("type", "tree-select"); + parentField.put("name", parentCol); + parentField.put("label", "上级分类"); + parentField.put("source", "/api/template/" + treeRef + "/tree"); + parentField.put("clearable", true); + parentField.put("placeholder", "留空 = 顶级分类"); + fields.add(parentField); + + Map form = new LinkedHashMap<>(); + form.put("type", "form"); + form.put("title", "新增分类"); + form.put("api", "post:/api/flow/" + addSvc); // 送整表单(含各必填列 + 选填 parentid) + form.put("reload", treeCompId); // 成功后重载左树 + form.put("body", fields); + + Map dialog = new LinkedHashMap<>(); + dialog.put("title", "新增分类"); + dialog.put("body", form); + + Map btn = new LinkedHashMap<>(); + btn.put("type", "button"); + btn.put("label", "新增分类"); + btn.put("level", "primary"); + btn.put("icon", "fa fa-plus"); + btn.put("className", "m-b-sm"); + btn.put("actionType", "dialog"); + btn.put("dialog", dialog); + return btn; + } + + /** 树实体可编辑必填列 → amis 表单字段(排除主键/审计/version/isactive/父列;排序类用 input-number 默认 0)。 */ + private List> buildTreeAddFormFields(String treeTable, String parentCol, + ConversionResult result) { + List> fields = new ArrayList<>(); + List> cols; + try { + cols = jdbcTemplate.queryForList( + "SELECT TRIM(a.columnname) AS col, TRIM(a.displayname) AS dn" + + " FROM sys_entityattribute a JOIN sys_entity e ON TRIM(a.entityid)=TRIM(e.sys_entity_id)" + + " WHERE UPPER(TRIM(e.tablename))=? AND a.ismandatory=1 AND a.iskey=0" + + " AND COALESCE(a.isreadonly,0)=0 AND a.isactive=1" + + " AND UPPER(TRIM(a.columnname)) NOT IN" + + " ('CREATETIME','CREATEBY','UPDATETIME','UPDATEBY','VERSION','ISACTIVE')" + + " AND UPPER(TRIM(a.columnname))<>? ORDER BY a.entitydisplayseq NULLS LAST", + treeTable.toUpperCase(), parentCol == null ? "" : parentCol.toUpperCase()); + } catch (Exception e) { + result.degrade("tree+grid: 新增表单字段查询失败(" + treeTable + ") → 仅上级选择器"); + return fields; + } + for (Map c : cols) { + String col = c.get("col") == null ? "" : c.get("col").toString(); + String dn = c.get("dn") == null ? "" : c.get("dn").toString(); + if (col.isBlank()) { + continue; + } + boolean isSort = col.toUpperCase().matches(".*(SORT|SEQ|ORDER).*") + || dn.contains("排序") || dn.contains("顺序"); + Map f = new LinkedHashMap<>(); + f.put("type", isSort ? "input-number" : "input-text"); + f.put("name", col); + f.put("label", dn.isBlank() ? col : dn); + f.put("required", true); + if (isSort) { + f.put("value", 0); + } + fields.add(f); + } + return fields; } /** 取被引用模板(view_template)的根元素;失败/空返 null。treegrid 列定义、右表按钮、左树节点 CRUD 共用。 */