Commit f47bba70 by zhouhaibao

fix(data-process): 解决店中店数据处理中的业务类型过滤和模糊匹配问题

- 添加了详细的日志记录用于调试文件路径和数据解析过程
- 实现了门店名称的模糊匹配功能替代精确匹配
- 移除了店中店交车触客表中的业务类型过滤逻辑
- 修复了Excel文件读取时的表头偏移问题
- 更新了SQL查询中的表别名引用确保数据准确性
- 在数据聚合过程中添加了业务类型过滤的诊断日志
1 parent d29b00d3
...@@ -129,8 +129,11 @@ class LightReformInshopProcessor: ...@@ -129,8 +129,11 @@ class LightReformInshopProcessor:
# 店中店交车触客表(区域运营填的,Excel 第一优先) # 店中店交车触客表(区域运营填的,Excel 第一优先)
delivery_touch_map = {} delivery_touch_map = {}
dt_path = file_map.get('店中店交车触客表') dt_path = file_map.get('店中店交车触客表')
logger.info(f"[店中店] file_map keys={list(file_map.keys())}")
logger.info(f"[店中店交车触客表] dt_path={dt_path}")
if dt_path: if dt_path:
delivery_touch_map = parse_inshop_delivery_touch(dt_path, biz_type='1.1') delivery_touch_map = parse_inshop_delivery_touch(dt_path)
logger.info(f"[店中店交车触客表] 解析后 {len(delivery_touch_map)} 个门店, 样本keys={list(delivery_touch_map.keys())[:5]}")
# 商务政策表(店中店处理版) # 商务政策表(店中店处理版)
commerce_map = {} commerce_map = {}
...@@ -218,7 +221,8 @@ class LightReformInshopProcessor: ...@@ -218,7 +221,8 @@ class LightReformInshopProcessor:
brand_category = is_luxury_brand(brand) brand_category = is_luxury_brand(brand)
# —— 双优先:Excel 第一,SQL 回退 —— # —— 双优先:Excel 第一,SQL 回退 ——
dt_excel = delivery_touch_map.get(sn, {}) # 模糊匹配:SAAS 门店名 vs Excel 门店名 / SQL merchant_name
dt_excel = fuzzy_match_store(sn, delivery_touch_map) or {}
sql_data = biz_sql_data.get(sn, {}) sql_data = biz_sql_data.get(sn, {})
sales_order_cnt = get_with_fallback( sales_order_cnt = get_with_fallback(
......
...@@ -920,18 +920,20 @@ def _to_decimal(val): ...@@ -920,18 +920,20 @@ def _to_decimal(val):
return Decimal('0') return Decimal('0')
def parse_inshop_delivery_touch(file_path):
"""
解析店中店交车触客表(区域运营填写)。
返回 dict[store_name] = {sales_order_cnt, sales_touch_cnt, delivery_cnt, delivery_touch_cnt,
aftersales_baohui_cnt, aftersales_shigu_cnt, aftersales_touch_cnt, purchase_indicator}
"""
def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fields, biz_type=None): def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fields, biz_type=None):
""" """
通用聚合器:按 (store_name, biz_type) 过滤后,同门店多行 → 数值字段求和取平均,文本字段取第一条。 通用聚合器:按 (store_name, biz_type) 过滤后,同门店多行 → 数值字段求和取平均,文本字段取第一条。
返回 dict[store_name] = {field: value}。 返回 dict[store_name] = {field: value}。
""" """
biz_col = col_map.get('biz_type') biz_col = col_map.get('biz_type')
logger.info(f"[_aggregate_by_biz_type] biz_col={biz_col}, 目标biz_type={biz_type}, 总行数={len(df)}")
# 诊断:如果有 biz_col,打印实际有哪些值
if biz_col and biz_type is not None:
actual_vals = df.iloc[:, df.columns.get_loc(biz_col)].astype(str).str.strip().unique().tolist()
logger.info(f"[_aggregate_by_biz_type] 业务类型列实际值={actual_vals}, 目标值={biz_type}")
skipped_by_biz = 0
agg = {} # (sn, bt) → {field: [sum, count]} for numeric, 'text'→first_val for text agg = {} # (sn, bt) → {field: [sum, count]} for numeric, 'text'→first_val for text
for _, row in df.iterrows(): for _, row in df.iterrows():
...@@ -940,6 +942,7 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie ...@@ -940,6 +942,7 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie
continue continue
bt_val = str(row.get(biz_col, '')).strip() if biz_col else '' bt_val = str(row.get(biz_col, '')).strip() if biz_col else ''
if biz_type is not None and biz_col and bt_val != biz_type: if biz_type is not None and biz_col and bt_val != biz_type:
skipped_by_biz += 1
continue continue
key = (sn, bt_val) if biz_col else (sn, '') key = (sn, bt_val) if biz_col else (sn, '')
...@@ -973,6 +976,9 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie ...@@ -973,6 +976,9 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie
if f not in agg[key] or not agg[key][f]: if f not in agg[key] or not agg[key][f]:
agg[key][f] = val agg[key][f] = val
if skipped_by_biz > 0:
logger.warning(f"[_aggregate_by_biz_type] 因biz_type过滤跳过 {skipped_by_biz} 行")
result = {} result = {}
for (sn, _bt), fields in agg.items(): for (sn, _bt), fields in agg.items():
if sn not in result: if sn not in result:
...@@ -988,23 +994,24 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie ...@@ -988,23 +994,24 @@ def _aggregate_by_biz_type(df, col_map, store_name_col, numeric_fields, text_fie
return result return result
def parse_inshop_delivery_touch(file_path, biz_type='1.1'): def parse_inshop_delivery_touch(file_path):
""" """
解析店中店交车触客表(区域运营填写)。 解析店中店交车触客表(区域运营填写)。
同门店同业务类型多行时,数值字段取**平均**。 只按门店名称聚合,不按业务类型过滤(Excel 无业务类型列)。
同门店多行时,数值字段取**平均**。
返回 dict[store_name] = {...} 返回 dict[store_name] = {...}
""" """
logger.info(f"[店中店交车触客表] 解析 {file_path}, biz_type={biz_type}") logger.info(f"[店中店交车触客表] 解析 {file_path}")
try: try:
df = pd.read_excel(file_path) df = pd.read_excel(file_path, header=1)
except Exception: except Exception:
logger.warning(f"[店中店交车触客表] 读取失败 {file_path}") logger.warning(f"[店中店交车触客表] 读取失败 {file_path}")
return {} return {}
df.columns = [str(c).strip() if c else '' for c in df.columns] df.columns = [str(c).strip() if c else '' for c in df.columns]
logger.info(f"[店中店交车触客表] Excel原始列={df.columns.tolist()}")
col_map = match_columns(df.columns, { col_map = match_columns(df.columns, {
'store_name': ['门店名称', '门店名'], 'store_name': ['门店名称', '门店名'],
'biz_type': ['业务类型', '业务线'],
'sales_order_cnt': ['销售大定数', '销售大定'], 'sales_order_cnt': ['销售大定数', '销售大定'],
'sales_touch_cnt': ['销售触客数', '销售触客'], 'sales_touch_cnt': ['销售触客数', '销售触客'],
'delivery_cnt': ['交车数', '交车'], 'delivery_cnt': ['交车数', '交车'],
...@@ -1014,6 +1021,7 @@ def parse_inshop_delivery_touch(file_path, biz_type='1.1'): ...@@ -1014,6 +1021,7 @@ def parse_inshop_delivery_touch(file_path, biz_type='1.1'):
'aftersales_touch_cnt': ['售后触客数', '售后触客'], 'aftersales_touch_cnt': ['售后触客数', '售后触客'],
'purchase_indicator': ['采买', '兜底指标', '采买兜底'], 'purchase_indicator': ['采买', '兜底指标', '采买兜底'],
}) })
logger.info(f"[店中店交车触客表] 列映射={col_map}")
if 'store_name' not in col_map: if 'store_name' not in col_map:
logger.warning("[店中店交车触客表] 未找到门店名列") logger.warning("[店中店交车触客表] 未找到门店名列")
...@@ -1023,7 +1031,8 @@ def parse_inshop_delivery_touch(file_path, biz_type='1.1'): ...@@ -1023,7 +1031,8 @@ def parse_inshop_delivery_touch(file_path, biz_type='1.1'):
'aftersales_baohui_cnt', 'aftersales_shigu_cnt', 'aftersales_touch_cnt'] 'aftersales_baohui_cnt', 'aftersales_shigu_cnt', 'aftersales_touch_cnt']
text = ['purchase_indicator'] text = ['purchase_indicator']
raw = _aggregate_by_biz_type(df, col_map, col_map['store_name'], numeric, text, biz_type) # 不传 biz_type → _aggregate_by_biz_type 内部 biz_col=None,不做业务类型过滤
raw = _aggregate_by_biz_type(df, col_map, col_map['store_name'], numeric, text, biz_type=None)
# int 字段转回 int # int 字段转回 int
result = {} result = {}
for sn, v in raw.items(): for sn, v in raw.items():
...@@ -1208,7 +1217,7 @@ def parse_park_delivery_touch(file_path, biz_type='1.1'): ...@@ -1208,7 +1217,7 @@ def parse_park_delivery_touch(file_path, biz_type='1.1'):
""" """
logger.info(f"[园区店交车触客表] 解析 {file_path}, biz_type={biz_type}") logger.info(f"[园区店交车触客表] 解析 {file_path}, biz_type={biz_type}")
try: try:
df = pd.read_excel(file_path) df = pd.read_excel(file_path, header=1)
except Exception: except Exception:
logger.warning(f"[园区店交车触客表] 读取失败 {file_path}") logger.warning(f"[园区店交车触客表] 读取失败 {file_path}")
return {} return {}
...@@ -1921,24 +1930,24 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1): ...@@ -1921,24 +1930,24 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1):
go.merchant_name AS merchant_name, go.merchant_name AS merchant_name,
-- 产品成本(财务列表)= 非内部结算 dj_order_type != 6 -- 产品成本(财务列表)= 非内部结算 dj_order_type != 6
SUM(CASE WHEN gopa.dj_order_type != 6 SUM(CASE WHEN gso.dj_order_type != 6
THEN gso.upgrade_shop_price - gso.base_shop_price THEN gso.upgrade_shop_price - gso.base_shop_price
ELSE 0 END) AS product_cost_finance, ELSE 0 END) AS product_cost_finance,
-- 内部结算成本(财务列表)= 内部结算 dj_order_type = 6 -- 内部结算成本(财务列表)= 内部结算 dj_order_type = 6
SUM(CASE WHEN gopa.dj_order_type = 6 SUM(CASE WHEN gso.dj_order_type = 6
THEN gso.upgrade_shop_price - gso.base_shop_price THEN gso.upgrade_shop_price - gso.base_shop_price
ELSE 0 END) AS internal_settle_cost, ELSE 0 END) AS internal_settle_cost,
-- 原厂产品成本(非内部结算 + 名称含 AITO/HIMA) -- 原厂产品成本(非内部结算 + 名称含 AITO/HIMA)
SUM(CASE WHEN gopa.dj_order_type != 6 SUM(CASE WHEN gso.dj_order_type != 6
AND (gso.upgrade_goods_name LIKE '%AITO%' AND (gso.upgrade_goods_name LIKE '%AITO%'
OR gso.upgrade_goods_name LIKE '%HIMA%') OR gso.upgrade_goods_name LIKE '%HIMA%')
THEN gso.upgrade_shop_price - gso.base_shop_price THEN gso.upgrade_shop_price - gso.base_shop_price
ELSE 0 END) AS oe_product_cost, ELSE 0 END) AS oe_product_cost,
-- 副厂产品成本(非内部结算 + 名称不含 AITO/HIMA) -- 副厂产品成本(非内部结算 + 名称不含 AITO/HIMA)
SUM(CASE WHEN gopa.dj_order_type != 6 SUM(CASE WHEN gso.dj_order_type != 6
AND gso.upgrade_goods_name NOT LIKE '%AITO%' AND gso.upgrade_goods_name NOT LIKE '%AITO%'
AND gso.upgrade_goods_name NOT LIKE '%HIMA%' AND gso.upgrade_goods_name NOT LIKE '%HIMA%'
THEN gso.upgrade_shop_price - gso.base_shop_price THEN gso.upgrade_shop_price - gso.base_shop_price
...@@ -1960,7 +1969,7 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1): ...@@ -1960,7 +1969,7 @@ def fetch_light_reform_finance_stats(start_date, end_date, scene_type=1):
ELSE 0 END) AS premium_cost, ELSE 0 END) AS premium_cost,
-- 门店分成/流量费(财务列表)= 非内部结算 shop_traffic_money -- 门店分成/流量费(财务列表)= 非内部结算 shop_traffic_money
SUM(CASE WHEN gopa.dj_order_type != 6 SUM(CASE WHEN gso.dj_order_type != 6
THEN IFNULL(gopa.shop_traffic_money, 0) THEN IFNULL(gopa.shop_traffic_money, 0)
ELSE 0 END) AS store_share, ELSE 0 END) AS store_share,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!