diff --git a/scripts/RelationFormFieldBuilder-output.json b/scripts/RelationFormFieldBuilder-output.json new file mode 100644 index 0000000..b711815 --- /dev/null +++ b/scripts/RelationFormFieldBuilder-output.json @@ -0,0 +1 @@ +{"__config__":{"formId":"formItem1781185555079","jnpfKey":"relationForm","label":"关联表单","tipLabel":"","labelWidth":null,"showLabel":true,"tag":"JnpfRelationForm","tagIcon":"icon-ym icon-ym-generator-menu","tableAlign":"left","tableFixed":"none","className":[],"defaultValue":"","required":true,"layout":"colFormItem","span":24,"dragDisabled":false,"visibility":["pc","app"],"tableName":"","noShow":false,"regList":[],"trigger":"change","renderKey":1781185555080,"transferList":[]},"on":{"change":"({ data, rowIndex, formData, setFormData, setShowOrHide, setRequired, setDisabled, onlineUtils }) => {\n // 在此编写代码\n \n}"},"style":{"width":"100%"},"placeholder":"请选择","modelId":"form123","relationField":"f_name","hasPage":false,"pageSize":20,"queryType":0,"propsValue":"f_id","extraOptions":[],"columnOptions":[{"value":"f_id","label":"ID"},{"value":"f_name","label":"名称"}],"clearable":true,"popupType":"dialog","popupTitle":"选择数据","popupWidth":"800px","filterable":false,"disabled":false,"__vModel__":"f_relate","id":"f_relate","multiple":false,"actionType":"table"} \ No newline at end of file diff --git a/scripts/extract-relationForm-baseline.py b/scripts/extract-relationForm-baseline.py new file mode 100644 index 0000000..593bf2d --- /dev/null +++ b/scripts/extract-relationForm-baseline.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""从 35 form baseline 提取所有 relationForm 控件的 formData 真实样本. +relationForm 控件判定: __config__.jnpfKey == 'relationForm'.""" +import json, glob, os + +OUT = "scripts/relationForm-baseline-samples.jsonl" +samples = [] +for path in glob.glob("/home/ubuntu/ccWorkDir/baseline/*/form/*.json"): + try: + d = json.load(open(path))['data'] + fd = json.loads(d['formData']) if isinstance(d.get('formData'), str) else {} + for f in fd.get('fields', []): + if isinstance(f, dict): + cfg = f.get('__config__', {}) + if isinstance(cfg, dict) and cfg.get('jnpfKey') == 'relationForm': + samples.append({ + "source": os.path.basename(path), + "label": f.get('label') or cfg.get('label'), + "formData": f + }) + except Exception as e: + pass + +with open(OUT, 'w') as out: + for s in samples: + out.write(json.dumps(s, ensure_ascii=False) + "\n") + +print(f"Extracted {len(samples)} relationForm 控件 samples to {OUT}") diff --git a/scripts/relationForm-baseline-samples.jsonl b/scripts/relationForm-baseline-samples.jsonl new file mode 100644 index 0000000..e69de29 diff --git a/ssw-dacli/src/main/java/ssw/dacli/form/builder/field/RelationFormFieldBuilder.java b/ssw-dacli/src/main/java/ssw/dacli/form/builder/field/RelationFormFieldBuilder.java new file mode 100644 index 0000000..2a4e8e3 --- /dev/null +++ b/ssw-dacli/src/main/java/ssw/dacli/form/builder/field/RelationFormFieldBuilder.java @@ -0,0 +1,137 @@ +package ssw.dacli.form.builder.field; + +import ssw.dacli.form.builder.FieldBuilder; +import ssw.dacli.form.builder.FieldBuildContext; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.*; + +/** + * v13 relationForm 控件 formData 0 误差生成器 (Task 1.8, P0 两阶段). + * + *

对应 JNPF 设计器 jnpfKey="relationForm" / tag="JnpfRelationForm" (关联表单). + * baseline 0 误差源: scripts/relationForm-baseline-samples.jsonl (0 sample, baseline 无此控件). + * 字段对齐权威源: JNPF 前端 componentMap.ts (line 1363-1406) + RRelationForm/index.vue (line 113-114 配置项). + * + *

输出结构 (44 字段 = 22 顶层 + 22 __config__): + *

+ * + *

relationForm 特有字段 (P0 两阶段 - 选 + 弹窗): + *

+ * + *

字段对齐策略: 关键字段用 ctx 值 (label/field/required/modelId/relationField); + * 弹窗配置用 componentMap 默认 (popupType=dialog, popupTitle=选择数据, popupWidth=800px, pageSize=20). + * columnOptions 默认 [], 弹窗配置完成后用户手动添加 (CLAUDE.md §6 约束). + */ +public class RelationFormFieldBuilder implements FieldBuilder { + private static final ObjectMapper mapper = new ObjectMapper(); + + @Override + public String jnpfKey() { return "relationForm"; } + + @Override + @SuppressWarnings("unchecked") + public String buildFormField(FieldBuildContext ctx) { + try { + Map field = new LinkedHashMap<>(); + + // 从 ctx.extra 提取关联表单特有参数 (允许为空) + String modelId = ""; + String relationField = ""; + String propsValue = ""; + String showField = ""; + String interfaceId = ""; + Object columnOptionsRaw = new ArrayList<>(); + Object extraOptionsRaw = new ArrayList<>(); + if (ctx.extra != null) { + if (ctx.extra.get("modelId") != null) modelId = ctx.extra.get("modelId").toString(); + if (ctx.extra.get("relationField") != null) relationField = ctx.extra.get("relationField").toString(); + if (ctx.extra.get("propsValue") != null) propsValue = ctx.extra.get("propsValue").toString(); + if (ctx.extra.get("showField") != null) showField = ctx.extra.get("showField").toString(); + if (ctx.extra.get("interfaceId") != null) interfaceId = ctx.extra.get("interfaceId").toString(); + if (ctx.extra.get("columnOptions") != null) columnOptionsRaw = ctx.extra.get("columnOptions"); + if (ctx.extra.get("extraOptions") != null) extraOptionsRaw = ctx.extra.get("extraOptions"); + } + + // ============================================================ + // __config__ (22 字段, 跟 componentMap 0 误差) + // ============================================================ + Map config = new LinkedHashMap<>(); + config.put("formId", "formItem" + System.currentTimeMillis()); + config.put("jnpfKey", "relationForm"); + config.put("label", ctx.label); + config.put("tipLabel", ""); // componentMap = "" + config.put("labelWidth", null); // componentMap = undefined → null + config.put("showLabel", true); // componentMap = true + config.put("tag", "JnpfRelationForm"); // componentMap = "JnpfRelationForm" + config.put("tagIcon", "icon-ym icon-ym-generator-menu"); // componentMap = "icon-ym icon-ym-generator-menu" + config.put("tableAlign", "left"); // componentMap = "left" + config.put("tableFixed", "none"); // componentMap = "none" + config.put("className", new ArrayList<>()); // componentMap = [] + config.put("defaultValue", ""); // componentMap = "" + config.put("required", ctx.required); + config.put("layout", "colFormItem"); // componentMap = "colFormItem" + config.put("span", 24); + config.put("dragDisabled", false); // componentMap = false + config.put("visibility", new ArrayList<>(Arrays.asList("pc", "app"))); // componentMap = ["pc","app"] + config.put("tableName", ""); // componentMap = "" + config.put("noShow", false); // componentMap = false + config.put("regList", new ArrayList<>()); // componentMap = [] + config.put("trigger", "change"); // componentMap = "change" + config.put("renderKey", System.currentTimeMillis()); // JNPF runtime 同步需要 + config.put("transferList", new ArrayList<>()); // componentMap = [] (填充规则) + field.put("__config__", config); + + // ============================================================ + // 顶层 (22 字段, 跟 componentMap 0 误差) + // ============================================================ + field.put("on", new LinkedHashMap() {{ + // componentMap 提供完整 JS 模板 + put("change", "({ data, rowIndex, formData, setFormData, setShowOrHide, setRequired, setDisabled, onlineUtils }) => {\n // 在此编写代码\n \n}"); + }}); + field.put("style", new LinkedHashMap() {{ + put("width", "100%"); // componentMap = {"width":"100%"} + }}); + field.put("placeholder", "请选择"); // componentMap = "请选择" + field.put("modelId", modelId); // 关联功能 ID, 必填 (选填) + field.put("relationField", relationField); // 回显字段 (必填, 选填) + field.put("hasPage", false); // componentMap = false + field.put("pageSize", 20); // componentMap = 20 + field.put("queryType", 0); // componentMap = 0 (简易查询) + field.put("propsValue", propsValue.isEmpty() ? null : propsValue); // 存储字段 (componentMap = undefined → null, 有 ctx 时填) + field.put("extraOptions", extraOptionsRaw); // 弹窗显示字段 (componentMap = []) + field.put("columnOptions", columnOptionsRaw); // 弹窗列表字段 (componentMap = [], CLAUDE.md §6 ≤5 列) + field.put("clearable", true); // componentMap = true + field.put("popupType", "dialog"); // componentMap = "dialog" + field.put("popupTitle", "选择数据"); // componentMap = "选择数据" + field.put("popupWidth", "800px"); // componentMap = "800px" + field.put("filterable", false); // componentMap = false + field.put("disabled", false); // componentMap = false + field.put("__vModel__", ctx.field); + field.put("id", ctx.field); + field.put("multiple", false); // relationForm Props 多选默认 false + field.put("actionType", "table"); // 基线无 sample, 推断 table 默认 + + return mapper.writeValueAsString(field); + } catch (Exception e) { + throw new RuntimeException("RelationFormFieldBuilder.buildFormField failed: " + e.getMessage(), e); + } + } +} diff --git a/ssw-dacli/src/main/resources/META-INF/services/ssw.dacli.form.builder.FieldBuilder b/ssw-dacli/src/main/resources/META-INF/services/ssw.dacli.form.builder.FieldBuilder index 69ba902..339c8e5 100644 --- a/ssw-dacli/src/main/resources/META-INF/services/ssw.dacli.form.builder.FieldBuilder +++ b/ssw-dacli/src/main/resources/META-INF/services/ssw.dacli.form.builder.FieldBuilder @@ -5,3 +5,4 @@ ssw.dacli.form.builder.field.DatePickerFieldBuilder ssw.dacli.form.builder.field.TimePickerFieldBuilder ssw.dacli.form.builder.field.UploadFileFieldBuilder ssw.dacli.form.builder.field.UploadImgFieldBuilder +ssw.dacli.form.builder.field.RelationFormFieldBuilder diff --git a/ssw-dacli/src/test/java/ssw/dacli/form/builder/field/RelationFormFieldBuilderTest.java b/ssw-dacli/src/test/java/ssw/dacli/form/builder/field/RelationFormFieldBuilderTest.java new file mode 100644 index 0000000..f1d2e8e --- /dev/null +++ b/ssw-dacli/src/test/java/ssw/dacli/form/builder/field/RelationFormFieldBuilderTest.java @@ -0,0 +1,193 @@ +package ssw.dacli.form.builder.field; + +import ssw.dacli.form.builder.FieldBuildContext; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.nio.file.*; +import java.util.*; + +/** + * v13 Task 1.8 — RelationFormFieldBuilder 单元测试 (4 用例). + * + *

校验关键结构 (jnpfKey/tag/columnOptions/relationField/transferList) 与 componentMap 0 误差. + * baseline 0 sample, 字段对齐权威源: JNPF 前端 componentMap.ts (line 1363-1406). + * + *

CLAUDE.md §6 约束: columnOptions ≤ 5 列. + */ +public class RelationFormFieldBuilderTest { + + private static final ObjectMapper M = new ObjectMapper(); + + @Test + public void testRelationForm_basic() throws Exception { + FieldBuildContext ctx = new FieldBuildContext(); + ctx.field = "f_relate"; + ctx.label = "关联表单"; + ctx.required = false; + + String json = new RelationFormFieldBuilder().buildFormField(ctx); + var parsed = M.readTree(json); + + // 顶层关键键 + assertNotNull(parsed.get("__config__")); + assertNotNull(parsed.get("__vModel__")); + assertEquals("f_relate", parsed.get("__vModel__").asText()); + + // __config__ 关键键 (跟 componentMap 0 误差) + var cfg = parsed.get("__config__"); + assertEquals("relationForm", cfg.get("jnpfKey").asText()); + assertEquals("JnpfRelationForm", cfg.get("tag").asText()); + assertEquals("关联表单", cfg.get("label").asText()); + assertEquals("colFormItem", cfg.get("layout").asText()); + assertEquals("icon-ym icon-ym-generator-menu", cfg.get("tagIcon").asText()); + assertEquals("change", cfg.get("trigger").asText()); + + // relationForm 特有 (componentMap 默认值) + assertEquals("请选择", parsed.get("placeholder").asText()); + assertEquals("", parsed.get("modelId").asText()); + assertEquals("", parsed.get("relationField").asText()); + assertFalse(parsed.get("hasPage").asBoolean()); + assertEquals(20, parsed.get("pageSize").asInt()); + assertEquals(0, parsed.get("queryType").asInt()); + // propsValue 默认 null (componentMap = undefined) + assertTrue(parsed.get("propsValue") == null || parsed.get("propsValue").isNull()); + // 弹窗默认配置 + assertEquals("dialog", parsed.get("popupType").asText()); + assertEquals("选择数据", parsed.get("popupTitle").asText()); + assertEquals("800px", parsed.get("popupWidth").asText()); + assertTrue(parsed.get("clearable").asBoolean()); + assertFalse(parsed.get("filterable").asBoolean()); + assertFalse(parsed.get("disabled").asBoolean()); + // 推断字段 + assertEquals("table", parsed.get("actionType").asText()); + assertFalse(parsed.get("multiple").asBoolean()); + } + + @Test + public void testRelationForm_withExtra() throws Exception { + // 测试 ctx.extra 字段透传 (modelId/relationField/columnOptions 等) + FieldBuildContext ctx = new FieldBuildContext(); + ctx.field = "f_relate"; + ctx.label = "关联表单"; + ctx.required = true; + Map extra = new LinkedHashMap<>(); + extra.put("modelId", "form123"); + extra.put("relationField", "f_name"); + extra.put("propsValue", "f_id"); + extra.put("showField", "f_name"); + // 弹窗列表字段 (CLAUDE.md §6 约束 ≤5 列) + List> cols = new ArrayList<>(); + cols.add(new LinkedHashMap() {{ put("value", "f_id"); put("label", "ID"); }}); + cols.add(new LinkedHashMap() {{ put("value", "f_name"); put("label", "名称"); }}); + cols.add(new LinkedHashMap() {{ put("value", "f_code"); put("label", "编码"); }}); + extra.put("columnOptions", cols); + ctx.extra = extra; + + String json = new RelationFormFieldBuilder().buildFormField(ctx); + var parsed = M.readTree(json); + + // 透传字段 + assertEquals("form123", parsed.get("modelId").asText()); + assertEquals("f_name", parsed.get("relationField").asText()); + assertEquals("f_id", parsed.get("propsValue").asText()); + // required 透传 + assertTrue(parsed.get("__config__").get("required").asBoolean()); + + // columnOptions (≤5 列) + var columnOptions = parsed.get("columnOptions"); + assertTrue(columnOptions.isArray()); + assertEquals(3, columnOptions.size()); + assertEquals("ID", columnOptions.get(0).get("label").asText()); + assertEquals("名称", columnOptions.get(1).get("label").asText()); + assertEquals("编码", columnOptions.get(2).get("label").asText()); + } + + @Test + public void testRelationForm_baselineKeyStructure() throws Exception { + // 跟 componentMap relationForm 控件 formData 关键结构对齐检查 + FieldBuildContext ctx = new FieldBuildContext(); + ctx.field = "f_relate"; + ctx.label = "关联表单"; + + String json = new RelationFormFieldBuilder().buildFormField(ctx); + var parsed = M.readTree(json); + var cfg = parsed.get("__config__"); + + // __config__ 必有 (22 字段) + assertNotNull(cfg.get("formId")); + assertNotNull(cfg.get("jnpfKey")); + assertNotNull(cfg.get("label")); + assertNotNull(cfg.get("tipLabel")); + assertNotNull(cfg.get("showLabel")); + assertNotNull(cfg.get("tag")); + assertNotNull(cfg.get("tagIcon")); + assertNotNull(cfg.get("tableAlign")); + assertNotNull(cfg.get("tableFixed")); + assertNotNull(cfg.get("className")); + assertNotNull(cfg.get("defaultValue")); + assertNotNull(cfg.get("required")); + assertNotNull(cfg.get("layout")); + assertNotNull(cfg.get("span")); + assertNotNull(cfg.get("dragDisabled")); + assertNotNull(cfg.get("visibility")); + assertNotNull(cfg.get("tableName")); + assertNotNull(cfg.get("noShow")); + assertNotNull(cfg.get("regList")); + assertNotNull(cfg.get("trigger")); + assertNotNull(cfg.get("transferList")); + + // 顶层 baseline 必有 (22 字段) + assertNotNull(parsed.get("__vModel__")); + assertNotNull(parsed.get("style")); + assertNotNull(parsed.get("placeholder")); + assertNotNull(parsed.get("modelId")); + assertNotNull(parsed.get("relationField")); + assertNotNull(parsed.get("hasPage")); + assertNotNull(parsed.get("pageSize")); + assertNotNull(parsed.get("queryType")); + assertNotNull(parsed.get("extraOptions")); + assertNotNull(parsed.get("columnOptions")); + assertNotNull(parsed.get("clearable")); + assertNotNull(parsed.get("popupType")); + assertNotNull(parsed.get("popupTitle")); + assertNotNull(parsed.get("popupWidth")); + assertNotNull(parsed.get("filterable")); + assertNotNull(parsed.get("disabled")); + assertNotNull(parsed.get("on")); + assertNotNull(parsed.get("id")); + assertNotNull(parsed.get("multiple")); + assertNotNull(parsed.get("actionType")); + + // on.change 必有 (componentMap 完整 JS 模板) + assertNotNull(parsed.get("on").get("change")); + // 触发器 = "change" (componentMap) + assertEquals("change", cfg.get("trigger").asText()); + // 弹窗触发配置 + assertTrue(parsed.get("style").get("width").asText().equals("100%")); + } + + @Test + public void testRelationForm_dumpToFile() throws Exception { + // 工具方法: 把生成结果写到 scripts/RelationFormFieldBuilder-output.json 用于 diff 对比 + FieldBuildContext ctx = new FieldBuildContext(); + ctx.field = "f_relate"; + ctx.label = "关联表单"; + ctx.required = true; + Map extra = new LinkedHashMap<>(); + extra.put("modelId", "form123"); + extra.put("relationField", "f_name"); + extra.put("propsValue", "f_id"); + List> cols = new ArrayList<>(); + cols.add(new LinkedHashMap() {{ put("value", "f_id"); put("label", "ID"); }}); + cols.add(new LinkedHashMap() {{ put("value", "f_name"); put("label", "名称"); }}); + extra.put("columnOptions", cols); + ctx.extra = extra; + String json = new RelationFormFieldBuilder().buildFormField(ctx); + Path out = Paths.get("../scripts/RelationFormFieldBuilder-output.json"); + Files.createDirectories(out.getParent()); + Files.writeString(out, json); + assertTrue(Files.exists(out)); + assertTrue(Files.size(out) > 100); + } +}