/* === Rule Strategy Page === */ let _globalBizTypeCache = []; async function loadBizTypes(force = false) { if (!force && _globalBizTypeCache.length > 0) return _globalBizTypeCache; try { const list = await window.apiFetch('/api/rule/biz-type/list'); _globalBizTypeCache = list.map(item => ({ value: item.code, label: item.name })); return _globalBizTypeCache; } catch (e) { console.error('加载业务类型失败:', e); return []; } } function getBizTypeLabel(code) { const found = _globalBizTypeCache.find(b => b.value === code); return found ? found.label : code; } function RuleBizTypePanel() { const [list, setList] = React.useState([]); const [showModal, setShowModal] = React.useState(false); const [editing, setEditing] = React.useState(null); const [form, setForm] = React.useState({ code: '', name: '', remark: '', sort: 0, status: 1 }); const fetchList = async () => { const data = await window.apiFetch('/api/rule/biz-type/list?include_disabled=true'); setList(data); }; React.useEffect(() => { fetchList(); }, []); const resetForm = () => { setForm({ code: '', name: '', remark: '', sort: 0, status: 1, short_circuit: 1 }); }; const openAdd = () => { resetForm(); setEditing(null); setShowModal(true); }; const openEdit = (row) => { setEditing(row); setForm({ code: row.code, name: row.name, remark: row.remark || '', sort: row.sort, status: row.status, short_circuit: (row.short_circuit ?? 1) }); setShowModal(true); }; const handleSave = async () => { if (!form.code || !form.name) { alert('请输入编码和名称'); return; } try { if (editing) { await window.apiFetch(`/api/rule/biz-type/${editing.id}`, { method: 'PUT', body: JSON.stringify(form) }); } else { await window.apiFetch('/api/rule/biz-type/add', { method: 'POST', body: JSON.stringify(form) }); } setShowModal(false); fetchList(); } catch (e) { alert(e.message || '保存失败'); } }; const handleDelete = async (id) => { if (!confirm('确定删除?删除后使用该分组的规则将无法匹配到分组名称')) return; try { await window.apiFetch(`/api/rule/biz-type/${id}`, { method: 'DELETE' }); fetchList(); } catch (e) { alert('删除失败'); } }; return (
{list.map(row => ( ))}
排序 分组编码 分组名称 备注 状态 操作
{row.sort} {row.code} {row.name} {row.remark || '-'} {row.status === 1 ? '启用' : '禁用'}
{showModal && (
setShowModal(false)}>
e.stopPropagation()}>
{editing ? '编辑' : '新增'}规则分组
setForm({...form, code: e.target.value})} placeholder="如: product_drawdown" />
setForm({...form, name: e.target.value})} placeholder="如: 产品最大回撤" />
setForm({...form, remark: e.target.value})} placeholder="说明这个分组的用途" />
setForm({...form, sort: parseInt(e.target.value)||0})} />
)}
); } function RuleConfigPanel() { const [rules, setRules] = React.useState([]); const [showModal, setShowModal] = React.useState(false); const [editingRule, setEditingRule] = React.useState(null); const [searchKeyword, setSearchKeyword] = React.useState(''); const fetchRules = async () => { try { const data = await window.apiFetch('/api/rule/config/list'); setRules(data); } catch (error) { console.error('获取规则列表失败:', error); } }; React.useEffect(() => { fetchRules(); }, []); const handleDelete = async (ruleId) => { if (!confirm('确定要删除这条规则吗?')) return; try { await window.apiFetch(`/api/rule/config/${ruleId}`, { method: 'DELETE' }); fetchRules(); } catch (error) { console.error('删除规则失败:', error); } }; const handleToggleStatus = async (rule) => { const endpoint = rule.status === 1 ? 'disable' : 'enable'; try { await window.apiFetch(`/api/rule/config/${rule.id}/${endpoint}`, { method: 'POST' }); fetchRules(); } catch (error) { console.error('切换状态失败:', error); } }; const filteredRules = rules.filter(rule => rule.rule_name.includes(searchKeyword) || rule.rule_code.includes(searchKeyword) ); return (
setSearchKeyword(e.target.value)} />
{filteredRules.map(rule => ( ))}
规则编码 规则名称 业务类型 规则表达式 状态 操作
{rule.rule_code} {rule.rule_name} {getBizTypeLabel(rule.biz_type)} {rule.rule_expr} {rule.status === 1 ? '启用' : '禁用'}
{showModal && ( setShowModal(false)} onSuccess={() => { setShowModal(false); fetchRules(); }} /> )}
); } function RuleConfigModal({ rule, onClose, onSuccess }) { const [formData, setFormData] = React.useState({ rule_code: rule?.rule_code || '', rule_name: rule?.rule_name || '', rule_expr: rule?.rule_expr || '', biz_type: rule?.biz_type || 'product_drawdown', status: rule?.status ?? 1, short_circuit: rule?.short_circuit ?? 1, remark: rule?.remark || '', }); const handleSubmit = async () => { try { const url = rule ? `/api/rule/config/${rule.id}` : '/api/rule/config/add'; const method = rule ? 'PUT' : 'POST'; await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); onSuccess(); } catch (error) { console.error('保存规则失败:', error); } }; return (
e.stopPropagation()}>
{rule ? '编辑规则' : '新增规则'}
基本信息
setFormData({ ...formData, rule_code: e.target.value })} required disabled={!!rule} />
setFormData({ ...formData, rule_name: e.target.value })} required />
规则配置