import { useDeleteConfirmation } from './useDeleteConfirmation'; import { useEffect, useRef, useState } from 'react'; import { Alert, Button, Form, Input, Modal, Select } from 'antd'; import { ArrowDownOutlined, ArrowUpOutlined, PlusOutlined } from '@ant-design/icons'; import { readableSelectPopup } from './readableSelectPopup'; import { blankCondition, conditionOperators, describeResultFormula, type ResultFormula } from './resultFormulas'; import { ChineseFormulaEditor } from './ChineseFormulaEditor'; import { compileChineseFormula, rowFormulaFields } from './formulaLanguage'; import { ruleObject as obj, type RuleDefinition } from './rulePresentation'; type Props = { result: ResultFormula; existing: boolean; schema: RuleDefinition; availableTargets: string[]; onCancel: () => void; onApply: (result: ResultFormula) => string | undefined }; const strings = (value: unknown): string[] => Array.isArray(value) ? value.map(String) : []; const missingLabels: Record = { PROPAGATE:'任一输入缺失,结果为空', ZERO:'空值按0参与运算', IGNORE:'忽略空项,全部为空时结果为空' }; const outcomeLabels: Record = { ONE:'取1', ZERO:'取0', NULL:'保留空白', ERROR:'停止并报错' }; export function ResultFormulaModal({ result, existing, schema, availableTargets, onCancel, onApply }: Props) { const confirmDelete = useDeleteConfirmation(); const [draft, setDraft] = useState(() => structuredClone(result)); const [error, setError] = useState(''); const errorHost = useRef(null); useEffect(() => { if (error) errorHost.current?.scrollIntoView({block:'nearest'}); }, [error]); const fields = obj(obj(schema.sourceFields)[draft.stage]), labels = obj(schema.componentLabels); const maxLength = Number(obj(schema.limits).maxFormulaLength || 500); const formulaFields = rowFormulaFields(schema,draft.stage); const baseIndex = draft.rules.findIndex(rule => !rule.when); const branchIndexes = draft.rules.flatMap((rule, index) => rule.when ? [index] : []).reverse(); const label = (key: string) => String(labels[key] || key); const update = (index: number, patch: RuleDefinition) => setDraft(current => ({ ...current, rules: current.rules.map((rule, i) => i === index ? { ...rule, ...patch } : rule) })); const condition = (index: number, patch: RuleDefinition) => update(index, { when: { ...obj(draft.rules[index].when), ...patch } }); const changeTarget = (target: string) => setDraft(current => ({ ...current, target, rules: current.rules.map(rule => ({ ...rule, target })) })); const addBranch = () => setDraft(current => ({ ...current, rules: [...current.rules, { stage: current.stage, target: current.target, formula: '', when: { field: Object.hasOwn(fields, current.stage === 'bright' ? 'applicability' : 'issueLocation') ? (current.stage === 'bright' ? 'applicability' : 'issueLocation') : Object.keys(fields)[0], operator: current.stage === 'bright' ? 'EQ' : 'CONTAINS', value: '' } }] })); const remove = (index: number) => setDraft(current => ({ ...current, rules: current.rules.filter((_, i) => i !== index) })); const moveBranch = (index: number, direction: number) => { const position = branchIndexes.indexOf(index), other = branchIndexes[position + direction]; if (other === undefined) return; setDraft(current => { const rules = current.rules.slice(); [rules[index], rules[other]] = [rules[other], rules[index]]; return { ...current, rules }; }); }; const apply = () => { if (!draft.target || !draft.rules.length) { setError('请设置计算结果及公式'); return; } if (draft.rules.some(rule => !String(rule.formula || '').trim())) { setError('请填写完整公式'); return; } if (draft.rules.some(rule => rule.when && !blankCondition(obj(rule.when).operator) && !String(obj(rule.when).value ?? '').trim())) { setError('请填写条件值'); return; } const rules: RuleDefinition[] = []; for (const rule of draft.rules) { const formula = compileChineseFormula(rule.formula,formulaFields,maxLength); if (formula.error) { setError(formula.error); return; } const next: RuleDefinition = { ...rule, formula:formula.formula }; if (rule.otherwiseFormula !== undefined) { const otherwise = compileChineseFormula(rule.otherwiseFormula,formulaFields,maxLength); if (otherwise.error) { setError(otherwise.error); return; } next.otherwiseFormula = otherwise.formula; } rules.push(next); } const failure = onApply({ ...draft,rules }); if (failure) setError(failure); }; const formulaInput = (index: number, title: string) => update(index, { formula })} fields={formulaFields} maxLength={maxLength}/>; const policies = (index: number) =>
特殊情况处理
{(['nullPolicy','zeroDenominatorPolicy', ...(strings(schema.nullResultPolicies).length ? ['nullResultPolicy'] : [])]).map(key => ({value,label:label(value)}))} onChange={changeTarget}/>}
完整公式

{describeResultFormula(draft,schema)}

{branchIndexes.length === 0 && baseFormula} {(branchIndexes.length > 0) &&

{draft.rules.some(rule=>rule.otherwiseFormula !== undefined) ? '已设置其他情况取值,最终取值以完整公式为准。' : '条件同时满足时,上方分支优先。'}

} {branchIndexes.map((index, position) => { const rule=draft.rules[index], when=obj(rule.when); return
条件 {position + 1}
({value,label:conditionOperators[value]||value}))} onChange={operator=>condition(index,{operator})}/> {!blankCondition(when.operator) && condition(index,{value:event.target.value})}/>}
{formulaInput(index,'满足条件时取值')}{policies(index)}
; })} {draft.rules.flatMap((rule,index)=>!rule.when&&index!==baseIndex?[index]:[]).map(index=>
{formulaInput(index,'后续覆盖公式')}{policies(index)}
)} {branchIndexes.length > 0 && baseFormula} ; }