diff --git a/src/commands/form/_relationForm_auto.ts b/src/commands/form/_relationForm_auto.ts new file mode 100644 index 0000000..d586a24 --- /dev/null +++ b/src/commands/form/_relationForm_auto.ts @@ -0,0 +1,207 @@ +/** + * commands/form/_relationForm_auto.ts — relationForm 字段自动检测 (BS-18 fix) + * + * 问题 (BS-18, 2026-06-27 tds_batch4 复盘): + * tds_product 5 个跨 form 关联字段 (f_target_space / f_provider_space / f_user_space + * / f_register_space / f_audit_space) 创建时是 input 文本框, 不是 relationForm 弹窗. + * 数据能存能查, 但浏览器层不能弹窗选 tds_space; 关系用 enCode 字符串匹配. + * 违反 CLAUDE.md §6 "关联字段必须用 relationForm 组件". + * + * 修复策略: + * 镜像 web 端 FormGenerator.vue:272-276 的校验逻辑, 但反过来——web 校验"已标 relationForm 但缺 + * modelId/propsValue/relationField" → 我们在 dacli 端**主动注入**这 3 项 + columnOptions, + * 当用户字段名符合命名模式 (*_space / *_id / *_code) 且能找到匹配的 form enCode. + * + * 镜像源: + * - web/src/components/FormGenerator/src/FormGenerator.vue:272-276 (relationForm 校验) + * - web/src/components/FormGenerator/src/helper/formUtils.ts:14-77 (buildFormData schema 生成) + * - dacli B-13 fix: create.ts:169-220 (relationForm 注入逻辑 — fieldRelationConfig 路径) + * + * 命名启发 (启发式, 不强制): + * - `*_space` → 目标 form enCode 推 "tds_space" + * - `*_id` → 推关联 form enCode (e.g. f_policy_id → tds_policy, 但 f_user_id / f_id 跳过) + * - `*_code` → 推 (e.g. f_product_code → tds_product, 但 f_en_code 跳过) + * - 字段名前缀去除: f__ → entity 即目标 form enCode + * + * 调用入口: + * form create --ai-json 时, 调用 `autoDetectRelationForm` 给每个 fld 填 fieldRelationConfig + * 当用户**没**显式给 fieldRelationConfig 时, 启发式推断. + */ +import { JnpfClient } from '../../core/client.ts' +import { log } from '../../core/log.ts' + +/** 关系字段命名模式 → 目标 form enCode 候选 */ +const RELATION_SUFFIXES = ['_space', '_id', '_code'] as const + +/** 跳过通用 ID 字段 (非关联语义) */ +const SKIP_FIELDS = new Set([ + 'f_id', 'f_tenant_id', 'f_user_id', 'f_create_user_id', 'f_modify_user_id', + 'f_en_code', 'f_flow_id', 'f_flow_task_id', 'f_version', +]) + +export interface RelationConfig { + /** 目标 form 的 enCode (e.g. "tds_space") — relationForm 必填 modelId */ + modelId: string + /** 存储字段 = 当前字段 vmodel (e.g. "f_target_space") — 必填 propsValue */ + propsValue: string + /** 回显字段 = 目标 form 的显示字段 (e.g. "f_space_name") — 必填 relationField */ + relationField: string + /** 弹窗列表列 (默认 3 列: 编码/名称) */ + columnOptions?: Array<{ label: string; value: string; enCode: string }> +} + +interface FormListItem { + id?: string + enCode?: string + fullName?: string + hasTable?: boolean +} + +/** + * 启发式: 字段名 → 候选目标 form enCode + * + * 规则: + * f_target_space → 试 "tds_space" (去掉 f_ 前缀 + 末尾 _space 后缀 + 加 appCode 前缀? 直接用剩余部分) + * 实际上: f_target_space 推断 entity = "target_space"? 不一定. 更安全: + * 1. 取 f_ 后缀去掉, 按 _ 分割, 找 "space" / "id" / "code" 在最后位置 + * 2. 候选 entity = "space" / "policy" / "product" 等 (去掉末尾关系后缀的部分) + * 3. 候选 form enCode = entity (e.g. "space") + * 4. 还要加上 appCode 前缀? web 端 relationForm 不强制 appCode 前缀, form enCode 唯一即可 + * 简化: 直接用 entity 部分当 form enCode 候选 + */ +function deriveTargetEnCodes(vmodel: string): string[] { + if (!vmodel || !vmodel.startsWith('f_')) return [] + if (SKIP_FIELDS.has(vmodel)) return [] + const body = vmodel.slice(2) // 去 f_ + // 找到末尾 _space / _id / _code 后缀 + for (const suf of RELATION_SUFFIXES) { + if (body.endsWith(suf)) { + const stem = body.slice(0, -suf.length) + if (!stem) return [] + // 候选 1: stem 整体作为 form enCode (e.g. "target_space" → "target_space") + // 候选 2: stem 拆最后一段 (e.g. "target_space" → "space"? 不, 用户可能真用 "target_space" 当 form) + // 候选 3: stem 拆首段 (e.g. "target_space" → "target"? 不一定) + // 安全做法: 全部尝试, 让 lookupFormsByEnCode 模糊匹配 + // 这里返回去重数组, 按"最匹配"排序 + return Array.from(new Set([stem, stem.replace(/_/g, '')])) + } + } + return [] +} + +/** 缓存 form list (避免每个字段都拉 list) */ +let cachedFormList: FormListItem[] | null = null +let cacheTs = 0 +const CACHE_TTL_MS = 60_000 + +/** 拉 form 列表 (含 enCode), 缓存 60s */ +async function getFormList(client: JnpfClient): Promise { + const now = Date.now() + if (cachedFormList && now - cacheTs < CACHE_TTL_MS) return cachedFormList + try { + // visualdev/Base list (webapi 镜像: loadVisualDev.getVisualDevList) + const r = await client.get<{ list?: FormListItem[] }>('/visualdev/Base', { + currentPage: 1, + pageSize: 200, + }) + const list = (r.data as { list?: FormListItem[] } | undefined)?.list || [] + cachedFormList = list.filter(f => f.enCode) + cacheTs = now + return cachedFormList + } catch (e) { + log.warn(` BS-18: 拉 form list 失败 (${(e as Error).message?.slice(0, 80)}), 跳过 relationForm 自动检测`) + return [] + } +} + +/** + * 自动检测 + 注入 relationForm 字段配置 + * + * @returns 注入的字段数 + 修改后的 aiModelList (in-place mutation) + */ +export async function autoDetectAndInjectRelationForm( + aiModelList: Array<{ + tableTitle?: string + tableName?: string + isMain?: boolean + fields?: Array<{ + fieldName?: string + fieldTitle?: string + fieldComponent?: string + fieldRelationConfig?: RelationConfig + }> + }>, +): Promise { + let injected = 0 + // 内部创建 client, 不依赖调用方 (create.ts 早期阶段 c 未初始化) + const client = await JnpfClient.fromToken() + const formList = await getFormList(client) + if (formList.length === 0) return 0 + + for (const m of aiModelList) { + if (!Array.isArray(m?.fields)) continue + for (const fld of m.fields) { + // 用户已显式给 fieldRelationConfig → 跳过 (B-13 已处理) + if (fld?.fieldRelationConfig?.modelId) continue + + const vmodel = fld?.fieldName + if (!vmodel) continue + + // 启发式推断候选 form enCode + const candidates = deriveTargetEnCodes(vmodel) + if (candidates.length === 0) continue + + // 在 form list 找匹配 (按 enCode 精确匹配 → 包含匹配) + let matched: FormListItem | null = null + for (const cand of candidates) { + matched = formList.find(f => f.enCode === cand) || null + if (matched) break + } + // 兜底: 包含匹配 (e.g. "target_space" → 找 enCode 含 "target_space" 的) + if (!matched) { + for (const cand of candidates) { + matched = formList.find(f => f.enCode && f.enCode.includes(cand)) || null + if (matched) break + } + } + if (!matched) continue + + // 推断 relationField (回显字段) - 默认目标 form 的 f_*_name (镜像 web 拖拽 relationForm 默认行为) + // 简化: 优先 f__name, 否则 f__code, 否则 f_full_name, 否则 f_name + const targetEnc = matched.enCode || '' + const stem = targetEnc.replace(/^tds_/, '').replace(/^jxcj_/, '') // 去 appCode 前缀 + const candidateRelationFields = [ + `f_${stem}_name`, + `f_${targetEnc}_name`, + `f_${stem}_code`, + `f_full_name`, + `f_name`, + ] + // 直接取第一个 (web 端允许用户后改) + const relationField = candidateRelationFields[0] + + // 注入 fieldRelationConfig (让 B-13 逻辑接管注入 formData 字段) + fld.fieldRelationConfig = { + modelId: targetEnc, + propsValue: vmodel, + relationField, + columnOptions: [ + { label: '编码', value: `f_${stem}_code`, enCode: `f_${stem}_code` }, + { label: '名称', value: relationField, enCode: relationField }, + ], + } + // 关键: 显式设 fieldComponent='relationForm', 否则 buildFormData (formUtils.ts:19) + // 会 fallback 到 componentList[0] = input, 我们刚注入的 relationForm 就丢了 + if (!fld.fieldComponent) { + fld.fieldComponent = 'relationForm' + } + injected++ + log.info(` BS-18: ${vmodel} → relationForm → ${targetEnc} (relationField=${relationField})`) + } + } + + if (injected > 0) { + log.info(` BS-18: 自动注入 ${injected} 个 relationForm 字段 (镜像 web FormGenerator.vue:272-276)`) + } + return injected +} \ No newline at end of file diff --git a/src/commands/form/create.ts b/src/commands/form/create.ts index e2f7754..8d23660 100644 --- a/src/commands/form/create.ts +++ b/src/commands/form/create.ts @@ -163,6 +163,15 @@ export function buildFormCreateCommand(): Command { if (!opts.fullName) opts.fullName = aiModelList[0].tableTitle || opts.enCode log.info(` enCode: ${opts.enCode} fullName: ${opts.fullName}`) log.info(` 表数: ${aiModelList.length} (主表 ${aiModelList[0].fields?.length || 0} 字段)`) + + // BS-18 fix (2026-06-27): relationForm 字段自动检测 + // 启发式: 字段名符合 *_space / *_id / *_code 模式 → 拉 form list 匹配 enCode → + // 自动注入 fieldRelationConfig + fieldComponent='relationForm' + // 镜像 web FormGenerator.vue:272-276 校验逻辑反向应用 (web 校验"必填", 我们主动填) + // 解决: tds_product 5 个 *_space 字段之前默认变 input (CLAUDE.md §6 违反) + const { autoDetectAndInjectRelationForm } = await import('./_relationForm_auto.ts') + await autoDetectAndInjectRelationForm(aiModelList) + // 走 buildFormData (同 --ai 路径) await initBuildFormData() const bfdResult = webBuildFormData(aiModelList)