/** 允许「参与流程条件分支」的 FormCreate type */ export const CONDITION_ELIGIBLE_TYPES = new Set([ 'input', 'inputNumber', 'number', 'money', 'person', 'radio', 'select', 'datePicker', 'date' ]) /** 系统条件伪字段:发起人(非常驻表单控件) */ export const INITIATOR_CONDITION_FIELD = '__initiator__' export const INITIATOR_CONDITION_CANDIDATE: ConditionCandidate = { field: INITIATOR_CONDITION_FIELD, title: '发起人', type: 'initiator' } export type ConditionOp = '>' | '>=' | '<' | '<=' | '==' | '!=' | 'contains' export interface ConditionCandidate { field: string title: string type: string /** 单选/下拉开选项 label/value */ options?: Array<{ label: string; value: string | number }> } export function opsForFieldType(type: string): Array<{ value: ConditionOp; label: string }> { if (type === 'person' || type === 'initiator') { return [{ value: 'contains', label: '包含' }] } // 单选/下拉为离散选项,仅等于/不等于;输入框常填数字参与区间比较,与数字/金额/日期同套运算符 if (type === 'radio' || type === 'select') { return [ { value: '==', label: '等于' }, { value: '!=', label: '不等于' } ] } return [ { value: '>', label: '大于' }, { value: '>=', label: '大于等于' }, { value: '<', label: '小于' }, { value: '<=', label: '小于等于' }, { value: '==', label: '等于' }, { value: '!=', label: '不等于' } ] } function walkRules(rules: unknown[], out: any[]) { for (const r of rules) { if (!r || typeof r !== 'object') continue const rule = r as any out.push(rule) const children = rule.children || rule.props?.rule || [] if (Array.isArray(children)) walkRules(children, out) } } export function flattenFormRules(schema: unknown): any[] { if (!schema) return [] if (Array.isArray(schema)) { const out: any[] = [] walkRules(schema, out) return out } const root = schema as any const rules = root.rule || root.rules || [] const out: any[] = [] if (Array.isArray(rules)) walkRules(rules, out) return out } /** 勾选了 useInCondition 的合格字段 + 系统「发起人」 */ export function listConditionCandidates(schema: unknown): ConditionCandidate[] { const out: ConditionCandidate[] = [INITIATOR_CONDITION_CANDIDATE] for (const rule of flattenFormRules(schema)) { const type = String(rule.type || '') if (!CONDITION_ELIGIBLE_TYPES.has(type)) continue const field = String(rule.field || rule.name || '') if (!field) continue const flagged = rule.useInCondition === true ? true : rule.useInCondition === false ? false : rule.props?.useInCondition === true if (!flagged) continue const options = (rule.options || rule.props?.options || []) .map((o: any) => typeof o === 'object' ? { label: String(o.label ?? o.text ?? o.value), value: o.value } : { label: String(o), value: o } ) out.push({ field, title: String(rule.title || rule.label || field), type, options: options.length ? options : undefined }) } return out } /** 流程树中条件 rules 引用的 field 集合 */ export function collectConditionReferencedFields(flow: unknown): Set { const refs = new Set() const walk = (node: any) => { if (!node || typeof node !== 'object') return for (const g of node.conditionGroups || []) { for (const r of g.rules || []) { if (r && r.field) refs.add(String(r.field)) } if (g.child) walk(g.child) } if (node.child) walk(node.child) } const root = (flow as any)?.root?.type ? (flow as any).root : flow walk(root) return refs } /** 会锁定表单「参与条件」勾选的引用字段(排除系统伪字段) */ export function collectFormConditionReferencedFields(flow: unknown): Set { const refs = collectConditionReferencedFields(flow) refs.delete(INITIATOR_CONDITION_FIELD) return refs } /** 发布/保存前:useInCondition ⇒ $required / required = true;写回顶层 useInCondition */ export function normalizeSchemaConditionRequired(schema: unknown): { rule: unknown[] option: Record } { const plain = JSON.parse(JSON.stringify(schema || {})) const rules = Array.isArray(plain.rule) ? plain.rule : Array.isArray(plain) ? plain : [] const walk = (list: any[]) => { for (const rule of list) { const flagged = rule.useInCondition === true ? true : rule.useInCondition === false ? false : rule.props?.useInCondition === true if (flagged) { rule.useInCondition = true if (rule.props && typeof rule.props === 'object') { rule.props.useInCondition = true } rule.$required = true rule.required = true } else if (rule.useInCondition === false || rule.props?.useInCondition === false) { rule.useInCondition = false if (rule.props && typeof rule.props === 'object') { rule.props.useInCondition = false } } const children = rule.children || rule.props?.rule if (Array.isArray(children)) walk(children) } } walk(rules) return { rule: rules, option: plain.option && typeof plain.option === 'object' ? plain.option : {} } } export function formatConditionPreview( rules: Array<{ field: string; op: string; value: unknown }>, candidates: ConditionCandidate[] ): string { const titleOf = (f: string) => candidates.find((c) => c.field === f)?.title || f const opLabel: Record = { '>': '大于', '>=': '大于等于', '<': '小于', '<=': '小于等于', '==': '等于', '!=': '不等于', contains: '包含' } if (!rules?.length) return '未配置' return rules .map((r) => { const valueText = Array.isArray(r.value) ? r.value.join('、') : (r.value ?? '?') return `${titleOf(r.field)} ${opLabel[r.op] || r.op} ${valueText}` }) .join(' 且 ') }