/** * commands/org/create.ts — 新建组织 (Phase D1, v3.0+ 走 webapi/permission 镜像, v2.1+ 三层接入) * * 镜像 web organize/Form.vue + 后端 OrganizeController * - PS036: 组织层级限制 * - PS013: 当前机构 id 不能与父机构 id 相同 * * v0.11e+ Phase D1: P3 命令 step machine 集成 * v3.0+ (2026-06-29 regression audit P0): 改用 webapi/permission.createOrg * * v2.1+ (2026-07-21) 三层接入 (跨命令 auto-fix 模式, 参照 dict/typecreate.ts): * L1: --verbose / --json 三模式 (commands/_output_filter.ts:setupOutputFilter) * L2: 跨命令 auto-fix pipeline * - enCode 含 `-`/`_` 自动 strip (S-X1 silent, BS-新38) * - fullName 含 space/parens 自动 soften (S-X2 silent, BS-新52) * - 已有 org 重名 → suggest 加 `` 前缀 (G-X1 suggest) * L3: 完成后落 CommandLog, 失败时也落 (含 PS013/PS036 错误) */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { validateOrgCreate } from './_validate.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { initWebapi as initPermissionWebapi, createOrg, getOrganizeList, } from '../../webapi/permission.ts' import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix (S-X1/S-X2 silent + G-X1 suggest) import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): stdout 三模式 + CommandLog 落盘 interface CreateOpts { fullName: string enCode: string parentId: string category: string enabledMark?: string sortCode?: string json?: boolean verbose?: boolean } export function buildOrgCreateCommand(): Command { const cmd = new Command('create') .description('新建组织 (v0.11e+ Phase D1 + step machine 校验)') .requiredOption('--fullName ', '组织名称') .requiredOption('--enCode ', '组织编码') .requiredOption('--parentId ', '上级组织 id (顶级传 -1)') .requiredOption('--category ', '组织类型: 0=公司 1=部门 2=小组') .option('--enabledMark ', '0/1 启用', '1') .option('--sortCode ', '排序', '0') .option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false) .option('--json', '原始 JSON 输出', false) .action(async (opts: CreateOpts) => { // v2.1+ L1: stdout 三模式 — 默认 quiet, --verbose 全部输出, --json 原始 JSON setupOutputFilter(opts) const c = await JnpfClient.fromToken() const startTime = Date.now() // v2.1+ L2 silent+assertive+suggest: 跨命令 auto-fix pipeline (S-X1/S-X2/G-X1) // - enCode 含 `-`/`_` 自动 strip (BS-新38 silent) // - fullName 含 space/parens 自动 soften (BS-新52 silent) // - 已有 org 重名 → suggest 加 `` 前缀 (启用需 fetchExistingLists) const fixCtx = await fetchExistingLists(c, c.appCode) const cmdFix = runCommandAutoFix(opts as unknown as Record, fixCtx) if (cmdFix.silent.length > 0) { Object.assign(opts, cmdFix.opts) } // v0.11e+ Phase D1: step machine 校验 (PS036 层级 + PS013 parentId) const validation = validateOrgCreate({ fullName: opts.fullName, enCode: opts.enCode, parentId: opts.parentId, category: opts.category, enabledMark: opts.enabledMark, sortCode: opts.sortCode, }) if (!validation.ok) { for (const s of validation.steps) { for (const e of s.errors) { log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`) } } // v2.1+ L3: 失败也落 CommandLog (PS013/PS036 错误供 AI 查 dacli log last) printCommandEnd({ command: 'org create', appCode: c.appCode, result: 'failure', summary: { enCode: opts.enCode, fullName: opts.fullName }, fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], error: validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '), duration_ms: Date.now() - startTime, verbose: opts.verbose, json: opts.json, }) process.exit(1) } log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`) const body = { fullName: opts.fullName, enCode: opts.enCode, parentId: opts.parentId, category: opts.category, enabledMark: parseInt(opts.enabledMark || '1', 10), sortCode: parseInt(opts.sortCode || '0', 10), } // v3.0+ P0: 走 webapi/permission.createOrg (web 端 create 函数) await initPermissionWebapi(c) const r = (await createOrg(body as any)) as { id?: string; code?: number; msg?: string } if (r.code !== 200 && r.code !== undefined) { const enhanced = enhanceFromBackend(r.msg || '', 'org', { entity: 'org' }) log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`) // v2.1+ L3: API 失败也落 CommandLog printCommandEnd({ command: 'org create', appCode: c.appCode, result: 'failure', summary: { enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId }, fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], error: r.msg || `code=${r.code}`, duration_ms: Date.now() - startTime, verbose: opts.verbose, json: opts.json, }) process.exit(1) } let id = r.id if (!id) { const listR = (await getOrganizeList({ parentId: opts.parentId, pageSize: 50 })) as | { list?: { id: string; enCode: string; fullName: string }[] } | { id: string; enCode: string; fullName: string }[] const list = Array.isArray(listR) ? listR : listR.list || [] const found = list.find( (d) => d.enCode === opts.enCode || d.fullName === opts.fullName, ) id = found?.id } log.ok(`✓ 组织创建成功 id=${id}`) if (opts.json) printJson({ ...(r as object), id }) // v2.1+ L3: 落 CommandLog + 打印摘要 + "详情: dacli log last" 提示 printCommandEnd({ command: 'org create', appCode: c.appCode, summary: { id, enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId, category: opts.category }, fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], duration_ms: Date.now() - startTime, verbose: opts.verbose, json: opts.json, }) }) installHelpHook(cmd, { purpose: '新建组织 (v0.11e+ Phase D1 + step machine 校验, 镜像 web organize/Form.vue + 后端 OrganizeController + PS013/PS036)', required: { '--fullName': '组织名称', '--enCode': '组织编码', '--parentId': '上级组织 id (顶级 -1)', '--category': '0=公司 1=部门 2=小组', }, optional: { '--enabledMark': '0/1 启用', '--sortCode': '排序' }, example: 'dacli org create --fullName=技术部 --enCode=tech_dept --parentId=-1 --category=1', source: { web: 'web/apps/jnpf-web-apps-main/src/views/permission/organize/Form.vue', api: 'POST /api/permission/Organize', ref: 'jnpf/后端/jnpf-java-boot/jnpf-permission/jnpf-permission-controller/src/main/java/jnpf/controller/OrganizeController.java', }, notes: [ 'v0.11e+ step machine (镜像 web organize/Form.vue + PS013/PS036):', ' step 0 基础信息: fullName 必填 + enCode 必填 + parentId 必填 + category ∈ {0,1,2}', ], }) return cmd }