Commit 45b68eba by zhouhaibao

feat(data-process): 添加预算达成Excel解析功能以获取营销费用占比

- 在 earnings_report_processor.py 和 equity_processor.py 中导入 parse_budget_achieve_excel 函数
- 添加从预算达成数据(商务)Excel文件解析人员工资占比的逻辑
- 实现对中英文括号兼容的文件名匹配机制
- 移除原有的上月营销费用占比数据库查询逻辑
- 新增 parse_budget_achieve_excel 函数用于解析Excel中的营销费用占比数据
- 添加门店名列和人员工资占比列的智能匹配功能
- 实现数据清洗和类型转换,确保返回正确的Decimal数值格式
1 parent 2769193a
......@@ -24,7 +24,8 @@ from report_utils import (
SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED,
download_all_files,
parse_saas_base, parse_region_map, parse_delivery_table,
fetch_indicator_data, fetch_prev_month_labor_ratio,
parse_budget_achieve_excel,
fetch_indicator_data,
safe_div, safe_div_pct, safe_div_pct_decimal,
)
from models import ReportExtendedWarranty, db
......@@ -140,6 +141,13 @@ class WarrantyProcessor:
for k, v in part.items():
delivery_map[k] = delivery_map.get(k, 0) + v
# 预算达成数据(商务)→ 人员工资占比 = 营销费用占比
# form_type 兼容中英文括号
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
prev_labor_ratio_map = {}
if budget_path:
prev_labor_ratio_map = parse_budget_achieve_excel(budget_path)
finally:
cleanup_tmp_dir(tmp_dir)
......@@ -152,8 +160,7 @@ class WarrantyProcessor:
biz_sql_data = {}
logger.warning("[延保] Step3 业务系统 SQL 暂未实现")
# ========== Step4: 上月营销费用占比 ==========
prev_labor_ratio_map = fetch_prev_month_labor_ratio(task, base_records)
# ========== Step4: 上月营销费用占比(已在 Step1 从 Excel 解析) ==========
# ========== Step5: 逐行公式计算 ==========
rows_to_insert = []
......
......@@ -24,7 +24,8 @@ from report_utils import (
SUBTASK_PROCESSING, SUBTASK_COMPLETED, SUBTASK_FAILED,
download_all_files,
parse_saas_base, parse_region_map, parse_delivery_table,
fetch_indicator_data, fetch_prev_month_labor_ratio,
parse_budget_achieve_excel,
fetch_indicator_data,
safe_div, safe_div_pct, safe_div_pct_decimal,
)
from models import ReportEquityService, db
......@@ -143,6 +144,12 @@ class EquityProcessor:
for k, v in part.items():
delivery_map[k] = delivery_map.get(k, 0) + v
# 预算达成数据(商务)→ 人员工资占比 = 营销费用占比
budget_path = file_map.get('预算达成数据(商务)') or file_map.get('预算达成数据(商务)')
prev_labor_ratio_map = {}
if budget_path:
prev_labor_ratio_map = parse_budget_achieve_excel(budget_path)
finally:
cleanup_tmp_dir(tmp_dir)
......@@ -155,8 +162,7 @@ class EquityProcessor:
biz_sql_data = {}
logger.warning("[权益] Step3 业务系统 SQL 暂未实现")
# ========== Step4: 上月营销费用占比 ==========
prev_labor_ratio_map = fetch_prev_month_labor_ratio(task, base_records)
# ========== Step4: 上月营销费用占比(已在 Step1 从 Excel 解析) ==========
# ========== Step5: 逐行公式计算 ==========
rows_to_insert = []
......
......@@ -511,6 +511,55 @@ def fetch_prev_month_labor_ratio(task, base_records):
return results
def parse_budget_achieve_excel(file_path):
"""
解析上传的"预算达成数据(商务)"Excel 文件,取"人员工资占比" → 营销费用占比。
门店名映射列:流量费名称(target_name / 流量方名称 / 门店名)。
返回 dict[store_name] = Decimal(营销费用占比)。
注:这是延保/权益营销费用占比的**主要来源**(form_type=预算达成数据(商务))。
"""
logger.info(f"[预算达成Excel] 解析 {file_path}")
try:
df = pd.read_excel(file_path)
except Exception:
logger.warning(f"[预算达成Excel] 读取失败 {file_path}")
return {}
df.columns = [str(c).strip() if c else '' for c in df.columns]
col_map = match_columns(df.columns, {
# 门店名映射列(流量费名称 / 流量方名称)
'store_name': ['流量费名称', '流量方名称', '门店名', 'target_name'],
# 人员工资占比 → 营销费用占比
'labor_ratio': ['人员工资占比', '人员工资', '人员成本占比', '营销费用占比'],
})
if 'store_name' not in col_map:
logger.warning("[预算达成Excel] 未找到门店名列(流量费名称)")
return {}
if 'labor_ratio' not in col_map:
logger.warning("[预算达成Excel] 未找到人员工资占比列")
return {}
store_col = col_map['store_name']
ratio_col = col_map['labor_ratio']
result = {}
for _, row in df.iterrows():
sn = str(row.get(store_col, '')).strip()
if not sn or sn in ('nan', 'None', ''):
continue
val = row.get(ratio_col, None)
if val is None or (isinstance(val, float) and pd.isna(val)) or str(val).strip() in ('', 'nan', 'None', '-', '/'):
continue
try:
result[sn] = Decimal(str(val))
except Exception:
continue
logger.info(f"[预算达成Excel] {len(result)} 个门店匹配到人员工资占比")
return result
# ============================================================
# 轻改店中店 - 专属解析函数
# ============================================================
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!