Commit b2d32a1a by zhouhaibao

feat(data-process): 实现园区店人力成本按GMV比例分摊功能

- 构建park_rows索引用于存储园区店GMV比例数据
- 优先使用gmv_ratio_park进行人力成本精确分摊
- 当gmv_ratio_park不可用时回退到gmv_ratio分摊
- 保留平均分摊作为最终回退方案
- 添加详细的分摊过程日志记录
- 支持标准和原始园区名称的数据处理
1 parent 19bae038
...@@ -2032,6 +2032,26 @@ class DataProcessor: ...@@ -2032,6 +2032,26 @@ class DataProcessor:
if ps_std != ps_raw: if ps_std != ps_raw:
park_to_flows[ps_std].append((flow_name, biz_type)) park_to_flows[ps_std].append((flow_name, biz_type))
# 构建 park_rows 索引(用于 gmv_ratio_park 分摊)
park_rows = defaultdict(list)
if flow_ratio_entries:
for (flow_name, biz_type), entries in flow_ratio_entries.items():
for brand, ps_raw, gmv_ratio, gmv_ratio_park in entries:
if ps_raw:
park_rows[(ps_raw, biz_type)].append((flow_name, brand, gmv_ratio_park, gmv_ratio))
ps_std = to_standard(ps_raw, 'yq')
if ps_std != ps_raw:
park_rows[(ps_std, biz_type)].append((flow_name, brand, gmv_ratio_park, gmv_ratio))
if not park_rows and settlement_map:
for (flow_name, biz_type, brand), data in settlement_map.items():
ps_raw = data.get("park_store_raw", "")
gmv_ratio = data.get("gmv_ratio")
gmv_ratio_park = data.get("gmv_ratio_park")
if ps_raw:
park_rows[(ps_raw, biz_type)].append((flow_name, brand, gmv_ratio_park, gmv_ratio))
logger.info(f"[人力成本] park_rows 构建: {sum(len(v) for v in park_rows.values())} 条记录, {len(park_rows)} 个 (ps, bt) 组合")
if not park_to_flows and settlement_map: if not park_to_flows and settlement_map:
for (flow_name, biz_type, brand), data in settlement_map.items(): for (flow_name, biz_type, brand), data in settlement_map.items():
ps_raw = data.get("park_store_raw", "") ps_raw = data.get("park_store_raw", "")
...@@ -2159,12 +2179,29 @@ class DataProcessor: ...@@ -2159,12 +2179,29 @@ class DataProcessor:
matching_flows = related_flows # 回退:使用所有关联流量方 matching_flows = related_flows # 回退:使用所有关联流量方
if matching_flows: if matching_flows:
# 按流量方聚合(平均分配到各流量方) # 优先使用 gmv_ratio_park 分摊
park_ratio_rows = park_rows.get((ps, biz_type), [])
if park_ratio_rows:
for flow_name, brand, gmv_ratio_park, gmv_ratio in park_ratio_rows:
if gmv_ratio_park is not None:
r = Decimal(str(gmv_ratio_park))
ratio_type = "gmv_ratio_park"
elif gmv_ratio is not None:
r = Decimal(str(gmv_ratio))
ratio_type = "gmv_ratio(回退)"
else:
continue
result[(flow_name, biz_type)]["技师工资"] += technician_salary * r
result[(flow_name, biz_type)]["营销团队工资"] += marketing_salary * r
logger.info(f"[人力成本] 园区店分摊({ratio_type}): ps='{ps}', {len(park_ratio_rows)}行, tech={technician_salary}, mkt={marketing_salary}")
else:
# 回退:平均分配
flow_count = len(matching_flows) flow_count = len(matching_flows)
for flow_name, _ in matching_flows: for flow_name, _ in matching_flows:
key = (flow_name, biz_type) key = (flow_name, biz_type)
result[key]["技师工资"] += technician_salary / flow_count result[key]["技师工资"] += technician_salary / flow_count
result[key]["营销团队工资"] += marketing_salary / flow_count result[key]["营销团队工资"] += marketing_salary / flow_count
logger.info(f"[人力成本] 园区店分摊(平均回退): ps='{ps}', flows={flow_count}")
else: else:
failed_match_count += 1 failed_match_count += 1
continue continue
...@@ -2185,11 +2222,29 @@ class DataProcessor: ...@@ -2185,11 +2222,29 @@ class DataProcessor:
if not matching_flows: if not matching_flows:
matching_flows = related_flows matching_flows = related_flows
if matching_flows: if matching_flows:
# 优先使用 gmv_ratio_park 分摊
park_ratio_rows = park_rows.get((ps_std, biz_type), [])
if park_ratio_rows:
for flow_name, brand, gmv_ratio_park, gmv_ratio in park_ratio_rows:
if gmv_ratio_park is not None:
r = Decimal(str(gmv_ratio_park))
ratio_type = "gmv_ratio_park"
elif gmv_ratio is not None:
r = Decimal(str(gmv_ratio))
ratio_type = "gmv_ratio(回退)"
else:
continue
result[(flow_name, biz_type)]["技师工资"] += technician_salary * r
result[(flow_name, biz_type)]["营销团队工资"] += marketing_salary * r
logger.info(f"[人力成本] 园区店分摊({ratio_type}): ps='{ps_std}', {len(park_ratio_rows)}行, tech={technician_salary}, mkt={marketing_salary}")
else:
# 回退:平均分配
flow_count = len(matching_flows) flow_count = len(matching_flows)
for flow_name, _ in matching_flows: for flow_name, _ in matching_flows:
key = (flow_name, biz_type) key = (flow_name, biz_type)
result[key]["技师工资"] += technician_salary / flow_count result[key]["技师工资"] += technician_salary / flow_count
result[key]["营销团队工资"] += marketing_salary / flow_count result[key]["营销团队工资"] += marketing_salary / flow_count
logger.info(f"[人力成本] 园区店分摊(平均回退): ps='{ps_std}', flows={flow_count}")
else: else:
failed_match_count += 1 failed_match_count += 1
else: else:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!