Commit 3d8ae864 by zhouhaibao

fix(processor): 统一由conservation check追踪分摊差值

- 将total_input_fee和row_count累加提前至分摊逻辑之前,确保无论分摊成功与否都计入
- 移除多处直接累加unmatched['rent_utilities']的代码,改由conservation check统一追踪
- allocation_results为空时添加warning日志说明费用将由conservation check追踪
- 直接匹配中嘀加过滤后全空时设置ratio_applied=True,避免100%兜底硬塞给非嘀加行
- 园区店匹配失败、比例和为0、flow_ratio_list空等场景统一改为跳过并记录日志
- 避免费用在分摊路径和unmatched累加中被双算
1 parent 901fe3c7
...@@ -3539,6 +3539,10 @@ class DataProcessor: ...@@ -3539,6 +3539,10 @@ class DataProcessor:
flow_names_list = [t['name'] for t in alloc_targets if t['type'] == 'flow'] flow_names_list = [t['name'] for t in alloc_targets if t['type'] == 'flow']
park_names_list = [t['name'] for t in alloc_targets if t['type'] == 'park'] park_names_list = [t['name'] for t in alloc_targets if t['type'] == 'park']
# 先加进 total_input_fee(无论分摊是否成功都加,让 conservation check 追踪差值)
total_input_fee += merged_total_fee
row_count += 1
gmv_data = query_gmv_by_targets(db, task_id, BIZ_TYPE_1_1, gmv_data = query_gmv_by_targets(db, task_id, BIZ_TYPE_1_1,
flow_names=flow_names_list, flow_names=flow_names_list,
park_names=park_names_list) park_names=park_names_list)
...@@ -3558,9 +3562,10 @@ class DataProcessor: ...@@ -3558,9 +3562,10 @@ class DataProcessor:
"__gmv_allocated__": True, "__gmv_allocated__": True,
"target_type": alloc['type'], "target_type": alloc['type'],
}) })
total_input_fee += merged_total_fee
row_count += 1
logger.info(f"[房租水电][合并行分摊] targets={len(alloc_targets)}, total_fee={merged_total_fee}, results={len(allocation_results)}") logger.info(f"[房租水电][合并行分摊] targets={len(alloc_targets)}, total_fee={merged_total_fee}, results={len(allocation_results)}")
else:
# 2026-10 精简:allocation_results 空(嘀加过滤等)→ 进 conservation check 算差值
logger.warning(f"[房租水电][合并行分摊] allocation_results空, fee={merged_total_fee} 将由 conservation check 追踪")
# 处理独立费用行(每个行独立分摊) # 处理独立费用行(每个行独立分摊)
for ind_row in independent_rows: for ind_row in independent_rows:
...@@ -3570,6 +3575,10 @@ class DataProcessor: ...@@ -3570,6 +3575,10 @@ class DataProcessor:
if ind_target and ind_total > 0 and task_id: if ind_target and ind_total > 0 and task_id:
ind_alloc_targets = parse_allocation_targets(ind_target, flow_names_set, park_to_flows) ind_alloc_targets = parse_allocation_targets(ind_target, flow_names_set, park_to_flows)
if ind_alloc_targets: if ind_alloc_targets:
# 先加进 total_input_fee(无论分摊是否成功都加)
total_input_fee += ind_total
row_count += 1
ind_flow_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'flow'] ind_flow_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'flow']
ind_park_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'park'] ind_park_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'park']
...@@ -3590,9 +3599,9 @@ class DataProcessor: ...@@ -3590,9 +3599,9 @@ class DataProcessor:
"__gmv_allocated__": True, "__gmv_allocated__": True,
"target_type": ind_alloc['type'], "target_type": ind_alloc['type'],
}) })
total_input_fee += ind_total
row_count += 1
logger.info(f"[房租水电][独立行分摊] target='{ind_target}', fee={ind_total}, results={len(ind_results)}") logger.info(f"[房租水电][独立行分摊] target='{ind_target}', fee={ind_total}, results={len(ind_results)}")
else:
logger.warning(f"[房租水电][独立行分摊] ind_results空, fee={ind_total} 将由 conservation check 追踪")
continue continue
elif merged_total_fee > 0 and len(collected_targets) == 1: elif merged_total_fee > 0 and len(collected_targets) == 1:
...@@ -3619,6 +3628,10 @@ class DataProcessor: ...@@ -3619,6 +3628,10 @@ class DataProcessor:
if ind_target and ind_total > 0 and task_id: if ind_target and ind_total > 0 and task_id:
ind_alloc_targets = parse_allocation_targets(ind_target, flow_names_set, park_to_flows) ind_alloc_targets = parse_allocation_targets(ind_target, flow_names_set, park_to_flows)
if ind_alloc_targets: if ind_alloc_targets:
# 先加进 total_input_fee(无论分摊是否成功都加)
total_input_fee += ind_total
row_count += 1
ind_flow_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'flow'] ind_flow_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'flow']
ind_park_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'park'] ind_park_names = [t['name'] for t in ind_alloc_targets if t['type'] == 'park']
...@@ -3639,9 +3652,9 @@ class DataProcessor: ...@@ -3639,9 +3652,9 @@ class DataProcessor:
"__gmv_allocated__": True, "__gmv_allocated__": True,
"target_type": ind_alloc['type'], "target_type": ind_alloc['type'],
}) })
total_input_fee += ind_total
row_count += 1
logger.info(f"[房租水电][独立行分摊] target='{ind_target}', fee={ind_total}, results={len(ind_results)}") logger.info(f"[房租水电][独立行分摊] target='{ind_target}', fee={ind_total}, results={len(ind_results)}")
else:
logger.warning(f"[房租水电][独立行分摊] ind_results空, fee={ind_total} 将由 conservation check 追踪")
continue continue
...@@ -3653,6 +3666,10 @@ class DataProcessor: ...@@ -3653,6 +3666,10 @@ class DataProcessor:
logger.info(f"[房租水电][GMV分摊路径] 行{idx}: target='{target}', alloc_targets={alloc_targets}") logger.info(f"[房租水电][GMV分摊路径] 行{idx}: target='{target}', alloc_targets={alloc_targets}")
if alloc_targets: if alloc_targets:
# 先加进 total_input_fee(无论分摊是否成功都加,让 conservation check 追踪差值)
total_input_fee += total_fee
row_count += 1
flow_names = [t['name'] for t in alloc_targets if t['type'] == 'flow'] flow_names = [t['name'] for t in alloc_targets if t['type'] == 'flow']
park_names = [t['name'] for t in alloc_targets if t['type'] == 'park'] park_names = [t['name'] for t in alloc_targets if t['type'] == 'park']
...@@ -3674,10 +3691,10 @@ class DataProcessor: ...@@ -3674,10 +3691,10 @@ class DataProcessor:
"target_type": alloc['type'], "target_type": alloc['type'],
}) })
gmv_allocated = True gmv_allocated = True
total_input_fee += total_fee
row_count += 1
merged_flag = " [合并行]" if is_merged_row else "" merged_flag = " [合并行]" if is_merged_row else ""
logger.info(f"[房租水电][GMV分摊完成]{merged_flag} target='{target}', fee={total_fee}, targets={len(alloc_targets)}, allocated={len(allocation_results)}") logger.info(f"[房租水电][GMV分摊完成]{merged_flag} target='{target}', fee={total_fee}, targets={len(alloc_targets)}, allocated={len(allocation_results)}")
else:
logger.warning(f"[房租水电][GMV分摊路径] allocation_results空, fee={total_fee} 将由 conservation check 追踪")
except Exception as e: except Exception as e:
logger.warning(f"[房租水电][GMV分摊] 分摊失败,回退原有逻辑: {e}") logger.warning(f"[房租水电][GMV分摊] 分摊失败,回退原有逻辑: {e}")
...@@ -3746,9 +3763,8 @@ class DataProcessor: ...@@ -3746,9 +3763,8 @@ class DataProcessor:
else: else:
# 尝试模糊匹配 # 尝试模糊匹配
if park_fail_count < 10: if park_fail_count < 10:
logger.warning(f"[房租水电] 园区店匹配失败: store='{target}'") logger.warning(f"[房租水电] 园区店匹配失败: store='{target}' → 跳过,fee 将由 conservation check 追踪")
park_fail_count += 1 park_fail_count += 1
unmatched['rent_utilities'] += total_fee
continue continue
key = (ps, BIZ_TYPE_1_1) key = (ps, BIZ_TYPE_1_1)
...@@ -3819,6 +3835,12 @@ class DataProcessor: ...@@ -3819,6 +3835,12 @@ class DataProcessor:
}) })
ratio_applied = True ratio_applied = True
logger.info(f"[直接匹配](嘀加归一化) flow={original_flow}, brand={brand}, raw_r={r}, norm_r={normalized_r:.4f}, fee_share={fee_share}") logger.info(f"[直接匹配](嘀加归一化) flow={original_flow}, brand={brand}, raw_r={r}, norm_r={normalized_r:.4f}, fee_share={fee_share}")
else:
# ratio_rows 非空但嘀加过滤后全空 → 全部品牌都不是嘀加自营
# 这笔费用不进 raw_records(避免 100% 兜底硬塞给非嘀加行),
# 差值由下面的 conservation check 统一追踪进 unmatched
logger.warning(f"[直接匹配] flow={original_flow} ratio_rows有{len(ratio_rows)}行但嘀加过滤后全空 → 跳过,fee={fee}")
ratio_applied = True # 已处理(跳过),跳过下面的 100% 兜底
if not ratio_applied: if not ratio_applied:
logger.warning(f"[直接匹配] flow={original_flow}, biz_type={biz_type} 无gmv_ratio, 回退100%分配") logger.warning(f"[直接匹配] flow={original_flow}, biz_type={biz_type} 无gmv_ratio, 回退100%分配")
...@@ -3842,10 +3864,9 @@ class DataProcessor: ...@@ -3842,10 +3864,9 @@ class DataProcessor:
if not matching_flows: if not matching_flows:
park_fail_count += 1 park_fail_count += 1
failed_count += 1 failed_count += 1
logger.warning(f"[园区店回退] ps='{ps}' 无匹配业务类型={biz_type}的流量方") logger.warning(f"[园区店回退] ps='{ps}' 无匹配业务类型={biz_type}的流量方 → 跳过,fee 将由 conservation check 追踪")
if len(failed_stores) < 10: if len(failed_stores) < 10:
failed_stores.append(ps) failed_stores.append(ps)
unmatched['rent_utilities'] += fee
continue continue
# 优先查找 gmv_ratio_park # 优先查找 gmv_ratio_park
...@@ -3881,8 +3902,8 @@ class DataProcessor: ...@@ -3881,8 +3902,8 @@ class DataProcessor:
park_allocated_total += allocated_fee park_allocated_total += allocated_fee
logger.info(f"[园区店回退] {ratio_type}(归一化): ps='{ps}', {len(raw_ratios)}行, sum_r={total_r:.4f}, fee={allocated_fee}") logger.info(f"[园区店回退] {ratio_type}(归一化): ps='{ps}', {len(raw_ratios)}行, sum_r={total_r:.4f}, fee={allocated_fee}")
else: else:
# 比例和为0 → 直接进 unmatched # 比例和为0 → 跳过,fee 将由 conservation check 追踪进 unmatched
unmatched['rent_utilities'] += fee logger.warning(f"[园区店回退] ps='{ps}' sum_r=0 → 跳过,fee={fee}")
continue continue
else: else:
# 回退:使用 gmv_ratio 按流量方分配 # 回退:使用 gmv_ratio 按流量方分配
...@@ -3923,15 +3944,15 @@ class DataProcessor: ...@@ -3923,15 +3944,15 @@ class DataProcessor:
for _, _, r in flow_ratio_list for _, _, r in flow_ratio_list
) )
else: else:
# 比例数据完全不存在,跳过该桶(不分均) # flow_ratio_list 空(通常是嘀加过滤后全非嘀加)→ 跳过
logger.warning(f"[园区店回退] gmv_ratio也不存在, 跳过该桶: ps='{ps}', bt={biz_type}, fee={fee}") # fee 由 conservation check 统一追踪进 unmatched,避免双算
logger.warning(f"[园区店回退] gmv_ratio也不存在或嘀加过滤后全空, 跳过该桶: ps='{ps}', bt={biz_type}, fee={fee}")
park_fail_count += 1 park_fail_count += 1
failed_count += 1 failed_count += 1
if len(failed_stores) < 10: if len(failed_stores) < 10:
failed_stores.append(ps) failed_stores.append(ps)
available_keys = [(k, len(v)) for k, v in park_rows.items() if ps in str(k[0])] available_keys = [(k, len(v)) for k, v in park_rows.items() if ps in str(k[0])]
logger.warning(f"[园区店回退] 诊断: park_rows中含'{ps}'的key={available_keys}") logger.warning(f"[园区店回退] 诊断: park_rows中含'{ps}'的key={available_keys}")
unmatched['rent_utilities'] += fee
continue continue
finally: finally:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!