优化:完善分类规则与Excel配置维护并加速查询翻页
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { useContext, useRef, useState, type ReactNode } from 'react';
|
||||
import { Alert, App, AutoComplete, Button, Input, Modal, Select, Space, Tag } from 'antd';
|
||||
import { DeleteOutlined, PlusOutlined } from '@ant-design/icons';
|
||||
import { QualityTable as Table } from './QualityTable';
|
||||
import { api } from './api';
|
||||
import { MappingEditorContext } from './MappingEditorContext';
|
||||
import { classificationConditions, editClassification, type ClassificationCondition, type ClassificationSelection } from './classificationMaintenance';
|
||||
|
||||
const labels: Record<string,string> = {'@department':'产业部','@product':'产品','PDT代码':'PDT代码','销售片区':'销售区域'};
|
||||
export function ClassificationRuleEditor({ children, onUnauthorized }: {
|
||||
children: (edit: (selection: ClassificationSelection)=>void)=>ReactNode;
|
||||
onUnauthorized: (error: unknown)=>boolean;
|
||||
}) {
|
||||
const context=useContext(MappingEditorContext);
|
||||
const {message}=App.useApp();
|
||||
const [selection,setSelection]=useState<ClassificationSelection|null>(null);
|
||||
const [conditions,setConditions]=useState<ClassificationCondition[]>([]);
|
||||
const [stage,setStage]=useState('all');const [target,setTarget]=useState('');const [name,setName]=useState('');
|
||||
const [preview,setPreview]=useState<Record<string,unknown>|null>(null);
|
||||
const project=selection?.kind==='projectGroup';
|
||||
const [busy,setBusy]=useState(false);const [error,setError]=useState('');const revision=useRef(0);
|
||||
const resetPreview=()=>{revision.current++;setPreview(null);setError('');};
|
||||
const open=(next:ClassificationSelection)=>{
|
||||
setSelection(next);setConditions(classificationConditions(next.row));setStage('all');setTarget(next.kind==='projectGroup'?String((context?.definition.projectGroupLookup as Record<string,unknown>|undefined)?.[String(next.row.sourceKey)]??''):'');setName('');resetPreview();
|
||||
};
|
||||
const validate=()=>{
|
||||
if(!context?.ready||!selection)throw new Error('请等待当前配置读取完成');
|
||||
return editClassification(context.definition,selection,target,context.defaultRules,{conditions,stage,name});
|
||||
};
|
||||
const inspect=async()=>{
|
||||
const generation=++revision.current;setError('');setPreview(null);
|
||||
try{
|
||||
validate();setBusy(true);
|
||||
const result=await api.previewClassification({stage,target:target.trim(),conditions});
|
||||
if(generation===revision.current)setPreview(result);
|
||||
}catch(value){if(generation===revision.current&&!onUnauthorized(value))setError(value instanceof Error?value.message:'影响预览失败');}
|
||||
finally{if(generation===revision.current)setBusy(false);}
|
||||
};
|
||||
const save=()=>{
|
||||
try{if(!project&&!preview)throw new Error('请先预览当前条件的影响范围');const definition=validate();context!.update(definition);setSelection(null);message.success(project?'对应关系已加入当前编辑,保存并更新报表后生效':'规则已加入条件匹配,保存并更新报表后统一生效');}
|
||||
catch(value){setError(value instanceof Error?value.message:'规则保存失败');}
|
||||
};
|
||||
return <>{children(open)}<Modal className="management-modal classification-rule-modal" width={880} open={!!selection}
|
||||
title={project?'编辑项目组分类':'编辑业务分组分类'} okText={project?'保存到基础对应':'保存到条件匹配'} okButtonProps={{disabled:busy||(!project&&!preview)||!context?.ready}}
|
||||
onOk={save} onCancel={()=>{revision.current++;setBusy(false);setSelection(null);}}>
|
||||
{project?<p>客户代码 {String(selection?.row.customerCode)} · 业务分组 {String(selection?.row.businessGroup)}</p>:<><div className="classification-rule-fields">
|
||||
<label>规则名称<Input aria-label="分类规则名称" value={name} maxLength={160} placeholder="默认使用业务分组名称" onChange={e=>{setName(e.target.value);}}/></label>
|
||||
<label>适用范围<Select aria-label="分类适用范围" value={stage} options={[{value:'all',label:'黑片和亮片'},{value:'black',label:'黑片'},{value:'bright',label:'亮片'}]} onChange={value=>{setStage(value);resetPreview();setBusy(false);}}/></label>
|
||||
</div>
|
||||
<div className="classification-condition-heading"><strong>匹配条件</strong><Select aria-label="添加分类条件" placeholder="添加条件" value={undefined} style={{width:150}}
|
||||
options={Object.entries(labels).filter(([key])=>!conditions.some(c=>c.field===key)).map(([value,label])=>({value,label}))}
|
||||
disabled={conditions.length===4} suffixIcon={<PlusOutlined/>} onChange={field=>{setConditions([...conditions,classificationConditions(selection!.row).find(c=>c.field===field)!]);resetPreview();setBusy(false);}}/></div>
|
||||
<div className="classification-conditions">{conditions.map(condition=><div className="classification-condition-row" key={condition.field}>
|
||||
<span>{labels[condition.field]}</span><Select aria-label={`${labels[condition.field]}匹配方式`} value={condition.operator} style={{width:110}}
|
||||
options={[{value:'EQUALS',label:'等于'},{value:'IS_BLANK',label:'为空'}]} onChange={operator=>{setConditions(conditions.map(c=>c===condition?{...c,operator,value:operator==='IS_BLANK'?'':c.value}:c));resetPreview();setBusy(false);}}/>
|
||||
<Input aria-label={`${labels[condition.field]}匹配值`} disabled={condition.operator==='IS_BLANK'} value={condition.value} maxLength={200}
|
||||
placeholder={condition.operator==='IS_BLANK'?'空值':'填写匹配值'} onChange={e=>{setConditions(conditions.map(c=>c===condition?{...c,value:e.target.value}:c));resetPreview();setBusy(false);}}/>
|
||||
<Button aria-label={`移除${labels[condition.field]}条件`} icon={<DeleteOutlined/>} disabled={conditions.length===1} onClick={()=>{setConditions(conditions.filter(c=>c!==condition));resetPreview();setBusy(false);}}>移除</Button>
|
||||
</div>)}</div></>}
|
||||
<label className="classification-target">{project?'项目组':'业务分组'}<AutoComplete aria-label={project?'目标项目组':'目标业务分组'} value={target} maxLength={160} options={(project?context?.projectGroups??[]:context?.groups??[]).map(value=>({value}))}
|
||||
filterOption={(input,option)=>String(option?.value).includes(input)} placeholder={project?'选择项目组':'选择业务分组'} onChange={value=>{setTarget(value);resetPreview();setBusy(false);}}/></label>
|
||||
{!project&&<div className="classification-preview-action"><span>保留条件同时满足时匹配,覆盖全部月份和来源文件。</span><Button loading={busy} onClick={()=>void inspect()}>预览影响</Button></div>}
|
||||
{error&&<Alert type="error" showIcon message={error}/>}
|
||||
{preview&&<div className="classification-impact"><Space wrap><Tag color="blue">命中 {Number(preview.matchedRows).toLocaleString()} 行</Tag><Tag>总重量 {Number(preview.totalWeight).toLocaleString()} kg</Tag><Tag color={Number(preview.changedRows)>0?'orange':'default'}>当前归属不同 {Number(preview.changedRows).toLocaleString()} 行</Tag></Space>
|
||||
<Table size="small" bordered scroll={{x:720}} pagination={{pageSize:15,showSizeChanger:false}} rowKey={row=>JSON.stringify([row.stage,row.department,row.month,row.currentGroup])} dataSource={(preview.items??[]) as Record<string,unknown>[]}
|
||||
columns={[{title:'阶段',dataIndex:'stage',render:value=>value==='black'?'黑片':'亮片'},{title:'产业部',dataIndex:'department'},{title:'月份',dataIndex:'month'},{title:'当前业务分组',dataIndex:'currentGroup',render:value=>value||'未分类'},{title:'行数',dataIndex:'rowCount',align:'right'},{title:'目标业务分组',render:()=>target}]}/>
|
||||
</div>}
|
||||
</Modal></>;
|
||||
}
|
||||
Reference in New Issue
Block a user