fix: 修复权重指示器显示错误的分类数量问题

- 修改 SegmentWeightIndicator 只统计实际选择的分类数量
- 对于按顺序匹配规则,只显示用户选择的分类权重统计
- 传递 segmentMatchingRule 参数以获取准确的分类选择信息
- 解决显示'4 分类'但实际只选择2个分类的问题
This commit is contained in:
imeepos 2025-07-25 17:30:40 +08:00
parent 68124b92e9
commit 076878287d
2 changed files with 28 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import React, { useState, useEffect } from 'react';
import { CogIcon, ArrowPathIcon } from '@heroicons/react/24/outline';
import { TemplateSegmentWeightHelper } from '../../types/template';
import { TemplateSegmentWeightHelper, SegmentMatchingRuleHelper } from '../../types/template';
import { TemplateSegmentWeightService } from '../../services/templateSegmentWeightService';
interface SegmentWeightIndicatorProps {
@ -8,6 +8,8 @@ interface SegmentWeightIndicatorProps {
templateId: string;
/** 轨道片段ID */
trackSegmentId: string;
/** 片段匹配规则 - 用于确定实际选择的分类 */
segmentMatchingRule?: any;
/** 是否禁用编辑 */
disabled?: boolean;
/** 点击编辑按钮的回调 */
@ -25,6 +27,7 @@ interface SegmentWeightIndicatorProps {
export const SegmentWeightIndicator: React.FC<SegmentWeightIndicatorProps> = ({
templateId,
trackSegmentId,
segmentMatchingRule,
disabled = false,
onEditClick,
onWeightsUpdated,
@ -53,8 +56,27 @@ export const SegmentWeightIndicator: React.FC<SegmentWeightIndicatorProps> = ({
setHasCustomWeights(hasCustom);
// 计算权重摘要
const weightValues = Object.values(weights);
// 计算权重摘要 - 只统计实际选择的分类
let relevantWeights: Record<string, number> = {};
if (segmentMatchingRule && SegmentMatchingRuleHelper.isPriorityOrder(segmentMatchingRule)) {
// 对于按顺序匹配规则,只统计选择的分类
const selectedCategoryIds = typeof segmentMatchingRule === 'object' && 'PriorityOrder' in segmentMatchingRule
? segmentMatchingRule.PriorityOrder.category_ids
: [];
// 只包含选择的分类的权重
relevantWeights = Object.fromEntries(
Object.entries(weights).filter(([classificationId]) =>
selectedCategoryIds.includes(classificationId)
)
);
} else {
// 对于其他规则类型,使用所有权重
relevantWeights = weights;
}
const weightValues = Object.values(relevantWeights);
if (weightValues.length > 0) {
const totalClassifications = weightValues.length;
const averageWeight = weightValues.reduce((sum, weight) => sum + weight, 0) / totalClassifications;
@ -65,6 +87,8 @@ export const SegmentWeightIndicator: React.FC<SegmentWeightIndicatorProps> = ({
averageWeight: Math.round(averageWeight * 10) / 10,
maxWeight,
});
} else {
setWeightSummary(null);
}
} catch (error) {
console.error('加载权重信息失败:', error);

View File

@ -795,6 +795,7 @@ export const TemplateDetailModal: React.FC<TemplateDetailModalProps> = ({
<SegmentWeightIndicator
templateId={currentTemplate.id}
trackSegmentId={segment.id}
segmentMatchingRule={segment.matching_rule}
onEditClick={() => toggleWeightEditor(segment.id)}
onWeightsUpdated={handleWeightsUpdated}
className="inline-block"