feat: 从根本上解决权重配置问题

- 添加 get_segment_weights_for_categories 方法,只获取选中分类的权重
- 修改权重指示器和预览组件,使用新方法只显示实际选中的分类
- 修改权重编辑器,只加载和编辑选中分类的权重
- 确保权重配置与匹配规则完全同步
- 解决显示所有分类权重但实际只选择部分分类的根本问题
This commit is contained in:
imeepos 2025-07-25 17:47:45 +08:00
parent d7411de33d
commit bce0c30cab
6 changed files with 97 additions and 34 deletions

View File

@ -182,4 +182,30 @@ impl TemplateSegmentWeightService {
Ok(stats)
}
/// 获取指定分类的权重配置(用于按顺序匹配规则)
pub async fn get_segment_weights_for_categories(&self, template_id: &str, track_segment_id: &str, category_ids: &[String]) -> Result<HashMap<String, i32>> {
// 获取模板片段的自定义权重配置
let custom_weights = self.repository.get_weight_map_for_segment(template_id, track_segment_id).await?;
// 获取指定分类的AI分类信息
let mut final_weights = HashMap::new();
for category_id in category_ids {
// 优先使用自定义权重
if let Some(&custom_weight) = custom_weights.get(category_id) {
final_weights.insert(category_id.clone(), custom_weight);
} else {
// 如果没有自定义权重,获取全局权重
if let Ok(Some(classification)) = self.ai_classification_service.get_classification_by_id(category_id).await {
final_weights.insert(category_id.clone(), classification.weight);
} else {
// 如果分类不存在,使用默认权重
final_weights.insert(category_id.clone(), 50);
}
}
}
Ok(final_weights)
}
}

View File

@ -385,7 +385,8 @@ pub fn run() {
commands::template_segment_weight_commands::delete_template_weights,
commands::template_segment_weight_commands::update_template_segment_weight,
commands::template_segment_weight_commands::has_custom_segment_weights,
commands::template_segment_weight_commands::get_template_weight_statistics
commands::template_segment_weight_commands::get_template_weight_statistics,
commands::template_segment_weight_commands::get_segment_weights_for_categories
])
.setup(|app| {
// 初始化日志系统

View File

@ -168,3 +168,18 @@ pub async fn get_template_weight_statistics(
.await
.map_err(|e| e.to_string())
}
/// 获取指定分类的权重配置(用于按顺序匹配规则)
#[tauri::command]
pub async fn get_segment_weights_for_categories(
state: State<'_, AppState>,
template_id: String,
track_segment_id: String,
category_ids: Vec<String>,
) -> Result<HashMap<String, i32>, String> {
let service = create_template_segment_weight_service(&state);
service.get_segment_weights_for_categories(&template_id, &track_segment_id, &category_ids)
.await
.map_err(|e| e.to_string())
}

View File

@ -57,11 +57,23 @@ export const SegmentMatchingRuleEditor: React.FC<SegmentMatchingRuleEditorProps>
if (!templateId) return;
try {
const weights = await TemplateSegmentWeightService.getSegmentWeightsWithDefaults(
if (SegmentMatchingRuleHelper.isPriorityOrder(editingRule)) {
const selectedCategoryIds = typeof editingRule === 'object' && 'PriorityOrder' in editingRule
? editingRule.PriorityOrder.category_ids
: [];
if (selectedCategoryIds.length > 0) {
// 只加载选中分类的权重
const weights = await TemplateSegmentWeightService.getSegmentWeightsForCategories(
templateId,
segmentId
segmentId,
selectedCategoryIds
);
setEditingWeights({ ...weights });
} else {
setEditingWeights({});
}
}
} catch (error) {
console.error('Failed to load weight data:', error);
}

View File

@ -49,28 +49,25 @@ export const SegmentWeightIndicator: React.FC<SegmentWeightIndicatorProps> = ({
try {
setLoading(true);
const [hasCustom, weights] = await Promise.all([
TemplateSegmentWeightService.hasCustomSegmentWeights(templateId, trackSegmentId),
TemplateSegmentWeightService.getSegmentWeightsWithDefaults(templateId, trackSegmentId),
]);
const hasCustom = await TemplateSegmentWeightService.hasCustomSegmentWeights(templateId, trackSegmentId);
setHasCustomWeights(hasCustom);
// 计算权重摘要 - 只统计实际选择的分类
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)
)
if (selectedCategoryIds.length > 0) {
relevantWeights = await TemplateSegmentWeightService.getSegmentWeightsForCategories(
templateId,
trackSegmentId,
selectedCategoryIds
);
}
} else {
// 对于其他规则类型,不显示权重信息(因为不相关)
relevantWeights = {};
@ -223,26 +220,23 @@ export const WeightPreviewTooltip: React.FC<WeightPreviewTooltipProps> = ({
try {
setLoading(true);
const allWeights = await TemplateSegmentWeightService.getSegmentWeightsWithDefaults(
templateId,
trackSegmentId
);
// 过滤权重数据,只显示实际选中的分类
// 只获取实际选中的分类权重
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(allWeights).filter(([classificationId]) =>
selectedCategoryIds.includes(classificationId)
)
if (selectedCategoryIds.length > 0) {
relevantWeights = await TemplateSegmentWeightService.getSegmentWeightsForCategories(
templateId,
trackSegmentId,
selectedCategoryIds
);
}
} else {
// 对于其他规则类型,不显示权重信息
relevantWeights = {};

View File

@ -199,6 +199,21 @@ export class TemplateSegmentWeightService {
return results;
}
/**
*
*/
static async getSegmentWeightsForCategories(
templateId: string,
trackSegmentId: string,
categoryIds: string[]
): Promise<Record<string, number>> {
return await invoke('get_segment_weights_for_categories', {
templateId,
trackSegmentId,
categoryIds,
});
}
/**
*
*/