diff --git a/apps/desktop/src/components/template/SegmentMatchingRuleEditor.tsx b/apps/desktop/src/components/template/SegmentMatchingRuleEditor.tsx index 3ee23ff..fc3ada6 100644 --- a/apps/desktop/src/components/template/SegmentMatchingRuleEditor.tsx +++ b/apps/desktop/src/components/template/SegmentMatchingRuleEditor.tsx @@ -31,9 +31,7 @@ export const SegmentMatchingRuleEditor: React.FC const [error, setError] = useState(null); // 权重配置相关状态 - const [currentWeights, setCurrentWeights] = useState>({}); const [editingWeights, setEditingWeights] = useState>({}); - const [weightLoading, setWeightLoading] = useState(false); const { updateSegmentMatchingRule } = useTemplateStore(); @@ -59,17 +57,13 @@ export const SegmentMatchingRuleEditor: React.FC if (!templateId) return; try { - setWeightLoading(true); const weights = await TemplateSegmentWeightService.getSegmentWeightsWithDefaults( templateId, segmentId ); - setCurrentWeights(weights); setEditingWeights({ ...weights }); } catch (error) { console.error('Failed to load weight data:', error); - } finally { - setWeightLoading(false); } }; @@ -107,12 +101,23 @@ export const SegmentMatchingRuleEditor: React.FC // 如果是按顺序匹配规则且有模板ID,同时保存权重配置 if (SegmentMatchingRuleHelper.isPriorityOrder(editingRule) && templateId) { try { + // 获取当前选中的分类ID + const selectedCategoryIds = typeof editingRule === 'object' && 'PriorityOrder' in editingRule + ? editingRule.PriorityOrder.category_ids + : []; + + // 只保存选中分类的权重,过滤掉未选中的分类 + const selectedWeights = Object.fromEntries( + Object.entries(editingWeights).filter(([classificationId]) => + selectedCategoryIds.includes(classificationId) + ) + ); + await TemplateSegmentWeightService.setSegmentWeights( templateId, segmentId, - editingWeights + selectedWeights ); - setCurrentWeights({ ...editingWeights }); } catch (weightError) { console.error('保存权重配置失败:', weightError); // 权重保存失败不阻止规则保存 @@ -168,9 +173,23 @@ export const SegmentMatchingRuleEditor: React.FC if (isSelected) { // 添加分类ID newCategoryIds = [...currentCategoryIds, categoryId]; + // 为新选中的分类设置默认权重(如果还没有的话) + if (!editingWeights[categoryId]) { + const classification = aiClassifications.find(c => c.id === categoryId); + setEditingWeights(prev => ({ + ...prev, + [categoryId]: classification?.weight || 50 + })); + } } else { // 移除分类ID newCategoryIds = currentCategoryIds.filter(id => id !== categoryId); + // 从权重配置中移除未选中的分类 + setEditingWeights(prev => { + const newWeights = { ...prev }; + delete newWeights[categoryId]; + return newWeights; + }); } setEditingRule(SegmentMatchingRuleHelper.createPriorityOrder(newCategoryIds)); @@ -189,12 +208,7 @@ export const SegmentMatchingRuleEditor: React.FC return weight >= 0 && weight <= 100; }; - const getWeightDisplayInfo = (weight: number) => { - if (weight >= 80) return { text: '高', colorClass: 'text-green-600' }; - if (weight >= 60) return { text: '中', colorClass: 'text-yellow-600' }; - if (weight >= 40) return { text: '低', colorClass: 'text-orange-600' }; - return { text: '极低', colorClass: 'text-red-600' }; - }; + const getCurrentRuleType = (rule: SegmentMatchingRule): string => { if (SegmentMatchingRuleHelper.isFixedMaterial(rule)) { @@ -325,7 +339,7 @@ export const SegmentMatchingRuleEditor: React.FC const isSelected = currentCategoryIds.includes(classification.id); return ( -