feat(oa): add condition field helpers and rules-match tests

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-07-15 16:59:08 +08:00
1 parent e22be666f0
commit e1c41af308
2 files changed
+186

No files matched your search

@@ -0,0 +1,153 @@
/** 允许「参与流程条件分支」的 FormCreate type */
export const CONDITION_ELIGIBLE_TYPES = new Set([
'inputNumber',
'number',
'money',
'radio',
'select',
'datePicker',
'date'
])
export type ConditionOp = '>' | '>=' | '<' | '<=' | '==' | '!='
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 === '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[] = []
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 || 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<string> {
const refs = new Set<string>()
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
}
/** 发布/保存前:useInCondition ⇒ $required / required = true;写回顶层 useInCondition */
export function normalizeSchemaConditionRequired(schema: unknown): {
rule: unknown[]
option: Record<string, unknown>
} {
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 || 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
}
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<string, string> = {
'>': '大于',
'>=': '大于等于',
'<': '小于',
'<=': '小于等于',
'==': '等于',
'!=': '不等于'
}
if (!rules?.length) return '未配置'
return rules
.map((r) => `${titleOf(r.field)} ${opLabel[r.op] || r.op} ${r.value ?? '?'}`)
.join(' 且 ')
}