diff --git a/backend/tests/test_oa_engine.py b/backend/tests/test_oa_engine.py index 44f3f2de..f9e3ed10 100644 --- a/backend/tests/test_oa_engine.py +++ b/backend/tests/test_oa_engine.py @@ -43,6 +43,39 @@ class TestOaEngineHelpers(unittest.TestCase): g2 = _match_condition_group(groups, {"days": 1}) self.assertEqual(g2["name"], "default") + def test_condition_match_rules_list(self): + groups = [ + { + "name": "gt3", + "rules": [{"field": "days", "op": ">", "value": 3}], + "child": {"id": "n1", "type": "end"}, + }, + {"name": "default", "isDefault": True, "rules": [], "child": {"id": "n2", "type": "end"}}, + ] + self.assertEqual(_match_condition_group(groups, {"days": 5})["name"], "gt3") + self.assertEqual(_match_condition_group(groups, {"days": 1})["name"], "default") + + def test_condition_match_rules_and(self): + groups = [ + { + "name": "both", + "rules": [ + {"field": "days", "op": ">", "value": 3}, + {"field": "leaveType", "op": "==", "value": "事假"}, + ], + "child": {"id": "n1", "type": "end"}, + }, + {"name": "default", "rules": [], "isDefault": True, "child": {"id": "n2", "type": "end"}}, + ] + self.assertEqual( + _match_condition_group(groups, {"days": 5, "leaveType": "事假"})["name"], + "both", + ) + self.assertEqual( + _match_condition_group(groups, {"days": 5, "leaveType": "病假"})["name"], + "default", + ) + def test_flow_node_key_order_cc_before_approval(self): root = { "id": "s", diff --git a/web-admin/src/components/oa-approval/oa-condition-fields.ts b/web-admin/src/components/oa-approval/oa-condition-fields.ts new file mode 100644 index 00000000..3323dc89 --- /dev/null +++ b/web-admin/src/components/oa-approval/oa-condition-fields.ts @@ -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 { + 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 +} + +/** 发布/保存前: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 || 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 = { + '>': '大于', + '>=': '大于等于', + '<': '小于', + '<=': '小于等于', + '==': '等于', + '!=': '不等于' + } + if (!rules?.length) return '未配置' + return rules + .map((r) => `${titleOf(r.field)} ${opLabel[r.op] || r.op} ${r.value ?? '?'}`) + .join(' 且 ') +}